From 339ad9e442c6dcb5cc20e79fa2e02fd65bbcad5b Mon Sep 17 00:00:00 2001 From: Elmar Kenigs Date: Fri, 13 Sep 2024 14:40:37 +0300 Subject: [PATCH] Config fixes --- examples/mrl-simple/index.ts | 8 ++------ .../mrl/providers/wormhole/wormhole/wormhole.ts | 10 ++++++---- packages/config/src/chains.ts | 6 ++++++ packages/config/src/types/AssetRoute.ts | 13 ++++++++++++- .../mrl/src/getTransferData/getTransferData.ts | 6 +++--- .../src/getTransferData/getTransferData.utils.ts | 2 +- .../sdk/src/getTransferData/getTransferData.ts | 6 +++--- .../src/getTransferData/getTransferData.utils.ts | 14 +++++--------- packages/types/src/asset/AssetAmount.ts | 2 +- tsup.config.ts | 2 +- 10 files changed, 40 insertions(+), 29 deletions(-) diff --git a/examples/mrl-simple/index.ts b/examples/mrl-simple/index.ts index 590636c3..7d8a5571 100644 --- a/examples/mrl-simple/index.ts +++ b/examples/mrl-simple/index.ts @@ -1,9 +1,5 @@ import { Mrl } from '@moonbeam-network/mrl'; -import { - fantomTestnet, - ftmwh, - peaqAlphanet, -} from '@moonbeam-network/xcm-config'; +import { fantomTestnet, ftm, peaqAlphanet } from '@moonbeam-network/xcm-config'; import { Keyring } from '@polkadot/api'; import { cryptoWaitReady } from '@polkadot/util-crypto'; import { http, type Address, createWalletClient } from 'viem'; @@ -54,7 +50,7 @@ async function main() { const data = await Mrl() .setSource(fantomTestnet) .setDestination(peaqAlphanet) - .setAsset(ftmwh) + .setAsset(ftm) .setAddresses({ sourceAddress: account.address, destinationAddress: pair.address, diff --git a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts index 6e58616a..ca156dd9 100644 --- a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts +++ b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts @@ -25,16 +25,18 @@ export function wormhole() { source, sourceAddress, }) => { - if (!asset.address) { + const isNativeAsset = asset.isSame(source.nativeAsset); + const isDestinationMoonChain = destination.isEqual(moonChain); + const tokenAddress = isNativeAsset ? 'native' : asset.address; + + if (!tokenAddress) { throw new Error(`Asset ${asset.key} has no address`); } - const isDestinationMoonChain = destination.isEqual(moonChain); - const wh = wormholeFactory(source); const whSource = wh.getChain(source.getWormholeName()); const whMoonChain = wh.getChain(moonChain.getWormholeName()); - const whAsset = Wormhole.tokenId(whSource.chain, asset.address); + const whAsset = Wormhole.tokenId(whSource.chain, tokenAddress); const whSourceAddress = Wormhole.chainAddress( whSource.chain, sourceAddress, diff --git a/packages/config/src/chains.ts b/packages/config/src/chains.ts index cc26c208..e801f6f4 100644 --- a/packages/config/src/chains.ts +++ b/packages/config/src/chains.ts @@ -469,6 +469,9 @@ export const fantomTestnet = new EvmChain({ name: 'Fantom Testnet', nativeAsset: ftm, rpc: 'https://rpc.testnet.fantom.network', + wh: { + name: 'Fantom', + }, }); export const hydration = new Parachain({ @@ -944,6 +947,9 @@ export const moonbaseAlpha = new EvmParachain({ rpc: 'https://rpc.api.moonbase.moonbeam.network', ss58Format: 1287, ws: ['wss://wss.api.moonbase.moonbeam.network'], + wh: { + name: 'Moonbeam', + }, }); export const moonbaseBeta = new EvmParachain({ diff --git a/packages/config/src/types/AssetRoute.ts b/packages/config/src/types/AssetRoute.ts index 0fb2947d..8f7d5f4a 100644 --- a/packages/config/src/types/AssetRoute.ts +++ b/packages/config/src/types/AssetRoute.ts @@ -6,7 +6,12 @@ import type { FeeConfigBuilder, MrlConfigBuilder, } from '@moonbeam-network/xcm-builder'; -import type { AnyChain, Asset, SetOptional } from '@moonbeam-network/xcm-types'; +import type { + AnyChain, + Asset, + ChainAsset, + SetOptional, +} from '@moonbeam-network/xcm-types'; export interface AssetRouteConstructorParams { source: SourceConfig; @@ -79,4 +84,10 @@ export class AssetRoute { this.extrinsic = extrinsic; this.mrl = mrl; } + + getDestinationFeeAssetOnSource(): ChainAsset { + return this.source.chain.getChainAsset( + this.source.destinationFee?.asset || this.destination.fee.asset, + ); + } } diff --git a/packages/mrl/src/getTransferData/getTransferData.ts b/packages/mrl/src/getTransferData/getTransferData.ts index 4622d002..1072c43a 100644 --- a/packages/mrl/src/getTransferData/getTransferData.ts +++ b/packages/mrl/src/getTransferData/getTransferData.ts @@ -50,11 +50,11 @@ export async function getTransferData({ destinationAddress, }); - // Here we need to convert the fee on the destination chain to an asset on source chain. + // NOTE: Here we need to convert the fee on the destination chain + // to an asset on source chain. const destinationFee = convertToChainDecimals({ asset: destinationData.fee, - chain: route.source.chain, - targetAsset: route.source.destinationFee?.asset, + target: route.getDestinationFeeAssetOnSource(), }); const sourceData = await getSourceData({ diff --git a/packages/mrl/src/getTransferData/getTransferData.utils.ts b/packages/mrl/src/getTransferData/getTransferData.utils.ts index f4cc199d..a4568260 100644 --- a/packages/mrl/src/getTransferData/getTransferData.utils.ts +++ b/packages/mrl/src/getTransferData/getTransferData.utils.ts @@ -64,7 +64,7 @@ export function getMoonChainFeeValueOnSource({ isSameAssetPayingMoonChainFee ? convertToChainDecimals({ asset: moonChainData.fee, - chain: sourceData.chain, + target: sourceData.chain.getChainAsset(moonChainData.fee), }).toBig() : Big(0); } diff --git a/packages/sdk/src/getTransferData/getTransferData.ts b/packages/sdk/src/getTransferData/getTransferData.ts index 15dbfce5..a8b8d26f 100644 --- a/packages/sdk/src/getTransferData/getTransferData.ts +++ b/packages/sdk/src/getTransferData/getTransferData.ts @@ -29,11 +29,11 @@ export async function getTransferData({ destinationAddress, }); - // Here we need to convert the fee on the destination chain to an asset on source chain. + // NOTE: Here we need to convert the fee on the destination chain + // to an asset on source chain. const destinationFee = convertToChainDecimals({ asset: destinationData.fee, - chain: route.source.chain, - targetAsset: route.source.destinationFee?.asset, + target: route.getDestinationFeeAssetOnSource(), }); const sourceData = await getSourceData({ diff --git a/packages/sdk/src/getTransferData/getTransferData.utils.ts b/packages/sdk/src/getTransferData/getTransferData.utils.ts index a8d1d40b..e1de03f8 100644 --- a/packages/sdk/src/getTransferData/getTransferData.utils.ts +++ b/packages/sdk/src/getTransferData/getTransferData.utils.ts @@ -192,19 +192,15 @@ export async function getDestinationFee({ export interface ConvertToChainDecimalsParams { asset: AssetAmount; - chain: AnyChain; - targetAsset?: Asset; + target: ChainAsset; } export function convertToChainDecimals({ asset, - chain, - targetAsset, + target, }: ConvertToChainDecimalsParams): AssetAmount { - const chainAsset = chain.getChainAsset(targetAsset ?? asset); - - return AssetAmount.fromChainAsset(chainAsset, { - amount: asset.convertDecimals(chainAsset.decimals).amount, + return AssetAmount.fromChainAsset(target, { + amount: asset.convertDecimals(target.decimals).amount, }); } @@ -249,7 +245,7 @@ export async function getDestinationFeeBalance({ return getBalance({ address: sourceAddress, - asset: route.source.chain.getChainAsset(route.destination.fee.asset), + asset: route.getDestinationFeeAssetOnSource(), builder: route.source.destinationFee?.balance, chain: route.source.chain, }); diff --git a/packages/types/src/asset/AssetAmount.ts b/packages/types/src/asset/AssetAmount.ts index 6a502f52..0974b30d 100644 --- a/packages/types/src/asset/AssetAmount.ts +++ b/packages/types/src/asset/AssetAmount.ts @@ -37,7 +37,7 @@ export class AssetAmount extends ChainAsset { }); } - isSame(asset: AssetAmount): boolean { + isSame(asset: ChainAsset | AssetAmount): boolean { return super.isEqual(asset) && this.decimals === asset.decimals; } diff --git a/tsup.config.ts b/tsup.config.ts index c079ed45..9c0a1f99 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -5,7 +5,7 @@ export const baseConfig: Options = { format: 'esm', outDir: 'build', target: 'es2022', - minify: true, + minify: false, splitting: false, sourcemap: true, clean: true,