@moonbeam-network/xcm-sdk 1.0.0-dev.7 → 1.0.0-dev.71

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