Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26 use runtime managed erc20 tokens in tests #27

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
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
```

17 changes: 12 additions & 5 deletions nabla/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"contracts": {
"TestableERC20Wrapper": {
"repository": "pendulumWrappers",
"path": "testable-erc20-wrapper/TestableERC20Wrapper.sol"
},
"Router": {
"repository": "nablaContracts",
"path": "contracts/src/core/Router.sol"
Expand Down Expand Up @@ -50,11 +54,14 @@
"git": "https://github.com/0xamberhq/contracts.git",
"branch": "feature/backstop-pool-coverage-ratio",
"init": "yarn",
"importpaths": ["contracts/@chain/pendulum", "node_modules"]
"importpaths": [
"contracts/@chain/pendulum",
"node_modules"
]
},
"pendulumWrappers": {
"git": "https://github.com/pendulum-chain/pendulum-ink-wrapper",
"branch": "32-support-latest-solang-version"
"branch": "master"
}
},
"networks": {
Expand Down Expand Up @@ -89,9 +96,9 @@
"buildFolder": "../target",
"limits": {
"gas": {
"refTime": "100000000000",
"proofSize": "10000000"
"refTime": "10000000000000000",
"proofSize": "10000000000000000"
},
"storageDeposit": "10000000000000"
}
}
}
77 changes: 77 additions & 0 deletions nabla/test/TestableERC20Tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import { TestContract, TestSuiteEnvironment } from "../../src/index";
import { assertApproxEqAbs, assertApproxEqRel, assertEq, assertGt, assertLt, e } from "../../src/index";

const MAX_UINT256 = 2n ** 256n - 1n;

const BOB = "6k6gXPB9idebCxqSJuqpjPaqfYLQbdLHhvsANH8Dg8GQN3tT";

export default async function (environment: TestSuiteEnvironment) {
const {
address,
unit,
milliUnit,
microUnit,
getContractByAddress,
vm,
tester,
constructors: {
newTestableERC20Wrapper,
},
} = environment;

let router: TestContract;
let backstop: TestContract;
let swapPool1: TestContract;
let swapPool2: TestContract;

const assetNative = await newTestableERC20Wrapper("TestNative", "TEST1", 12, [0], [0], [], []);
const token1 = await newTestableERC20Wrapper("TestNonNative", "TEST2", 12, [1], [1], [], []);

const MINT_AMOUNT = unit(10000);
const BURN_AMOUNT = unit(10);

return {
async setUp() {

},
async testMintsNative() {
let totalSupplyBef = await assetNative.totalSupply();
await assetNative.mint(BOB, MINT_AMOUNT);
let totalSupplyAft = await assetNative.totalSupply();

assertEq(totalSupplyAft - totalSupplyBef, MINT_AMOUNT);
},

async testMintsTokensPallet() {
let totalSupplyBef = await token1.totalSupply();
let balanceBobBef = await token1.balanceOf(BOB);

await token1.mint(BOB, MINT_AMOUNT);

let totalSupplyAft = await token1.totalSupply();
let balanceBob = await token1.balanceOf(BOB);

assertEq(totalSupplyAft - totalSupplyBef, MINT_AMOUNT);
assertEq(balanceBob - balanceBobBef, MINT_AMOUNT);
},

async testBurnsNative() {
let totalSupplyBef = await assetNative.totalSupply();
await assetNative.burn(BOB, BURN_AMOUNT);
let totalSupplyAft = await assetNative.totalSupply();

assertEq(totalSupplyBef - totalSupplyAft, BURN_AMOUNT);
},

async testBurnsToken() {
let totalSupplyBef = await token1.totalSupply();
await token1.burn(BOB, BURN_AMOUNT);
let totalSupplyAft = await token1.totalSupply();

assertEq(totalSupplyBef - totalSupplyAft, BURN_AMOUNT);
},
};
}
27 changes: 24 additions & 3 deletions nabla/test/backstop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default async function (environment: TestSuiteEnvironment) {
newRouter,
newTestableBackstopPool,
newMockERC20,
newTestableERC20Wrapper,
newMockOracle,
newNablaCurve,
newTestableSwapPool,
Expand All @@ -41,9 +42,10 @@ 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");
const usd = await newTestableERC20Wrapper("Test Backstop Token", "USD", 18, [1], [1], [], []);
const asset1 = await newTestableERC20Wrapper("Test Token 1", "TEST1", 18, [1], [2], [], []);
const asset2 = await newTestableERC20Wrapper("Test Token 2", "TEST2", 18, [1], [3], [], []);


const oracleUsd = await newMockOracle(address(usd), unit(1));
const oracle1 = await newMockOracle(address(asset1), unit(5));
Expand Down Expand Up @@ -118,17 +120,35 @@ export default async function (environment: TestSuiteEnvironment) {
await backstop.addSwapPool(address(swapPool1), 0);
await backstop.addSwapPool(address(swapPool2), 0);

//we ensure that only the MINT_AMOUNT is on the required accounts by
//burning pre-existing balances.

//This is required since the assets are on the standalone testing
//chain and we cannot ensure in the test alone that the balances
//of these tokens is indeed 0 (a test could have run earlier)
await asset1.burn(tester, await asset1.balanceOf(tester));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to burn before we mint?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is related to

Specifically cleaning token balances before execution of the tests and modifying time delay for swapExecution.

Maybe we should find a clearer way to do this? How about a teardown function, or we add cleanup-related code to the end of each test? Otherwise we should maybe at least add a comment that explains why we burn before we mint.

Copy link
Contributor Author

@gianfra-t gianfra-t Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this is just to ensure that the balance of the tokens for the relevant accounts is 0 before the mint and at the end of this setUp() function we have only the expected minted amount.

I will add some comments to clarify this, but I don't think we should add another function since we are already in the setUp() function and this is the cleanup for a test that came before (if any), let me know it makes sense @ebma.

BTW, it is true that perhaps a teardown at the end also makes sense, but given that the tests stops if there is an error, as of now I don't think there is a way to actually make sure that the teardown actually runs. That's why I think for now cleaning just in case in the setUp makes more sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I see your point. Although we don't support 'teardown' functions yet, we could just try to execute them (if any) in a new finally {} block at the end of the try-catch here. This way it will be executed in any case.

I'm also fine with your existing implementation actually, the only thing that bothers me a bit is that the comment reasoning why we need to burn before minting is only included in one file. And I see why since copy-pasting it into any file is annoying. But with a teardown function for each test, it might be self-explanatory enough that we don't even need a comment in the first place. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree that would be the better approach. Could we perhaps open a separate issue and wait for this one? Since it would be touching a separate part of the code logic not just the tests but also how they run.
In the meantime if you are okay with it I can just copy the comment to all the setups so we don't forget what is going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍 could you create that follow-up ticket?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

await asset1.mint(tester, MINT_AMOUNT);

await asset2.burn(tester, await asset2.balanceOf(tester));
await asset2.mint(tester, MINT_AMOUNT);

await usd.burn(tester, await usd.balanceOf(tester));
await usd.mint(tester, MINT_AMOUNT);

vm.startPrank(BOB);
await asset1.approve(address(swapPool1), MAX_UINT256);
await asset2.approve(address(swapPool2), MAX_UINT256);
await usd.approve(address(backstop), MAX_UINT256);

await asset1.burn(BOB, await asset1.balanceOf(BOB));
await asset1.mint(BOB, MINT_AMOUNT);

await asset2.burn(BOB, await asset2.balanceOf(BOB));
await asset2.mint(BOB, MINT_AMOUNT);

await usd.burn(BOB, await usd.balanceOf(BOB));
await usd.mint(BOB, MINT_AMOUNT);

await swapPool1.deposit(MINT_AMOUNT);
await swapPool2.deposit(MINT_AMOUNT);
await backstop.deposit(MINT_AMOUNT);
Expand Down Expand Up @@ -210,6 +230,7 @@ export default async function (environment: TestSuiteEnvironment) {
},

async testImmediateBackstopWithdrawal() {

const [lpTokens, fee] = await backstop.deposit(unit(20));
const [reservesBefore, liabilitiesBefore] = await backstop.coverage();
const [simulatedPayout] = await backstop.simulateWithdrawal((lpTokens * 3n) / 4n);
Expand Down
16 changes: 14 additions & 2 deletions nabla/test/swapPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function (environment: TestSuiteEnvironment) {
getContractByAddress,
vm,
tester,
constructors: { newMockERC20, newNablaCurve, newTestableSwapPool },
constructors: { newMockERC20, newNablaCurve, newTestableSwapPool, newTestableERC20Wrapper },
} = environment;

function assertApproxEq(a: bigint, b: bigint, errorMessage: string): void {
Expand Down Expand Up @@ -46,17 +46,29 @@ 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");

return {
async setUp() {
await asset.approve(address(pool), MAX_UINT256);

await asset.burn(tester, await asset.balanceOf(tester));
await asset.mint(tester, MINT_AMOUNT);

// Important to deposit something before tests start, as first deposit
// does not invoke usual _sharesToMint() logic, due to total supply being 0

//we ensure that only the MINT_AMOUNT is on the required accounts by
//burning pre-existing balances.

//This is required since the assets are on the standalone testing
//chain and we cannot ensure in the test alone that the balances
//of these tokens is indeed 0 (a test could have run earlier)
await asset.burn(CHARLIE, await asset.balanceOf(CHARLIE));
await asset.mint(CHARLIE, unit(1));

vm.startPrank(CHARLIE);
await asset.approve(address(pool), unit(1));
await pool.deposit(unit(1));
Expand Down
Loading