diff --git a/packages/mrl/src/getTransferData/getTransferData.ts b/packages/mrl/src/getTransferData/getTransferData.ts index 44d9cdf8..323641bf 100644 --- a/packages/mrl/src/getTransferData/getTransferData.ts +++ b/packages/mrl/src/getTransferData/getTransferData.ts @@ -10,7 +10,6 @@ import { type Signers, convertToChainDecimals, getDestinationData, - getMin, } from '@moonbeam-network/xcm-sdk'; import { AssetAmount, @@ -23,7 +22,11 @@ import type { TransferData } from '../mrl.interfaces'; import { WormholeService } from '../services/wormhole'; import { getMoonChainData } from './getMoonChainData'; import { getSourceData } from './getSourceData'; -import { buildTransfer } from './getTransferData.utils'; +import { + buildTransfer, + getMoonChainFeeValueOnSource, + getMrlMin, +} from './getTransferData.utils'; export interface GetTransferDataParams { route: AssetRoute; @@ -69,21 +72,32 @@ export async function getTransferData({ return { destination: destinationData, getEstimate(amount: number | string) { + const isSameAssetPayingDestinationFee = + sourceData.balance.isSame(destinationFee); const bigAmount = Big( toBigInt(amount, sourceData.balance.decimals).toString(), ); - const result = bigAmount.minus( - sourceData.balance.isSame(destinationFee) - ? destinationFee.toBig() - : Big(0), - ); + const fee = getMoonChainFeeValueOnSource({ + destinationData, + moonChainData, + sourceData, + }); + const result = bigAmount + .minus( + isSameAssetPayingDestinationFee ? destinationFee.toBig() : Big(0), + ) + .minus(fee); return sourceData.balance.copyWith({ amount: result.lt(0) ? 0n : BigInt(result.toFixed()), }); }, max: sourceData.max, - min: getMin(destinationData), + min: getMrlMin({ + destinationData, + moonChainData, + sourceData, + }), moonChain: moonChainData, source: sourceData, async transfer( diff --git a/packages/mrl/src/getTransferData/getTransferData.utils.ts b/packages/mrl/src/getTransferData/getTransferData.utils.ts index d152c456..82c2cca0 100644 --- a/packages/mrl/src/getTransferData/getTransferData.utils.ts +++ b/packages/mrl/src/getTransferData/getTransferData.utils.ts @@ -14,24 +14,78 @@ import { moonbaseAlpha, moonbeam, } from '@moonbeam-network/xcm-config'; -import { PolkadotService } from '@moonbeam-network/xcm-sdk'; +import { + type DestinationChainTransferData, + PolkadotService, + type SourceChainTransferData, + convertToChainDecimals, + getMin, +} from '@moonbeam-network/xcm-sdk'; import { type AssetAmount, EvmParachain } from '@moonbeam-network/xcm-types'; import { getMultilocationDerivedAddresses, getPolkadotApi, } from '@moonbeam-network/xcm-utils'; +import Big from 'big.js'; import { http, type Address, createPublicClient, encodeFunctionData, } from 'viem'; +import type { MoonChainTransferData } from '../mrl.interfaces'; const MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION = { [moonbeam.key]: 657226n, [moonbaseAlpha.key]: 1271922n, }; +export interface DataParams { + destinationData: DestinationChainTransferData; + moonChainData: MoonChainTransferData; + sourceData: SourceChainTransferData; +} + +export function getMoonChainFeeValueOnSource({ + destinationData, + moonChainData, + sourceData, +}: DataParams): Big { + const isSourceParachain = EvmParachain.isAnyParachain(sourceData.chain); + const isDestinationMoonChain = destinationData.chain.isEqual( + moonChainData.chain, + ); + const isSameAssetPayingMoonChainFee = sourceData.balance.isSame( + moonChainData.fee, + ); + + return !isDestinationMoonChain && + isSourceParachain && + isSameAssetPayingMoonChainFee + ? convertToChainDecimals({ + asset: moonChainData.fee, + chain: sourceData.chain, + }).toBig() + : Big(0); +} + +export function getMrlMin({ + destinationData, + moonChainData, + sourceData, +}: DataParams): AssetAmount { + const min = getMin(destinationData); + const fee = getMoonChainFeeValueOnSource({ + destinationData, + moonChainData, + sourceData, + }); + + return min.copyWith({ + amount: BigInt(min.toBig().add(fee).toFixed()), + }); +} + export interface BuildTransferParams { asset: AssetAmount; destinationAddress: string; diff --git a/packages/mrl/src/mrl.interfaces.ts b/packages/mrl/src/mrl.interfaces.ts index a9bd3c5f..29e02134 100644 --- a/packages/mrl/src/mrl.interfaces.ts +++ b/packages/mrl/src/mrl.interfaces.ts @@ -22,7 +22,6 @@ export interface TransferData { } export interface SourceTransferData extends ChainTransferData { - chain: AnyChain; destinationFeeBalance: AssetAmount; feeBalance: AssetAmount; max: AssetAmount;