Skip to content

Commit

Permalink
improved error handling (#67)
Browse files Browse the repository at this point in the history
* improved error handling

* fix grammar
  • Loading branch information
shunjizhan authored Jul 10, 2024
1 parent c9264df commit 802c46a
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 47 deletions.
37 changes: 37 additions & 0 deletions scripts/bridgeToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { JsonRpcProvider } from '@ethersproject/providers';
import { Wallet } from 'ethers';
import { parseUnits } from 'ethers/lib/utils';
import assert from 'assert';
import dotenv from 'dotenv';

import { ETH_RPC } from '../src/consts';
import { bridgeToken } from './utils';

dotenv.config();

(async () => {
const key = process.env.ACALA_PRIVATE_KEY;
assert(key, 'KEY is required');

const provider = new JsonRpcProvider(ETH_RPC.BSC);
const wallet = new Wallet(key, provider);

const srcChain = 'bsc';
const dstChain = 'acala';
const dstAddr = '0xb0D205eB2355795e7F95B02E23e030FacEa1E002';
const DAI_BSC_ADDR = '0x3413a030EF81a3dD5a302F4B4D11d911e12ed337';
const DAI_DECIMALS = 18;
const amount = parseUnits('10.2', DAI_DECIMALS);

const receipt = await bridgeToken(
wallet,
srcChain,
dstChain,
dstAddr,
DAI_BSC_ADDR,
amount,
);

console.log(`txHash: ${receipt.transactionHash}`);
assert(receipt.status === 1, 'tx failed!');
})();
30 changes: 30 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BigNumberish, ContractReceipt, Wallet } from 'ethers';
import { CONTRACTS, ChainName, hexToUint8Array, transferFromEth, tryNativeToHexString } from '@certusone/wormhole-sdk';
import assert from 'assert';

export const bridgeToken = async (
signer: Wallet,
srcChain: ChainName,
dstChain: ChainName,
dstAddr: string,
sourceTokenAddr: string,
amount: BigNumberish,
): Promise<ContractReceipt> => {
const dstChainHex = tryNativeToHexString(dstAddr, dstChain);
const dstChainUint8 = hexToUint8Array(dstChainHex);

const tokenBridgeAddr = CONTRACTS.MAINNET[srcChain].token_bridge;
assert(tokenBridgeAddr, 'token bridge address not found');

console.log(`sending bridging tx with wallet ${signer.address} and amount ${amount} ...`);
const receipt = await transferFromEth(
tokenBridgeAddr,
signer,
sourceTokenAddr,
amount,
dstChain,
dstChainUint8,
);

return receipt;
};
20 changes: 10 additions & 10 deletions src/__tests__/__snapshots__/euphrates.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`/shouldRouteEuphrates > when should route 1`] = `
{
"data": {
"routerAddr": "0x6dd8372A9059254C19F4145556592eD01a5D8c06",
"routerAddr": "0x9e7aDd1De1AB0668D9db4508D4653045f311F173",
"shouldRoute": true,
},
}
Expand All @@ -12,7 +12,7 @@ exports[`/shouldRouteEuphrates > when should route 1`] = `
exports[`/shouldRouteEuphrates > when should route 2`] = `
{
"data": {
"routerAddr": "0x6dd8372A9059254C19F4145556592eD01a5D8c06",
"routerAddr": "0x9e7aDd1De1AB0668D9db4508D4653045f311F173",
"shouldRoute": true,
},
}
Expand All @@ -21,7 +21,7 @@ exports[`/shouldRouteEuphrates > when should route 2`] = `
exports[`/shouldRouteEuphrates > when should route 3`] = `
{
"data": {
"routerAddr": "0x844d07eCe43bF65bf36aa0398AeCE12641755FaA",
"routerAddr": "0xC04Fb8c649DD4Ce87634187E5b2325aCf9C56aBb",
"shouldRoute": true,
},
}
Expand All @@ -30,7 +30,7 @@ exports[`/shouldRouteEuphrates > when should route 3`] = `
exports[`/shouldRouteEuphrates > when should route 4`] = `
{
"data": {
"routerAddr": "0x844d07eCe43bF65bf36aa0398AeCE12641755FaA",
"routerAddr": "0xC04Fb8c649DD4Ce87634187E5b2325aCf9C56aBb",
"shouldRoute": true,
},
}
Expand All @@ -39,7 +39,7 @@ exports[`/shouldRouteEuphrates > when should route 4`] = `
exports[`/shouldRouteEuphrates > when should route 5`] = `
{
"data": {
"routerAddr": "0xCaC1D0397669E71B25410171CE6f77Af750e1B2d",
"routerAddr": "0x1e76808C973c86a4e89DF7374bB5Ad8C62110009",
"shouldRoute": true,
},
}
Expand All @@ -48,7 +48,7 @@ exports[`/shouldRouteEuphrates > when should route 5`] = `
exports[`/shouldRouteEuphrates > when should route 6`] = `
{
"data": {
"routerAddr": "0xCaC1D0397669E71B25410171CE6f77Af750e1B2d",
"routerAddr": "0x1e76808C973c86a4e89DF7374bB5Ad8C62110009",
"shouldRoute": true,
},
}
Expand All @@ -57,7 +57,7 @@ exports[`/shouldRouteEuphrates > when should route 6`] = `
exports[`/shouldRouteEuphrates > when should route 7`] = `
{
"data": {
"routerAddr": "0x274d69D18Ec8d3b9d3E6732013DeB57E4Cf89Be8",
"routerAddr": "0xC934FFE7468Ec5fd49677335771BFa45e71824B8",
"shouldRoute": true,
},
}
Expand All @@ -66,7 +66,7 @@ exports[`/shouldRouteEuphrates > when should route 7`] = `
exports[`/shouldRouteEuphrates > when should route 8`] = `
{
"data": {
"routerAddr": "0x274d69D18Ec8d3b9d3E6732013DeB57E4Cf89Be8",
"routerAddr": "0xC934FFE7468Ec5fd49677335771BFa45e71824B8",
"shouldRoute": true,
},
}
Expand All @@ -75,7 +75,7 @@ exports[`/shouldRouteEuphrates > when should route 8`] = `
exports[`/shouldRouteEuphrates > when should route 9`] = `
{
"data": {
"routerAddr": "0x825E69D8f27010d8e5D4e96913A8571221373789",
"routerAddr": "0xa1d73e877D860f327C0b3394Ed0687D5c6ec86B6",
"shouldRoute": true,
},
}
Expand All @@ -84,7 +84,7 @@ exports[`/shouldRouteEuphrates > when should route 9`] = `
exports[`/shouldRouteEuphrates > when should route 10`] = `
{
"data": {
"routerAddr": "0x825E69D8f27010d8e5D4e96913A8571221373789",
"routerAddr": "0xa1d73e877D860f327C0b3394Ed0687D5c6ec86B6",
"shouldRoute": true,
},
}
Expand Down
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: 6535566
block: 6540185
db: ./db.sqlite
runtime-log-level: 5

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 @@ -49,7 +49,7 @@ describe.concurrent('/shouldRouteHoma', () => {
expect(res).toMatchInlineSnapshot(`
{
"data": {
"routerAddr": "0x8A4f03B2D615172f0714AaC2E8C399a6f0d9e448",
"routerAddr": "0x54c85E9005C8Dc055850f36d8D2810aa7Ba85088",
"shouldRoute": true,
},
}
Expand All @@ -64,7 +64,7 @@ describe.concurrent('/shouldRouteHoma', () => {
expect(res).toMatchInlineSnapshot(`
{
"data": {
"routerAddr": "0x8A4f03B2D615172f0714AaC2E8C399a6f0d9e448",
"routerAddr": "0x54c85E9005C8Dc055850f36d8D2810aa7Ba85088",
"shouldRoute": true,
},
}
Expand All @@ -83,7 +83,7 @@ describe.concurrent('/shouldRouteHoma', () => {
expect(res).toMatchInlineSnapshot(`
{
"data": {
"routerAddr": "0x1140EFc2C45e9307701DA521884F75dDDe28f28f",
"routerAddr": "0x904e0Ce034FDCaA122547C0FCe41A171e3F6f4A5",
"shouldRoute": true,
},
}
Expand Down
16 changes: 15 additions & 1 deletion src/__tests__/relay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ 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 { VAA_10_USDC_ETH_TO_ACALA, VAA_TINY_JITOSOL_SOL_TO_ACALA } from './vaa';
import { expectError, relay, shouldRelay } from './testUtils';

const provider = new AcalaJsonRpcProvider(ETH_RPC.LOCAL);

const USDC_ADDR = ROUTER_TOKEN_INFO.usdc.acalaAddr;
const JITOSOL_ADDR = ROUTER_TOKEN_INFO.jitosol.acalaAddr;

describe('/shouldRelay', () => {
it('when should relay', async () => {
Expand Down Expand Up @@ -153,4 +154,17 @@ describe('/relay', () => {

expect(afterBalRelayer - curBalRelayer).to.eq(10467941n); // 10.467941 USDC
});

it('when amount too small', async () => {
try {
await relay({
targetChain: CHAIN_ID_ACALA,
signedVAA: VAA_TINY_JITOSOL_SOL_TO_ACALA,
});

expect.fail('should throw error but did not');
} catch (err) {
expectError(err, 'transfer amount too small, expect at least 1000000', 500);
}
});
});
Loading

0 comments on commit 802c46a

Please sign in to comment.