From 64f32e535d512c143c471c69795bfc9a25f12181 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 1 Oct 2024 20:00:21 +0200 Subject: [PATCH] add moonbase to fantom routes and fix configuration accordingly to consider moonChain as source --- .../providers/wormhole/wormhole/wormhole.ts | 17 +++-- packages/config/src/mrl-configs/index.ts | 6 +- .../config/src/mrl-configs/moonbaseAlpha.ts | 72 +++++++++++++++++++ .../src/getTransferData/getMoonChainData.ts | 3 +- .../mrl/src/getTransferData/getSourceData.ts | 27 ++++--- 5 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 packages/config/src/mrl-configs/moonbaseAlpha.ts diff --git a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts index ca156dd9..72a17960 100644 --- a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts +++ b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts @@ -26,7 +26,8 @@ export function wormhole() { sourceAddress, }) => { const isNativeAsset = asset.isSame(source.nativeAsset); - const isDestinationMoonChain = destination.isEqual(moonChain); + const isSourceOrDestinationMoonChain = + destination.isEqual(moonChain) || source.isEqual(moonChain); const tokenAddress = isNativeAsset ? 'native' : asset.address; if (!tokenAddress) { @@ -35,15 +36,17 @@ export function wormhole() { const wh = wormholeFactory(source); const whSource = wh.getChain(source.getWormholeName()); - const whMoonChain = wh.getChain(moonChain.getWormholeName()); + const whDestination = wh.getChain(destination.getWormholeName()); const whAsset = Wormhole.tokenId(whSource.chain, tokenAddress); const whSourceAddress = Wormhole.chainAddress( whSource.chain, sourceAddress, ); - const whMoonChainAddress = Wormhole.chainAddress( - whMoonChain.chain, - isDestinationMoonChain ? destinationAddress : GMP_CONTRACT_ADDRESS, + const whDestinationAddress = Wormhole.chainAddress( + whDestination.chain, + isSourceOrDestinationMoonChain + ? destinationAddress + : GMP_CONTRACT_ADDRESS, ); return new WormholeConfig({ @@ -51,9 +54,9 @@ export function wormhole() { whAsset, asset.amount, whSourceAddress, - whMoonChainAddress, + whDestinationAddress, isAutomatic, - isDestinationMoonChain + isSourceOrDestinationMoonChain ? undefined : getPayload({ destination, destinationAddress, moonApi }), ], diff --git a/packages/config/src/mrl-configs/index.ts b/packages/config/src/mrl-configs/index.ts index b7f3a12b..11adfcaf 100644 --- a/packages/config/src/mrl-configs/index.ts +++ b/packages/config/src/mrl-configs/index.ts @@ -1,7 +1,11 @@ import type { ChainRoutes } from '../types/ChainRoutes'; import { fantomTestnetRoutes } from './fantomTestnet'; +import { moonbaseAlphaRoutes } from './moonbaseAlpha'; -export const mrlRoutesList: ChainRoutes[] = [fantomTestnetRoutes]; +export const mrlRoutesList: ChainRoutes[] = [ + fantomTestnetRoutes, + moonbaseAlphaRoutes, +]; export const mrlRoutesMap = new Map( mrlRoutesList.map((config) => [config.chain.key, config]), diff --git a/packages/config/src/mrl-configs/moonbaseAlpha.ts b/packages/config/src/mrl-configs/moonbaseAlpha.ts new file mode 100644 index 00000000..ac238499 --- /dev/null +++ b/packages/config/src/mrl-configs/moonbaseAlpha.ts @@ -0,0 +1,72 @@ +import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder'; +import { dev, ftm, ftmwh } from '../assets'; +import { fantomTestnet, moonbaseAlpha } from '../chains'; +import { ChainRoutes } from '../types/ChainRoutes'; + +export const moonbaseAlphaRoutes = new ChainRoutes({ + chain: moonbaseAlpha, + routes: [ + { + source: { + asset: ftmwh, + balance: BalanceBuilder().evm().erc20(), + destinationFee: { + asset: ftmwh, + balance: BalanceBuilder().evm().erc20(), + }, + }, + destination: { + asset: ftm, + chain: fantomTestnet, + balance: BalanceBuilder().evm().native(), + fee: { + asset: ftm, + amount: 0, + }, + }, + mrl: { + isAutomatic: true, + transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(), + moonChain: { + asset: ftmwh, + fee: { + asset: dev, + amount: 0.1, // TODO not really, it would be the source fee as source is moonChain + balance: BalanceBuilder().substrate().system().account(), + }, + }, + }, + }, + { + source: { + asset: dev, + balance: BalanceBuilder().substrate().system().account(), + destinationFee: { + asset: dev, + balance: BalanceBuilder().substrate().system().account(), + }, + }, + destination: { + asset: dev, + chain: fantomTestnet, + balance: BalanceBuilder().evm().erc20(), + fee: { + asset: dev, + amount: 0, + }, + }, + mrl: { + isAutomatic: true, + transfer: MrlBuilder().wormhole().wormhole().tokenTransfer(), + moonChain: { + asset: dev, + fee: { + asset: dev, + amount: 0.1, + balance: BalanceBuilder().substrate().system().account(), + }, + }, + }, + }, + ], +}); diff --git a/packages/mrl/src/getTransferData/getMoonChainData.ts b/packages/mrl/src/getTransferData/getMoonChainData.ts index 24d2b6cb..fb4f7270 100644 --- a/packages/mrl/src/getTransferData/getMoonChainData.ts +++ b/packages/mrl/src/getTransferData/getMoonChainData.ts @@ -26,6 +26,7 @@ export async function getMoonChainData({ const moonChain = getMoonChain(route.source.chain); const asset = moonChain.getChainAsset(route.mrl.moonChain.asset); + const feeAsset = moonChain.getChainAsset(route.mrl.moonChain.fee.asset); const isDestinationMoonChain = route.destination.chain.isEqual(moonChain); if (isDestinationMoonChain) { @@ -37,7 +38,7 @@ export async function getMoonChainData({ } const fee = await getDestinationFee({ - asset, + asset: feeAsset, chain: moonChain, fee: route.mrl.moonChain.fee.amount, }); diff --git a/packages/mrl/src/getTransferData/getSourceData.ts b/packages/mrl/src/getTransferData/getSourceData.ts index 4c7f4160..9e475de1 100644 --- a/packages/mrl/src/getTransferData/getSourceData.ts +++ b/packages/mrl/src/getTransferData/getSourceData.ts @@ -3,7 +3,11 @@ import { type ExtrinsicConfig, WormholeConfig, } from '@moonbeam-network/xcm-builder'; -import type { AssetRoute, FeeConfig } from '@moonbeam-network/xcm-config'; +import { + type AssetRoute, + type FeeConfig, + dev, +} from '@moonbeam-network/xcm-config'; import { type SourceChainTransferData, getAssetMin, @@ -14,12 +18,12 @@ import { getExtrinsicFee, getMax, } from '@moonbeam-network/xcm-sdk'; -import type { - AnyChain, - AnyParachain, +import { + type AnyChain, + type AnyParachain, AssetAmount, - EvmChain, - EvmParachain, + type EvmChain, + type EvmParachain, } from '@moonbeam-network/xcm-types'; import { WormholeService } from '../services/wormhole'; import { buildTransfer } from './getTransferData.utils'; @@ -137,11 +141,14 @@ export async function getFee({ const wh = WormholeService.create(chain as EvmChain | EvmParachain); const fee = await wh.getFee(transfer); - console.log('fee', fee); + // TODO technically this is not the fee on source chain, it's relayer fee + // source fee should be the fee paid to send the message in polkadot to eth or to sign the transaction in eth to polkadot + console.log('fee in getFee', fee); - // TODO: finish - // biome-ignore lint/suspicious/noExplicitAny: - return {} as any; + return AssetAmount.fromChainAsset(chain.getChainAsset(dev), { + // TODO not dev + amount: fee.relayFee?.amount || 0n, + }); } if (ContractConfig.is(transfer)) {