From e4630c89a1600d3b954c4373e140cc28a171b4c9 Mon Sep 17 00:00:00 2001 From: mmaurello <93129175+mmaurello@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:11:01 +0200 Subject: [PATCH] Add relayer fee to min (#363) * add relayer fee to min and rename interface * fix lint --- .../builder/fixtures/builderParamsMock.ts | 12 ++++----- .../asset-min/AssetMinBuilder.interfaces.ts | 4 +-- .../src/balance/BalanceBuilder.interfaces.ts | 4 +-- packages/builder/src/builder.interfaces.ts | 4 +-- .../pallets/polkadotXcm/polkadotXcm.util.ts | 6 ++--- .../builder/src/fee/FeeBuilder.interfaces.ts | 4 +-- packages/builder/src/fee/FeeBuilder.ts | 4 +-- .../builder/src/mrl/MrlBuilder.interfaces.ts | 4 +-- .../wormhole/wormhole/wormhole.test.ts | 8 +++--- .../providers/wormhole/wormhole/wormhole.ts | 4 ++- .../config/src/mrl-configs/moonbaseAlpha.ts | 2 +- .../getTransferData/getTransferData.utils.ts | 25 +++++++++++++------ 12 files changed, 47 insertions(+), 34 deletions(-) diff --git a/packages/builder/fixtures/builderParamsMock.ts b/packages/builder/fixtures/builderParamsMock.ts index 5fcaad0a..c5baee0a 100644 --- a/packages/builder/fixtures/builderParamsMock.ts +++ b/packages/builder/fixtures/builderParamsMock.ts @@ -8,7 +8,7 @@ import { Parachain, } from '@moonbeam-network/xcm-types'; import { vi } from 'vitest'; -import type { BuilderPrams, MrlBuilderParams } from '../src'; +import type { BuilderParams, MrlBuilderParams } from '../src'; export const apiMock = { tx: { @@ -98,7 +98,7 @@ export const fantomTestnet = new EvmChain({ }, }); -export const buildParamsMock: BuilderPrams = { +export const buildParamsMock: BuilderParams = { asset: testAssetAmount, destination: moonbaseAlphaMock, destinationAddress: '0xeF46c7649270C912704fB09B75097f6E32208b85', @@ -109,7 +109,7 @@ export const buildParamsMock: BuilderPrams = { sourceApi: apiMock, }; -export const buildParamsSameAssetMock: BuilderPrams = { +export const buildParamsSameAssetMock: BuilderParams = { asset: testAssetAmount, destination: moonbaseAlphaMock, destinationAddress: '0xeF46c7649270C912704fB09B75097f6E32208b85', @@ -120,7 +120,7 @@ export const buildParamsSameAssetMock: BuilderPrams = { sourceApi: apiMock, }; -export const buildParachainParamsMock: BuilderPrams = { +export const buildParachainParamsMock: BuilderParams = { asset: testAssetAmount, destination: interlayTestnetMock, /* cspell:disable-next-line */ @@ -164,7 +164,7 @@ export const mrlBuildParamsMock2: MrlBuilderParams = { }, }; -export const wormholeConfigBuilderPrams: MrlBuilderParams = { +export const wormholeConfigBuilderParams: MrlBuilderParams = { asset: testAssetAmount, destination: alphanetAssetHubMock, destinationAddress: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', @@ -180,7 +180,7 @@ export const wormholeConfigBuilderPrams: MrlBuilderParams = { sourceApi: apiMock, }; -export const wormholeToMoonchainConfigBuilderPrams: MrlBuilderParams = { +export const wormholeToMoonchainConfigBuilderParams: MrlBuilderParams = { asset: testAssetAmount, destination: moonbaseAlphaMock, destinationAddress: '0x98891e5FD24Ef33A488A47101F65D212Ff6E650E', diff --git a/packages/builder/src/asset-min/AssetMinBuilder.interfaces.ts b/packages/builder/src/asset-min/AssetMinBuilder.interfaces.ts index f1330350..cea8f714 100644 --- a/packages/builder/src/asset-min/AssetMinBuilder.interfaces.ts +++ b/packages/builder/src/asset-min/AssetMinBuilder.interfaces.ts @@ -4,9 +4,9 @@ import type { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConf export type AssetMinConfigBuilder = ConfigBuilder< SubstrateQueryConfig, - AssetMinConfigBuilderPrams + AssetMinConfigBuilderParams >; -export interface AssetMinConfigBuilderPrams { +export interface AssetMinConfigBuilderParams { asset: ChainAssetId; } diff --git a/packages/builder/src/balance/BalanceBuilder.interfaces.ts b/packages/builder/src/balance/BalanceBuilder.interfaces.ts index 38d12ad7..93875b00 100644 --- a/packages/builder/src/balance/BalanceBuilder.interfaces.ts +++ b/packages/builder/src/balance/BalanceBuilder.interfaces.ts @@ -7,10 +7,10 @@ import type { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConf export type BalanceConfigBuilder = ConfigBuilder< ContractConfig | SubstrateQueryConfig | EvmQueryConfig, - BalanceBuilderPrams + BalanceBuilderParams >; -export interface BalanceBuilderPrams { +export interface BalanceBuilderParams { address: string; asset: ChainAsset; } diff --git a/packages/builder/src/builder.interfaces.ts b/packages/builder/src/builder.interfaces.ts index ba4376d2..1a6459fc 100644 --- a/packages/builder/src/builder.interfaces.ts +++ b/packages/builder/src/builder.interfaces.ts @@ -5,11 +5,11 @@ import type { } from '@moonbeam-network/xcm-types'; import type { ApiPromise } from '@polkadot/api'; -export interface ConfigBuilder { +export interface ConfigBuilder { build: (params: Params) => Config; } -export interface BuilderPrams { +export interface BuilderParams { asset: AssetAmount; destination: IChain; destinationAddress: string; diff --git a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts index 2f318ba7..ce3ed1d2 100644 --- a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts +++ b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts @@ -1,12 +1,12 @@ import type { SubmittableExtrinsicFunction } from '@polkadot/api/types'; -import type { BuilderPrams } from '../../../builder.interfaces'; +import type { BuilderParams } from '../../../builder.interfaces'; import type { Parents } from '../../ExtrinsicBuilder.interfaces'; import { getExtrinsicAccount, getExtrinsicArgumentVersion, } from '../../ExtrinsicBuilder.utils'; -export interface GetExtrinsicParams extends BuilderPrams { +export interface GetExtrinsicParams extends BuilderParams { // biome-ignore lint/suspicious/noExplicitAny: not sure how to fix this asset: any; func?: SubmittableExtrinsicFunction<'promise'>; @@ -55,7 +55,7 @@ export function getPolkadotXcmExtrinsicArgs({ export function shouldFeeAssetPrecedeAsset({ asset, fee, -}: BuilderPrams): boolean { +}: BuilderParams): boolean { const assetIdNumber = Number(asset.getAssetId()); const feeAssetIdNumber = Number(fee.getAssetId()); diff --git a/packages/builder/src/fee/FeeBuilder.interfaces.ts b/packages/builder/src/fee/FeeBuilder.interfaces.ts index ae424bb7..d0cc946d 100644 --- a/packages/builder/src/fee/FeeBuilder.interfaces.ts +++ b/packages/builder/src/fee/FeeBuilder.interfaces.ts @@ -7,10 +7,10 @@ import type { SubstrateCallConfig } from '../types/substrate/SubstrateCallConfig export type FeeConfigBuilder = ConfigBuilder< SubstrateCallConfig, - FeeConfigBuilderPrams + FeeConfigBuilderParams >; -export interface FeeConfigBuilderPrams { +export interface FeeConfigBuilderParams { address: string; api: ApiPromise; asset: ChainAsset; diff --git a/packages/builder/src/fee/FeeBuilder.ts b/packages/builder/src/fee/FeeBuilder.ts index 3204595a..ce74589b 100644 --- a/packages/builder/src/fee/FeeBuilder.ts +++ b/packages/builder/src/fee/FeeBuilder.ts @@ -4,7 +4,7 @@ import { SubstrateCallConfig } from '../types/substrate/SubstrateCallConfig'; import type { FeeConfigBuilder, - FeeConfigBuilderPrams, + FeeConfigBuilderParams, XcmPaymentFeeProps, } from './FeeBuilder.interfaces'; import { @@ -36,7 +36,7 @@ function xcmPaymentApi() { asset, destination, feeAsset, - }: FeeConfigBuilderPrams) => + }: FeeConfigBuilderParams) => new SubstrateCallConfig({ api, call: async (): Promise => { diff --git a/packages/builder/src/mrl/MrlBuilder.interfaces.ts b/packages/builder/src/mrl/MrlBuilder.interfaces.ts index 8d85587c..0e9456b9 100644 --- a/packages/builder/src/mrl/MrlBuilder.interfaces.ts +++ b/packages/builder/src/mrl/MrlBuilder.interfaces.ts @@ -5,7 +5,7 @@ import type { } from '@moonbeam-network/xcm-types'; import type { ApiPromise } from '@polkadot/api'; import type { HexString } from '@polkadot/util/types'; -import type { BuilderPrams, ConfigBuilder } from '../builder.interfaces'; +import type { BuilderParams, ConfigBuilder } from '../builder.interfaces'; import type { ContractConfig } from '../contract'; import type { ExtrinsicConfig } from '../extrinsic'; import type { WormholeConfig } from './providers/wormhole/wormhole'; @@ -15,7 +15,7 @@ export type MrlConfigBuilder = ConfigBuilder< MrlBuilderParams >; -export interface MrlBuilderParams extends BuilderPrams { +export interface MrlBuilderParams extends BuilderParams { isAutomatic: boolean; moonApi: ApiPromise; moonAsset: ChainAsset; diff --git a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.test.ts b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.test.ts index 385bd21f..29053fe1 100644 --- a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.test.ts +++ b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.test.ts @@ -2,8 +2,8 @@ import { describe, expect, it } from 'vitest'; import { getPolkadotApi } from '@moonbeam-network/xcm-utils'; import { - wormholeConfigBuilderPrams, - wormholeToMoonchainConfigBuilderPrams, + wormholeConfigBuilderParams, + wormholeToMoonchainConfigBuilderParams, } from '../../../../../fixtures'; import { wormhole } from './wormhole'; @@ -18,7 +18,7 @@ describe('wormhole', async () => { it('should be correct config', () => { expect( transfer.build({ - ...wormholeConfigBuilderPrams, + ...wormholeConfigBuilderParams, moonApi, isAutomatic: true, }), @@ -32,7 +32,7 @@ describe('wormhole', async () => { it('should be correct config to moon chain', () => { expect( transfer.build({ - ...wormholeToMoonchainConfigBuilderPrams, + ...wormholeToMoonchainConfigBuilderParams, moonApi, isAutomatic: false, }), diff --git a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts index 8ff19c30..4575ade5 100644 --- a/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts +++ b/packages/builder/src/mrl/providers/wormhole/wormhole/wormhole.ts @@ -26,9 +26,11 @@ export function wormhole() { source, sourceAddress, }): WormholeConfig => { - const isNativeAsset = asset.isSame(source.nativeAsset); const isDestinationMoonChain = destination.isEqual(moonChain); const isDestinationEvmChain = EvmChain.is(destination); + const isNativeAsset = asset.isSame( + isDestinationEvmChain ? moonChain.nativeAsset : source.nativeAsset, + ); const tokenAddress = isNativeAsset ? 'native' : isDestinationEvmChain diff --git a/packages/config/src/mrl-configs/moonbaseAlpha.ts b/packages/config/src/mrl-configs/moonbaseAlpha.ts index efca13c9..fcd61d07 100644 --- a/packages/config/src/mrl-configs/moonbaseAlpha.ts +++ b/packages/config/src/mrl-configs/moonbaseAlpha.ts @@ -66,7 +66,7 @@ export const moonbaseAlphaRoutes = new ChainRoutes({ asset: dev, fee: { asset: dev, - amount: 0.1, + amount: 0, balance: BalanceBuilder().substrate().system().account(), }, }, diff --git a/packages/mrl/src/getTransferData/getTransferData.utils.ts b/packages/mrl/src/getTransferData/getTransferData.utils.ts index 93c1b94c..a7730c3d 100644 --- a/packages/mrl/src/getTransferData/getTransferData.utils.ts +++ b/packages/mrl/src/getTransferData/getTransferData.utils.ts @@ -17,11 +17,10 @@ import { import { type DestinationChainTransferData, PolkadotService, - type SourceChainTransferData, convertToChainDecimals, getMin, } from '@moonbeam-network/xcm-sdk'; -import { type AssetAmount, EvmParachain } from '@moonbeam-network/xcm-types'; +import { AssetAmount, EvmParachain } from '@moonbeam-network/xcm-types'; import { getMultilocationDerivedAddresses, getPolkadotApi, @@ -33,7 +32,10 @@ import { createPublicClient, encodeFunctionData, } from 'viem'; -import type { MoonChainTransferData } from '../mrl.interfaces'; +import type { + MoonChainTransferData, + SourceTransferData, +} from '../mrl.interfaces'; const MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION = { [moonbeam.key]: 1273110n, @@ -43,7 +45,7 @@ const MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION = { export interface DataParams { destinationData: DestinationChainTransferData; moonChainData: MoonChainTransferData; - sourceData: SourceChainTransferData; + sourceData: SourceTransferData; } export function getMoonChainFeeValueOnSource({ @@ -73,15 +75,24 @@ export function getMrlMin({ moonChainData, sourceData, }: DataParams): AssetAmount { - const min = getMin(destinationData); - const fee = getMoonChainFeeValueOnSource({ + const minInDestination = getMin(destinationData); + const min = AssetAmount.fromChainAsset( + sourceData.chain.getChainAsset(sourceData.balance), + { + amount: minInDestination.amount, + }, + ); + const moonChainFee = getMoonChainFeeValueOnSource({ destinationData, moonChainData, sourceData, }); + const relayerFee = sourceData.relayerFee?.amount + ? sourceData.relayerFee.toBig() + : Big(0); return min.copyWith({ - amount: BigInt(min.toBig().add(fee).toFixed()), + amount: BigInt(min.toBig().add(moonChainFee).add(relayerFee).toFixed()), }); }