Skip to content

Commit

Permalink
support route tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Nov 26, 2024
1 parent 9319ac9 commit 5282b63
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 137 deletions.
6 changes: 3 additions & 3 deletions examples/xcm-transfer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ configureExternal(externals, configService);
// Define transfer
const srcChain = configService.getChain('ethereum');
const destChain = configService.getChain('hydration');
const asset = configService.getAsset('weth');
const asset = configService.getAsset('eth');

const configBuilder = ConfigBuilder(configService);
const { sourceChains } = configBuilder.assets().asset(asset);
Expand All @@ -71,8 +71,8 @@ logDestChains(asset.key, destinationChains);
logSrcChains(asset.key, sourceChains);

// Define source & dest accounts
const srcAddr = '0x26f5C2370e563e9f4dDA435f03A63D7C109D8D04';
const destAddr = '7KATdGamwo5s8P31iNxKbKStR4SmprTjkwzeSnSbQuQJsgym';
const srcAddr = 'INSERT_ADDRESS';
const destAddr = 'INSERT_ADDRESS';

// Subscribe source chain token balance
const balanceObserver = (balances: AssetAmount[]) => console.log(balances);
Expand Down
23 changes: 13 additions & 10 deletions packages/xcm-cfg/src/configs/ethereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
eth,
wbtc,
wbtc_mwh,
weth,
weth_mwh,
usdc,
usdc_mwh,
Expand All @@ -17,10 +16,11 @@ import {
} from '../../assets';
import { ethereum, hydration, moonbeam } from '../../chains';
import { BalanceBuilder, ContractBuilder } from '../../builders';
import { Tag } from '../../tags';

import {
toHydrationErc20Template,
toHydrationSnowbridgeTemplate,
toHydrationViaWormholeTemplate,
toHydrationViaSnowbridgeTemplate,
} from './templates';

const toHydrationViaWormhole: AssetRoute[] = [
Expand All @@ -46,19 +46,20 @@ const toHydrationViaWormhole: AssetRoute[] = [
.TokenBridge()
.wrapAndTransferETHWithPayload()
.viaMrl({ moonchain: moonbeam }),
tags: [Tag.Mrl, Tag.Wormhole],
}),
toHydrationErc20Template(dai, dai_mwh),
toHydrationErc20Template(wbtc, wbtc_mwh),
toHydrationErc20Template(usdc, usdc_mwh),
toHydrationErc20Template(usdt, usdt_mwh),
toHydrationViaWormholeTemplate(dai, dai_mwh),
toHydrationViaWormholeTemplate(wbtc, wbtc_mwh),
toHydrationViaWormholeTemplate(usdc, usdc_mwh),
toHydrationViaWormholeTemplate(usdt, usdt_mwh),
];

const toHydrationViaSnowbridge: AssetRoute[] = [
toHydrationSnowbridgeTemplate(aave, aave),
toHydrationSnowbridgeTemplate(susde, susde),
toHydrationViaSnowbridgeTemplate(aave, aave),
toHydrationViaSnowbridgeTemplate(susde, susde),
];

const toMoonbeam: AssetRoute[] = [
const toMoonbeamViaWormhole: AssetRoute[] = [
new AssetRoute({
source: {
asset: eth,
Expand All @@ -77,6 +78,7 @@ const toMoonbeam: AssetRoute[] = [
},
},
contract: ContractBuilder().Wormhole().TokenBridge().wrapAndTransferETH(),
tags: [Tag.Mrl, Tag.Wormhole],
}),
new AssetRoute({
source: {
Expand All @@ -100,6 +102,7 @@ const toMoonbeam: AssetRoute[] = [
},
},
contract: ContractBuilder().Wormhole().TokenBridge().transferTokens(),
tags: [Tag.Mrl, Tag.Wormhole],
}),
];

Expand Down
10 changes: 8 additions & 2 deletions packages/xcm-cfg/src/configs/ethereum/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
FeeAmountBuilder,
} from '../../builders';
import { hydration, moonbeam } from '../../chains';
import { Tag } from '../../tags';

export function toHydrationErc20Template(
export function toHydrationViaWormholeTemplate(
assetIn: Asset,
assetOut: Asset
): AssetRoute {
Expand Down Expand Up @@ -38,10 +39,14 @@ export function toHydrationErc20Template(
.TokenBridge()
.transferTokensWithPayload()
.viaMrl({ moonchain: moonbeam }),
tags: [Tag.Mrl, Tag.Wormhole],
});
}

export function toHydrationSnowbridgeTemplate(assetIn: Asset, assetOut: Asset) {
export function toHydrationViaSnowbridgeTemplate(
assetIn: Asset,
assetOut: Asset
) {
return new AssetRoute({
source: {
asset: assetIn,
Expand All @@ -63,5 +68,6 @@ export function toHydrationSnowbridgeTemplate(assetIn: Asset, assetOut: Asset) {
},
},
contract: ContractBuilder().Snowbridge().sendToken(),
tags: [Tag.Snowbridge],
});
}
68 changes: 7 additions & 61 deletions packages/xcm-cfg/src/configs/polkadot/hydration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
wbtc,
wbtc_awh,
wbtc_mwh,
weth,
weth_awh,
weth_mwh,
wud,
Expand All @@ -47,7 +46,6 @@ import {
} from '../../../assets';
import {
acala,
acala_evm,
ajuna,
assetHub,
astar,
Expand All @@ -67,25 +65,20 @@ import {
subsocial,
unique,
zeitgeist,
ethereum,
} from '../../../chains';
import {
ContractBuilder,
ExtrinsicBuilder,
ExtrinsicBuilderV4,
FeeAmountBuilder,
XcmTransferType,
} from '../../../builders';

import { balance, fee } from './configs';
import {
MRL_EXECUTION_FEE,
MRL_XCM_FEE,
toHubExtTemplate,
toEthereumWithRelayerTemplate,
toEthereumViaSnowbridgeTemplate,
toEthereumViaWormholeTemplate,
toMoonbeamErc20Template,
toZeitgeistErc20Template,
toEthereumViaSnowbridgeTemplate,
} from './templates';

const toAcala: AssetRoute[] = [
Expand Down Expand Up @@ -824,59 +817,12 @@ const toAjuna: AssetRoute[] = [
}),
];

const toAcalaViaWormhole: AssetRoute[] = [
new AssetRoute({
source: {
asset: dai_mwh,
balance: balance(),
fee: fee(),
destinationFee: {
balance: balance(),
},
},
destination: {
chain: acala_evm,
asset: dai_awh,
fee: {
amount: 0.06,
asset: aca,
},
},
extrinsic: ExtrinsicBuilder()
.utility()
.batchAll([
ExtrinsicBuilder().xTokens().transferMultiCurrencies(),
ExtrinsicBuilder().polkadotXcm().send().transact({
fee: MRL_EXECUTION_FEE,
}),
]),
transact: {
chain: moonbeam,
fee: {
amount: MRL_XCM_FEE,
asset: glmr,
balance: balance(),
},
extrinsic: ExtrinsicBuilder()
.ethereumXcm()
.transact(
ContractBuilder()
.Batch()
.batchAll([
ContractBuilder().Erc20().approve(),
ContractBuilder().Wormhole().TokenBridge().transferTokens(),
])
),
},
}),
];

const toEthereumViaWormhole: AssetRoute[] = [
toEthereumWithRelayerTemplate(dai_mwh, dai),
toEthereumWithRelayerTemplate(weth_mwh, eth),
toEthereumWithRelayerTemplate(wbtc_mwh, wbtc),
toEthereumWithRelayerTemplate(usdt_mwh, usdt),
toEthereumWithRelayerTemplate(usdc_mwh, usdc),
toEthereumViaWormholeTemplate(dai_mwh, dai),
toEthereumViaWormholeTemplate(weth_mwh, eth),
toEthereumViaWormholeTemplate(wbtc_mwh, wbtc),
toEthereumViaWormholeTemplate(usdt_mwh, usdt),
toEthereumViaWormholeTemplate(usdc_mwh, usdc),
];

const toEthereumViaSnowbridge: AssetRoute[] = [
Expand Down
5 changes: 4 additions & 1 deletion packages/xcm-cfg/src/configs/polkadot/hydration/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
XcmTransferType,
} from '../../../builders';
import { assetHub, ethereum, moonbeam, zeitgeist } from '../../../chains';
import { Tag } from '../../../tags';

import { balance, fee } from './configs';

Expand Down Expand Up @@ -100,7 +101,7 @@ export function toZeitgeistErc20Template(asset: Asset): AssetRoute {
});
}

export function toEthereumWithRelayerTemplate(
export function toEthereumViaWormholeTemplate(
assetIn: Asset,
assetOut: Asset
): AssetRoute {
Expand Down Expand Up @@ -152,6 +153,7 @@ export function toEthereumWithRelayerTemplate(
])
),
},
tags: [Tag.Mrl, Tag.Wormhole],
});
}

Expand Down Expand Up @@ -181,5 +183,6 @@ export function toEthereumViaSnowbridgeTemplate(
.transferAssetsUsingTypeAndThen({
transferType: XcmTransferType.DestinationReserve,
}),
tags: [Tag.Snowbridge],
});
}
43 changes: 4 additions & 39 deletions packages/xcm-cfg/src/configs/polkadot/moonbeam/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { AssetRoute, ChainRoutes } from '@galacticcouncil/xcm-core';

import {
aca,
dai,
dai_awh,
dai_mwh,
dot,
eth,
Expand All @@ -17,18 +15,13 @@ import {
wbtc_mwh,
weth_mwh,
} from '../../../assets';
import {
acala,
assetHub,
ethereum,
hydration,
moonbeam,
} from '../../../chains';
import { assetHub, ethereum, hydration, moonbeam } from '../../../chains';
import {
BalanceBuilder,
ContractBuilder,
FeeAmountBuilder,
} from '../../../builders';
import { Tag } from '../../../tags';

import { toHydrationErc20Template, toHydrationXcTemplate } from './templates';

Expand Down Expand Up @@ -129,36 +122,6 @@ const toAssetHub: AssetRoute[] = [
}),
];

const toAcalaViaWormhole: AssetRoute[] = [
new AssetRoute({
source: {
asset: dai_mwh,
balance: BalanceBuilder().evm().erc20(),
fee: {
asset: glmr,
balance: BalanceBuilder().substrate().system().account(),
},
destinationFee: {
balance: BalanceBuilder().substrate().assets().account(),
},
},
destination: {
chain: acala,
asset: dai_awh,
fee: {
amount: 0.05,
asset: aca,
},
},
contract: ContractBuilder()
.Batch()
.batchAll([
ContractBuilder().Erc20().approve(),
ContractBuilder().Wormhole().TokenBridge().transferTokens(),
]),
}),
];

const toEthereumViaWormhole: AssetRoute[] = [
new AssetRoute({
source: {
Expand Down Expand Up @@ -190,6 +153,7 @@ const toEthereumViaWormhole: AssetRoute[] = [
ContractBuilder().Erc20().approve(),
ContractBuilder().Wormhole().TokenRelayer().transferTokensWithRelay(),
]),
tags: [Tag.Mrl, Tag.Wormhole],
}),
new AssetRoute({
source: {
Expand Down Expand Up @@ -221,6 +185,7 @@ const toEthereumViaWormhole: AssetRoute[] = [
ContractBuilder().Erc20().approve(),
ContractBuilder().Wormhole().TokenRelayer().transferTokensWithRelay(),
]),
tags: [Tag.Mrl, Tag.Wormhole],
}),
];

Expand Down
5 changes: 5 additions & 0 deletions packages/xcm-cfg/src/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Tag {
Wormhole = 'Wormhole',
Snowbridge = 'Snowbridge',
Mrl = 'Mrl',
}
10 changes: 5 additions & 5 deletions packages/xcm-core/src/config/definition/AssetRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ export interface TransactConfig {
extrinsic: ExtrinsicConfigBuilder;
}

export enum Bridge {
Snowbridge = 'Snowbridge',
Wormhole = 'Wormhole',
}

export interface AssetRouteParams {
source: SourceConfig;
destination: DestinationConfig;
contract?: ContractConfigBuilder;
extrinsic?: ExtrinsicConfigBuilder;
transact?: TransactConfig;
tags?: string[];
}

export class AssetRoute {
Expand All @@ -54,17 +50,21 @@ export class AssetRoute {

readonly transact?: TransactConfig;

readonly tags?: string[];

constructor({
source,
destination,
contract,
extrinsic,
transact,
tags,
}: AssetRouteParams) {
this.source = source;
this.destination = destination;
this.contract = contract;
this.extrinsic = extrinsic;
this.transact = transact;
this.tags = tags;
}
}
Loading

0 comments on commit 5282b63

Please sign in to comment.