Skip to content

Commit

Permalink
fix deadline issue in swap.ts test
Browse files Browse the repository at this point in the history
  • Loading branch information
gianfra-t committed Oct 4, 2023
1 parent 27a8096 commit 480962f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Alternatively you can use the parameter `local` instead of `foucoco`. This expec
```
brew install binaryen
```

2 changes: 1 addition & 1 deletion nabla/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"contracts": {
"TestableERC20Wrapper": {
"path": "contracts/TestableERC20Wrapper.sol"
"path": "testable-erc20-wrapper/TestableERC20Wrapper.sol"
},
"Router": {
"repository": "nablaContracts",
Expand Down
5 changes: 0 additions & 5 deletions nabla/test/backstop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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], [], []);
Expand Down
2 changes: 1 addition & 1 deletion nabla/test/swapPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
57 changes: 37 additions & 20 deletions nabla/test/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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], [], []);

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/actions/compileContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 480962f

Please sign in to comment.