@@ -5,13 +5,18 @@ import {
5
5
SubstrateQueryConfig ,
6
6
} from '@moonbeam-network/xcm-builder' ;
7
7
import { FeeAssetConfig , TransferConfig } from '@moonbeam-network/xcm-config' ;
8
- import { AssetAmount } from '@moonbeam-network/xcm-types' ;
8
+ import { AnyChain , AssetAmount } from '@moonbeam-network/xcm-types' ;
9
9
import { convertDecimals , toBigInt } from '@moonbeam-network/xcm-utils' ;
10
10
import Big from 'big.js' ;
11
11
import { TransferContractInterface , createContract } from '../contract' ;
12
12
import { PolkadotService } from '../polkadot' ;
13
13
import { EvmSigner , SourceChainTransferData } from '../sdk.interfaces' ;
14
- import { getBalance , getDecimals , getMin } from './getTransferData.utils' ;
14
+ import {
15
+ GetBalancesParams ,
16
+ getBalance ,
17
+ getDecimals ,
18
+ getMin ,
19
+ } from './getTransferData.utils' ;
15
20
16
21
export interface GetSourceDataParams {
17
22
transferConfig : TransferConfig ;
@@ -39,6 +44,7 @@ export async function getSourceData({
39
44
amount : 0n ,
40
45
decimals : await getDecimals ( {
41
46
address : destinationAddress ,
47
+ chain,
42
48
config,
43
49
evmSigner,
44
50
polkadot,
@@ -50,6 +56,7 @@ export async function getSourceData({
50
56
decimals : await getDecimals ( {
51
57
address : destinationAddress ,
52
58
asset : config . fee . asset ,
59
+ chain,
53
60
config,
54
61
evmSigner,
55
62
polkadot,
@@ -62,6 +69,7 @@ export async function getSourceData({
62
69
decimals : await getDecimals ( {
63
70
address : destinationAddress ,
64
71
asset : config . destinationFee . asset ,
72
+ chain,
65
73
config,
66
74
evmSigner,
67
75
polkadot,
@@ -71,14 +79,18 @@ export async function getSourceData({
71
79
72
80
const balance = await getBalance ( {
73
81
address : sourceAddress ,
82
+ chain,
74
83
config,
84
+ decimals : zeroAmount . decimals ,
75
85
evmSigner,
76
86
polkadot,
77
87
} ) ;
78
88
79
- const feeBalance = await getFeeBalances ( {
89
+ const feeBalance = await getFeeBalance ( {
80
90
address : sourceAddress ,
81
91
balance,
92
+ chain,
93
+ decimals : zeroFeeAmount . decimals ,
82
94
feeConfig : config . fee ,
83
95
polkadot,
84
96
} ) ;
@@ -87,9 +99,11 @@ export async function getSourceData({
87
99
config . destinationFee . asset ,
88
100
)
89
101
? balance
90
- : await getFeeBalances ( {
102
+ : await getFeeBalance ( {
91
103
address : sourceAddress ,
92
104
balance,
105
+ chain,
106
+ decimals : zeroDestinationFeeAmount . decimals ,
93
107
feeConfig : config . destinationFee ,
94
108
polkadot,
95
109
} ) ;
@@ -117,6 +131,7 @@ export async function getSourceData({
117
131
} ) ;
118
132
const fee = await getFee ( {
119
133
balance,
134
+ chain,
120
135
contract,
121
136
decimals : zeroFeeAmount . decimals ,
122
137
evmSigner,
@@ -134,6 +149,7 @@ export async function getSourceData({
134
149
amount : destinationFeeBalance ,
135
150
} ) ;
136
151
const minAmount = zeroAmount . copyWith ( { amount : min } ) ;
152
+
137
153
const maxAmount = getMax ( {
138
154
balanceAmount,
139
155
existentialDeposit,
@@ -153,32 +169,41 @@ export async function getSourceData({
153
169
} ;
154
170
}
155
171
156
- export interface GetBalancesParams {
157
- address : string ;
172
+ export interface GetFeeBalanceParams
173
+ extends Omit < GetBalancesParams , 'config' | 'evmSigner' > {
158
174
balance : bigint ;
159
175
feeConfig : FeeAssetConfig | undefined ;
160
- polkadot : PolkadotService ;
161
176
}
162
177
163
- export async function getFeeBalances ( {
178
+ export async function getFeeBalance ( {
164
179
address,
165
180
balance,
181
+ chain,
182
+ decimals,
166
183
feeConfig,
167
184
polkadot,
168
- } : GetBalancesParams ) {
169
- return feeConfig
170
- ? polkadot . query (
171
- feeConfig . balance . build ( {
172
- address,
173
- asset : polkadot . chain . getBalanceAssetId ( feeConfig . asset ) ,
174
- } ) as SubstrateQueryConfig ,
175
- )
176
- : balance ;
185
+ } : GetFeeBalanceParams ) {
186
+ if ( ! feeConfig ) {
187
+ return balance ;
188
+ }
189
+
190
+ const convertAmount = chain . usesOwnDecimalsInternally ;
191
+ const feeBalance = await polkadot . query (
192
+ feeConfig . balance . build ( {
193
+ address,
194
+ asset : polkadot . chain . getBalanceAssetId ( feeConfig . asset ) ,
195
+ } ) as SubstrateQueryConfig ,
196
+ ) ;
197
+
198
+ return convertAmount
199
+ ? convertDecimals ( feeBalance , polkadot . decimals , decimals )
200
+ : feeBalance ;
177
201
}
178
202
179
203
export interface GetFeeParams {
180
204
balance : bigint ;
181
205
contract ?: ContractConfig ;
206
+ chain : AnyChain ;
182
207
decimals : number ;
183
208
evmSigner ?: EvmSigner ;
184
209
extrinsic ?: ExtrinsicConfig ;
@@ -189,6 +214,7 @@ export interface GetFeeParams {
189
214
190
215
export async function getFee ( {
191
216
balance,
217
+ chain,
192
218
contract,
193
219
decimals,
194
220
evmSigner,
@@ -215,7 +241,13 @@ export async function getFee({
215
241
216
242
const xcmDeliveryFee = getXcmDeliveryFee ( decimals , feeConfig ) ;
217
243
218
- return extrinsicFee + xcmDeliveryFee ;
244
+ const convertAmount = chain . usesOwnDecimalsInternally ;
245
+
246
+ const totalFee = extrinsicFee + xcmDeliveryFee ;
247
+
248
+ return convertAmount
249
+ ? convertDecimals ( totalFee , polkadot . decimals , decimals )
250
+ : totalFee ;
219
251
}
220
252
221
253
throw new Error ( 'Either contract or extrinsic must be provided' ) ;
0 commit comments