Skip to content

Commit

Permalink
Inform required amount in error message for multi assets (#223)
Browse files Browse the repository at this point in the history
* control destination fee balance before calculating contract fee and throw error if balance not enough

* return 0 as fees in multiassets and let UI handle the balance

* revert unimplemented option

* add changeset

* remove check for balances 0
  • Loading branch information
mmaurello authored Mar 27, 2024
1 parent 3ade51f commit 8a488f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-pugs-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-sdk': patch
---

Inform required amount in error message for multi assets
1 change: 0 additions & 1 deletion packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export class Xtokens implements TransferContractInterface {
if (amount === 0n) {
return 0n;
}

/**
* Contract can throw an error if user balance is smaller than fee.
* Or if you try to send 0 as amount.
Expand Down
47 changes: 42 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,27 @@ 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 (
destinationFeeBalance &&
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 8a488f2

Please sign in to comment.