Skip to content

Commit

Permalink
control destination fee balance before calculating contract fee and t…
Browse files Browse the repository at this point in the history
…hrow error if balance not enough
  • Loading branch information
mmaurello committed Mar 26, 2024
1 parent 3ade51f commit 5100ef3
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import {
ExtrinsicConfig,
SubstrateQueryConfig,
} from '@moonbeam-network/xcm-builder';
import { FeeAssetConfig, TransferConfig } from '@moonbeam-network/xcm-config';
import {
DestinationFeeConfig,
FeeAssetConfig,
TransferConfig,
} from '@moonbeam-network/xcm-config';
import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types';
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
import {
convertDecimals,
toBigInt,
toDecimal,
} from '@moonbeam-network/xcm-utils';
import Big from 'big.js';
import { TransferContractInterface, createContract } from '../contract';
import { PolkadotService } from '../polkadot';
Expand Down Expand Up @@ -129,11 +137,17 @@ export async function getSourceData({
fee: destinationFee.amount,
feeAsset: chain.getAssetId(destinationFee),
});
const destinationFeeBalanceAmount = zeroDestinationFeeAmount.copyWith({
amount: destinationFeeBalance,
});

const fee = await getFee({
balance,
chain,
contract,
decimals: zeroFeeAmount.decimals,
destinationFeeBalanceAmount,
destinationFeeConfig: config.destinationFee,
evmSigner,
extrinsic,
feeConfig: config.fee,
Expand All @@ -145,9 +159,7 @@ export async function getSourceData({
const { existentialDeposit } = polkadot;
const feeAmount = zeroFeeAmount.copyWith({ amount: fee });
const feeBalanceAmount = zeroFeeAmount.copyWith({ amount: feeBalance });
const destinationFeeBalanceAmount = zeroDestinationFeeAmount.copyWith({
amount: destinationFeeBalance,
});

const minAmount = zeroAmount.copyWith({ amount: min });

const maxAmount = getMax({
Expand Down Expand Up @@ -207,6 +219,8 @@ export interface GetFeeParams {
evmSigner?: EvmSigner;
extrinsic?: ExtrinsicConfig;
feeConfig?: FeeAssetConfig;
destinationFeeConfig?: DestinationFeeConfig;
destinationFeeBalanceAmount?: AssetAmount;
polkadot: PolkadotService;
sourceAddress: string;
}
Expand All @@ -216,6 +230,8 @@ export async function getFee({
chain,
contract,
decimals,
destinationFeeConfig,
destinationFeeBalanceAmount,
evmSigner,
extrinsic,
feeConfig,
Expand All @@ -227,6 +243,24 @@ export async function getFee({
throw new Error('EVM Signer must be provided');
}

if (
destinationFeeConfig &&
destinationFeeBalanceAmount &&
typeof destinationFeeConfig.amount === 'number'
) {
const destinationFeeBalance = Number(
toDecimal(
destinationFeeBalanceAmount.amount,
destinationFeeBalanceAmount.decimals,
),
);
if (destinationFeeConfig.amount > destinationFeeBalance) {
throw new Error(
`Can't get a fee, make sure you have ${destinationFeeConfig?.amount} ${destinationFeeConfig?.asset.originSymbol} needed for fees in destination`,
);
}
}

return getContractFee(balance, contract, decimals, evmSigner);
}

Expand Down

0 comments on commit 5100ef3

Please sign in to comment.