Skip to content

Commit

Permalink
Merge pull request #27 from pendulum-chain/26-use-runtime-managed-erc…
Browse files Browse the repository at this point in the history
…20-tokens-in-tests

26 use runtime managed erc20 tokens in tests
  • Loading branch information
gianfra-t authored Oct 20, 2023
2 parents 228d3bb + b1e2938 commit ba1123b
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 21 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
```

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));
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

0 comments on commit ba1123b

Please sign in to comment.