From 480962f1bec92e28fbe3a76991a388d0742b7219 Mon Sep 17 00:00:00 2001 From: Gianfranco Tasteri Date: Fri, 29 Sep 2023 13:07:40 -0300 Subject: [PATCH] fix deadline issue in swap.ts test --- README.md | 1 + nabla/config.json | 2 +- nabla/test/backstop.ts | 5 --- nabla/test/swapPool.ts | 2 +- nabla/test/swaps.ts | 57 ++++++++++++++++++++++------------ src/actions/compileContract.ts | 2 +- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 2b31b25..3af5983 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ Alternatively you can use the parameter `local` instead of `foucoco`. This expec ``` brew install binaryen ``` + diff --git a/nabla/config.json b/nabla/config.json index 8e89a2d..050e436 100644 --- a/nabla/config.json +++ b/nabla/config.json @@ -1,7 +1,7 @@ { "contracts": { "TestableERC20Wrapper": { - "path": "contracts/TestableERC20Wrapper.sol" + "path": "testable-erc20-wrapper/TestableERC20Wrapper.sol" }, "Router": { "repository": "nablaContracts", diff --git a/nabla/test/backstop.ts b/nabla/test/backstop.ts index 39f7ab8..a116e2e 100644 --- a/nabla/test/backstop.ts +++ b/nabla/test/backstop.ts @@ -42,11 +42,6 @@ export default async function (environment: TestSuiteEnvironment) { let swapPool1: TestContract; let swapPool2: TestContract; - //const asset1 = await newMockERC20("Test Token 1", "TEST1"); - //const asset2 = await newMockERC20("Test Token 2", "TEST2"); - //const usd = await newMockERC20("Test Backstop Token", "USD"); - - //WE MUST ENSURE THAT INITIAL SUPPLY IS 0 const usd = await newTestableERC20Wrapper("Test Backstop Token", "USD", 18, [1], [1], [], []); const asset1 = await newTestableERC20Wrapper("TestNative", "TEST1", 18, [1], [2], [], []); const asset2 = await newTestableERC20Wrapper("Test Token 2", "TEST2", 18, [1], [3], [], []); diff --git a/nabla/test/swapPool.ts b/nabla/test/swapPool.ts index 780af68..07190c7 100644 --- a/nabla/test/swapPool.ts +++ b/nabla/test/swapPool.ts @@ -46,7 +46,7 @@ export default async function (environment: TestSuiteEnvironment) { }; const nablaCurve = await newNablaCurve(0, e(0.01, 18)); - //const asset = await newMockERC20("Test Token", "TEST"); + const asset = await newTestableERC20Wrapper("Test Token", "TEST", 18, [1], [1], [], []); const pool = await newTestableSwapPool(address(asset), address(nablaCurve), 0, 0, 0, "Test LP Token", "LP"); diff --git a/nabla/test/swaps.ts b/nabla/test/swaps.ts index 0c9df08..de3b8ac 100644 --- a/nabla/test/swaps.ts +++ b/nabla/test/swaps.ts @@ -27,6 +27,8 @@ export default async function (environment: TestSuiteEnvironment) { constructors: { newRouter, newMockERC20, newTestableERC20Wrapper, newSwapPool, newMockOracle, newNablaCurve }, } = environment; + const deadlineTime = Math.floor((Date.now() / 1000) + 10); + function assertApproxEq(a: bigint, b: bigint, errorMessage: string): void { if (a !== 0n && b !== 0n) { assertApproxEqRel(a, b, 5n * 10n ** 15n, errorMessage); @@ -72,7 +74,7 @@ export default async function (environment: TestSuiteEnvironment) { WITHDRAW_AMOUNT / 2n - LOW_SLIPPAGE, path, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); const balanceAfter = await asset2.balanceOf(tester); @@ -94,9 +96,6 @@ export default async function (environment: TestSuiteEnvironment) { const router = await newRouter(); const treasury = FERDIE; - //const asset1 = await newMockERC20("Test Token 1", "TEST1"); - //const asset2 = await newMockERC20("Test Token 2", "TEST2"); - const asset1 = await newTestableERC20Wrapper("Test Token 1", "TEST1", 18, [1], [2], [], []); const asset2 = await newTestableERC20Wrapper("Test Token 2", "TEST2", 18, [1], [3], [], []); @@ -124,30 +123,37 @@ export default async function (environment: TestSuiteEnvironment) { return { async setUp() { + + //cleaning mock tokens in standalone chain + await asset1.burn(tester, await asset1.balanceOf(tester)); + await asset2.burn(tester, await asset2.balanceOf(tester)); + + await asset1.burn(treasury, await asset1.balanceOf(treasury)); + await asset2.burn(treasury, await asset2.balanceOf(treasury)); + + await asset1.burn(address(swapPool1), await asset1.balanceOf(address(swapPool1))); + await asset1.burn(address(swapPool2), await asset1.balanceOf(address(swapPool2))); + + await asset2.burn(address(swapPool1), await asset2.balanceOf(address(swapPool1))); + await asset2.burn(address(swapPool2), await asset2.balanceOf(address(swapPool2))); + + await asset1.approve(address(router), MAX_UINT256); await asset2.approve(address(router), MAX_UINT256); await asset1.approve(address(swapPool1), MAX_UINT256); await asset2.approve(address(swapPool2), MAX_UINT256); + //set up await router.setPriceOracle(address(asset1), address(oracle1)); await router.setPriceOracle(address(asset2), address(oracle2)); await router.registerPool(address(asset1), address(swapPool1)); await router.registerPool(address(asset2), address(swapPool2)); - await asset1.burn(tester, await asset1.balanceOf(tester)); await asset1.mint(tester, MINT_AMOUNT); - - await asset2.burn(tester, await asset2.balanceOf(tester)); await asset2.mint(tester, MINT_AMOUNT); - await asset1.burn(treasury, await asset1.balanceOf(treasury)); - await asset2.burn(treasury, await asset2.balanceOf(treasury)); - - await asset1.burn(address(swapPool1), await asset1.balanceOf(address(swapPool1))); - - }, async testSwap() { @@ -180,12 +186,15 @@ export default async function (environment: TestSuiteEnvironment) { ((WITHDRAW_AMOUNT / 2n) * 98n) / 100n - LOW_SLIPPAGE, path, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); + + const balanceAfter = await asset2.balanceOf(tester); const treasuryAfter = await asset2.balanceOf(treasury); + const swapFees = quoteWithoutFee - quoteWithFee; const [, liabilitiesAfter] = await swapPool2.coverage(); @@ -242,7 +251,7 @@ export default async function (environment: TestSuiteEnvironment) { unit(10) - HIGH_SLIPPAGE, path1, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); const path2 = makePath(asset2, asset1); @@ -251,7 +260,7 @@ export default async function (environment: TestSuiteEnvironment) { unit(20) - HIGH_SLIPPAGE, path2, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); const [finalReserves, finalLiabilities] = await swapPool1.coverage(); @@ -302,37 +311,43 @@ export default async function (environment: TestSuiteEnvironment) { unit(10) - HIGH_SLIPPAGE, path1, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); const initialBalance1 = await asset1.balanceOf(tester); const initialBalance2 = await asset2.balanceOf(tester); + // Swap forward const [initialReserves, initialLiabilities] = await swapPool1.coverage(); const [initialReserves2, initialLiabilities2] = await swapPool2.coverage(); + const path2 = makePath(asset1, asset2); const [, forwardSwapOutput] = await router.swapExactTokensForTokens( unit(20), unit(10) - HIGH_SLIPPAGE, path2, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); + const path3 = makePath(asset2, asset1); + await router.swapExactTokensForTokens( forwardSwapOutput, unit(20) - HIGH_SLIPPAGE, path3, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); const [finalReserves, finalLiabilities] = await swapPool1.coverage(); + const [finalReserves2, finalLiabilities2] = await swapPool2.coverage(); + assertEq(finalLiabilities, initialLiabilities, "Liabilities must eventually be equal to initial ones"); assertEq(finalLiabilities2, initialLiabilities2, "Liabilities must eventually be equal to initial ones"); assertGt(finalReserves, (initialReserves * 99999n) / 100000n, "Reserves 1 must eventually be >= initially"); @@ -378,12 +393,14 @@ export default async function (environment: TestSuiteEnvironment) { assertTrue(await router.paused()); vm.expectRevert("Pausable: paused"); + + await router.swapExactTokensForTokens( WITHDRAW_AMOUNT, WITHDRAW_AMOUNT / 2n - LOW_SLIPPAGE, path, tester, - Math.floor(Date.now() / 1000) + deadlineTime ); await router.unpause(); diff --git a/src/actions/compileContract.ts b/src/actions/compileContract.ts index 2cdfb53..8cda032 100644 --- a/src/actions/compileContract.ts +++ b/src/actions/compileContract.ts @@ -60,7 +60,7 @@ async function actuallyCompileContract( updateContractStatus("compiling"); const solangResult = await runCommand([ - "/home/giannipop/root/pendulum/solang/target/release/solang", + "../clones/solang/target/release/solang", "compile", "--no-strength-reduce", // temporary fix for https://github.com/hyperledger/solang/issues/1507 "--target",