Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Jul 8, 2024
1 parent 9162889 commit df81c06
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 234 deletions.
3 changes: 2 additions & 1 deletion scripts/e2e-prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'path';
import { BSC_TOKEN, ETH_RPC , RELAYER_URL } from '../src/consts';
import { RouterChainId, getTokenBalance } from '../src/utils';
import { transferErc20, transferFromBSC } from './scriptUtils';
import { transferToken } from '../src/__tests__/testUtils';

dotenv.config({ path: path.join(__dirname, '.env') });
const key = process.env.KEY;
Expand Down Expand Up @@ -89,7 +90,7 @@ const routeWormhole = async (chainId: RouterChainId) => {
console.log(`token balance: ${bal}`);

console.log('xcming to router ...');
const txHash = await transferErc20(token, '0.05', routerAddr, wallet);
const txHash = await transferToken(routerAddr, wallet, token, 0.05);
console.log(`router received token: ${txHash}, waiting for routing ...`);

const routeRes = await axios.post(RELAYER_URL.ROUTE_WORMHOLE, { ...params });
Expand Down
56 changes: 0 additions & 56 deletions scripts/scriptUtils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/__tests__/configs/acala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ endpoint:
- wss://acala-rpc.dwellir.com
mock-signature-host: true
# block: ${env.ACALA_BLOCK_NUMBER}
# block: 6510220
block: 6535566
db: ./db.sqlite
runtime-log-level: 5

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/euphrates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
expectError,
routeEuphrates,
shouldRouteEuphrates,
transferToRouter,
transferToken,
} from './testUtils';

const provider = new AcalaJsonRpcProvider(ETH_RPC.LOCAL);
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('/routeEuphrates', () => {
const bal0 = await fetchTokenBalances(poolId);

console.log('transferring token to router ...');
await transferToRouter(routerAddr, user, inTokenAddr, stakeAmount);
await transferToken(routerAddr, user, inTokenAddr, stakeAmount);

console.log('routing ...');
const routeRes = await routeEuphrates({
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/homa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import {
} from './testConsts';
import {
expectError,
mockXcmToRouter,
routeHoma,
routeHomaAuto,
routeStatus,
shouldRouteHoma,
transferToken,
} from './testUtils';

const provider = new AcalaJsonRpcProvider(ETH_RPC.LOCAL);
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('/routeHoma', () => {
const bal0 = await fetchTokenBalances();

console.log('xcming to router ...');
await mockXcmToRouter(routerAddr, user, DOT, stakeAmount);
await transferToken(routerAddr, user, DOT, stakeAmount);

console.log('routing ...');
const routeRes = await routeHoma({
Expand Down Expand Up @@ -293,7 +293,7 @@ describe('/routeHoma', () => {
});

console.log('xcming to router ...');
await mockXcmToRouter(routerAddr, user, DOT, stakeAmount);
await transferToken(routerAddr, user, DOT, stakeAmount);

console.log('waiting for auto routing ...');
await waitForRoute;
Expand Down
36 changes: 7 additions & 29 deletions src/__tests__/relay.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { AcalaJsonRpcProvider } from '@acala-network/eth-providers';
import { AxiosError } from 'axios';
import { CHAIN_ID_ACALA } from '@certusone/wormhole-sdk';
import { ERC20__factory } from '@acala-network/asset-router/dist/typechain-types';
import { ROUTER_TOKEN_INFO } from '@acala-network/asset-router/dist/consts';
import { describe, expect, it } from 'vitest';

import { ETH_RPC, RELAY_CONFIG } from '../consts';
import { PROD_ADDR } from './testConsts';
import { VAA_10_USDC_ETH_TO_ACALA } from './vaa';
import { relay, shouldRelay } from './testUtils';
import { expectError, relay, shouldRelay } from './testUtils';

const provider = new AcalaJsonRpcProvider(ETH_RPC.LOCAL);

const USDC_ADDR = ROUTER_TOKEN_INFO.usdc.acalaAddr;
const USER_ADDR = '0xBbBBa9Ebe50f9456E106e6ef2992179182889999';

describe('/shouldRelay', () => {
it('when should relay', async () => {
Expand Down Expand Up @@ -55,14 +54,7 @@ describe('/shouldRelay', () => {

expect.fail('should throw error but did not');
} catch (err) {
expect((err as AxiosError).response?.data).toMatchInlineSnapshot(`
{
"error": [
"targetChain is a required field",
],
"msg": "invalid request params!",
}
`);
expectError(err, ['targetChain is a required field'], 400);
}

try {
Expand All @@ -73,14 +65,7 @@ describe('/shouldRelay', () => {

expect.fail('should throw error but did not');
} catch (err) {
expect((err as AxiosError).response?.data).toMatchInlineSnapshot(`
{
"error": [
"originAsset is a required field",
],
"msg": "invalid request params!",
}
`);
expectError(err, ['originAsset is a required field'], 400);
}

try {
Expand All @@ -91,14 +76,7 @@ describe('/shouldRelay', () => {

expect.fail('should throw error but did not');
} catch (err) {
expect((err as AxiosError).response?.data).toMatchInlineSnapshot(`
{
"error": [
"amount is a required field",
],
"msg": "invalid request params!",
}
`);
expectError(err, ['amount is a required field'], 400);
}
});

Expand Down Expand Up @@ -161,7 +139,7 @@ describe('/shouldRelay', () => {
describe('/relay', () => {
it('relay USDC to user', async () => {
const usdc = ERC20__factory.connect(USDC_ADDR, provider);
const curBalRelayer = (await usdc.balanceOf(USER_ADDR)).toBigInt();
const curBalRelayer = (await usdc.balanceOf(PROD_ADDR)).toBigInt();
console.log({ curBalRelayer });

const result = await relay({
Expand All @@ -170,7 +148,7 @@ describe('/relay', () => {
});
expect(result.data?.status).to.eq(1);

const afterBalRelayer = (await usdc.balanceOf(USER_ADDR)).toBigInt();
const afterBalRelayer = (await usdc.balanceOf(PROD_ADDR)).toBigInt();
console.log({ afterBalRelayer });

expect(afterBalRelayer - curBalRelayer).to.eq(10467941n); // 10.467941 USDC
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/testConsts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export const TEST_KEY = {

export const TEST_ADDR_USER = new Wallet(TEST_KEY.USER).address; // 0x0085560b24769dAC4ed057F1B2ae40746AA9aAb6
export const TEST_ADDR_RELAYER = new Wallet(TEST_KEY.RELAYER).address; // 0xe3234f433914d4cfCF846491EC5a7831ab9f0bb3
export const PROD_ADDR = '0xBbBBa9Ebe50f9456E106e6ef2992179182889999';

export const NOT_SUPPORTED_ADDRESS = '';
9 changes: 4 additions & 5 deletions src/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export const sudoTransferToken = async (
}
};

export const transferToRouter = async (
routerAddr: string,
export const transferToken = async (
toAddr: string,
signer: Wallet,
tokenAddr: string,
amount: number,
Expand All @@ -65,18 +65,17 @@ export const transferToRouter = async (
const decimals = await token.decimals();
const routeAmount = parseUnits(String(amount), decimals);

const routerBal = await token.balanceOf(routerAddr);
const routerBal = await token.balanceOf(toAddr);
if (routerBal.gt(0)) {
expect(routerBal.toBigInt()).to.eq(routeAmount.toBigInt());
} else {
const fromTokenBal = await token.balanceOf(signer.address);
if (fromTokenBal.lt(routeAmount)) {
throw new Error(`signer ${signer.address} has no enough token [${tokenAddr}] to transfer! ${fromTokenBal.toBigInt()} < ${routeAmount.toBigInt()}`);
}
await (await token.transfer(routerAddr, routeAmount)).wait();
await (await token.transfer(toAddr, routeAmount)).wait();
}
};
export const mockXcmToRouter = transferToRouter;

export const expectError = (err: any, msg: any, code: number) => {
if (axios.isAxiosError(err)) {
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/wormhole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { describe, expect, it } from 'vitest';

import { ETH_RPC, PARA_ID } from '../consts';
import {
PROD_ADDR,
TEST_ADDR_RELAYER,
TEST_ADDR_USER,
} from './testConsts';
Expand All @@ -15,6 +16,7 @@ import {
shouldRouteWormhole,
sudoTransferToken,
} from './testUtils';
import { parseUnits } from 'ethers/lib/utils';

const USDC_ADDR = ROUTER_TOKEN_INFO.usdc.acalaAddr;
const USDC_ORIGIN_ADDR = ROUTER_TOKEN_INFO.usdc.originAddr;
Expand Down Expand Up @@ -138,8 +140,7 @@ describe('/routeWormhole', () => {
expect(routerAddr).toBeDefined();

console.log('xcming to router ...');
await sudoTransferToken('0xBbBBa9Ebe50f9456E106e6ef2992179182889999', routerAddr, provider, USDC_ADDR, 0.123);
// await transferToRouter(routerAddr, relayer, USDC_ADDR, 0.123);
await sudoTransferToken(PROD_ADDR, routerAddr, provider, USDC_ADDR, 0.123);

const curBalRelayer = (await usdc.balanceOf(TEST_ADDR_RELAYER)).toBigInt();
console.log({ curBalRelayer });
Expand All @@ -157,7 +158,7 @@ describe('/routeWormhole', () => {
const afterBalRelayer = (await usdc.balanceOf(TEST_ADDR_RELAYER)).toBigInt();
console.log({ afterBalRelayer });

expect(afterBalRelayer - curBalRelayer).to.eq(40000n);
expect(afterBalRelayer - curBalRelayer).to.eq(parseUnits('0.04', 6).toBigInt()); // USDC has 6 decimals
});

// describe.skip('when should not route', () => {})
Expand Down
8 changes: 5 additions & 3 deletions src/__tests__/xcm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it } from 'vitest';

import { ETH_RPC, PARA_ID } from '../consts';
import {
PROD_ADDR,
TEST_ADDR_RELAYER,
} from './testConsts';
import {
Expand All @@ -22,6 +23,7 @@ import {
shouldRouteXcm,
sudoTransferToken,
} from './testUtils';
import { parseUnits } from 'ethers/lib/utils';


const DAI_ADDR = ROUTER_TOKEN_INFO.dai.acalaAddr;
Expand Down Expand Up @@ -130,7 +132,7 @@ describe('/routeXcm', () => {
const { routerAddr } = res.data;

console.log('transferring to router ...');
await sudoTransferToken('0xBbBBa9Ebe50f9456E106e6ef2992179182889999', routerAddr, provider, DAI_ADDR, 1.23);
await sudoTransferToken(PROD_ADDR, routerAddr, provider, DAI_ADDR, 1.23);

const dai = ERC20__factory.connect(DAI_ADDR, provider);
const curBalRelayer = (await dai.balanceOf(TEST_ADDR_RELAYER)).toBigInt();
Expand All @@ -143,7 +145,7 @@ describe('/routeXcm', () => {
const afterBalRelayer = (await dai.balanceOf(TEST_ADDR_RELAYER)).toBigInt();
console.log({ afterBalRelayer });

expect(afterBalRelayer - curBalRelayer).to.eq(40000000000000000n);
expect(afterBalRelayer - curBalRelayer).to.eq(parseUnits('0.04', 18).toBigInt()); // DAI has 18 decimals
expect((await dai.balanceOf(routerAddr)).toBigInt()).to.eq(0n);

// router should be destroyed
Expand Down Expand Up @@ -182,7 +184,7 @@ describe('/relayAndRoute', () => {
const afterBalRelayer = (await dai.balanceOf(TEST_ADDR_RELAYER)).toBigInt();
console.log({ afterBalRelayer });

expect(afterBalRelayer - curBalRelayer).to.eq(40000000000000000n);
expect(afterBalRelayer - curBalRelayer).to.eq(parseUnits('0.04', 18).toBigInt()); // DAI has 18 decimals
expect((await dai.balanceOf(routerAddr)).toBigInt()).to.eq(0n);

// router should be destroyed
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export * from './validate';
export * from './wormhole';
export * from './address';
export * from './error';
export * from './token';
29 changes: 0 additions & 29 deletions src/utils/token.ts

This file was deleted.

Loading

0 comments on commit df81c06

Please sign in to comment.