@moonbeam-network/xcm-sdk 1.0.0-dev.14 → 1.0.0-dev.140

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
1
+ import { SubstrateQueryConfig, ExtrinsicConfig, BalanceConfigBuilder, ContractConfig } from '@moonbeam-network/xcm-builder';
2
+ import { IConfigService, AssetConfig, FeeAssetConfig, TransferConfig, DestinationFeeConfig } from '@moonbeam-network/xcm-config';
3
+ import { AnyParachain, Asset, AssetAmount, ChainAssetId, AnyChain, EvmParachain, Ecosystem } from '@moonbeam-network/xcm-types';
4
+ import { ApiPromise } from '@polkadot/api';
5
+ import { Signer } from '@polkadot/api/types';
6
+ import { IKeyringPair } from '@polkadot/types/types';
7
+ import { Signer as Signer$1 } from 'ethers';
8
+ import { WalletClient } from 'viem';
9
+
10
+ declare class PolkadotService {
11
+ #private;
12
+ readonly api: ApiPromise;
13
+ readonly chain: AnyParachain;
14
+ readonly configService: IConfigService;
15
+ constructor(api: ApiPromise, chain: AnyParachain, configService: IConfigService);
16
+ static create(chain: AnyParachain, configService: IConfigService): Promise<PolkadotService>;
17
+ static createMulti(chains: AnyParachain[], configService: IConfigService): Promise<PolkadotService[]>;
18
+ get decimals(): number;
19
+ get asset(): Asset;
20
+ get existentialDeposit(): AssetAmount;
21
+ getAssetMeta(asset: ChainAssetId): Promise<{
22
+ symbol: string;
23
+ decimals: number;
24
+ } | undefined>;
25
+ getAssetDecimalsFromQuery(asset: ChainAssetId): Promise<number | undefined>;
26
+ getAssetDecimals(asset: Asset): Promise<number>;
27
+ query(config: SubstrateQueryConfig): Promise<bigint>;
28
+ getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
29
+ transfer(account: string, config: ExtrinsicConfig, signer: Signer | IKeyringPair): Promise<string>;
30
+ }
31
+
32
+ type EvmSigner = Signer$1 | WalletClient;
33
+ interface Signers {
34
+ /**
35
+ * @deprecated ethersSigner - is deprecated and will be removed in v2, use evmSigner instead
36
+ */
37
+ ethersSigner?: Signer$1;
38
+ evmSigner?: EvmSigner;
39
+ polkadotSigner: Signer | IKeyringPair;
40
+ }
41
+ interface TransferData {
42
+ destination: DestinationChainTransferData;
43
+ getEstimate(amount: number | string): AssetAmount;
44
+ isSwapPossible: boolean;
45
+ max: AssetAmount;
46
+ min: AssetAmount;
47
+ source: SourceChainTransferData;
48
+ swap(): Promise<TransferData | undefined>;
49
+ transfer(amount: bigint | number | string): Promise<string>;
50
+ }
51
+ interface SourceChainTransferData extends ChainTransferData {
52
+ destinationFee: AssetAmount;
53
+ destinationFeeBalance: AssetAmount;
54
+ feeBalance: AssetAmount;
55
+ max: AssetAmount;
56
+ }
57
+ interface SovereignAccountBalance {
58
+ feeAssetBalance: bigint | undefined;
59
+ transferAssetBalance: bigint;
60
+ }
61
+ interface DestinationChainTransferData extends ChainTransferData {
62
+ sovereignAccountBalances?: SovereignAccountBalance;
63
+ }
64
+ interface ChainTransferData {
65
+ balance: AssetAmount;
66
+ chain: AnyChain;
67
+ existentialDeposit: AssetAmount;
68
+ fee: AssetAmount;
69
+ min: AssetAmount;
70
+ }
71
+
72
+ interface BaseParams {
73
+ address: string;
74
+ chain: AnyChain;
75
+ polkadot: PolkadotService;
76
+ }
77
+ interface GetBalancesParams extends BaseParams {
78
+ asset: Asset;
79
+ balanceBuilder: BalanceConfigBuilder;
80
+ decimals: number;
81
+ }
82
+ interface GetDecimalsParams extends BaseParams {
83
+ asset?: Asset;
84
+ config: AssetConfig | FeeAssetConfig;
85
+ assetBuiltConfig?: SubstrateQueryConfig | ContractConfig;
86
+ }
87
+ declare function getBalance({ address, chain, balanceBuilder, asset, decimals, polkadot, }: GetBalancesParams): Promise<bigint>;
88
+ declare function getDecimals({ address, asset, config, polkadot, chain, assetBuiltConfig, }: GetDecimalsParams): Promise<number>;
89
+ declare function getMin(config: AssetConfig, polkadot: PolkadotService): Promise<bigint>;
90
+ interface ValidateSovereignAccountBalancesProps {
91
+ amount: bigint;
92
+ destination: DestinationChainTransferData;
93
+ source: SourceChainTransferData;
94
+ }
95
+ declare function validateSovereignAccountBalances({ amount, source, destination, }: ValidateSovereignAccountBalancesProps): void;
96
+
97
+ interface GetSourceDataParams {
98
+ transferConfig: TransferConfig;
99
+ destinationAddress: string;
100
+ destinationFee: AssetAmount;
101
+ evmSigner?: EvmSigner;
102
+ polkadot: PolkadotService;
103
+ sourceAddress: string;
104
+ }
105
+ declare function getSourceData({ transferConfig, destinationAddress, destinationFee, polkadot, sourceAddress, evmSigner, }: GetSourceDataParams): Promise<SourceChainTransferData>;
106
+ interface GetFeeBalanceParams extends BaseParams {
107
+ balance: bigint;
108
+ feeConfig: FeeAssetConfig | undefined;
109
+ decimals: number;
110
+ }
111
+ declare function getFeeBalance({ address, balance, chain, decimals, feeConfig, polkadot, }: GetFeeBalanceParams): Promise<bigint>;
112
+ interface GetFeeParams {
113
+ balance: bigint;
114
+ contract?: ContractConfig;
115
+ chain: AnyChain;
116
+ decimals: number;
117
+ evmSigner?: EvmSigner;
118
+ extrinsic?: ExtrinsicConfig;
119
+ feeConfig?: FeeAssetConfig;
120
+ destinationFeeConfig?: DestinationFeeConfig;
121
+ destinationFeeBalanceAmount?: AssetAmount;
122
+ polkadot: PolkadotService;
123
+ sourceAddress: string;
124
+ }
125
+ declare function getFee({ balance, chain, contract, decimals, destinationFeeConfig, destinationFeeBalanceAmount, evmSigner, extrinsic, feeConfig, polkadot, sourceAddress, }: GetFeeParams): Promise<bigint>;
126
+ declare function getContractFee({ balance, config, decimals, evmSigner, chain, }: {
127
+ balance: bigint;
128
+ config: ContractConfig;
129
+ decimals: number;
130
+ evmSigner: EvmSigner;
131
+ chain: EvmParachain;
132
+ }): Promise<bigint>;
133
+ declare function getExtrinsicFee(balance: bigint, extrinsic: ExtrinsicConfig, polkadot: PolkadotService, sourceAddress: string): Promise<bigint>;
134
+ interface GetMaxParams {
135
+ balanceAmount: AssetAmount;
136
+ existentialDeposit: AssetAmount;
137
+ feeAmount: AssetAmount;
138
+ minAmount: AssetAmount;
139
+ }
140
+ declare function getMax({ balanceAmount, existentialDeposit, feeAmount, minAmount, }: GetMaxParams): AssetAmount;
141
+ interface GetAssetsBalancesParams {
142
+ address: string;
143
+ chain: AnyChain;
144
+ assets: AssetConfig[];
145
+ evmSigner?: EvmSigner;
146
+ polkadot: PolkadotService;
147
+ }
148
+ declare function getAssetsBalances({ address, chain, assets, polkadot, }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
149
+
150
+ interface SdkOptions extends Partial<Signers> {
151
+ configService?: IConfigService;
152
+ }
153
+ declare function Sdk(options?: SdkOptions): {
154
+ assets(ecosystem?: Ecosystem): {
155
+ assets: Asset[];
156
+ asset(keyOrAsset: string | Asset): {
157
+ sourceChains: AnyChain[];
158
+ source(keyOrChain: string | AnyChain): {
159
+ destinationChains: AnyChain[];
160
+ destination(destKeyOrChain: string | AnyChain): {
161
+ accounts(sourceAddress: string, destinationAddress: string, signers?: Partial<Signers>): Promise<TransferData>;
162
+ };
163
+ };
164
+ };
165
+ };
166
+ getTransferData({ destinationAddress, destinationKeyOrChain, ethersSigner, evmSigner, keyOrAsset, polkadotSigner, sourceAddress, sourceKeyOrChain, }: SdkTransferParams): Promise<TransferData>;
167
+ };
168
+ declare function getParachainBalances(chain: AnyChain, address: string): Promise<AssetAmount[]>;
169
+ interface SdkTransferParams extends Partial<Signers> {
170
+ destinationAddress: string;
171
+ destinationKeyOrChain: string | AnyChain;
172
+ keyOrAsset: string | Asset;
173
+ sourceAddress: string;
174
+ sourceKeyOrChain: string | AnyChain;
175
+ }
176
+
177
+ export { type BaseParams, type ChainTransferData, type DestinationChainTransferData, type EvmSigner, type GetAssetsBalancesParams, type GetBalancesParams, type GetDecimalsParams, type GetFeeBalanceParams, type GetFeeParams, type GetMaxParams, type GetSourceDataParams, Sdk, type SdkOptions, type SdkTransferParams, type Signers, type SourceChainTransferData, type SovereignAccountBalance, type TransferData, getAssetsBalances, getBalance, getContractFee, getDecimals, getExtrinsicFee, getFee, getFeeBalance, getMax, getMin, getParachainBalances, getSourceData, validateSovereignAccountBalances };
package/build/index.d.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  import {
2
2
  SubstrateQueryConfig,
3
3
  ExtrinsicConfig,
4
+ BalanceConfigBuilder,
4
5
  ContractConfig,
5
6
  } from '@moonbeam-network/xcm-builder';
6
7
  import {
7
- TransferConfig,
8
- FeeAssetConfig,
9
8
  IConfigService,
9
+ AssetConfig,
10
+ FeeAssetConfig,
11
+ TransferConfig,
12
+ DestinationFeeConfig,
10
13
  } from '@moonbeam-network/xcm-config';
11
14
  import {
12
15
  AnyParachain,
@@ -14,6 +17,7 @@ import {
14
17
  AssetAmount,
15
18
  ChainAssetId,
16
19
  AnyChain,
20
+ EvmParachain,
17
21
  Ecosystem,
18
22
  } from '@moonbeam-network/xcm-types';
19
23
  import { ApiPromise } from '@polkadot/api';
@@ -26,9 +30,20 @@ declare class PolkadotService {
26
30
  #private;
27
31
  readonly api: ApiPromise;
28
32
  readonly chain: AnyParachain;
29
- constructor(api: ApiPromise, chain: AnyParachain);
30
- static create(chain: AnyParachain): Promise<PolkadotService>;
31
- static createMulti(chains: AnyParachain[]): Promise<PolkadotService[]>;
33
+ readonly configService: IConfigService;
34
+ constructor(
35
+ api: ApiPromise,
36
+ chain: AnyParachain,
37
+ configService: IConfigService,
38
+ );
39
+ static create(
40
+ chain: AnyParachain,
41
+ configService: IConfigService,
42
+ ): Promise<PolkadotService>;
43
+ static createMulti(
44
+ chains: AnyParachain[],
45
+ configService: IConfigService,
46
+ ): Promise<PolkadotService[]>;
32
47
  get decimals(): number;
33
48
  get asset(): Asset;
34
49
  get existentialDeposit(): AssetAmount;
@@ -39,6 +54,7 @@ declare class PolkadotService {
39
54
  }
40
55
  | undefined
41
56
  >;
57
+ getAssetDecimalsFromQuery(asset: ChainAssetId): Promise<number | undefined>;
42
58
  getAssetDecimals(asset: Asset): Promise<number>;
43
59
  query(config: SubstrateQueryConfig): Promise<bigint>;
44
60
  getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
@@ -69,11 +85,18 @@ interface TransferData {
69
85
  transfer(amount: bigint | number | string): Promise<string>;
70
86
  }
71
87
  interface SourceChainTransferData extends ChainTransferData {
88
+ destinationFee: AssetAmount;
72
89
  destinationFeeBalance: AssetAmount;
73
90
  feeBalance: AssetAmount;
74
91
  max: AssetAmount;
75
92
  }
76
- interface DestinationChainTransferData extends ChainTransferData {}
93
+ interface SovereignAccountBalance {
94
+ feeAssetBalance: bigint | undefined;
95
+ transferAssetBalance: bigint;
96
+ }
97
+ interface DestinationChainTransferData extends ChainTransferData {
98
+ sovereignAccountBalances?: SovereignAccountBalance;
99
+ }
77
100
  interface ChainTransferData {
78
101
  balance: AssetAmount;
79
102
  chain: AnyChain;
@@ -82,11 +105,57 @@ interface ChainTransferData {
82
105
  min: AssetAmount;
83
106
  }
84
107
 
108
+ interface BaseParams {
109
+ address: string;
110
+ chain: AnyChain;
111
+ polkadot: PolkadotService;
112
+ }
113
+ interface GetBalancesParams extends BaseParams {
114
+ asset: Asset;
115
+ balanceBuilder: BalanceConfigBuilder;
116
+ decimals: number;
117
+ }
118
+ interface GetDecimalsParams extends BaseParams {
119
+ asset?: Asset;
120
+ config: AssetConfig | FeeAssetConfig;
121
+ assetBuiltConfig?: SubstrateQueryConfig | ContractConfig;
122
+ }
123
+ declare function getBalance({
124
+ address,
125
+ chain,
126
+ balanceBuilder,
127
+ asset,
128
+ decimals,
129
+ polkadot,
130
+ }: GetBalancesParams): Promise<bigint>;
131
+ declare function getDecimals({
132
+ address,
133
+ asset,
134
+ config,
135
+ polkadot,
136
+ chain,
137
+ assetBuiltConfig,
138
+ }: GetDecimalsParams): Promise<number>;
139
+ declare function getMin(
140
+ config: AssetConfig,
141
+ polkadot: PolkadotService,
142
+ ): Promise<bigint>;
143
+ interface ValidateSovereignAccountBalancesProps {
144
+ amount: bigint;
145
+ destination: DestinationChainTransferData;
146
+ source: SourceChainTransferData;
147
+ }
148
+ declare function validateSovereignAccountBalances({
149
+ amount,
150
+ source,
151
+ destination,
152
+ }: ValidateSovereignAccountBalancesProps): void;
153
+
85
154
  interface GetSourceDataParams {
86
155
  transferConfig: TransferConfig;
87
156
  destinationAddress: string;
88
157
  destinationFee: AssetAmount;
89
- evmSigner: EvmSigner;
158
+ evmSigner?: EvmSigner;
90
159
  polkadot: PolkadotService;
91
160
  sourceAddress: string;
92
161
  }
@@ -94,46 +163,62 @@ declare function getSourceData({
94
163
  transferConfig,
95
164
  destinationAddress,
96
165
  destinationFee,
97
- evmSigner,
98
166
  polkadot,
99
167
  sourceAddress,
168
+ evmSigner,
100
169
  }: GetSourceDataParams): Promise<SourceChainTransferData>;
101
- interface GetBalancesParams {
102
- address: string;
170
+ interface GetFeeBalanceParams extends BaseParams {
103
171
  balance: bigint;
104
172
  feeConfig: FeeAssetConfig | undefined;
105
- polkadot: PolkadotService;
173
+ decimals: number;
106
174
  }
107
- declare function getFeeBalances({
175
+ declare function getFeeBalance({
108
176
  address,
109
177
  balance,
178
+ chain,
179
+ decimals,
110
180
  feeConfig,
111
181
  polkadot,
112
- }: GetBalancesParams): Promise<bigint>;
182
+ }: GetFeeBalanceParams): Promise<bigint>;
113
183
  interface GetFeeParams {
114
184
  balance: bigint;
115
185
  contract?: ContractConfig;
186
+ chain: AnyChain;
116
187
  decimals: number;
117
188
  evmSigner?: EvmSigner;
118
189
  extrinsic?: ExtrinsicConfig;
190
+ feeConfig?: FeeAssetConfig;
191
+ destinationFeeConfig?: DestinationFeeConfig;
192
+ destinationFeeBalanceAmount?: AssetAmount;
119
193
  polkadot: PolkadotService;
120
194
  sourceAddress: string;
121
195
  }
122
196
  declare function getFee({
123
197
  balance,
198
+ chain,
124
199
  contract,
125
200
  decimals,
201
+ destinationFeeConfig,
202
+ destinationFeeBalanceAmount,
126
203
  evmSigner,
127
204
  extrinsic,
205
+ feeConfig,
128
206
  polkadot,
129
207
  sourceAddress,
130
208
  }: GetFeeParams): Promise<bigint>;
131
- declare function getContractFee(
132
- balance: bigint,
133
- config: ContractConfig,
134
- decimals: number,
135
- evmSigner: EvmSigner,
136
- ): Promise<bigint>;
209
+ declare function getContractFee({
210
+ balance,
211
+ config,
212
+ decimals,
213
+ evmSigner,
214
+ chain,
215
+ }: {
216
+ balance: bigint;
217
+ config: ContractConfig;
218
+ decimals: number;
219
+ evmSigner: EvmSigner;
220
+ chain: EvmParachain;
221
+ }): Promise<bigint>;
137
222
  declare function getExtrinsicFee(
138
223
  balance: bigint,
139
224
  extrinsic: ExtrinsicConfig,
@@ -152,6 +237,19 @@ declare function getMax({
152
237
  feeAmount,
153
238
  minAmount,
154
239
  }: GetMaxParams): AssetAmount;
240
+ interface GetAssetsBalancesParams {
241
+ address: string;
242
+ chain: AnyChain;
243
+ assets: AssetConfig[];
244
+ evmSigner?: EvmSigner;
245
+ polkadot: PolkadotService;
246
+ }
247
+ declare function getAssetsBalances({
248
+ address,
249
+ chain,
250
+ assets,
251
+ polkadot,
252
+ }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
155
253
 
156
254
  interface SdkOptions extends Partial<Signers> {
157
255
  configService?: IConfigService;
@@ -184,6 +282,10 @@ declare function Sdk(options?: SdkOptions): {
184
282
  sourceKeyOrChain,
185
283
  }: SdkTransferParams): Promise<TransferData>;
186
284
  };
285
+ declare function getParachainBalances(
286
+ chain: AnyChain,
287
+ address: string,
288
+ ): Promise<AssetAmount[]>;
187
289
  interface SdkTransferParams extends Partial<Signers> {
188
290
  destinationAddress: string;
189
291
  destinationKeyOrChain: string | AnyChain;
@@ -193,23 +295,34 @@ interface SdkTransferParams extends Partial<Signers> {
193
295
  }
194
296
 
195
297
  export {
196
- ChainTransferData,
197
- DestinationChainTransferData,
198
- EvmSigner,
199
- GetBalancesParams,
200
- GetFeeParams,
201
- GetMaxParams,
202
- GetSourceDataParams,
298
+ type BaseParams,
299
+ type ChainTransferData,
300
+ type DestinationChainTransferData,
301
+ type EvmSigner,
302
+ type GetAssetsBalancesParams,
303
+ type GetBalancesParams,
304
+ type GetDecimalsParams,
305
+ type GetFeeBalanceParams,
306
+ type GetFeeParams,
307
+ type GetMaxParams,
308
+ type GetSourceDataParams,
203
309
  Sdk,
204
- SdkOptions,
205
- SdkTransferParams,
206
- Signers,
207
- SourceChainTransferData,
208
- TransferData,
310
+ type SdkOptions,
311
+ type SdkTransferParams,
312
+ type Signers,
313
+ type SourceChainTransferData,
314
+ type SovereignAccountBalance,
315
+ type TransferData,
316
+ getAssetsBalances,
317
+ getBalance,
209
318
  getContractFee,
319
+ getDecimals,
210
320
  getExtrinsicFee,
211
321
  getFee,
212
- getFeeBalances,
322
+ getFeeBalance,
213
323
  getMax,
324
+ getMin,
325
+ getParachainBalances,
214
326
  getSourceData,
327
+ validateSovereignAccountBalances,
215
328
  };
package/build/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{AssetAmount as F}from"@moonbeam-network/xcm-types";import{convertDecimals as ut}from"@moonbeam-network/xcm-utils";import N from"big.js";import{Contract as nt}from"ethers";import{createPublicClient as at,getContract as rt,http as st}from"viem";function P(a){return"provider"in a}function h(a){return"signer"in a}var M=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var E=class{address;#t;#e;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#t=t,this.#e=P(e)?new nt(this.address,M,e):rt({abi:M,address:this.address,publicClient:at({chain:e.chain,transport:st()}),walletClient:e})}async getBalance(){return h(this.#e)?(await this.#e.balanceOf(...this.#t.args)).toBigInt():this.#e.read.balanceOf(this.#t.args)}async getDecimals(){return h(this.#e)?await this.#e.decimals():this.#e.read.decimals()}};import{Contract as ot}from"ethers";import{createPublicClient as z,getContract as ct,http as K}from"viem";var k=[{inputs:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transfer",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Xtokens.Currency[]",name:"currencies",type:"tuple[]"},{internalType:"uint32",name:"feeItem",type:"uint32"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transferMultiCurrencies",outputs:[],stateMutability:"nonpayable",type:"function"}];var v=class{address="0x0000000000000000000000000000000000000804";#t;#e;#n;constructor(t,e){this.#t=t,this.#n=e,this.#e=P(e)?new ot(this.address,k,e):ct({abi:k,address:this.address,publicClient:z({chain:e.chain,transport:K()}),walletClient:e})}async transfer(){return h(this.#e)?this.#e[this.#t.func](...this.#t.args):this.#e.write[this.#t.func](this.#t.args)}async getEthersContractEstimatedGas(t){return(await t.estimateGas[this.#t.func](...this.#t.args)).toBigInt()}async getViemContractEstimatedGas(t){return t?t.estimateGas[this.#t.func](this.#t.args):0n}async getFee(t){if(t===0n)return 0n;try{let e=h(this.#e)?await this.getEthersContractEstimatedGas(this.#e):await this.getViemContractEstimatedGas(this.#e),n=await this.getGasPrice();return e*n}catch(e){throw console.error(e),new Error("Can't get a fee. Make sure that you have enough balance!")}}async getGasPrice(){if(P(this.#n)){let{gasPrice:e,maxPriorityFeePerGas:n}=await this.#n.getFeeData();return((e==null?void 0:e.toBigInt())||0n)+((n==null?void 0:n.toBigInt())||0n)}return z({chain:this.#n.chain,transport:K()}).getGasPrice()}};function y(a,t){if(a.module==="Erc20")return new E(a,t);if(a.module==="Xtokens")return new v(a,t);throw new Error(`Contract ${a.module} not found`)}import{CallType as V}from"@moonbeam-network/xcm-builder";import{toBigInt as mt}from"@moonbeam-network/xcm-utils";async function B({address:a,config:t,evmSigner:e,polkadot:n}){let r=t.balance.build({address:a,asset:n.chain.getBalanceAssetId(t.asset)});return r.type===V.Substrate?n.query(r):y(r,e).getBalance()}async function C({address:a,asset:t,config:e,evmSigner:n,polkadot:r}){let i=e.balance.build({address:a,asset:r.chain.getBalanceAssetId(t||e.asset)});return i.type===V.Substrate?r.getAssetDecimals(t||e.asset):y(i,n).getDecimals()}async function I(a,t){if(a.min)return t.query(a.min.build({asset:t.chain.getMinAssetId(a.asset)}));let e=t.chain.getAssetMin(a.asset);if(e){let n=await t.getAssetDecimals(a.asset);return mt(e,n)}return 0n}async function j({transferConfig:a,destinationAddress:t,destinationFee:e,evmSigner:n,polkadot:r,sourceAddress:i}){var O,_,X,Q;let{asset:o,destination:m,source:{chain:c,config:s}}=a,f=F.fromAsset(o,{amount:0n,decimals:await C({address:t,config:s,evmSigner:n,polkadot:r})}),d=(O=s.fee)!=null&&O.asset?F.fromAsset(s.fee.asset,{amount:0n,decimals:await C({address:t,asset:s.fee.asset,config:s,evmSigner:n,polkadot:r})}):f,l=(_=s.destinationFee)!=null&&_.asset?F.fromAsset(s.destinationFee.asset,{amount:0n,decimals:await C({address:t,asset:s.destinationFee.asset,config:s,evmSigner:n,polkadot:r})}):f,u=await B({address:i,config:s,evmSigner:n,polkadot:r}),p=await $({address:i,balance:u,feeConfig:s.fee,polkadot:r}),w=s.asset.isEqual(s.destinationFee.asset)?u:await $({address:i,balance:u,feeConfig:s.destinationFee,polkadot:r}),S=await I(s,r),x=(X=s.extrinsic)==null?void 0:X.build({address:t,amount:u,asset:c.getAssetId(o),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(o),source:c}),T=(Q=s.contract)==null?void 0:Q.build({address:t,amount:u,asset:c.getAssetId(o),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),A=await pt({balance:u,contract:T,decimals:d.decimals,evmSigner:n,extrinsic:x,polkadot:r,sourceAddress:i}),G=f.copyWith({amount:u}),{existentialDeposit:W}=r,R=d.copyWith({amount:A}),Y=d.copyWith({amount:p}),Z=l.copyWith({amount:w}),q=f.copyWith({amount:S}),tt=dt({balanceAmount:G,existentialDeposit:W,feeAmount:R,minAmount:q});return{balance:G,chain:c,destinationFeeBalance:Z,existentialDeposit:W,fee:R,feeBalance:Y,max:tt,min:q}}async function $({address:a,balance:t,feeConfig:e,polkadot:n}){return e?n.query(e.balance.build({address:a,asset:n.chain.getBalanceAssetId(e.asset)})):t}async function pt({balance:a,contract:t,decimals:e,evmSigner:n,extrinsic:r,polkadot:i,sourceAddress:o}){if(t){if(!n)throw new Error("EVM Signer must be provided");return ft(a,t,e,n)}if(r)return lt(a,r,i,o);throw new Error("Either contract or extrinsic must be provided")}async function ft(a,t,e,n){let i=await y(t,n).getFee(a);return ut(i,18,e)}async function lt(a,t,e,n){try{return await e.getFee(n,t)}catch(r){if(a)throw r;return 0n}}function dt({balanceAmount:a,existentialDeposit:t,feeAmount:e,minAmount:n}){let r=a.toBig().minus(n.toBig()).minus(a.isSame(t)?t.toBig():N(0)).minus(a.isSame(e)?e.toBig():N(0));return a.copyWith({amount:r.lt(0)?0n:BigInt(r.toFixed())})}import{ConfigBuilder as U}from"@moonbeam-network/xcm-config";import{convertDecimals as xt,toBigInt as J}from"@moonbeam-network/xcm-utils";import b from"big.js";import"@polkadot/api-augment";import{assetsMap as yt,darwiniaPangoro as gt,eq as ht,equilibriumAlphanet as Ct,paring as bt}from"@moonbeam-network/xcm-config";import{AssetAmount as At}from"@moonbeam-network/xcm-types";import{getPolkadotApi as Pt}from"@moonbeam-network/xcm-utils";var g=class{api;chain;constructor(t,e){this.api=t,this.chain=e}static async create(t){return new g(await Pt(t.ws),t)}static async createMulti(t){return Promise.all(t.map(g.create))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}get asset(){let t=this.api.registry.chainTokens.at(0),e=t==null?void 0:t.toString().toLowerCase();if(e==="token"&&this.chain.key===Ct.key)return ht;if(e==="oring"&&this.chain.key===gt.key)return bt;if(!e)throw new Error("No native symbol key found");let n=yt.get(e);if(!n)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return n}get existentialDeposit(){var r,i;let t=(r=this.api.consts.balances)==null?void 0:r.existentialDeposit,e=(i=this.api.consts.eqBalances)==null?void 0:i.existentialDeposit,n=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return At.fromAsset(this.asset,{amount:n,decimals:this.decimals})}async getAssetMeta(t){var i,o,m,c,s;let e=((i=this.api.query.assets)==null?void 0:i.metadata)||((o=this.api.query.assetRegistry)==null?void 0:o.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.currencyMetadatas)||((c=this.api.query.assetRegistry)==null?void 0:c.assetMetadatas)||((s=this.api.query.assetRegistry)==null?void 0:s.assetMetadataMap);if(!e)return;let n=await e(t),r="unwrapOrDefault"in n?n.unwrapOrDefault():n;return{decimals:r.decimals.toNumber(),symbol:r.symbol.toString()}}async getAssetDecimals(t){var e;return((e=await this.getAssetMeta(this.chain.getMetadataAssetId(t)))==null?void 0:e.decimals)||this.chain.getAssetDecimals(t)||this.decimals}async query(t){let e=await this.api.query[t.module][t.func](...t.args);return t.transform(e)}async getFee(t,e){let n=this.api.tx[e.module][e.func],r=e.getArgs(n);return(await n(...r).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,n){let r=this.api.tx[e.module][e.func],i=e.getArgs(r);return(await r(...i).signAndSend(this.#t(n)?t:n,{nonce:-1,signer:this.#t(n)?n:void 0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as L}from"@moonbeam-network/xcm-types";import{toBigInt as wt}from"@moonbeam-network/xcm-utils";async function H({transferConfig:a,destinationAddress:t,evmSigner:e,polkadot:n}){let{asset:r,destination:{chain:i,config:o}}=a,m=L.fromAsset(r,{amount:0n,decimals:await C({address:t,config:o,evmSigner:e,polkadot:n})}),c=await B({address:t,config:o,evmSigner:e,polkadot:n}),s=await I(o,n),f=m.copyWith({amount:c}),{existentialDeposit:d}=n,l=await St({config:a,polkadot:n}),u=m.copyWith({amount:s});return{balance:f,chain:i,existentialDeposit:d,fee:l,min:u}}async function St({config:a,polkadot:t}){let{amount:e,asset:n}=a.source.config.destinationFee,r=await t.getAssetDecimals(n),i=L.fromAsset(n,{amount:0n,decimals:r});if(Number.isFinite(e))return i.copyWith({amount:wt(e,r)});let o=e.build({api:t.api,asset:t.chain.getAssetId(n)});return i.copyWith({amount:await o.call()})}async function D({destinationAddress:a,evmSigner:t,polkadotSigner:e,sourceAddress:n,transferConfig:r}){if(!t)throw new Error("EVM Signer must be provided");let[i,o]=await g.createMulti([r.destination.chain,r.source.chain]),m=await H({destinationAddress:a,evmSigner:t,polkadot:i,transferConfig:r}),c=await Et(o,m.fee),s=await j({destinationAddress:a,destinationFee:c,evmSigner:t,polkadot:o,sourceAddress:n,transferConfig:r});return{destination:m,getEstimate(f){let l=b(J(f,s.balance.decimals).toString()).minus(s.balance.isSame(c)?c.toBig():b(0));return s.balance.copyWith({amount:l.lt(0)?0n:BigInt(l.toFixed())})},isSwapPossible:!0,max:s.max,min:Tt(m),source:s,async swap(){return D({destinationAddress:n,evmSigner:t,polkadotSigner:e,sourceAddress:a,transferConfig:{...r,destination:r.source,source:r.destination}})},async transfer(f){var x,T;let d=J(f,s.balance.decimals),{asset:l,source:{chain:u,config:p}}=r,w=(x=p.contract)==null?void 0:x.build({address:a,amount:d,asset:u.getAssetId(l),destination:m.chain,fee:c.amount,feeAsset:u.getAssetId(c)}),S=(T=p.extrinsic)==null?void 0:T.build({address:a,amount:d,asset:u.getAssetId(l),destination:m.chain,fee:c.amount,feeAsset:u.getAssetId(c),palletInstance:u.getAssetPalletInstance(l),source:u});if(w){if(!t)throw new Error("EVM Signer must be provided");return y(w,t).transfer().then(A=>typeof A=="object"?A.hash:A)}if(S){if(!e)throw new Error("Polkadot signer must be provided");return o.transfer(n,S,e)}throw new Error("Either contract or extrinsic must be provided")}}}function Tt({balance:a,existentialDeposit:t,fee:e,min:n}){let r=b(0).plus(a.isSame(e)?e.toBig():b(0)).plus(a.isSame(t)&&a.toBig().lt(t.toBig())?t.toBig():b(0)).plus(a.toBig().lt(n.toBig())?n.toBig():b(0));return a.copyWith({amount:BigInt(r.toFixed())})}async function Et(a,t){let e=await a.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:xt(t.amount,t.decimals,e),decimals:e})}function Ve(a){let t=a==null?void 0:a.configService;return{assets(e){let{assets:n,asset:r}=U(t).assets(e);return{assets:n,asset(i){let{sourceChains:o,source:m}=r(i);return{sourceChains:o,source(c){let{destinationChains:s,destination:f}=m(c);return{destinationChains:s,destination(d){return{async accounts(l,u,p){return D({...a,destinationAddress:u,evmSigner:(p==null?void 0:p.evmSigner)??(p==null?void 0:p.ethersSigner),sourceAddress:l,transferConfig:f(d).build(),polkadotSigner:p==null?void 0:p.polkadotSigner})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:n,ethersSigner:r,evmSigner:i,keyOrAsset:o,polkadotSigner:m,sourceAddress:c,sourceKeyOrChain:s}){return D({destinationAddress:e,evmSigner:i??r,polkadotSigner:m,sourceAddress:c,transferConfig:U(t).assets().asset(o).source(s).destination(n).build()})}}}export{Ve as Sdk,ft as getContractFee,lt as getExtrinsicFee,pt as getFee,$ as getFeeBalances,dt as getMax,j as getSourceData};
1
+ import{CallType as xt}from"@moonbeam-network/xcm-builder";import{AssetAmount as k}from"@moonbeam-network/xcm-types";import{convertDecimals as W,toBigInt as Dt,toDecimal as Et}from"@moonbeam-network/xcm-utils";import tt from"big.js";import{Contract as pt}from"ethers";import{createPublicClient as gt,getContract as yt,http as ht}from"viem";function B(n){return"provider"in n}function C(n){return!("abi"in n)}var A=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var T=class{address;#t;#e;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#t=t,this.#e=B(e)?new pt(this.address,A,e):yt({abi:A,address:this.address,client:{public:gt({chain:e.chain,transport:ht()}),wallet:e}})}async getBalance(){return C(this.#e)?(await this.#e.balanceOf(...this.#t.args)).toBigInt():this.#e.read.balanceOf(this.#t.args)}async getDecimals(){return C(this.#e)?await this.#e.decimals():this.#e.read.decimals()}};import{Contract as At}from"ethers";import{createPublicClient as J,getContract as bt,http as U}from"viem";var O=[{inputs:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transfer",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Xtokens.Currency[]",name:"currencies",type:"tuple[]"},{internalType:"uint32",name:"feeItem",type:"uint32"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transferMultiCurrencies",outputs:[],stateMutability:"nonpayable",type:"function"}];var x=class{defaultMoonChainAddress="0x0000000000000000000000000000000000000804";address;#t;#e;#a;constructor(t,e,a){this.#t=t,this.#a=e,this.address=a??this.defaultMoonChainAddress,this.#e=B(e)?new At(this.address,O,e):bt({abi:O,address:this.address,client:{public:J({chain:e.chain,transport:U()}),wallet:e}})}async transfer(){return C(this.#e)?this.#e[this.#t.func](...this.#t.args):this.#e.write[this.#t.func](this.#t.args)}async getEthersContractEstimatedGas(t){return t[this.#t.func].estimateGas(...this.#t.args)}async getViemContractEstimatedGas(t){return t?t.estimateGas[this.#t.func](this.#t.args):0n}async getFee(t){if(t===0n)return 0n;try{let e=C(this.#e)?await this.getEthersContractEstimatedGas(this.#e):await this.getViemContractEstimatedGas(this.#e),a=await this.getGasPrice();return e*a}catch(e){throw console.error(e),new Error("Can't get a fee. Make sure that you have enough balance!")}}async getGasPrice(){if(B(this.#a)){if(!this.#a.provider)return 0n;let{gasPrice:e,maxPriorityFeePerGas:a}=await this.#a.provider.getFeeData();return(e||0n)+(a||0n)}return J({chain:this.#a.chain,transport:U()}).getGasPrice()}};import{createPublicClient as Pt,http as St}from"viem";var F=class{address;#t;#e;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#t=t,this.#e=Pt({chain:e,transport:St()})}async getBalance(){return await this.#e.readContract({abi:A,address:this.#t.address,args:this.#t.args,functionName:"balanceOf"})}async getDecimals(){return await this.#e.readContract({abi:A,address:this.#t.address,functionName:"decimals"})}};function wt(n,t,e){var a;if(n.module==="Erc20")return new T(n,t);if(n.module==="Xtokens"){if(e&&"contracts"in e){let s=(a=e==null?void 0:e.contracts)==null?void 0:a.Xtokens;return new x(n,t,s)}return new x(n,t)}throw new Error(`Contract ${n.module} not found`)}function b(n,t){if(n.module==="Erc20")return new F(n,t.getViemChain());throw new Error(`Public Contract ${n.module} not found`)}function M(n,t,e){return t?wt(n,t,e):b(n,e)}import{CallType as Y}from"@moonbeam-network/xcm-builder";import{convertDecimals as vt,toBigInt as Bt}from"@moonbeam-network/xcm-utils";async function y({address:n,chain:t,balanceBuilder:e,asset:a,decimals:s,polkadot:o}){let i=e.build({address:n,asset:o.chain.getBalanceAssetId(a)});if(i.type===Y.Substrate){let r=await o.query(i);return t.usesChainDecimals?vt(r,o.decimals,s):r}return await b(i,t).getBalance()}async function h({address:n,asset:t,config:e,polkadot:a,chain:s,assetBuiltConfig:o}){let i=o||e.balance.build({address:n,asset:a.chain.getBalanceAssetId(t||e.asset)});return i.type===Y.Substrate?a.getAssetDecimals(t||e.asset):b(i,s).getDecimals()}async function G(n,t){if(n.min)return t.query(n.min.build({asset:t.chain.getMinAssetId(n.asset)}));let e=t.chain.getAssetMin(n.asset);if(e){let a=await t.getAssetDecimals(n.asset);return Bt(e,a)}return 0n}function Z({amount:n,source:t,destination:e}){if(!e.chain.checkSovereignAccountBalances||!e.sovereignAccountBalances)return;let{feeAssetBalance:a,transferAssetBalance:s}=e.sovereignAccountBalances;if(n>s)throw new Error(`${t.chain.name} Sovereign account in ${e.chain.name} does not have enough balance for this transaction`);if(a&&t.destinationFee.amount>a)throw new Error(`${t.chain.name} Sovereign account in ${e.chain.name} does not have enough balance to pay for fees for this transaction`)}async function at({transferConfig:n,destinationAddress:t,destinationFee:e,polkadot:a,sourceAddress:s,evmSigner:o}){var _,V,z,K,j,L,H;let{asset:i,destination:m,source:{chain:c,config:r}}=n,u=k.fromAsset(i,{amount:0n,decimals:await h({address:t,chain:c,config:r,polkadot:a})}),f=a.chain.getBalanceAssetId(((_=r==null?void 0:r.fee)==null?void 0:_.asset)||r.asset),p=(V=r.fee)==null?void 0:V.balance.build({address:t,asset:f}),d=(z=r.fee)!=null&&z.asset?await h({address:t,asset:r.fee.asset,assetBuiltConfig:p,chain:c,config:r,polkadot:a}):void 0,l=(K=r.fee)!=null&&K.asset?k.fromAsset(r.fee.asset,{amount:0n,decimals:d||0}):u,w=(j=r.destinationFee)!=null&&j.asset?k.fromAsset(r.destinationFee.asset,{amount:0n,decimals:await h({address:t,asset:r.destinationFee.asset,chain:c,config:r,polkadot:a})}):u,g=await y({address:s,asset:r.asset,balanceBuilder:r.balance,chain:c,decimals:u.decimals,polkadot:a}),D=await et({address:s,balance:g,chain:c,decimals:l.decimals,feeConfig:r.fee,polkadot:a}),E=r.asset.isEqual(r.destinationFee.asset)?g:await et({address:s,balance:g,chain:c,decimals:w.decimals,feeConfig:r.destinationFee,polkadot:a}),I=await G(r,a),v=(L=r.extrinsic)==null?void 0:L.build({address:t,amount:g,asset:c.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(i),source:c}),mt=(H=r.contract)==null?void 0:H.build({address:t,amount:g,asset:c.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),q=w.copyWith({amount:E}),ut=await It({balance:g,chain:c,contract:mt,decimals:l.decimals,destinationFeeBalanceAmount:q,destinationFeeConfig:r.destinationFee,evmSigner:o,extrinsic:v,feeConfig:r.fee,polkadot:a,sourceAddress:s}),$=u.copyWith({amount:g}),{existentialDeposit:N}=a,Q=l.copyWith({amount:ut}),lt=l.copyWith({amount:D}),X=u.copyWith({amount:I}),ft=Gt({balanceAmount:$,existentialDeposit:N,feeAmount:Q,minAmount:X});return{balance:$,chain:c,destinationFee:e,destinationFeeBalance:q,existentialDeposit:N,fee:Q,feeBalance:lt,max:ft,min:X}}async function et({address:n,balance:t,chain:e,decimals:a,feeConfig:s,polkadot:o}){if(!s)return t;let i=s.balance.build({address:n,asset:o.chain.getBalanceAssetId(s.asset)});if(i.type===xt.Evm){let c=b(i,e),r=await c.getDecimals(),u=await c.getBalance();return e.usesChainDecimals?W(u,o.decimals,r):u}let m=await o.query(i);return e.usesChainDecimals?W(m,o.decimals,a):m}async function It({balance:n,chain:t,contract:e,decimals:a,destinationFeeConfig:s,destinationFeeBalanceAmount:o,evmSigner:i,extrinsic:m,feeConfig:c,polkadot:r,sourceAddress:u}){if(e){if(!i)throw new Error("EVM Signer must be provided");if(s&&o&&typeof s.amount=="number"){let f=Number(Et(o.amount,o.decimals));if(f&&s.amount>f)throw new Error(`Can't get a fee, make sure you have ${s==null?void 0:s.amount} ${s==null?void 0:s.asset.originSymbol} in your source balance, needed for destination fees`)}return Tt({balance:n,chain:t,config:e,decimals:a,evmSigner:i})}if(m){let f=await Ft(n,m,r,u),p=Mt(a,c),d=f+p;return t.usesChainDecimals?W(d,r.decimals,a):d}throw new Error("Either contract or extrinsic must be provided")}async function Tt({balance:n,config:t,decimals:e,evmSigner:a,chain:s}){let i=await M(t,a,s).getFee(n);return W(i,18,e)}async function Ft(n,t,e,a){try{return await e.getFee(a,t)}catch(s){if(n)throw s;return 0n}}function Mt(n,t){return t!=null&&t.xcmDeliveryFeeAmount?Dt(t.xcmDeliveryFeeAmount,n):0n}function Gt({balanceAmount:n,existentialDeposit:t,feeAmount:e,minAmount:a}){let s=n.toBig().minus(a.toBig()).minus(n.isSame(t)?t.toBig():tt(0)).minus(n.isSame(e)?e.toBig():tt(0));return n.copyWith({amount:s.lt(0)?0n:BigInt(s.toFixed())})}async function nt({address:n,chain:t,assets:e,polkadot:a}){let s=e.reduce((i,m)=>i.some(c=>c.asset.isEqual(m.asset))?i:[m,...i],[]);return await Promise.all(s.map(async i=>{let m=await h({address:n,asset:i.asset,chain:t,config:i,polkadot:a}),c=await y({address:n,asset:i.asset,balanceBuilder:i.balance,chain:t,decimals:m,polkadot:a});return k.fromAsset(i.asset,{amount:c,decimals:m})}))}import{ConfigBuilder as ot,ConfigService as ct}from"@moonbeam-network/xcm-config";import{convertDecimals as Nt,toBigInt as it}from"@moonbeam-network/xcm-utils";import S from"big.js";import"@polkadot/api-augment";import{AssetAmount as kt}from"@moonbeam-network/xcm-types";import{getPolkadotApi as Wt}from"@moonbeam-network/xcm-utils";var P=class n{api;chain;configService;constructor(t,e,a){this.api=t,this.chain=e,this.configService=a}static async create(t,e){return new n(await Wt(t.ws),t,e)}static async createMulti(t,e){return Promise.all(t.map(a=>n.create(a,e)))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}get asset(){let t=this.api.registry.chainTokens.at(0),e=t==null?void 0:t.toString().toLowerCase();if(!e)throw new Error("No native symbol key found");let a=this.configService.getAsset(e);if(!a)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return a}get existentialDeposit(){var s,o;let t=(s=this.api.consts.balances)==null?void 0:s.existentialDeposit,e=(o=this.api.consts.eqBalances)==null?void 0:o.existentialDeposit,a=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return kt.fromAsset(this.asset,{amount:a,decimals:this.decimals})}async getAssetMeta(t){var i,m,c,r,u,f;let e=((i=this.api.query.assets)==null?void 0:i.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.assets)||((c=this.api.query.assetRegistry)==null?void 0:c.metadata)||((r=this.api.query.assetRegistry)==null?void 0:r.currencyMetadatas)||((u=this.api.query.assetRegistry)==null?void 0:u.assetMetadatas)||((f=this.api.query.assetRegistry)==null?void 0:f.assetMetadataMap);if(!e)return;let a=await e(t),s="unwrapOrDefault"in a?a.unwrapOrDefault():a;return{decimals:("unwrapOrDefault"in s.decimals?s.decimals.unwrapOrDefault():s.decimals).toNumber(),symbol:s.symbol.toString()}}async getAssetDecimalsFromQuery(t){var s;let e=(s=this.api.query.assetsRegistry)==null?void 0:s.assetDecimals;return e?(await e(t)).unwrapOrDefault().toNumber():void 0}async getAssetDecimals(t){var s;let e=this.chain.getMetadataAssetId(t),a=this.chain.getAssetDecimals(t)||await this.getAssetDecimalsFromQuery(e)||((s=await this.getAssetMeta(e))==null?void 0:s.decimals)||this.decimals;if(!a)throw new Error(`No decimals found for asset ${t.originSymbol}`);return a}async query(t){let e=await this.api.query[t.module][t.func](...t.args);return t.transform(e)}async getFee(t,e){let a=this.api.tx[e.module][e.func],s=e.getArgs(a);return(await a(...s).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,a){let s=this.api.tx[e.module][e.func],o=e.getArgs(s);return(await s(...o).signAndSend(this.#t(a)?t:a,{nonce:-1,signer:this.#t(a)?a:void 0,withSignedTransaction:!0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as st}from"@moonbeam-network/xcm-types";import{getSovereignAccountAddresses as Rt,toBigInt as Ot}from"@moonbeam-network/xcm-utils";async function rt({transferConfig:n,destinationAddress:t,polkadot:e}){let{asset:a,destination:{chain:s,config:o}}=n,i=st.fromAsset(a,{amount:0n,decimals:await h({address:t,chain:s,config:o,polkadot:e})}),m=await y({address:t,asset:o.asset,balanceBuilder:o.balance,chain:s,decimals:i.decimals,polkadot:e}),c=await G(o,e),r=i.copyWith({amount:m}),{existentialDeposit:u}=e,f=await qt({address:t,config:n,polkadot:e}),p=i.copyWith({amount:c});return{balance:r,chain:s,existentialDeposit:u,fee:f,min:p,sovereignAccountBalances:s.checkSovereignAccountBalances?await $t({decimals:i.decimals,polkadot:e,transferConfig:n}):void 0}}async function qt({address:n,config:t,polkadot:e}){let{amount:a,asset:s}=t.source.config.destinationFee,o=await e.getAssetDecimals(s),i=st.fromAsset(s,{amount:0n,decimals:o});if(Number.isFinite(a))return i.copyWith({amount:Ot(a,o)});let m=a.build({address:n,api:e.api,chain:e.chain,feeAsset:e.chain.getRegisteredAssetIdOrAddress(s),transferAsset:e.chain.getRegisteredAssetIdOrAddress(t.asset)});return i.copyWith({amount:await m.call()})}async function $t({transferConfig:n,decimals:t,polkadot:e}){var f;let{destination:{chain:a,config:s},source:{config:o}}=n,i=Rt(n.source.chain.parachainId),m=(f=o.destinationFee)==null?void 0:f.destinationBalance,c=a.isRelay?i.relay:i.generic,r=await y({address:c,asset:s.asset,balanceBuilder:s.balance,chain:a,decimals:t,polkadot:e});return{feeAssetBalance:m?await y({address:c,asset:o.destinationFee.asset,balanceBuilder:m,chain:a,decimals:t,polkadot:e}):void 0,transferAssetBalance:r}}async function R({configService:n,destinationAddress:t,evmSigner:e,polkadotSigner:a,sourceAddress:s,transferConfig:o}){let[i,m]=await P.createMulti([o.destination.chain,o.source.chain],n),c=await rt({destinationAddress:t,polkadot:i,transferConfig:o}),r=await Xt(m,c.fee),u=await at({destinationAddress:t,destinationFee:r,evmSigner:e,polkadot:m,sourceAddress:s,transferConfig:o});return{destination:c,getEstimate(f){let d=S(it(f,u.balance.decimals).toString()).minus(u.balance.isSame(r)?r.toBig():S(0));return u.balance.copyWith({amount:d.lt(0)?0n:BigInt(d.toFixed())})},isSwapPossible:!0,max:u.max,min:Qt(c),source:u,async swap(){return R({configService:n,destinationAddress:s,evmSigner:e,polkadotSigner:a,sourceAddress:t,transferConfig:{...o,destination:o.source,source:o.destination}})},async transfer(f){var E,I;let p=it(f,u.balance.decimals);Z({amount:p,destination:c,source:u});let{asset:d,source:{chain:l,config:w}}=o,g=(E=w.contract)==null?void 0:E.build({address:t,amount:p,asset:l.getAssetId(d),destination:c.chain,fee:r.amount,feeAsset:l.getAssetId(r)}),D=(I=w.extrinsic)==null?void 0:I.build({address:t,amount:p,asset:l.getAssetId(d),destination:c.chain,fee:r.amount,feeAsset:l.getAssetId(r),palletInstance:l.getAssetPalletInstance(d),source:l});if(g){if(!e)throw new Error("EVM Signer must be provided");return M(g,e,l).transfer().then(v=>typeof v=="object"?v.hash:v)}if(D){if(!a)throw new Error("Polkadot signer must be provided");return m.transfer(s,D,a)}throw new Error("Either contract or extrinsic must be provided")}}}function Qt({balance:n,existentialDeposit:t,fee:e,min:a}){let s=S(0).plus(n.isSame(e)?e.toBig():S(0)).plus(n.isSame(t)&&n.toBig().lt(t.toBig())?t.toBig():S(0)).plus(n.toBig().lt(a.toBig())?a.toBig():S(0));return n.copyWith({amount:BigInt(s.toFixed())})}async function Xt(n,t){let e=await n.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:Nt(t.amount,t.decimals,e),decimals:e})}function va(n){let t=(n==null?void 0:n.configService)??new ct;return{assets(e){let{assets:a,asset:s}=ot(t).assets(e);return{assets:a,asset(o){let{sourceChains:i,source:m}=s(o);return{sourceChains:i,source(c){let{destinationChains:r,destination:u}=m(c);return{destinationChains:r,destination(f){return{async accounts(p,d,l){return R({...n,configService:t,destinationAddress:d,evmSigner:(l==null?void 0:l.evmSigner)??(l==null?void 0:l.ethersSigner),sourceAddress:p,transferConfig:u(f).build(),polkadotSigner:l==null?void 0:l.polkadotSigner})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:a,ethersSigner:s,evmSigner:o,keyOrAsset:i,polkadotSigner:m,sourceAddress:c,sourceKeyOrChain:r}){return R({configService:t,destinationAddress:e,evmSigner:o??s,polkadotSigner:m,sourceAddress:c,transferConfig:ot(t).assets().asset(i).source(r).destination(a).build()})}}}async function Ba(n,t){let e=new ct,s=e.getChainConfig(n).getAssetsConfigs(),o=await P.create(n,e);return await nt({chain:n,assets:s,address:t,polkadot:o})}export{va as Sdk,nt as getAssetsBalances,y as getBalance,Tt as getContractFee,h as getDecimals,Ft as getExtrinsicFee,It as getFee,et as getFeeBalance,Gt as getMax,G as getMin,Ba as getParachainBalances,at as getSourceData,Z as validateSovereignAccountBalances};
2
2
  //# sourceMappingURL=index.mjs.map