Skip to content

Commit 8a488f2

Browse files
authored
Inform required amount in error message for multi assets (#223)
* 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
1 parent 3ade51f commit 8a488f2

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

.changeset/chilled-pugs-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@moonbeam-network/xcm-sdk': patch
3+
---
4+
5+
Inform required amount in error message for multi assets

packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export class Xtokens implements TransferContractInterface {
7070
if (amount === 0n) {
7171
return 0n;
7272
}
73-
7473
/**
7574
* Contract can throw an error if user balance is smaller than fee.
7675
* Or if you try to send 0 as amount.

packages/sdk/src/getTransferData/getSourceData.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ import {
44
ExtrinsicConfig,
55
SubstrateQueryConfig,
66
} from '@moonbeam-network/xcm-builder';
7-
import { FeeAssetConfig, TransferConfig } from '@moonbeam-network/xcm-config';
7+
import {
8+
DestinationFeeConfig,
9+
FeeAssetConfig,
10+
TransferConfig,
11+
} from '@moonbeam-network/xcm-config';
812
import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types';
9-
import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils';
13+
import {
14+
convertDecimals,
15+
toBigInt,
16+
toDecimal,
17+
} from '@moonbeam-network/xcm-utils';
1018
import Big from 'big.js';
1119
import { TransferContractInterface, createContract } from '../contract';
1220
import { PolkadotService } from '../polkadot';
@@ -129,11 +137,17 @@ export async function getSourceData({
129137
fee: destinationFee.amount,
130138
feeAsset: chain.getAssetId(destinationFee),
131139
});
140+
const destinationFeeBalanceAmount = zeroDestinationFeeAmount.copyWith({
141+
amount: destinationFeeBalance,
142+
});
143+
132144
const fee = await getFee({
133145
balance,
134146
chain,
135147
contract,
136148
decimals: zeroFeeAmount.decimals,
149+
destinationFeeBalanceAmount,
150+
destinationFeeConfig: config.destinationFee,
137151
evmSigner,
138152
extrinsic,
139153
feeConfig: config.fee,
@@ -145,9 +159,7 @@ export async function getSourceData({
145159
const { existentialDeposit } = polkadot;
146160
const feeAmount = zeroFeeAmount.copyWith({ amount: fee });
147161
const feeBalanceAmount = zeroFeeAmount.copyWith({ amount: feeBalance });
148-
const destinationFeeBalanceAmount = zeroDestinationFeeAmount.copyWith({
149-
amount: destinationFeeBalance,
150-
});
162+
151163
const minAmount = zeroAmount.copyWith({ amount: min });
152164

153165
const maxAmount = getMax({
@@ -207,6 +219,8 @@ export interface GetFeeParams {
207219
evmSigner?: EvmSigner;
208220
extrinsic?: ExtrinsicConfig;
209221
feeConfig?: FeeAssetConfig;
222+
destinationFeeConfig?: DestinationFeeConfig;
223+
destinationFeeBalanceAmount?: AssetAmount;
210224
polkadot: PolkadotService;
211225
sourceAddress: string;
212226
}
@@ -216,6 +230,8 @@ export async function getFee({
216230
chain,
217231
contract,
218232
decimals,
233+
destinationFeeConfig,
234+
destinationFeeBalanceAmount,
219235
evmSigner,
220236
extrinsic,
221237
feeConfig,
@@ -227,6 +243,27 @@ export async function getFee({
227243
throw new Error('EVM Signer must be provided');
228244
}
229245

246+
if (
247+
destinationFeeConfig &&
248+
destinationFeeBalanceAmount &&
249+
typeof destinationFeeConfig.amount === 'number'
250+
) {
251+
const destinationFeeBalance = Number(
252+
toDecimal(
253+
destinationFeeBalanceAmount.amount,
254+
destinationFeeBalanceAmount.decimals,
255+
),
256+
);
257+
if (
258+
destinationFeeBalance &&
259+
destinationFeeConfig.amount > destinationFeeBalance
260+
) {
261+
throw new Error(
262+
`Can't get a fee, make sure you have ${destinationFeeConfig?.amount} ${destinationFeeConfig?.asset.originSymbol} needed for fees in destination`,
263+
);
264+
}
265+
}
266+
230267
return getContractFee(balance, contract, decimals, evmSigner);
231268
}
232269

0 commit comments

Comments
 (0)