Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inform required amount in error message for multi assets #223

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading