Skip to content

Commit

Permalink
wrap in api namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Sep 18, 2024
1 parent 1efe9f4 commit c0cb492
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 92 deletions.
17 changes: 8 additions & 9 deletions src/__tests__/euphrates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import {
TEST_KEY,
} from './testConsts';
import {
api,
expectError,
routeEuphrates,
shouldRouteEuphrates,
transferToken,
} from './testUtils';

Expand All @@ -41,11 +40,11 @@ describe.concurrent('/shouldRouteEuphrates', () => {
const recipient = '0x0085560b24769dAC4ed057F1B2ae40746AA9aAb6';

const testShouldRouteEuphrates = async (params: RouteParamsEuphrates) => {
let res = await shouldRouteEuphrates(params);
let res = await api.shouldRouteEuphrates(params);
expect(res).toMatchSnapshot();

// should be case insensitive
res = await shouldRouteEuphrates({
res = await api.shouldRouteEuphrates({
...params,
recipient: params.recipient.toLocaleLowerCase(),
});
Expand All @@ -64,7 +63,7 @@ describe.concurrent('/shouldRouteEuphrates', () => {
describe('when should not route', () => {
it('when missing params', async () => {
try {
await shouldRouteEuphrates({
await api.shouldRouteEuphrates({
recipient,
});
expect.fail('did not throw an err');
Expand All @@ -73,7 +72,7 @@ describe.concurrent('/shouldRouteEuphrates', () => {
}

try {
await shouldRouteEuphrates({
await api.shouldRouteEuphrates({
poolId: 0,
});
expect.fail('did not throw an err');
Expand All @@ -83,7 +82,7 @@ describe.concurrent('/shouldRouteEuphrates', () => {
});

it('when bad params', async () => {
const res = await shouldRouteEuphrates({
const res = await api.shouldRouteEuphrates({
recipient,
poolId: 520,
});
Expand Down Expand Up @@ -159,7 +158,7 @@ describe('/routeEuphrates', () => {
recipient: user.address,
poolId,
};
const res = await shouldRouteEuphrates(routeArgs);
const res = await api.shouldRouteEuphrates(routeArgs);
({ routerAddr } = res.data);

// make sure user has enough DOT/LCDOT to transfer to router
Expand All @@ -180,7 +179,7 @@ describe('/routeEuphrates', () => {
await transferToken(routerAddr, user, inTokenAddr, stakeAmount);

console.log('routing ...');
const routeRes = await routeEuphrates({
const routeRes = await api.routeEuphrates({
...routeArgs,
token: inTokenAddr,
});
Expand Down
31 changes: 14 additions & 17 deletions src/__tests__/homa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ import {
TEST_KEY,
} from './testConsts';
import {
api,
expectError,
routeHoma,
routeHomaAuto,
routeStatus,
shouldRouteHoma,
transferToken,
} from './testUtils';

Expand All @@ -41,7 +38,7 @@ describe.concurrent('/shouldRouteHoma', () => {
it('to evm address', async () => {
// for (const network of [Object.values(Mainnet)]) { // TODO: enable this after deploying contract to karura
for (const chain of ['acala']) {
let res = await shouldRouteHoma({
let res = await api.shouldRouteHoma({
destAddr,
chain,
});
Expand All @@ -56,7 +53,7 @@ describe.concurrent('/shouldRouteHoma', () => {
`);

// should be case insensitive
res = await shouldRouteHoma({
res = await api.shouldRouteHoma({
destAddr: destAddr.toLocaleLowerCase(),
chain,
});
Expand All @@ -75,7 +72,7 @@ describe.concurrent('/shouldRouteHoma', () => {
it('to substrate address', async () => {
// for (const network of [Object.values(Mainnet)]) { // TODO: enable this after deploying contract to karura
for (const chain of ['acala']) {
const res = await shouldRouteHoma({
const res = await api.shouldRouteHoma({
destAddr: destAddrSubstrate,
chain,
});
Expand All @@ -95,7 +92,7 @@ describe.concurrent('/shouldRouteHoma', () => {
describe('when should not route', () => {
it('when missing params', async () => {
try {
await shouldRouteHoma({
await api.shouldRouteHoma({
destAddr,
});
expect.fail('did not throw an err');
Expand All @@ -104,7 +101,7 @@ describe.concurrent('/shouldRouteHoma', () => {
}

try {
await shouldRouteHoma({
await api.shouldRouteHoma({
chain: Mainnet.Acala,
});
expect.fail('did not throw an err');
Expand All @@ -113,7 +110,7 @@ describe.concurrent('/shouldRouteHoma', () => {
}

try {
await shouldRouteHoma({
await api.shouldRouteHoma({
chain: 'mandala',
destAddr,
});
Expand All @@ -124,7 +121,7 @@ describe.concurrent('/shouldRouteHoma', () => {
});

it('when bad params', async () => {
const res = await shouldRouteHoma({
const res = await api.shouldRouteHoma({
chain: Mainnet.Acala,
destAddr: '0xaaaaaaaaaa',
});
Expand Down Expand Up @@ -197,7 +194,7 @@ describe('/routeHoma', () => {
destAddr,
chain: 'acala',
};
const res = await shouldRouteHoma(routeArgs);
const res = await api.shouldRouteHoma(routeArgs);
({ routerAddr } = res.data);

// make sure user has enough DOT to transfer to router
Expand All @@ -217,7 +214,7 @@ describe('/routeHoma', () => {
await transferToken(routerAddr, user, DOT, stakeAmount);

console.log('routing ...');
const routeRes = await routeHoma({
const routeRes = await api.routeHoma({
...routeArgs,
token: DOT,
});
Expand Down Expand Up @@ -253,7 +250,7 @@ describe('/routeHoma', () => {
destAddr,
chain: 'acala',
};
const res = await shouldRouteHoma(routeArgs);
const res = await api.shouldRouteHoma(routeArgs);
({ routerAddr } = res.data);

// make sure user has enough DOT to transfer to router
Expand All @@ -270,7 +267,7 @@ describe('/routeHoma', () => {
const bal0 = await fetchTokenBalances();

console.log('sending auto routing request ...');
const routeRes = await routeHomaAuto({
const routeRes = await api.routeHomaAuto({
...routeArgs,
token: DOT,
});
Expand All @@ -279,7 +276,7 @@ describe('/routeHoma', () => {

const waitForRoute = new Promise<void>((resolve, reject) => {
const pollRouteStatus = setInterval(async () => {
const res = await routeStatus({ id: reqId });
const res = await api.routeStatus({ id: reqId });
const { status } = res.data[0];
console.log(`current status: ${status}`);

Expand All @@ -299,7 +296,7 @@ describe('/routeHoma', () => {
await waitForRoute;

// query status by destAddr should also returns same result
const { data } = await routeStatus({ destAddr });
const { data } = await api.routeStatus({ destAddr });
const reqInfo = data.find(info => info.reqId === reqId);
expect(reqInfo).not.to.be.undefined;
expect(reqInfo.status).to.eq(RouteStatus.Complete);
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { api, expectError } from './testUtils';
import { describe, expect, it } from 'vitest';
import { expectError, health, noRoute, testTimeout, version } from './testUtils';

describe.concurrent('/miscellaneous', () => {
describe('when no route for the request', () => {
it('throws correct error', async () => {
try {
await noRoute({});
await api.noRoute({});

expect.fail('/noRoute did not throw when it should!');
} catch (err) {
Expand All @@ -16,15 +16,15 @@ describe.concurrent('/miscellaneous', () => {

describe('/version', () => {
it('works', async () => {
const res = await version({});
const res = await api.version({});
expect(res).to.not.be.undefined;
});
});

describe('/testTimeout', () => {
it('works', async () => {
const startTime = Date.now();
await testTimeout({
await api.testTimeout({
timeout: 1000,
});
const endTime = Date.now();
Expand All @@ -35,7 +35,7 @@ describe.concurrent('/miscellaneous', () => {

describe('/health', () => {
it('works', async () => {
const res = await health({});
const res = await api.health({});

expect(Number(res.data.relayerBalAcala)).to.be.gt(0);
// expect(Number(res.data.relayerBalKarura)).to.be.gt(0);
Expand Down
27 changes: 13 additions & 14 deletions src/__tests__/relay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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, VAA_ALREADY_COMPLETED, VAA_TINY_JITOSOL_SOL_TO_ACALA } from './vaa';
import { expectError, relay, shouldRelay } from './testUtils';
import { api, expectError } from './testUtils';

const provider = new AcalaJsonRpcProvider(ETH_RPC.LOCAL);

Expand All @@ -20,7 +20,7 @@ describe('/shouldRelay', () => {
const supported = RELAY_CONFIG[targetChain];

for (const [token, minTransfer] of Object.entries(supported)) {
const res = await shouldRelay({
const res = await api.shouldRelay({
targetChain,
originAsset: token,
amount: minTransfer,
Expand All @@ -31,7 +31,7 @@ describe('/shouldRelay', () => {

// if not lower case address
for (const [token, minTransfer] of Object.entries(supported)) {
const res = await shouldRelay({
const res = await api.shouldRelay({
targetChain,
originAsset: token.toUpperCase(),
amount: minTransfer,
Expand All @@ -48,7 +48,7 @@ describe('/shouldRelay', () => {

it('when missing params', async () => {
try {
await shouldRelay({
await api.shouldRelay({
originAsset: '0xddb64fe46a91d46ee29420539fc25fd07c5fea3e',
amount: '10000',
});
Expand All @@ -59,7 +59,7 @@ describe('/shouldRelay', () => {
}

try {
await shouldRelay({
await api.shouldRelay({
targetChain,
amount: '10000',
});
Expand All @@ -70,7 +70,7 @@ describe('/shouldRelay', () => {
}

try {
await shouldRelay({
await api.shouldRelay({
targetChain,
originAsset: '0xddb64fe46a91d46ee29420539fc25fd07c5fea3e',
});
Expand All @@ -82,7 +82,7 @@ describe('/shouldRelay', () => {
});

it('when relay condition not met', async () => {
let res = await shouldRelay({
let res = await api.shouldRelay({
targetChain: 3104,
originAsset: '0xddb64fe46a91d46ee29420539fc25fd07c5fea3e',
amount: '10000',
Expand All @@ -95,7 +95,7 @@ describe('/shouldRelay', () => {
`);

const originAsset = '0x111111111191d46ee29420539fc25f0000000000';
res = await shouldRelay({
res = await api.shouldRelay({
targetChain,
originAsset,
amount: '10000',
Expand All @@ -107,7 +107,7 @@ describe('/shouldRelay', () => {
}
`);

res = await shouldRelay({
res = await api.shouldRelay({
targetChain,
originAsset: JITOSOL,
amount: '10',
Expand All @@ -121,7 +121,7 @@ describe('/shouldRelay', () => {
});

it('when amount is not number', async () => {
const res = await shouldRelay({
const res = await api.shouldRelay({
targetChain,
originAsset: JITOSOL,
amount: '{"type":"BigNumber","hex":"0xe8d4a51000"}',
Expand All @@ -136,14 +136,13 @@ describe('/shouldRelay', () => {
});
});


describe('/relay', () => {
it('relay USDC to user', async () => {
const usdc = ERC20__factory.connect(USDC_ADDR, provider);
const curBalRelayer = (await usdc.balanceOf(PROD_ADDR)).toBigInt();
console.log({ curBalRelayer });

const result = await relay({
const result = await api.relay({
targetChain: CHAIN_ID_ACALA,
signedVAA: VAA_10_USDC_ETH_TO_ACALA,
});
Expand All @@ -157,7 +156,7 @@ describe('/relay', () => {

it('when amount too small', async () => {
try {
await relay({
await api.relay({
targetChain: CHAIN_ID_ACALA,
signedVAA: VAA_TINY_JITOSOL_SOL_TO_ACALA,
});
Expand All @@ -170,7 +169,7 @@ describe('/relay', () => {

it('when contract throws', async () => {
try {
await relay({
await api.relay({
targetChain: CHAIN_ID_ACALA,
signedVAA: VAA_ALREADY_COMPLETED,
});
Expand Down
Loading

0 comments on commit c0cb492

Please sign in to comment.