From c25dea063525c70b8bc8f89d65982e57d45d5e8e Mon Sep 17 00:00:00 2001 From: Artur Abliazimov Date: Wed, 20 Sep 2023 13:37:57 +0300 Subject: [PATCH 1/2] borrow() budget test --- contracts/pool/src/tests/mod.rs | 2 +- .../tests/snapshots/budget_utilization.snap | 66 +++++++++--------- contracts/pool/src/tests/sut.rs | 1 + .../tests/pool/6.tx.budget.spec.ts | 67 +++++++++++++++++++ 4 files changed, 102 insertions(+), 34 deletions(-) create mode 100644 integration-tests/tests/pool/6.tx.budget.spec.ts diff --git a/contracts/pool/src/tests/mod.rs b/contracts/pool/src/tests/mod.rs index 3e9a7a56..20c57385 100644 --- a/contracts/pool/src/tests/mod.rs +++ b/contracts/pool/src/tests/mod.rs @@ -1,6 +1,6 @@ pub mod account_position; pub mod borrow; -// #[cfg(feature = "budget")] +#[cfg(feature = "budget")] pub mod budget; pub mod collat_coeff; pub mod configure_as_collateral; diff --git a/contracts/pool/src/tests/snapshots/budget_utilization.snap b/contracts/pool/src/tests/snapshots/budget_utilization.snap index 28d6934a..2841816d 100644 --- a/contracts/pool/src/tests/snapshots/budget_utilization.snap +++ b/contracts/pool/src/tests/snapshots/budget_utilization.snap @@ -1,95 +1,95 @@ ['pool::tests::budget::account_position'] = { - "cpu_cost": 85009300, + "cpu_cost": 85009264, "memory_cost": 18025534, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::borrow'] = { - "cpu_cost": 115080954, + "cpu_cost": 115080578, "memory_cost": 26154416, "cpu_limit_exceeded": true, "memory_limit_exceeded": false, } ['pool::tests::budget::collat_coeff'] = { - "cpu_cost": 33308127, + "cpu_cost": 33321381, "memory_cost": 3641467, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::configure_as_collateral'] = { - "cpu_cost": 33499967, + "cpu_cost": 33504620, "memory_cost": 3672541, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::debt_coeff'] = { - "cpu_cost": 33201344, + "cpu_cost": 33204062, "memory_cost": 3640748, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::deposit'] = { - "cpu_cost": 57506415, + "cpu_cost": 57507628, "memory_cost": 7801939, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::enable_borrowing_on_reserve'] = { - "cpu_cost": 33464217, + "cpu_cost": 33461724, "memory_cost": 3670866, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::flash_loan_fee'] = { - "cpu_cost": 33050259, + "cpu_cost": 33050151, "memory_cost": 3631399, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::flash_loan_with_borrow'] = { - "cpu_cost": 179616487, + "cpu_cost": 179607527, "memory_cost": 44176818, "cpu_limit_exceeded": true, "memory_limit_exceeded": true, } ['pool::tests::budget::flash_loan_without_borrow'] = { - "cpu_cost": 76931953, + "cpu_cost": 76922644, "memory_cost": 14169119, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::get_reserve'] = { - "cpu_cost": 33132553, + "cpu_cost": 33137881, "memory_cost": 3633318, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::init_reserve'] = { - "cpu_cost": 262235, + "cpu_cost": 261191, "memory_cost": 19794, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::ir_params'] = { - "cpu_cost": 33149831, + "cpu_cost": 33149615, "memory_cost": 3640748, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::liquidate_receive_stoken'] = { - "cpu_cost": 160225857, + "cpu_cost": 160224640, "memory_cost": 38063730, "cpu_limit_exceeded": true, "memory_limit_exceeded": false, } ['pool::tests::budget::liquidate_receive_underlying'] = { - "cpu_cost": 77218241, + "cpu_cost": 77210498, "memory_cost": 19949790, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::paused'] = { - "cpu_cost": 33038395, + "cpu_cost": 33038287, "memory_cost": 3631189, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, @@ -101,67 +101,67 @@ "memory_limit_exceeded": false, } ['pool::tests::budget::repay_full'] = { - "cpu_cost": 46818052, + "cpu_cost": 46787486, "memory_cost": 7043235, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::repay_partial'] = { - "cpu_cost": 46862748, + "cpu_cost": 46838678, "memory_cost": 7034537, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::s_token_transfer'] = { - "cpu_cost": 79953718, + "cpu_cost": 79945130, "memory_cost": 17685848, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_as_collateral'] = { - "cpu_cost": 62356353, + "cpu_cost": 62358927, "memory_cost": 12850139, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_base_asset'] = { - "cpu_cost": 33445371, + "cpu_cost": 33449304, "memory_cost": 3670386, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_decimals'] = { - "cpu_cost": 33444151, + "cpu_cost": 33440182, "memory_cost": 3670352, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_flash_loan_fee'] = { - "cpu_cost": 33332393, + "cpu_cost": 33332933, "memory_cost": 3667999, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_ir_params'] = { - "cpu_cost": 33377906, + "cpu_cost": 33378482, "memory_cost": 3670110, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_pause'] = { - "cpu_cost": 33323556, + "cpu_cost": 33323268, "memory_cost": 3667959, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_price_feed'] = { - "cpu_cost": 167757, + "cpu_cost": 168045, "memory_cost": 12292, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::set_reserve_status'] = { - "cpu_cost": 33461093, + "cpu_cost": 33469067, "memory_cost": 3670824, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, @@ -179,37 +179,37 @@ "memory_limit_exceeded": false, } ['pool::tests::budget::upgrade'] = { - "cpu_cost": 33359807, + "cpu_cost": 33361535, "memory_cost": 3669997, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::upgrade_debt_token'] = { - "cpu_cost": 39031973, + "cpu_cost": 39027113, "memory_cost": 5253684, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::upgrade_s_token'] = { - "cpu_cost": 44564944, + "cpu_cost": 44570128, "memory_cost": 5665520, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::user_configuration'] = { - "cpu_cost": 32944537, + "cpu_cost": 32943340, "memory_cost": 3628849, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::withdraw_full'] = { - "cpu_cost": 92333825, + "cpu_cost": 92347725, "memory_cost": 19763624, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, } ['pool::tests::budget::withdraw_partial'] = { - "cpu_cost": 92226088, + "cpu_cost": 92221202, "memory_cost": 19754496, "cpu_limit_exceeded": false, "memory_limit_exceeded": false, diff --git a/contracts/pool/src/tests/sut.rs b/contracts/pool/src/tests/sut.rs index db36755b..67bac9b3 100644 --- a/contracts/pool/src/tests/sut.rs +++ b/contracts/pool/src/tests/sut.rs @@ -311,6 +311,7 @@ pub(crate) fn fill_pool_three<'a, 'b>( (lender, borrower, liquidator, debt_config) } +#[cfg(feature = "budget")] pub(crate) fn fill_pool_four<'a, 'b>(env: &'b Env, sut: &'a Sut) -> (Address, Address) { let lender = Address::random(&env); let borrower1 = Address::random(&env); diff --git a/integration-tests/tests/pool/6.tx.budget.spec.ts b/integration-tests/tests/pool/6.tx.budget.spec.ts new file mode 100644 index 00000000..2a9ad73f --- /dev/null +++ b/integration-tests/tests/pool/6.tx.budget.spec.ts @@ -0,0 +1,67 @@ +import { SorobanClient } from "../soroban.client"; +import { + borrow, + cleanSlenderEnvKeys, + deploy, + deposit, + init, + mintUnderlyingTo, +} from "../pool.sut"; +import { + borrower1Keys, + borrower2Keys, + lender1Keys, +} from "../soroban.config"; +import { expect, use } from "chai"; +import chaiAsPromised from 'chai-as-promised'; +use(chaiAsPromised); + +describe("LendingPool: methods must not exceed CPU/MEM limits", function () { + let client: SorobanClient; + let lender1Address: string; + let borrower1Address: string; + let borrower2Address: string; + + before(async function () { + client = new SorobanClient(); + + await cleanSlenderEnvKeys(); + await deploy(); + await init(client); + + lender1Address = lender1Keys.publicKey(); + borrower1Address = borrower1Keys.publicKey(); + borrower2Address = borrower2Keys.publicKey(); + + await client.registerAccount(lender1Address); + await client.registerAccount(borrower1Address); + await client.registerAccount(borrower2Address); + + await mintUnderlyingTo(client, "XLM", lender1Address, 100_000_000_000n); + await mintUnderlyingTo(client, "XRP", lender1Address, 100_000_000_000n); + await mintUnderlyingTo(client, "USDC", lender1Address, 100_000_000_000n); + await mintUnderlyingTo(client, "XLM", borrower1Address, 100_000_000_000n); + await mintUnderlyingTo(client, "XRP", borrower1Address, 100_000_000_000n); + await mintUnderlyingTo(client, "USDC", borrower2Address, 100_000_000_000n); + }); + + it("Case 1: borrow()", async function () { + // Lender1 deposits 10_000_000_000 XLM, XRP, USDC + await deposit(client, lender1Keys, "XLM", 10_000_000_000n); + await deposit(client, lender1Keys, "XRP", 10_000_000_000n); + await deposit(client, lender1Keys, "USDC", 10_000_000_000n); + + // Borrower1 deposits 10_000_000_000 XLM, XRP, borrows 6_000_000_000 USDC + await deposit(client, borrower1Keys, "XLM", 10_000_000_000n); + await deposit(client, borrower1Keys, "XRP", 10_000_000_000n); + await borrow(client, borrower1Keys, "USDC", 6_000_000_000n); + + // Borrower2 deposits 20_000_000_000 USDC, borrows 6_000_000_000 XLM, 5_999_000_000 XRP + await deposit(client, borrower2Keys, "USDC", 10_000_000_000n); + await borrow(client, borrower1Keys, "XLM", 6_000_000_000n); + await borrow(client, borrower1Keys, "XRP", 5_999_000_000n); + + // Borrower1 borrows 20_000_000 USDC + await expect(borrow(client, borrower1Keys, "USDC", 20_000_000n)).to.not.eventually.rejected; + }); +}); From 79076c0e224628362892bc0edc23440580a8edff Mon Sep 17 00:00:00 2001 From: Artur Abliazimov Date: Wed, 20 Sep 2023 14:20:34 +0300 Subject: [PATCH 2/2] Fix PR --- integration-tests/tests/pool.sut.ts | 2 -- integration-tests/tests/pool/6.tx.budget.spec.ts | 10 +++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/integration-tests/tests/pool.sut.ts b/integration-tests/tests/pool.sut.ts index e06913c1..d25206b0 100644 --- a/integration-tests/tests/pool.sut.ts +++ b/integration-tests/tests/pool.sut.ts @@ -88,8 +88,6 @@ export async function mintBurn( client: SorobanClient, mintsBurns: Array ): Promise { - return; - for (let i = 0; i < mintsBurns.length; i++) { const response = await client.sendTransaction( mintsBurns[i].asset_balance.get("asset"), diff --git a/integration-tests/tests/pool/6.tx.budget.spec.ts b/integration-tests/tests/pool/6.tx.budget.spec.ts index 2a9ad73f..2c751c5d 100644 --- a/integration-tests/tests/pool/6.tx.budget.spec.ts +++ b/integration-tests/tests/pool/6.tx.budget.spec.ts @@ -43,9 +43,7 @@ describe("LendingPool: methods must not exceed CPU/MEM limits", function () { await mintUnderlyingTo(client, "XLM", borrower1Address, 100_000_000_000n); await mintUnderlyingTo(client, "XRP", borrower1Address, 100_000_000_000n); await mintUnderlyingTo(client, "USDC", borrower2Address, 100_000_000_000n); - }); - it("Case 1: borrow()", async function () { // Lender1 deposits 10_000_000_000 XLM, XRP, USDC await deposit(client, lender1Keys, "XLM", 10_000_000_000n); await deposit(client, lender1Keys, "XRP", 10_000_000_000n); @@ -57,10 +55,12 @@ describe("LendingPool: methods must not exceed CPU/MEM limits", function () { await borrow(client, borrower1Keys, "USDC", 6_000_000_000n); // Borrower2 deposits 20_000_000_000 USDC, borrows 6_000_000_000 XLM, 5_999_000_000 XRP - await deposit(client, borrower2Keys, "USDC", 10_000_000_000n); - await borrow(client, borrower1Keys, "XLM", 6_000_000_000n); - await borrow(client, borrower1Keys, "XRP", 5_999_000_000n); + await deposit(client, borrower2Keys, "USDC", 20_000_000_000n); + await borrow(client, borrower2Keys, "XLM", 6_000_000_000n); + await borrow(client, borrower2Keys, "XRP", 5_900_000_000n); + }); + it("Case 1: borrow()", async function () { // Borrower1 borrows 20_000_000 USDC await expect(borrow(client, borrower1Keys, "USDC", 20_000_000n)).to.not.eventually.rejected; });