Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Mar 25, 2024
1 parent 2700b8a commit d67b2f6
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 230 deletions.
64 changes: 64 additions & 0 deletions src/api/euphrates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { EuphratesFactory__factory } from '@acala-network/asset-router/dist/typechain-types';

import { EUPHRATES_ADDR, EUPHRATES_POOLS } from '../consts';
import {
Mainnet,
RouteParamsEuphrates,
_populateRelayTx,
_populateRouteTx,
getChainConfig,
getMainnetChainId,
} from '../utils';
import { RelayerError } from '../middlewares';

const prepareRouteEuphrates = async (chain: Mainnet) => {
const chainId = getMainnetChainId(chain);
const chainConfig = await getChainConfig(chainId);
const { feeAddr, euphratesFactoryAddr, wallet } = chainConfig;

const euphratesFactory = EuphratesFactory__factory.connect(euphratesFactoryAddr!, wallet);

return { euphratesFactory, feeAddr };
};

export const shouldRouteEuphrates = async (params: RouteParamsEuphrates) => {
try {
const { euphratesFactory, feeAddr } = await prepareRouteEuphrates(Mainnet.Acala);
if (!EUPHRATES_POOLS.includes(params.poolId)) {
throw new RelayerError(`euphrates poolId ${params.poolId} is not supported`, params);
}

const routerAddr = await euphratesFactory.callStatic.deployEuphratesRouter(
feeAddr,
params,
EUPHRATES_ADDR,
);

return {
shouldRoute: true,
routerAddr,
};
} catch (err) {
return {
shouldRoute: false,
msg: err.message,
};
}
};

export const routeEuphrates = async (params: RouteParamsEuphrates) => {
if (params.token === undefined) {
throw new RelayerError('no token address provided for routeEuphrates', params);
}

const { euphratesFactory, feeAddr } = await prepareRouteEuphrates(Mainnet.Acala);
const tx = await euphratesFactory.deployEuphratesRouterAndRoute(
feeAddr,
params,
EUPHRATES_ADDR,
params.token,
);
const receipt = await tx.wait();

return receipt.transactionHash;
};
52 changes: 52 additions & 0 deletions src/api/homa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { DOT } from '@acala-network/contracts/utils/AcalaTokens';
import { HomaFactory__factory } from '@acala-network/asset-router/dist/typechain-types';
import { KSM } from '@acala-network/contracts/utils/KaruraTokens';

import {
Mainnet,
RouteParamsHoma,
_populateRelayTx,
_populateRouteTx,
getChainConfig,
getMainnetChainId,
toAddr32,
} from '../utils';

const prepareRouteHoma = async (chain: Mainnet) => {
const chainId = getMainnetChainId(chain);
const chainConfig = await getChainConfig(chainId);
const { feeAddr, homaFactoryAddr, wallet } = chainConfig;

const homaFactory = HomaFactory__factory.connect(homaFactoryAddr!, wallet);
const routeToken = chain === Mainnet.Acala ? DOT : KSM;

return { homaFactory, feeAddr, routeToken };
};

export const shouldRouteHoma = async ({ chain, destAddr }: RouteParamsHoma) => {
try {
const { homaFactory, feeAddr } = await prepareRouteHoma(chain);
const routerAddr = await homaFactory.callStatic.deployHomaRouter(
feeAddr,
toAddr32(destAddr),
);

return {
shouldRoute: true,
routerAddr,
};
} catch (err) {
return {
shouldRoute: false,
msg: err.message,
};
}
};

export const routeHoma = async ({ chain, destAddr }: RouteParamsHoma) => {
const { homaFactory, feeAddr, routeToken } = await prepareRouteHoma(chain);
const tx = await homaFactory.deployHomaRouterAndRoute(feeAddr, toAddr32(destAddr), routeToken);
const receipt = await tx.wait();

return receipt.transactionHash;
};
6 changes: 5 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export * from './relay';
export * from './route';
export * from './testTimeout';
export * from './getVersion';
export * from './wormhole';
export * from './xcm';
export * from './homa';
export * from './euphrates';
export * from './health';
228 changes: 0 additions & 228 deletions src/api/route.ts

This file was deleted.

Loading

0 comments on commit d67b2f6

Please sign in to comment.