Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Oct 28, 2024
1 parent 2083e40 commit b280701
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,13 @@ GET /shouldRouteSwapAndLp?recipient=0x0085560b24769dAC4ed057F1B2ae40746AA9aAb6&s
- swap small amount of token and airdrop ACA to recipient
- swap `swapAmount` token to LDOT, then add LP, refund the remaining token recipient
- stake Lp to euphrates for the recipient
- returns the txhash
- returns the txhash and how many records are removed
```
POST /routeSwapAndLp
data: {
poolId: string; // euphrates pool id
recipient: string; // dest evm address
token: string; // token to route
token: string; // token to route
swapAmount: string; // how many token to swap before adding liquidity
minShareAmount?: string; // add liquidity min share amount (default: 0)
}
Expand All @@ -661,7 +661,10 @@ data: {
=> tx hash
{
data: '0xe1c82c53796d82d87d2e31e289b3cc8ff18e304b8ac95f2bd7548a1706bb8655'
data: {
txHash: '0xe1c82c53796d82d87d2e31e289b3cc8ff18e304b8ac95f2bd7548a1706bb8655',
removed: 1
}
}
/* ---------- when error ---------- */
Expand All @@ -677,12 +680,14 @@ POST /rescueSwapAndLp
data: {
poolId: string; // euphrates pool id
recipient: string; // dest evm address
token: string; // token to route
token: string; // token to route
swapAmount: string; // how many token to swap before adding liquidity
minShareAmount?: string; // add liquidity min share amount (default: 0)
}
```

example
```
POST /rescueSwapAndLp
data: {
"poolId": 7,
Expand All @@ -700,6 +705,18 @@ data: {
// similar to /routeXcm
```

### `/saveRouterInfo`
save router info to db

```
POST /saveRouterInfo
data: {
routerAddr: string;
recipient: string;
params: string; // router params
}
```

### `/routerInfo`
get router info

Expand Down
19 changes: 8 additions & 11 deletions src/api/swapAndLp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
_populateRouteTx,
getChainConfig,
getMainnetChainId,
parseRouterAddr,
} from '../utils';
import { db } from '../db';

Expand Down Expand Up @@ -108,28 +109,24 @@ export const routeSwapAndLp = async (params: SwapAndLpParams) => {
);
const receipt = await tx.wait();

const errParams = { ...params, txHash: receipt.transactionHash };
if (receipt.status !== 1) {
throw new RouteError('swap and lp failed', { ...params, txHash: receipt.transactionHash });
throw new RouteError('swap and lp failed', errParams);
}

// parse receipit log to get router addr
const iface = BaseRouter__factory.createInterface();
const routerDeployedEventSig = iface.getEventTopic('RouterCreated');
const routerDeployedLog = receipt.logs.find(log => log.topics[0] === routerDeployedEventSig);
if (!routerDeployedLog) {
throw new RouteError('router deployed log not found', { ...params, txHash: receipt.transactionHash });
let routerAddr: string;
try {
routerAddr = parseRouterAddr(receipt);
} catch (err) {
throw new RouteError(`failed to parse router addr from receipt: ${err.message}`, errParams);
}

const parsedLog = iface.parseLog(routerDeployedLog);
const routerAddr = parsedLog.args.addr;

const removed = await db.removeRouterInfo({ routerAddr });

return {
txHash: receipt.transactionHash,
removed,
};
return receipt.transactionHash;
};

export const rescueSwapAndLp = async (params: SwapAndLpParams) => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/swapAndRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ERC20__factory } from '@certusone/wormhole-sdk/lib/cjs/ethers-contracts
import { SwapAndStakeEuphratesFactory__factory } from '@acala-network/asset-router/dist/typechain-types';
import { constants } from 'ethers';

import { DROP_SWAP_AMOUNT_JITOSOL, EUPHRATES_ADDR, EUPHRATES_POOLS, RELAYER_ADDR, SWAP_SUPPLY_TOKENS, DROP_AMOUNT_ACA } from '../consts';
import { DROP_AMOUNT_ACA, DROP_SWAP_AMOUNT_JITOSOL, EUPHRATES_ADDR, EUPHRATES_POOLS, RELAYER_ADDR, SWAP_SUPPLY_TOKENS } from '../consts';
import {
Mainnet,
RouteError,
Expand Down
19 changes: 17 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { BigNumber, PopulatedTransaction } from 'ethers';
import { BigNumber, Contract, ContractReceipt, PopulatedTransaction } from 'ethers';
import { CHAIN_ID_ACALA, CHAIN_ID_KARURA, hexToUint8Array } from '@certusone/wormhole-sdk';
import { DispatchError } from '@polkadot/types/interfaces';
import { ISubmittableResult } from '@polkadot/types/types';
Expand All @@ -10,7 +10,8 @@ import { cryptoWaitReady } from '@polkadot/util-crypto';
import { decodeEthGas, sleep } from '@acala-network/eth-providers';
import { options } from '@acala-network/api';

import { RelayerError } from './error';
import { BaseRouter__factory } from '@acala-network/asset-router/dist/typechain-types';
import { RelayerError, RouteError } from './error';
import { logger } from './logger';

export const ROUTER_CHAIN_IDS = [CHAIN_ID_KARURA, CHAIN_ID_ACALA] as const;
Expand Down Expand Up @@ -181,3 +182,17 @@ export const serialize = (params: any) => {
return 'failed to serialize';
}
};

export const parseRouterAddr = (receipt: ContractReceipt) => {
const iface = BaseRouter__factory.createInterface();
const routerDeployedEventSig = iface.getEventTopic('RouterCreated');
const routerDeployedLog = receipt.logs.find(log => log.topics[0] === routerDeployedEventSig);
if (!routerDeployedLog) {
throw new Error('router deployed log not found');
}

const parsedLog = iface.parseLog(routerDeployedLog);
const routerAddr = parsedLog.args.addr;

return routerAddr;
};

0 comments on commit b280701

Please sign in to comment.