@moonbeam-network/xcm-sdk 1.0.0-dev.12 → 1.0.0-dev.121

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,160 @@
1
+ import { SubstrateQueryConfig, ExtrinsicConfig, ContractConfig } from '@moonbeam-network/xcm-builder';
2
+ import { IConfigService, AssetConfig, TransferConfig, FeeAssetConfig, 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
+ destinationFeeBalance: AssetAmount;
53
+ feeBalance: AssetAmount;
54
+ max: AssetAmount;
55
+ }
56
+ interface DestinationChainTransferData extends ChainTransferData {
57
+ }
58
+ interface ChainTransferData {
59
+ balance: AssetAmount;
60
+ chain: AnyChain;
61
+ existentialDeposit: AssetAmount;
62
+ fee: AssetAmount;
63
+ min: AssetAmount;
64
+ }
65
+
66
+ interface GetBalancesParams {
67
+ address: string;
68
+ asset?: Asset;
69
+ chain: AnyChain;
70
+ config: AssetConfig;
71
+ decimals: number;
72
+ polkadot: PolkadotService;
73
+ }
74
+ type GetDecimalsParams = Omit<GetBalancesParams, 'decimals'> & {
75
+ assetBuiltConfig?: SubstrateQueryConfig | ContractConfig;
76
+ };
77
+ declare function getBalance({ address, chain, config, decimals, polkadot, }: GetBalancesParams): Promise<bigint>;
78
+ declare function getDecimals({ address, asset, config, polkadot, chain, assetBuiltConfig, }: GetDecimalsParams): Promise<number>;
79
+ declare function getMin(config: AssetConfig, polkadot: PolkadotService): Promise<bigint>;
80
+
81
+ interface GetSourceDataParams {
82
+ transferConfig: TransferConfig;
83
+ destinationAddress: string;
84
+ destinationFee: AssetAmount;
85
+ evmSigner?: EvmSigner;
86
+ polkadot: PolkadotService;
87
+ sourceAddress: string;
88
+ }
89
+ declare function getSourceData({ transferConfig, destinationAddress, destinationFee, polkadot, sourceAddress, evmSigner, }: GetSourceDataParams): Promise<SourceChainTransferData>;
90
+ interface GetFeeBalanceParams extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
91
+ balance: bigint;
92
+ feeConfig: FeeAssetConfig | undefined;
93
+ }
94
+ declare function getFeeBalance({ address, balance, chain, decimals, feeConfig, polkadot, }: GetFeeBalanceParams): Promise<bigint>;
95
+ interface GetFeeParams {
96
+ balance: bigint;
97
+ contract?: ContractConfig;
98
+ chain: AnyChain;
99
+ decimals: number;
100
+ evmSigner?: EvmSigner;
101
+ extrinsic?: ExtrinsicConfig;
102
+ feeConfig?: FeeAssetConfig;
103
+ destinationFeeConfig?: DestinationFeeConfig;
104
+ destinationFeeBalanceAmount?: AssetAmount;
105
+ polkadot: PolkadotService;
106
+ sourceAddress: string;
107
+ }
108
+ declare function getFee({ balance, chain, contract, decimals, destinationFeeConfig, destinationFeeBalanceAmount, evmSigner, extrinsic, feeConfig, polkadot, sourceAddress, }: GetFeeParams): Promise<bigint>;
109
+ declare function getContractFee({ balance, config, decimals, evmSigner, chain, }: {
110
+ balance: bigint;
111
+ config: ContractConfig;
112
+ decimals: number;
113
+ evmSigner: EvmSigner;
114
+ chain: EvmParachain;
115
+ }): Promise<bigint>;
116
+ declare function getExtrinsicFee(balance: bigint, extrinsic: ExtrinsicConfig, polkadot: PolkadotService, sourceAddress: string): Promise<bigint>;
117
+ interface GetMaxParams {
118
+ balanceAmount: AssetAmount;
119
+ existentialDeposit: AssetAmount;
120
+ feeAmount: AssetAmount;
121
+ minAmount: AssetAmount;
122
+ }
123
+ declare function getMax({ balanceAmount, existentialDeposit, feeAmount, minAmount, }: GetMaxParams): AssetAmount;
124
+ interface GetAssetsBalancesParams {
125
+ address: string;
126
+ chain: AnyChain;
127
+ assets: AssetConfig[];
128
+ evmSigner?: EvmSigner;
129
+ polkadot: PolkadotService;
130
+ }
131
+ declare function getAssetsBalances({ address, chain, assets, polkadot, }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
132
+
133
+ interface SdkOptions extends Partial<Signers> {
134
+ configService?: IConfigService;
135
+ }
136
+ declare function Sdk(options?: SdkOptions): {
137
+ assets(ecosystem?: Ecosystem): {
138
+ assets: Asset[];
139
+ asset(keyOrAsset: string | Asset): {
140
+ sourceChains: AnyChain[];
141
+ source(keyOrChain: string | AnyChain): {
142
+ destinationChains: AnyChain[];
143
+ destination(destKeyOrChain: string | AnyChain): {
144
+ accounts(sourceAddress: string, destinationAddress: string, signers?: Partial<Signers>): Promise<TransferData>;
145
+ };
146
+ };
147
+ };
148
+ };
149
+ getTransferData({ destinationAddress, destinationKeyOrChain, ethersSigner, evmSigner, keyOrAsset, polkadotSigner, sourceAddress, sourceKeyOrChain, }: SdkTransferParams): Promise<TransferData>;
150
+ };
151
+ declare function getParachainBalances(chain: AnyChain, address: string): Promise<AssetAmount[]>;
152
+ interface SdkTransferParams extends Partial<Signers> {
153
+ destinationAddress: string;
154
+ destinationKeyOrChain: string | AnyChain;
155
+ keyOrAsset: string | Asset;
156
+ sourceAddress: string;
157
+ sourceKeyOrChain: string | AnyChain;
158
+ }
159
+
160
+ export { 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 TransferData, getAssetsBalances, getBalance, getContractFee, getDecimals, getExtrinsicFee, getFee, getFeeBalance, getMax, getMin, getParachainBalances, getSourceData };
package/build/index.d.ts CHANGED
@@ -4,9 +4,11 @@ import {
4
4
  ContractConfig,
5
5
  } from '@moonbeam-network/xcm-builder';
6
6
  import {
7
+ IConfigService,
8
+ AssetConfig,
7
9
  TransferConfig,
8
10
  FeeAssetConfig,
9
- IConfigService,
11
+ DestinationFeeConfig,
10
12
  } from '@moonbeam-network/xcm-config';
11
13
  import {
12
14
  AnyParachain,
@@ -14,6 +16,7 @@ import {
14
16
  AssetAmount,
15
17
  ChainAssetId,
16
18
  AnyChain,
19
+ EvmParachain,
17
20
  Ecosystem,
18
21
  } from '@moonbeam-network/xcm-types';
19
22
  import { ApiPromise } from '@polkadot/api';
@@ -26,9 +29,20 @@ declare class PolkadotService {
26
29
  #private;
27
30
  readonly api: ApiPromise;
28
31
  readonly chain: AnyParachain;
29
- constructor(api: ApiPromise, chain: AnyParachain);
30
- static create(chain: AnyParachain): Promise<PolkadotService>;
31
- static createMulti(chains: AnyParachain[]): Promise<PolkadotService[]>;
32
+ readonly configService: IConfigService;
33
+ constructor(
34
+ api: ApiPromise,
35
+ chain: AnyParachain,
36
+ configService: IConfigService,
37
+ );
38
+ static create(
39
+ chain: AnyParachain,
40
+ configService: IConfigService,
41
+ ): Promise<PolkadotService>;
42
+ static createMulti(
43
+ chains: AnyParachain[],
44
+ configService: IConfigService,
45
+ ): Promise<PolkadotService[]>;
32
46
  get decimals(): number;
33
47
  get asset(): Asset;
34
48
  get existentialDeposit(): AssetAmount;
@@ -39,6 +53,7 @@ declare class PolkadotService {
39
53
  }
40
54
  | undefined
41
55
  >;
56
+ getAssetDecimalsFromQuery(asset: ChainAssetId): Promise<number | undefined>;
42
57
  getAssetDecimals(asset: Asset): Promise<number>;
43
58
  query(config: SubstrateQueryConfig): Promise<bigint>;
44
59
  getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
@@ -51,7 +66,11 @@ declare class PolkadotService {
51
66
 
52
67
  type EvmSigner = Signer$1 | WalletClient;
53
68
  interface Signers {
54
- evmSigner: EvmSigner;
69
+ /**
70
+ * @deprecated ethersSigner - is deprecated and will be removed in v2, use evmSigner instead
71
+ */
72
+ ethersSigner?: Signer$1;
73
+ evmSigner?: EvmSigner;
55
74
  polkadotSigner: Signer | IKeyringPair;
56
75
  }
57
76
  interface TransferData {
@@ -78,11 +97,42 @@ interface ChainTransferData {
78
97
  min: AssetAmount;
79
98
  }
80
99
 
100
+ interface GetBalancesParams {
101
+ address: string;
102
+ asset?: Asset;
103
+ chain: AnyChain;
104
+ config: AssetConfig;
105
+ decimals: number;
106
+ polkadot: PolkadotService;
107
+ }
108
+ type GetDecimalsParams = Omit<GetBalancesParams, 'decimals'> & {
109
+ assetBuiltConfig?: SubstrateQueryConfig | ContractConfig;
110
+ };
111
+ declare function getBalance({
112
+ address,
113
+ chain,
114
+ config,
115
+ decimals,
116
+ polkadot,
117
+ }: GetBalancesParams): Promise<bigint>;
118
+ declare function getDecimals({
119
+ address,
120
+ asset,
121
+ config,
122
+ polkadot,
123
+ chain,
124
+ assetBuiltConfig,
125
+ }: GetDecimalsParams): Promise<number>;
126
+ declare function getMin(
127
+ config: AssetConfig,
128
+ polkadot: PolkadotService,
129
+ ): Promise<bigint>;
130
+
81
131
  interface GetSourceDataParams {
82
132
  transferConfig: TransferConfig;
83
133
  destinationAddress: string;
84
134
  destinationFee: AssetAmount;
85
- evmSigner: EvmSigner;
135
+ evmSigner?: EvmSigner;
86
136
  polkadot: PolkadotService;
87
137
  sourceAddress: string;
88
138
  }
@@ -90,46 +140,62 @@ declare function getSourceData({
90
140
  transferConfig,
91
141
  destinationAddress,
92
142
  destinationFee,
93
- evmSigner,
94
143
  polkadot,
95
144
  sourceAddress,
145
+ evmSigner,
96
146
  }: GetSourceDataParams): Promise<SourceChainTransferData>;
97
- interface GetBalancesParams {
98
- address: string;
147
+ interface GetFeeBalanceParams
148
+ extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
99
149
  balance: bigint;
100
150
  feeConfig: FeeAssetConfig | undefined;
101
- polkadot: PolkadotService;
102
151
  }
103
- declare function getFeeBalances({
152
+ declare function getFeeBalance({
104
153
  address,
105
154
  balance,
155
+ chain,
156
+ decimals,
106
157
  feeConfig,
107
158
  polkadot,
108
- }: GetBalancesParams): Promise<bigint>;
159
+ }: GetFeeBalanceParams): Promise<bigint>;
109
160
  interface GetFeeParams {
110
161
  balance: bigint;
111
162
  contract?: ContractConfig;
163
+ chain: AnyChain;
112
164
  decimals: number;
113
165
  evmSigner?: EvmSigner;
114
166
  extrinsic?: ExtrinsicConfig;
167
+ feeConfig?: FeeAssetConfig;
168
+ destinationFeeConfig?: DestinationFeeConfig;
169
+ destinationFeeBalanceAmount?: AssetAmount;
115
170
  polkadot: PolkadotService;
116
171
  sourceAddress: string;
117
172
  }
118
173
  declare function getFee({
119
174
  balance,
175
+ chain,
120
176
  contract,
121
177
  decimals,
178
+ destinationFeeConfig,
179
+ destinationFeeBalanceAmount,
122
180
  evmSigner,
123
181
  extrinsic,
182
+ feeConfig,
124
183
  polkadot,
125
184
  sourceAddress,
126
185
  }: GetFeeParams): Promise<bigint>;
127
- declare function getContractFee(
128
- balance: bigint,
129
- config: ContractConfig,
130
- decimals: number,
131
- evmSigner: EvmSigner,
132
- ): Promise<bigint>;
186
+ declare function getContractFee({
187
+ balance,
188
+ config,
189
+ decimals,
190
+ evmSigner,
191
+ chain,
192
+ }: {
193
+ balance: bigint;
194
+ config: ContractConfig;
195
+ decimals: number;
196
+ evmSigner: EvmSigner;
197
+ chain: EvmParachain;
198
+ }): Promise<bigint>;
133
199
  declare function getExtrinsicFee(
134
200
  balance: bigint,
135
201
  extrinsic: ExtrinsicConfig,
@@ -148,6 +214,19 @@ declare function getMax({
148
214
  feeAmount,
149
215
  minAmount,
150
216
  }: GetMaxParams): AssetAmount;
217
+ interface GetAssetsBalancesParams {
218
+ address: string;
219
+ chain: AnyChain;
220
+ assets: AssetConfig[];
221
+ evmSigner?: EvmSigner;
222
+ polkadot: PolkadotService;
223
+ }
224
+ declare function getAssetsBalances({
225
+ address,
226
+ chain,
227
+ assets,
228
+ polkadot,
229
+ }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
151
230
 
152
231
  interface SdkOptions extends Partial<Signers> {
153
232
  configService?: IConfigService;
@@ -172,6 +251,7 @@ declare function Sdk(options?: SdkOptions): {
172
251
  getTransferData({
173
252
  destinationAddress,
174
253
  destinationKeyOrChain,
254
+ ethersSigner,
175
255
  evmSigner,
176
256
  keyOrAsset,
177
257
  polkadotSigner,
@@ -179,6 +259,10 @@ declare function Sdk(options?: SdkOptions): {
179
259
  sourceKeyOrChain,
180
260
  }: SdkTransferParams): Promise<TransferData>;
181
261
  };
262
+ declare function getParachainBalances(
263
+ chain: AnyChain,
264
+ address: string,
265
+ ): Promise<AssetAmount[]>;
182
266
  interface SdkTransferParams extends Partial<Signers> {
183
267
  destinationAddress: string;
184
268
  destinationKeyOrChain: string | AnyChain;
@@ -188,23 +272,31 @@ interface SdkTransferParams extends Partial<Signers> {
188
272
  }
189
273
 
190
274
  export {
191
- ChainTransferData,
192
- DestinationChainTransferData,
193
- EvmSigner,
194
- GetBalancesParams,
195
- GetFeeParams,
196
- GetMaxParams,
197
- GetSourceDataParams,
275
+ type ChainTransferData,
276
+ type DestinationChainTransferData,
277
+ type EvmSigner,
278
+ type GetAssetsBalancesParams,
279
+ type GetBalancesParams,
280
+ type GetDecimalsParams,
281
+ type GetFeeBalanceParams,
282
+ type GetFeeParams,
283
+ type GetMaxParams,
284
+ type GetSourceDataParams,
198
285
  Sdk,
199
- SdkOptions,
200
- SdkTransferParams,
201
- Signers,
202
- SourceChainTransferData,
203
- TransferData,
286
+ type SdkOptions,
287
+ type SdkTransferParams,
288
+ type Signers,
289
+ type SourceChainTransferData,
290
+ type TransferData,
291
+ getAssetsBalances,
292
+ getBalance,
204
293
  getContractFee,
294
+ getDecimals,
205
295
  getExtrinsicFee,
206
296
  getFee,
207
- getFeeBalances,
297
+ getFeeBalance,
208
298
  getMax,
299
+ getMin,
300
+ getParachainBalances,
209
301
  getSourceData,
210
302
  };
package/build/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{AssetAmount as k}from"@moonbeam-network/xcm-types";import{convertDecimals as ct}from"@moonbeam-network/xcm-utils";import z from"big.js";import{Contract as tt}from"ethers";import{createPublicClient as et,http as nt}from"viem";function B(a){return"provider"in a}function d(a){return"contract"in a}var T=[{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 I=class{address;#e;#t;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#e=t,this.#t=B(e)?{contract:new tt(this.address,T,e),signer:e}:{publicClient:et({chain:e.chain,transport:nt()}),walletClient:e}}async getBalance(){return d(this.#t)?(await this.#t.contract.balanceOf(...this.#e.args)).toBigInt():await this.#t.publicClient.readContract({abi:T,account:this.#t.walletClient.account,address:this.address,args:this.#e.args,functionName:"balanceOf"})}async getDecimals(){return d(this.#t)?await this.#t.contract.decimals():this.#t.publicClient.readContract({abi:T,account:this.#t.walletClient.account,address:this.address,args:[],functionName:"decimals"})}};import{Contract as st}from"ethers";import{createPublicClient as rt,http as it}from"viem";var D=[{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 E=class{address="0x0000000000000000000000000000000000000804";#e;#t;constructor(t,e){this.#e=t,this.#t=B(e)?{contract:new st(this.address,D,e),signer:e}:{publicClient:rt({chain:e.chain,transport:it()}),walletClient:e}}async transfer(){if(d(this.#t))return this.#t.contract[this.#e.func](...this.#e.args);let{request:t}=await this.#t.publicClient.simulateContract({abi:D,account:this.#t.walletClient.account,address:this.address,args:this.#e.args,functionName:this.#e.func});return this.#t.walletClient.writeContract(t)}async getEthersContractEstimatedGas(t){return(await t.estimateGas[this.#e.func](...this.#e.args)).toBigInt()}async getClientEstimatedGas(t){return t.walletClient.account?t.publicClient.estimateContractGas({abi:D,account:t.walletClient.account,address:this.address,args:this.#e.args,functionName:this.#e.func}):0n}async getFee(t){if(t===0n)return 0n;try{let e=d(this.#t)?await this.getEthersContractEstimatedGas(this.#t.contract):await this.getClientEstimatedGas(this.#t),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(d(this.#t)){let{gasPrice:t,maxPriorityFeePerGas:e}=await this.#t.signer.getFeeData();return((t==null?void 0:t.toBigInt())||0n)+((e==null?void 0:e.toBigInt())||0n)}return this.#t.publicClient.getGasPrice()}};function g(a,t){if(a.module==="Erc20")return new I(a,t);if(a.module==="Xtokens")return new E(a,t);throw new Error(`Contract ${a.module} not found`)}import{CallType as Q}from"@moonbeam-network/xcm-builder";import{toBigInt as ot}from"@moonbeam-network/xcm-utils";async function v({address:a,config:t,evmSigner:e,polkadot:n}){let s=t.balance.build({address:a,asset:n.chain.getBalanceAssetId(t.asset)});return s.type===Q.Substrate?n.query(s):g(s,e).getBalance()}async function C({address:a,asset:t,config:e,evmSigner:n,polkadot:s}){let r=e.balance.build({address:a,asset:s.chain.getBalanceAssetId(t||e.asset)});return r.type===Q.Substrate?s.getAssetDecimals(t||e.asset):g(r,n).getDecimals()}async function M(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 ot(e,n)}return 0n}async function $({transferConfig:a,destinationAddress:t,destinationFee:e,evmSigner:n,polkadot:s,sourceAddress:r}){var R,N,V,_;let{asset:o,destination:m,source:{chain:c,config:i}}=a,l=k.fromAsset(o,{amount:0n,decimals:await C({address:t,config:i,evmSigner:n,polkadot:s})}),p=(R=i.fee)!=null&&R.asset?k.fromAsset(i.fee.asset,{amount:0n,decimals:await C({address:t,asset:i.fee.asset,config:i,evmSigner:n,polkadot:s})}):l,f=(N=i.destinationFee)!=null&&N.asset?k.fromAsset(i.destinationFee.asset,{amount:0n,decimals:await C({address:t,asset:i.destinationFee.asset,config:i,evmSigner:n,polkadot:s})}):l,u=await v({address:r,config:i,evmSigner:n,polkadot:s}),h=await K({address:r,balance:u,feeConfig:i.fee,polkadot:s}),w=i.asset.isEqual(i.destinationFee.asset)?u:await K({address:r,balance:u,feeConfig:i.destinationFee,polkadot:s}),P=await M(i,s),x=(V=i.extrinsic)==null?void 0:V.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}),S=(_=i.contract)==null?void 0:_.build({address:t,amount:u,asset:c.getAssetId(o),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),A=await mt({balance:u,contract:S,decimals:p.decimals,evmSigner:n,extrinsic:x,polkadot:s,sourceAddress:r}),G=l.copyWith({amount:u}),{existentialDeposit:W}=s,q=p.copyWith({amount:A}),J=p.copyWith({amount:h}),U=f.copyWith({amount:w}),O=l.copyWith({amount:P}),Y=ft({balanceAmount:G,existentialDeposit:W,feeAmount:q,minAmount:O});return{balance:G,chain:c,destinationFeeBalance:U,existentialDeposit:W,fee:q,feeBalance:J,max:Y,min:O}}async function K({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 mt({balance:a,contract:t,decimals:e,evmSigner:n,extrinsic:s,polkadot:r,sourceAddress:o}){if(t){if(!n)throw new Error("EVM Signer must be provided");return ut(a,t,e,n)}if(s)return lt(a,s,r,o);throw new Error("Either contract or extrinsic must be provided")}async function ut(a,t,e,n){let r=await g(t,n).getFee(a);return ct(r,18,e)}async function lt(a,t,e,n){try{return await e.getFee(n,t)}catch(s){if(a)throw s;return 0n}}function ft({balanceAmount:a,existentialDeposit:t,feeAmount:e,minAmount:n}){let s=a.toBig().minus(n.toBig()).minus(a.isSame(t)?t.toBig():z(0)).minus(a.isSame(e)?e.toBig():z(0));return a.copyWith({amount:s.lt(0)?0n:BigInt(s.toFixed())})}import{ConfigBuilder as H}from"@moonbeam-network/xcm-config";import{convertDecimals as Pt,toBigInt as L}from"@moonbeam-network/xcm-utils";import b from"big.js";import"@polkadot/api-augment";import{assetsMap as pt,darwiniaPangoro as dt,eq as gt,equilibriumAlphanet as yt,paring as ht}from"@moonbeam-network/xcm-config";import{AssetAmount as Ct}from"@moonbeam-network/xcm-types";import{getPolkadotApi as bt}from"@moonbeam-network/xcm-utils";var y=class{api;chain;constructor(t,e){this.api=t,this.chain=e}static async create(t){return new y(await bt(t.ws),t)}static async createMulti(t){return Promise.all(t.map(y.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===yt.key)return gt;if(e==="oring"&&this.chain.key===dt.key)return ht;if(!e)throw new Error("No native symbol key found");let n=pt.get(e);if(!n)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return n}get existentialDeposit(){var s,r;let t=(s=this.api.consts.balances)==null?void 0:s.existentialDeposit,e=(r=this.api.consts.eqBalances)==null?void 0:r.existentialDeposit,n=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return Ct.fromAsset(this.asset,{amount:n,decimals:this.decimals})}async getAssetMeta(t){var r,o,m,c,i;let e=((r=this.api.query.assets)==null?void 0:r.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)||((i=this.api.query.assetRegistry)==null?void 0:i.assetMetadataMap);if(!e)return;let n=await e(t),s="unwrapOrDefault"in n?n.unwrapOrDefault():n;return{decimals:s.decimals.toNumber(),symbol:s.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],s=e.getArgs(n);return(await n(...s).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,n){let s=this.api.tx[e.module][e.func],r=e.getArgs(s);return(await s(...r).signAndSend(this.#e(n)?t:n,{nonce:-1,signer:this.#e(n)?n:void 0})).toString()}#e(t){return"signPayload"in t}};import{AssetAmount as X}from"@moonbeam-network/xcm-types";import{toBigInt as At}from"@moonbeam-network/xcm-utils";async function j({transferConfig:a,destinationAddress:t,evmSigner:e,polkadot:n}){let{asset:s,destination:{chain:r,config:o}}=a,m=X.fromAsset(s,{amount:0n,decimals:await C({address:t,config:o,evmSigner:e,polkadot:n})}),c=await v({address:t,config:o,evmSigner:e,polkadot:n}),i=await M(o,n),l=m.copyWith({amount:c}),{existentialDeposit:p}=n,f=await wt({config:a,polkadot:n}),u=m.copyWith({amount:i});return{balance:l,chain:r,existentialDeposit:p,fee:f,min:u}}async function wt({config:a,polkadot:t}){let{amount:e,asset:n}=a.source.config.destinationFee,s=await t.getAssetDecimals(n),r=X.fromAsset(n,{amount:0n,decimals:s});if(Number.isFinite(e))return r.copyWith({amount:At(e,s)});let o=e.build({api:t.api,asset:t.chain.getAssetId(n)});return r.copyWith({amount:await o.call()})}async function F({destinationAddress:a,evmSigner:t,polkadotSigner:e,sourceAddress:n,transferConfig:s}){if(!t)throw new Error("EVM Signer must be provided");let[r,o]=await y.createMulti([s.destination.chain,s.source.chain]),m=await j({destinationAddress:a,evmSigner:t,polkadot:r,transferConfig:s}),c=await St(o,m.fee),i=await $({destinationAddress:a,destinationFee:c,evmSigner:t,polkadot:o,sourceAddress:n,transferConfig:s});return{destination:m,getEstimate(l){let f=b(L(l,i.balance.decimals).toString()).minus(i.balance.isSame(c)?c.toBig():b(0));return i.balance.copyWith({amount:f.lt(0)?0n:BigInt(f.toFixed())})},isSwapPossible:!0,max:i.max,min:xt(m),source:i,async swap(){return F({destinationAddress:n,evmSigner:t,polkadotSigner:e,sourceAddress:a,transferConfig:{...s,destination:s.source,source:s.destination}})},async transfer(l){var x,S;let p=L(l,i.balance.decimals),{asset:f,source:{chain:u,config:h}}=s,w=(x=h.contract)==null?void 0:x.build({address:a,amount:p,asset:u.getAssetId(f),destination:m.chain,fee:c.amount,feeAsset:u.getAssetId(c)}),P=(S=h.extrinsic)==null?void 0:S.build({address:a,amount:p,asset:u.getAssetId(f),destination:m.chain,fee:c.amount,feeAsset:u.getAssetId(c),palletInstance:u.getAssetPalletInstance(f),source:u});if(w){if(!t)throw new Error("EVM Signer must be provided");return g(w,t).transfer().then(A=>typeof A=="object"?A.hash:A)}if(P){if(!e)throw new Error("Polkadot signer must be provided");return o.transfer(n,P,e)}throw new Error("Either contract or extrinsic must be provided")}}}function xt({balance:a,existentialDeposit:t,fee:e,min:n}){let s=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(s.toFixed())})}async function St(a,t){let e=await a.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:Pt(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:s}=H(t).assets(e);return{assets:n,asset(r){let{sourceChains:o,source:m}=s(r);return{sourceChains:o,source(c){let{destinationChains:i,destination:l}=m(c);return{destinationChains:i,destination(p){return{async accounts(f,u,h){return F({...a,...h,destinationAddress:u,sourceAddress:f,transferConfig:l(p).build()})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:n,evmSigner:s,keyOrAsset:r,polkadotSigner:o,sourceAddress:m,sourceKeyOrChain:c}){return F({destinationAddress:e,evmSigner:s,polkadotSigner:o,sourceAddress:m,transferConfig:H(t).assets().asset(r).source(c).destination(n).build()})}}}export{Ve as Sdk,ut as getContractFee,lt as getExtrinsicFee,mt as getFee,K as getFeeBalances,ft as getMax,$ 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 Bt}from"@moonbeam-network/xcm-utils";import Z from"big.js";import{Contract as pt}from"ethers";import{createPublicClient as dt,getContract as gt,http as yt}from"viem";function v(n){return"provider"in n}function h(n){return"signer"in n}var C=[{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=v(e)?new pt(this.address,C,e):gt({abi:C,address:this.address,client:{public:dt({chain:e.chain,transport:yt()}),wallet: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 Ct}from"ethers";import{createPublicClient as J,getContract as At,http as U}from"viem";var R=[{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=v(e)?new Ct(this.address,R,e):At({abi:R,address:this.address,client:{public:J({chain:e.chain,transport:U()}),wallet: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 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=h(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(v(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 bt,http as Pt}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=bt({chain:e,transport:Pt()})}async getBalance(){return await this.#e.readContract({abi:C,address:this.#t.address,args:this.#t.args,functionName:"balanceOf"})}async getDecimals(){return await this.#e.readContract({abi:C,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 A(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):A(n,e)}import{CallType as Y}from"@moonbeam-network/xcm-builder";import{convertDecimals as St,toBigInt as vt}from"@moonbeam-network/xcm-utils";async function D({address:n,chain:t,config:e,decimals:a,polkadot:s}){let i=e.balance.build({address:n,asset:s.chain.getBalanceAssetId(e.asset)});if(i.type===Y.Substrate){let c=await s.query(i);return t.usesChainDecimals?St(c,s.decimals,a):c}return await A(i,t).getBalance()}async function y({address:n,asset:t,config:e,polkadot:a,chain:s,assetBuiltConfig:i}){let o=i||e.balance.build({address:n,asset:a.chain.getBalanceAssetId(t||e.asset)});return o.type===Y.Substrate?a.getAssetDecimals(t||e.asset):A(o,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 vt(e,a)}return 0n}async function et({transferConfig:n,destinationAddress:t,destinationFee:e,polkadot:a,sourceAddress:s,evmSigner:i}){var _,z,K,V,j,L,H;let{asset:o,destination:m,source:{chain:c,config:r}}=n,u=k.fromAsset(o,{amount:0n,decimals:await y({address:t,chain:c,config:r,polkadot:a})}),l=a.chain.getBalanceAssetId(((_=r==null?void 0:r.fee)==null?void 0:_.asset)||r.asset),d=(z=r.fee)==null?void 0:z.balance.build({address:t,asset:l}),p=(K=r.fee)!=null&&K.asset?await y({address:t,asset:r.fee.asset,assetBuiltConfig:d,chain:c,config:r,polkadot:a}):void 0,f=(V=r.fee)!=null&&V.asset?k.fromAsset(r.fee.asset,{amount:0n,decimals:p||0}):u,w=(j=r.destinationFee)!=null&&j.asset?k.fromAsset(r.destinationFee.asset,{amount:0n,decimals:await y({address:t,asset:r.destinationFee.asset,chain:c,config:r,polkadot:a})}):u,g=await D({address:s,chain:c,config:r,decimals:u.decimals,polkadot:a}),B=await tt({address:s,balance:g,chain:c,decimals:f.decimals,feeConfig:r.fee,polkadot:a}),E=r.asset.isEqual(r.destinationFee.asset)?g:await tt({address:s,balance:g,chain:c,decimals:w.decimals,feeConfig:r.destinationFee,polkadot:a}),I=await G(r,a),S=(L=r.extrinsic)==null?void 0:L.build({address:t,amount:g,asset:c.getAssetId(o),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(o),source:c}),ct=(H=r.contract)==null?void 0:H.build({address:t,amount:g,asset:c.getAssetId(o),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),q=w.copyWith({amount:E}),mt=await Et({balance:g,chain:c,contract:ct,decimals:f.decimals,destinationFeeBalanceAmount:q,destinationFeeConfig:r.destinationFee,evmSigner:i,extrinsic:S,feeConfig:r.fee,polkadot:a,sourceAddress:s}),$=u.copyWith({amount:g}),{existentialDeposit:N}=a,Q=f.copyWith({amount:mt}),ut=f.copyWith({amount:B}),X=u.copyWith({amount:I}),ft=Mt({balanceAmount:$,existentialDeposit:N,feeAmount:Q,minAmount:X});return{balance:$,chain:c,destinationFeeBalance:q,existentialDeposit:N,fee:Q,feeBalance:ut,max:ft,min:X}}async function tt({address:n,balance:t,chain:e,decimals:a,feeConfig:s,polkadot:i}){if(!s)return t;let o=s.balance.build({address:n,asset:i.chain.getBalanceAssetId(s.asset)});if(o.type===xt.Evm){let c=A(o,e),r=await c.getDecimals(),u=await c.getBalance();return e.usesChainDecimals?W(u,i.decimals,r):u}let m=await i.query(o);return e.usesChainDecimals?W(m,i.decimals,a):m}async function Et({balance:n,chain:t,contract:e,decimals:a,destinationFeeConfig:s,destinationFeeBalanceAmount:i,evmSigner:o,extrinsic:m,feeConfig:c,polkadot:r,sourceAddress:u}){if(e){if(!o)throw new Error("EVM Signer must be provided");if(s&&i&&typeof s.amount=="number"){let l=Number(Bt(i.amount,i.decimals));if(l&&s.amount>l)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 It({balance:n,chain:t,config:e,decimals:a,evmSigner:o})}if(m){let l=await Tt(n,m,r,u),d=Ft(a,c),p=l+d;return t.usesChainDecimals?W(p,r.decimals,a):p}throw new Error("Either contract or extrinsic must be provided")}async function It({balance:n,config:t,decimals:e,evmSigner:a,chain:s}){let o=await M(t,a,s).getFee(n);return W(o,18,e)}async function Tt(n,t,e,a){try{return await e.getFee(a,t)}catch(s){if(n)throw s;return 0n}}function Ft(n,t){return t!=null&&t.xcmDeliveryFeeAmount?Dt(t.xcmDeliveryFeeAmount,n):0n}function Mt({balanceAmount:n,existentialDeposit:t,feeAmount:e,minAmount:a}){let s=n.toBig().minus(a.toBig()).minus(n.isSame(t)?t.toBig():Z(0)).minus(n.isSame(e)?e.toBig():Z(0));return n.copyWith({amount:s.lt(0)?0n:BigInt(s.toFixed())})}async function at({address:n,chain:t,assets:e,polkadot:a}){let s=e.reduce((o,m)=>o.some(c=>c.asset.isEqual(m.asset))?o:[m,...o],[]);return await Promise.all(s.map(async o=>{let m=await y({address:n,asset:o.asset,chain:t,config:o,polkadot:a}),c=await D({address:n,chain:t,config:o,decimals:m,polkadot:a});return k.fromAsset(o.asset,{amount:c,decimals:m})}))}import{ConfigBuilder as it,ConfigService as ot}from"@moonbeam-network/xcm-config";import{convertDecimals as Rt,toBigInt as rt}from"@moonbeam-network/xcm-utils";import P from"big.js";import"@polkadot/api-augment";import{AssetAmount as Gt}from"@moonbeam-network/xcm-types";import{getPolkadotApi as kt}from"@moonbeam-network/xcm-utils";var b=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 kt(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,i;let t=(s=this.api.consts.balances)==null?void 0:s.existentialDeposit,e=(i=this.api.consts.eqBalances)==null?void 0:i.existentialDeposit,a=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return Gt.fromAsset(this.asset,{amount:a,decimals:this.decimals})}async getAssetMeta(t){var o,m,c,r,u,l;let e=((o=this.api.query.assets)==null?void 0:o.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)||((l=this.api.query.assetRegistry)==null?void 0:l.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],i=e.getArgs(s);return(await s(...i).signAndSend(this.#t(a)?t:a,{nonce:-1,signer:this.#t(a)?a:void 0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as nt}from"@moonbeam-network/xcm-types";import{toBigInt as Wt}from"@moonbeam-network/xcm-utils";async function st({transferConfig:n,destinationAddress:t,polkadot:e}){let{asset:a,destination:{chain:s,config:i}}=n,o=nt.fromAsset(a,{amount:0n,decimals:await y({address:t,chain:s,config:i,polkadot:e})}),m=await D({address:t,chain:s,config:i,decimals:o.decimals,polkadot:e}),c=await G(i,e),r=o.copyWith({amount:m}),{existentialDeposit:u}=e,l=await Ot({address:t,config:n,polkadot:e}),d=o.copyWith({amount:c});return{balance:r,chain:s,existentialDeposit:u,fee:l,min:d}}async function Ot({config:n,polkadot:t}){let{amount:e,asset:a}=n.source.config.destinationFee,s=await t.getAssetDecimals(a),i=nt.fromAsset(a,{amount:0n,decimals:s});if(Number.isFinite(e))return i.copyWith({amount:Wt(e,s)});let o=e.build({api:t.api,asset:t.chain.getAssetId(a)});return i.copyWith({amount:await o.call()})}async function O({configService:n,destinationAddress:t,evmSigner:e,polkadotSigner:a,sourceAddress:s,transferConfig:i}){let[o,m]=await b.createMulti([i.destination.chain,i.source.chain],n),c=await st({destinationAddress:t,polkadot:o,transferConfig:i}),r=await $t(m,c.fee),u=await et({destinationAddress:t,destinationFee:r,evmSigner:e,polkadot:m,sourceAddress:s,transferConfig:i});return{destination:c,getEstimate(l){let p=P(rt(l,u.balance.decimals).toString()).minus(u.balance.isSame(r)?r.toBig():P(0));return u.balance.copyWith({amount:p.lt(0)?0n:BigInt(p.toFixed())})},isSwapPossible:!0,max:u.max,min:qt(c),source:u,async swap(){return O({configService:n,destinationAddress:s,evmSigner:e,polkadotSigner:a,sourceAddress:t,transferConfig:{...i,destination:i.source,source:i.destination}})},async transfer(l){var E,I;let d=rt(l,u.balance.decimals),{asset:p,source:{chain:f,config:w}}=i,g=(E=w.contract)==null?void 0:E.build({address:t,amount:d,asset:f.getAssetId(p),destination:c.chain,fee:r.amount,feeAsset:f.getAssetId(r)}),B=(I=w.extrinsic)==null?void 0:I.build({address:t,amount:d,asset:f.getAssetId(p),destination:c.chain,fee:r.amount,feeAsset:f.getAssetId(r),palletInstance:f.getAssetPalletInstance(p),source:f});if(g){if(!e)throw new Error("EVM Signer must be provided");return M(g,e,f).transfer().then(S=>typeof S=="object"?S.hash:S)}if(B){if(!a)throw new Error("Polkadot signer must be provided");return m.transfer(s,B,a)}throw new Error("Either contract or extrinsic must be provided")}}}function qt({balance:n,existentialDeposit:t,fee:e,min:a}){let s=P(0).plus(n.isSame(e)?e.toBig():P(0)).plus(n.isSame(t)&&n.toBig().lt(t.toBig())?t.toBig():P(0)).plus(n.toBig().lt(a.toBig())?a.toBig():P(0));return n.copyWith({amount:BigInt(s.toFixed())})}async function $t(n,t){let e=await n.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:Rt(t.amount,t.decimals,e),decimals:e})}function Aa(n){let t=(n==null?void 0:n.configService)??new ot;return{assets(e){let{assets:a,asset:s}=it(t).assets(e);return{assets:a,asset(i){let{sourceChains:o,source:m}=s(i);return{sourceChains:o,source(c){let{destinationChains:r,destination:u}=m(c);return{destinationChains:r,destination(l){return{async accounts(d,p,f){return O({...n,configService:t,destinationAddress:p,evmSigner:(f==null?void 0:f.evmSigner)??(f==null?void 0:f.ethersSigner),sourceAddress:d,transferConfig:u(l).build(),polkadotSigner:f==null?void 0:f.polkadotSigner})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:a,ethersSigner:s,evmSigner:i,keyOrAsset:o,polkadotSigner:m,sourceAddress:c,sourceKeyOrChain:r}){return O({configService:t,destinationAddress:e,evmSigner:i??s,polkadotSigner:m,sourceAddress:c,transferConfig:it(t).assets().asset(o).source(r).destination(a).build()})}}}async function ba(n,t){let e=new ot,s=e.getChainConfig(n).getAssetsConfigs(),i=await b.create(n,e);return await at({chain:n,assets:s,address:t,polkadot:i})}export{Aa as Sdk,at as getAssetsBalances,D as getBalance,It as getContractFee,y as getDecimals,Tt as getExtrinsicFee,Et as getFee,tt as getFeeBalance,Mt as getMax,G as getMin,ba as getParachainBalances,et as getSourceData};
2
2
  //# sourceMappingURL=index.mjs.map