Skip to content

Commit

Permalink
Run router test with just 2 coins
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Sep 20, 2024
1 parent cdd2e4c commit 95765a6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ on:

jobs:
test:
timeout-minutes: 90
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
10 changes: 2 additions & 8 deletions test/apy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import curve from "../src/index.js";
import { curve as _curve } from "../src/curve.js";
import { getPool, PoolTemplate } from "../src/pools/index.js";
import { IReward } from "../src/interfaces.js";
import { ETH_RPC, OPTIMISM_RPC, XDAI_RPC, POLYGON_RPC, FANTOM_RPC, MOONBEAM_RPC, KAVA_RPC, ARBITRUM_RPC, CELO_RPC, AVALANCHE_RPC, AURORA_RPC } from "./rpcUrls.test.js";
import { ETH_RPC } from "./rpcUrls.test.js";


const poolStatsTest = (name: string) => {
Expand Down Expand Up @@ -58,18 +58,12 @@ const poolStatsTest = (name: string) => {

describe('Compare calculated APY with APY from API', async function () {
this.timeout(120000);
let POOLS: string[] = [];

before(async function () {
await curve.init('JsonRpc', { url: ETH_RPC }, { gasPrice: 0 });
await curve.factory.fetchPools();
await curve.cryptoFactory.fetchPools();
POOLS = curve.getPoolList();
});

it('', async function () {
for (const poolName of POOLS) {
poolStatsTest(poolName);
}
});
curve.getPoolList().map(poolStatsTest);
})
7 changes: 2 additions & 5 deletions test/factoryPoolsData.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";
import { curve } from "../src/curve.js";
import { ETH_RPC, ARBITRUM_RPC, AURORA_RPC } from "./rpcUrls.test.js";
import { ETH_RPC } from "./rpcUrls.test.js";
import { IDict, IPoolData } from "../src/interfaces.js";
import { BLACK_LIST } from "../src/factory/factory.js";

Expand Down Expand Up @@ -69,10 +69,7 @@ const factoryPoolsDataTest = (factoryPoolsDataFromApi: IDict<IPoolData>, factory
describe('Factory pools data', async function () {
this.timeout(120000);

before(async function() {
await curve.init('JsonRpc', { url: ETH_RPC }, { gasPrice: 0 });
// await curve.init('JsonRpc', {},{ gasPrice: 0 });
});
before(() => curve.init('JsonRpc', { url: ETH_RPC }, { gasPrice: 0 }))

it('Factory', async function () {
await curve.fetchFactoryPools();
Expand Down
28 changes: 7 additions & 21 deletions test/required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import {expect} from "chai";
import curve from "../src/index.js";
import {ETH_RPC} from "./rpcUrls.test.js";

const test = (baseValue: number, resultValue: number, errorInPercentage: number) => {
const isAlmostEqual = (baseValue: number, resultValue: number, errorInPercentage: number) => {
const difference = Math.abs(baseValue - resultValue);
if ((difference / baseValue) * 100 < errorInPercentage) {
return true;
} else {
return false;
}
return (difference / baseValue) * 100 < errorInPercentage;
};

const testOfSwapRequired = (pool: string, curve: any) => {
Expand All @@ -17,7 +13,7 @@ const testOfSwapRequired = (pool: string, curve: any) => {
const poolInstance = curve.getPool(pool)
const to = await poolInstance.swapExpected(0, 1, 5);
const result = await poolInstance.swapRequired(0, 1, to);
const isTestSuccessful = test(5, Number(result), 0.5)
const isTestSuccessful = isAlmostEqual(5, Number(result), 0.5)
expect(isTestSuccessful).to.be.equal(true)
})
})
Expand All @@ -30,7 +26,7 @@ const testOfSwapWrappedRequired = (pool: string, curve: any) => {
const poolInstance = curve.getPool(pool)
const to = await poolInstance.swapWrappedExpected(0, 1, 5);
const result = await poolInstance.swapWrappedRequired(0, 1, to);
const isTestSuccessful = test(5, Number(result), 0.5)
const isTestSuccessful = isAlmostEqual(5, Number(result), 0.5)
expect(isTestSuccessful).to.be.equal(true)
} catch (e: any) {
const isTestSuccessful = e.message.startsWith(`swapWrappedExpected method doesn't exist for pool`)
Expand All @@ -43,27 +39,17 @@ const testOfSwapWrappedRequired = (pool: string, curve: any) => {
describe('Test swapRequired and swapWrappedRequired methods in PoolTemplate', async function (){
this.timeout(120000)

let allPools: string[] = [];

before(async function (){
await curve.init('JsonRpc', { url: ETH_RPC }, { gasPrice: 0 });
await curve.factory.fetchPools();
await curve.crvUSDFactory.fetchPools();
await curve.EYWAFactory.fetchPools();
await curve.cryptoFactory.fetchPools();
await curve.tricryptoFactory.fetchPools();

allPools = curve.getMainPoolList();
})

it('', function () {
describe('TEST', () => {
it('CONNECT WITH BLOCKCHAIN', () => {
for(const pool of allPools) {
testOfSwapRequired(pool, curve)
testOfSwapWrappedRequired(pool, curve)
}
})
})
curve.getMainPoolList().map(pool => {
testOfSwapRequired(pool, curve)
testOfSwapWrappedRequired(pool, curve)
})
})
58 changes: 34 additions & 24 deletions test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import {_getCoinAddresses, BN} from "../src/utils.js";
import curve from "../src/index.js";
import {curve as _curve} from "../src/curve.js";
import {JsonRpcSigner} from "ethers";
import {ETH_RPC} from "./rpcUrls.test.js";


const AAVE_TOKENS = ['adai', 'ausdc', 'ausdt', 'asusd', 'awbtc', 'amdai', 'amusdt', 'amusdc', 'amwbtc', 'avdai', 'avusdt', 'avusdc', 'avwbtc', 'gdai', 'gusdc', 'gfusdt'];

const routerSwapTest = async (coin1: string, coin2: string) => {
const title = 'Swap ' + coin1 + ' --> ' + coin2;
console.time(title);

const amount = '1';
const initialBalances = await curve.getBalances([coin1, coin2]) as string[];

Expand All @@ -21,7 +25,14 @@ const routerSwapTest = async (coin1: string, coin2: string) => {
console.log("Output:", output);
console.log("Required:", required);

await curve.router.swap(coin1, coin2, amount);
try {
await curve.router.swap(coin1, coin2, amount);
} catch (e) {
if ((e as Error).message === "This pair can't be exchanged") return;
throw e;
} finally {
console.timeEnd(title);
}

const balances = await curve.getBalances([coin1, coin2]) as string[];

Expand All @@ -37,21 +48,31 @@ const routerSwapTest = async (coin1: string, coin2: string) => {
}

describe('Router swap', async function () {
this.timeout(240000);
this.timeout(240_000); // 4 minutes

const resetFork = () => _curve.provider.send("hardhat_reset", [{ forking: { jsonRpcUrl: ETH_RPC } }]);

before(async function () {
console.time('init');
await curve.init('JsonRpc', {}, { gasPrice: 0 });
await curve.factory.fetchPools();
await curve.cryptoFactory.fetchPools();
await curve.tricryptoFactory.fetchPools();
await curve.crvUSDFactory.fetchPools();
await curve.EYWAFactory.fetchPools();
await resetFork();
await Promise.all([
curve.factory.fetchPools(),
curve.cryptoFactory.fetchPools(),
curve.tricryptoFactory.fetchPools(),
curve.crvUSDFactory.fetchPools(),
curve.EYWAFactory.fetchPools(),
]);
console.timeEnd('init');
});

beforeEach(resetFork);

// const coins = Object.keys(COINS_POLYGON).filter((c) => c !== 'snx' && c !== 'eurs'); // TODO remove eurs

// ETHEREUM
const coins = ['sbtc', 'susd', 'dai', 'mim', 'frax', 'crv', 'cvx', 'eth', 'steth', 'wsteth', 'frxeth', 'sfrxeth', 'wbeth', 'eurt', '3crv', '0x62b9c7356a2dc64a1969e19c23e4f579f9810aa7', '0x045da4bfe02b320f4403674b3b7d121737727a36']; // cvxCRV, DCHF
const coins = ['crv', 'dai'];
// const coins = ['sbtc', 'susd', 'dai', 'mim', 'frax', 'crv', 'cvx', 'eth', 'steth', 'wsteth', 'frxeth', 'sfrxeth', 'wbeth', 'eurt', '3crv', '0x62b9c7356a2dc64a1969e19c23e4f579f9810aa7', '0x045da4bfe02b320f4403674b3b7d121737727a36']; // cvxCRV, DCHF

// POLYGON
// const coins = ['wbtc', 'crv', 'dai', 'usdc', 'usdt', 'eurt', 'weth', 'renbtc', 'amdai', 'amusdc', 'amusdt', 'am3crv', 'matic',
Expand All @@ -78,26 +99,15 @@ describe('Router swap', async function () {
// AURORA && KAVA && CELO
// const coins = ['dai', 'usdc', 'usdt'];

for (const coin1 of coins) {
for (const coin2 of coins) {
if (coin1 !== coin2) {
it(`${coin1} --> ${coin2}`, async function () {
try {
await routerSwapTest(coin1, coin2);
} catch (err: any) {
if (err.message != "This pair can't be exchanged") {
throw err;
}
}
});
}
}
}
coins.forEach(coin1 => coins.forEach(coin2 =>
coin1 !== coin2 && it(`${coin1} --> ${coin2}`, () => routerSwapTest(coin1, coin2))
))
})


/**
* Function that limits the number of calls to the function per time period, delaying the next call.
* We use this to avoid hitting the rate limit of the API.
*/
function rateLimit<R, F extends (...args: Parameters<F>) => Promise<R>>(func: F, timeout: number): F {
let lastCall = Date.now() - timeout;
Expand Down Expand Up @@ -138,7 +148,7 @@ async function stealTokens(coinName: string, amount: string = `0x1${'0'.repeat(2
console.log(`Stole ${coinName} from ${richAddress}: ${JSON.stringify(tx)}`);
}
catch (e) {
console.error(`Cannot steal ${amount} ${coinName} from ${richAddress}: ${e}`);
console.error(`Cannot steal ${amount} ${coinName} from ${richAddress}, even if it has ${richAccount.rawBalance}: ${e}`);
throw e;
} finally {
cleanup();
Expand Down
12 changes: 1 addition & 11 deletions test/rpcUrls.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
export const ETH_RPC = process.env.ETH_RPC;
export const OPTIMISM_RPC = process.env.OPTIMISM_RPC;
export const XDAI_RPC = process.env.XDAI_RPC;
export const POLYGON_RPC = process.env.POLYGON_RPC;
export const FANTOM_RPC = process.env.FANTOM_RPC;
export const MOONBEAM_RPC = process.env.MOONBEAM_RPC;
export const KAVA_RPC = process.env.KAVA_RPC;
export const ARBITRUM_RPC = process.env.ARBITRUM_RPC;
export const CELO_RPC = process.env.CELO_RPC;
export const AVALANCHE_RPC = process.env.AVALANCHE_RPC;
export const AURORA_RPC = process.env.AURORA_RPC;
export const { ETH_RPC } = process.env;

0 comments on commit 95765a6

Please sign in to comment.