Skip to content

Commit

Permalink
multipayment route generator script
Browse files Browse the repository at this point in the history
  • Loading branch information
mrq1911 committed Mar 14, 2024
1 parent 8f73111 commit e137fd9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/sdk/test/script/examples/router/setMultipaymentRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ApiPromise } from '@polkadot/api';
import { ApiUrl, PolkadotExecutor } from '../../executor';
import { PoolService } from '../../../../src/pool';
import { TradeRouter } from '../../../../src/api';
import { ZERO } from '../../../../src/utils/bignumber';

const HDX = '0';

class MultiCurrencyPaymentRoutes extends PolkadotExecutor {
async script(api: ApiPromise): Promise<any> {
const poolService = new PoolService(api);
const router = new TradeRouter(poolService);
const [acceptedCurrencies, onchain] = await Promise.all([
api.query.multiTransactionPayment.acceptedCurrencies.keys().then(c => c.map(({ args: [currencyId] }) => currencyId.toString())),
api.query.router.routes.entries().then(r => r.map(([{args: [{assetIn, assetOut}]}, route]) => ([[assetIn.toString(), assetOut.toString()], route]))
.filter(([[asset_in]]) => asset_in.toString() === HDX)
.reduce((a, [[,asset_out], route]) => ({...a, [asset_out]: route.toHuman()}), {}))
]);
console.log(acceptedCurrencies, onchain);
const {amountOut: hdxAmount} = await router.getBestSell('10', HDX, 2000);
const routes = (await Promise.all(acceptedCurrencies.map(currencyId => router.getBestSell(HDX, currencyId, hdxAmount / 10 ** 12).catch(() => null))))
.filter(i => i)
.map(route => route.toTx(ZERO).get().toHuman().method)
.filter(({section}) => section === 'router')
.map(({args}) => args)
.map(({asset_in, asset_out, route}) => api.tx.router.setRoute([asset_in, asset_out], route))
console.log(routes.map(route => route.toHex()));
return api.tx.utility.forceBatch(routes);
}
}

new MultiCurrencyPaymentRoutes(
ApiUrl.HydraDx,
'Set MultiCurrencyPaymentRoutes on chain',
true
).run();

0 comments on commit e137fd9

Please sign in to comment.