Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ jobs:
test:
uses: ./.github/workflows/test.yml

gas-report:
uses: ./.github/workflows/gas-report.yml

deploy-staging:
name: Deploy to staging subaccount
permissions:
pull-requests: write
needs: [test]
needs: [test, gas-report]
runs-on: ubuntu-latest
env:
NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID: gh-${{ github.event.number }}.${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}
Expand Down Expand Up @@ -78,4 +81,9 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr comment "${{ github.event.number }}" --body "Staging contract is deployed to ["'`'"${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}"'`'" account](https://explorer.${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}.near.org/accounts/${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }})"
echo "Staging contract is deployed to ["'`'"${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}"'`'" account](https://explorer.${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}.near.org/accounts/${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }})" > comment.md
echo '' >> comment.md
echo "---" >> comment.md
echo '' >> comment.md
echo '${{ needs.gas-report.outputs.comment }}' >> comment.md
gh pr comment "${{ github.event.number }}" -F comment.md --create-if-none --edit-last
28 changes: 28 additions & 0 deletions .github/workflows/gas-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Generate gas report
on:
workflow_call:
outputs:
comment:
description: Markdown gas report
value: ${{ jobs.gas-report.outputs.comment }}

jobs:
gas-report:
name: Gas report
runs-on: ubuntu-latest
outputs:
comment: ${{ steps.generate.outputs.comment }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install cargo-near CLI
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.13.3/cargo-near-installer.sh | sh
- name: Generate gas report
id: generate
run: |
./gas_report.sh > gas-report.md
cat gas-report.md >> $GITHUB_STEP_SUMMARY

echo "comment<<EOF" >> $GITHUB_OUTPUT
cat gas-report.md >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
3 changes: 3 additions & 0 deletions contract/market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ tokio.workspace = true

[lints]
workspace = true

[[example]]
name = "gas_report"
63 changes: 63 additions & 0 deletions contract/market/examples/gas_report.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::collections::HashMap;

use near_sdk::{json_types::U64, Gas};
use templar_common::time_chunk::TimeChunkConfiguration;
use test_utils::{setup_everything, SetupEverything};
use tokio::task::JoinSet;

#[allow(clippy::unwrap_used)]
#[tokio::main]
async fn main() {
const STEP: usize = 10;
const COUNT: usize = 3;

let mut handles = JoinSet::new();

for i in 0..COUNT {
handles.spawn(async move {
let gas = harvest_yield_gas(i * STEP).await;
eprintln!("Completed {} iterations", i * STEP);
(i, gas)
});
}

let results = handles
.join_all()
.await
.into_iter()
.collect::<HashMap<_, _>>();

println!("**Gas Report**");
println!();
println!("`harvest_yield`");
println!();
println!("| Iterations | Gas |");
println!("| ---------: | ---: |");
for i in 0..COUNT {
println!("| {} | {} |", i * STEP, results.get(&i).unwrap());
}
}

async fn harvest_yield_gas(iterations: usize) -> Gas {
let SetupEverything {
c,
supply_user,
borrow_user,
..
} = setup_everything(|c| {
c.time_chunk_configuration = TimeChunkConfiguration::BlockHeight { divisor: U64(1) };
})
.await;

c.supply(&supply_user, 1200).await;
c.collateralize(&borrow_user, 2000).await;

for _ in 0..iterations {
c.borrow(&borrow_user, 1000).await;
c.repay(&borrow_user, 1100).await;
}

let r = c.harvest_yield(&supply_user, true).await;

r.total_gas_burnt
}
10 changes: 5 additions & 5 deletions contract/market/tests/compounding_yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn compounding_yield(

c.borrow(&borrow_user, principal).await;

println!("Sleeping...");
eprintln!("Sleeping...");
let mut iters = 0;
let done = std::sync::atomic::AtomicBool::new(false);
tokio::join!(
Expand All @@ -57,7 +57,7 @@ async fn compounding_yield(
done.store(true, Ordering::Relaxed);
}
);
println!("Done sleeping!");
eprintln!("Done sleeping!");

c.harvest_yield(&supply_user, false).await;

Expand Down Expand Up @@ -87,9 +87,9 @@ async fn compounding_yield(
.to_u128()
- principal * 5;

println!("supply 1 yield: {supply_yield_1:#?}");
println!("supply 2 yield: {supply_yield_2:#?}");
println!("iterations: {iters}");
eprintln!("supply 1 yield: {supply_yield_1:#?}");
eprintln!("supply 2 yield: {supply_yield_2:#?}");
eprintln!("iterations: {iters}");

if compounding {
// Supply user 2 will be rounded DOWN each iteration.
Expand Down
2 changes: 1 addition & 1 deletion contract/market/tests/happy_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn test_happy(#[case] native_asset_case: NativeAssetCase) {
// Step 3: Withdraw some of the borrow asset
let balance_before = c.borrow_asset_balance_of(borrow_user.id()).await;

println!("Price pair: {:#?}", c.get_prices().await);
eprintln!("Price pair: {:#?}", c.get_prices().await);

// Borrowing 1000 borrow tokens with 2000 collateral tokens should be fine given equal price and MCR of 120%.
c.borrow(&borrow_user, 1000).await;
Expand Down
33 changes: 0 additions & 33 deletions contract/market/tests/harvest_yield_gas.rs

This file was deleted.

18 changes: 9 additions & 9 deletions contract/market/tests/interest_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn interest_rate(#[case] principal: u128, #[case] strategy: InterestRateSt
let mut iters = 0;

for _ in 0..3 {
println!("Sleeping...");
eprintln!("Sleeping...");
let done = std::sync::atomic::AtomicBool::new(false);
tokio::join!(
async {
Expand All @@ -72,7 +72,7 @@ async fn interest_rate(#[case] principal: u128, #[case] strategy: InterestRateSt
done.store(true, Ordering::Relaxed);
}
);
println!("Done sleeping!");
eprintln!("Done sleeping!");

let duration_inner = time_inner.elapsed();
let (borrow_position_1, borrow_position_2, supply_position_1, supply_position_2) = tokio::join!(
Expand All @@ -98,8 +98,8 @@ async fn interest_rate(#[case] principal: u128, #[case] strategy: InterestRateSt
assert_eq!(supply_yield_1, 0);
assert_eq!(supply_yield_2, 0);

println!("Borrow position 1: {borrow_position_1:#?}");
println!("Borrow position 2: {borrow_position_2:#?}");
eprintln!("Borrow position 1: {borrow_position_1:#?}");
eprintln!("Borrow position 2: {borrow_position_2:#?}");

let f = principal * strategy.at(dec!("0.2")) / Decimal::from(MS_IN_A_YEAR);

Expand All @@ -111,7 +111,7 @@ async fn interest_rate(#[case] principal: u128, #[case] strategy: InterestRateSt
.borrow_asset_fees
.pending_estimate
.to_u128();
println!("{approximation_below} <= {actual_1} <= {approximation_above}?");
eprintln!("{approximation_below} <= {actual_1} <= {approximation_above}?");

assert!(approximation_below <= actual_1);
assert!(actual_1 <= approximation_above);
Expand All @@ -121,7 +121,7 @@ async fn interest_rate(#[case] principal: u128, #[case] strategy: InterestRateSt
.borrow_asset_fees
.pending_estimate
.to_u128();
println!("{approximation_below} <= {actual_2} <= {approximation_above} + {iters}?");
eprintln!("{approximation_below} <= {actual_2} <= {approximation_above} + {iters}?");

assert!(approximation_below <= actual_2);
assert!(actual_2 <= approximation_above + iters);
Expand Down Expand Up @@ -153,10 +153,10 @@ async fn interest_rate(#[case] principal: u128, #[case] strategy: InterestRateSt
/ 100, /* overpayment */
)
.await;
println!("{r:#?}");
println!("logs");
eprintln!("{r:#?}");
eprintln!("logs");
for log in r.logs() {
println!("\t{log}");
eprintln!("\t{log}");
}
let borrow_position_after = c.get_borrow_position(borrow_user.id()).await.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions contract/market/tests/supply_withdrawal_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn supply_withdrawal_fee_flat() {

c.supply(&supply_user, 1000).await;

println!("Sleeping 10s...");
eprintln!("Sleeping 10s...");
tokio::time::sleep(Duration::from_secs(10)).await;

let supply_user_balance_before = c.borrow_asset_balance_of(supply_user.id()).await;
Expand Down Expand Up @@ -77,7 +77,7 @@ async fn supply_withdrawal_fee_expired() {

c.supply(&supply_user, 1000).await;

println!("Sleeping 10s...");
eprintln!("Sleeping 10s...");
tokio::time::sleep(Duration::from_secs(10)).await;

let supply_user_balance_before = c.borrow_asset_balance_of(supply_user.id()).await;
Expand Down
17 changes: 17 additions & 0 deletions gas_report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -e

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

cd "$ROOT_DIR/mock/oracle"
cargo near build non-reproducible-wasm 1>&2

cd "$ROOT_DIR/mock/ft"
cargo near build non-reproducible-wasm 1>&2

cd "$ROOT_DIR/contract/market"
cargo near build non-reproducible-wasm 1>&2

cd "$ROOT_DIR"
export TEST_CONTRACTS_PREBUILT=1
cargo run --package templar-market-contract --example gas_report
Loading