@@ -4,9 +4,17 @@ import {
4
4
ExtrinsicConfig ,
5
5
SubstrateQueryConfig ,
6
6
} 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' ;
8
12
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' ;
10
18
import Big from 'big.js' ;
11
19
import { TransferContractInterface , createContract } from '../contract' ;
12
20
import { PolkadotService } from '../polkadot' ;
@@ -129,11 +137,17 @@ export async function getSourceData({
129
137
fee : destinationFee . amount ,
130
138
feeAsset : chain . getAssetId ( destinationFee ) ,
131
139
} ) ;
140
+ const destinationFeeBalanceAmount = zeroDestinationFeeAmount . copyWith ( {
141
+ amount : destinationFeeBalance ,
142
+ } ) ;
143
+
132
144
const fee = await getFee ( {
133
145
balance,
134
146
chain,
135
147
contract,
136
148
decimals : zeroFeeAmount . decimals ,
149
+ destinationFeeBalanceAmount,
150
+ destinationFeeConfig : config . destinationFee ,
137
151
evmSigner,
138
152
extrinsic,
139
153
feeConfig : config . fee ,
@@ -145,9 +159,7 @@ export async function getSourceData({
145
159
const { existentialDeposit } = polkadot ;
146
160
const feeAmount = zeroFeeAmount . copyWith ( { amount : fee } ) ;
147
161
const feeBalanceAmount = zeroFeeAmount . copyWith ( { amount : feeBalance } ) ;
148
- const destinationFeeBalanceAmount = zeroDestinationFeeAmount . copyWith ( {
149
- amount : destinationFeeBalance ,
150
- } ) ;
162
+
151
163
const minAmount = zeroAmount . copyWith ( { amount : min } ) ;
152
164
153
165
const maxAmount = getMax ( {
@@ -207,6 +219,8 @@ export interface GetFeeParams {
207
219
evmSigner ?: EvmSigner ;
208
220
extrinsic ?: ExtrinsicConfig ;
209
221
feeConfig ?: FeeAssetConfig ;
222
+ destinationFeeConfig ?: DestinationFeeConfig ;
223
+ destinationFeeBalanceAmount ?: AssetAmount ;
210
224
polkadot : PolkadotService ;
211
225
sourceAddress : string ;
212
226
}
@@ -216,6 +230,8 @@ export async function getFee({
216
230
chain,
217
231
contract,
218
232
decimals,
233
+ destinationFeeConfig,
234
+ destinationFeeBalanceAmount,
219
235
evmSigner,
220
236
extrinsic,
221
237
feeConfig,
@@ -227,6 +243,27 @@ export async function getFee({
227
243
throw new Error ( 'EVM Signer must be provided' ) ;
228
244
}
229
245
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
+
230
267
return getContractFee ( balance , contract , decimals , evmSigner ) ;
231
268
}
232
269
0 commit comments