Skip to content

Commit

Permalink
Finished with getEstimate and getMin
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Sep 12, 2024
1 parent 51388b1 commit 41a42b3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
30 changes: 22 additions & 8 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
type Signers,
convertToChainDecimals,
getDestinationData,
getMin,
} from '@moonbeam-network/xcm-sdk';
import {
AssetAmount,
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
56 changes: 55 additions & 1 deletion packages/mrl/src/getTransferData/getTransferData.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface TransferData {
}

export interface SourceTransferData extends ChainTransferData {
chain: AnyChain;
destinationFeeBalance: AssetAmount;
feeBalance: AssetAmount;
max: AssetAmount;
Expand Down

0 comments on commit 41a42b3

Please sign in to comment.