@moonbeam-network/xcm-sdk 1.0.0-dev.13 → 1.0.0-dev.131

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,6 +66,9 @@ declare class PolkadotService {
51
66
 
52
67
  type EvmSigner = Signer$1 | WalletClient;
53
68
  interface Signers {
69
+ /**
70
+ * @deprecated ethersSigner - is deprecated and will be removed in v2, use evmSigner instead
71
+ */
54
72
  ethersSigner?: Signer$1;
55
73
  evmSigner?: EvmSigner;
56
74
  polkadotSigner: Signer | IKeyringPair;
@@ -79,11 +97,42 @@ interface ChainTransferData {
79
97
  min: AssetAmount;
80
98
  }
81
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
+
82
131
  interface GetSourceDataParams {
83
132
  transferConfig: TransferConfig;
84
133
  destinationAddress: string;
85
134
  destinationFee: AssetAmount;
86
- evmSigner: EvmSigner;
135
+ evmSigner?: EvmSigner;
87
136
  polkadot: PolkadotService;
88
137
  sourceAddress: string;
89
138
  }
@@ -91,46 +140,62 @@ declare function getSourceData({
91
140
  transferConfig,
92
141
  destinationAddress,
93
142
  destinationFee,
94
- evmSigner,
95
143
  polkadot,
96
144
  sourceAddress,
145
+ evmSigner,
97
146
  }: GetSourceDataParams): Promise<SourceChainTransferData>;
98
- interface GetBalancesParams {
99
- address: string;
147
+ interface GetFeeBalanceParams
148
+ extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
100
149
  balance: bigint;
101
150
  feeConfig: FeeAssetConfig | undefined;
102
- polkadot: PolkadotService;
103
151
  }
104
- declare function getFeeBalances({
152
+ declare function getFeeBalance({
105
153
  address,
106
154
  balance,
155
+ chain,
156
+ decimals,
107
157
  feeConfig,
108
158
  polkadot,
109
- }: GetBalancesParams): Promise<bigint>;
159
+ }: GetFeeBalanceParams): Promise<bigint>;
110
160
  interface GetFeeParams {
111
161
  balance: bigint;
112
162
  contract?: ContractConfig;
163
+ chain: AnyChain;
113
164
  decimals: number;
114
165
  evmSigner?: EvmSigner;
115
166
  extrinsic?: ExtrinsicConfig;
167
+ feeConfig?: FeeAssetConfig;
168
+ destinationFeeConfig?: DestinationFeeConfig;
169
+ destinationFeeBalanceAmount?: AssetAmount;
116
170
  polkadot: PolkadotService;
117
171
  sourceAddress: string;
118
172
  }
119
173
  declare function getFee({
120
174
  balance,
175
+ chain,
121
176
  contract,
122
177
  decimals,
178
+ destinationFeeConfig,
179
+ destinationFeeBalanceAmount,
123
180
  evmSigner,
124
181
  extrinsic,
182
+ feeConfig,
125
183
  polkadot,
126
184
  sourceAddress,
127
185
  }: GetFeeParams): Promise<bigint>;
128
- declare function getContractFee(
129
- balance: bigint,
130
- config: ContractConfig,
131
- decimals: number,
132
- evmSigner: EvmSigner,
133
- ): 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>;
134
199
  declare function getExtrinsicFee(
135
200
  balance: bigint,
136
201
  extrinsic: ExtrinsicConfig,
@@ -149,6 +214,19 @@ declare function getMax({
149
214
  feeAmount,
150
215
  minAmount,
151
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[]>;
152
230
 
153
231
  interface SdkOptions extends Partial<Signers> {
154
232
  configService?: IConfigService;
@@ -181,6 +259,10 @@ declare function Sdk(options?: SdkOptions): {
181
259
  sourceKeyOrChain,
182
260
  }: SdkTransferParams): Promise<TransferData>;
183
261
  };
262
+ declare function getParachainBalances(
263
+ chain: AnyChain,
264
+ address: string,
265
+ ): Promise<AssetAmount[]>;
184
266
  interface SdkTransferParams extends Partial<Signers> {
185
267
  destinationAddress: string;
186
268
  destinationKeyOrChain: string | AnyChain;
@@ -190,23 +272,31 @@ interface SdkTransferParams extends Partial<Signers> {
190
272
  }
191
273
 
192
274
  export {
193
- ChainTransferData,
194
- DestinationChainTransferData,
195
- EvmSigner,
196
- GetBalancesParams,
197
- GetFeeParams,
198
- GetMaxParams,
199
- 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,
200
285
  Sdk,
201
- SdkOptions,
202
- SdkTransferParams,
203
- Signers,
204
- SourceChainTransferData,
205
- TransferData,
286
+ type SdkOptions,
287
+ type SdkTransferParams,
288
+ type Signers,
289
+ type SourceChainTransferData,
290
+ type TransferData,
291
+ getAssetsBalances,
292
+ getBalance,
206
293
  getContractFee,
294
+ getDecimals,
207
295
  getExtrinsicFee,
208
296
  getFee,
209
- getFeeBalances,
297
+ getFeeBalance,
210
298
  getMax,
299
+ getMin,
300
+ getParachainBalances,
211
301
  getSourceData,
212
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 g(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 g(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 g(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 rt}from"ethers";import{createPublicClient as st,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 rt(this.address,D,e),signer:e}:{publicClient:st({chain:e.chain,transport:it()}),walletClient:e}}async transfer(){if(g(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=g(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(g(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 y(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 r=t.balance.build({address:a,asset:n.chain.getBalanceAssetId(t.asset)});return r.type===Q.Substrate?n.query(r):y(r,e).getBalance()}async function C({address:a,asset:t,config:e,evmSigner:n,polkadot:r}){let i=e.balance.build({address:a,asset:r.chain.getBalanceAssetId(t||e.asset)});return i.type===Q.Substrate?r.getAssetDecimals(t||e.asset):y(i,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:r,sourceAddress:i}){var R,N,V,_;let{asset:o,destination:m,source:{chain:c,config:s}}=a,f=k.fromAsset(o,{amount:0n,decimals:await C({address:t,config:s,evmSigner:n,polkadot:r})}),d=(R=s.fee)!=null&&R.asset?k.fromAsset(s.fee.asset,{amount:0n,decimals:await C({address:t,asset:s.fee.asset,config:s,evmSigner:n,polkadot:r})}):f,p=(N=s.destinationFee)!=null&&N.asset?k.fromAsset(s.destinationFee.asset,{amount:0n,decimals:await C({address:t,asset:s.destinationFee.asset,config:s,evmSigner:n,polkadot:r})}):f,u=await v({address:i,config:s,evmSigner:n,polkadot:r}),l=await K({address:i,balance:u,feeConfig:s.fee,polkadot:r}),w=s.asset.isEqual(s.destinationFee.asset)?u:await K({address:i,balance:u,feeConfig:s.destinationFee,polkadot:r}),P=await M(s,r),S=(V=s.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}),x=(_=s.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:x,decimals:d.decimals,evmSigner:n,extrinsic:S,polkadot:r,sourceAddress:i}),G=f.copyWith({amount:u}),{existentialDeposit:W}=r,q=d.copyWith({amount:A}),J=d.copyWith({amount:l}),U=p.copyWith({amount:w}),O=f.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:r,polkadot:i,sourceAddress:o}){if(t){if(!n)throw new Error("EVM Signer must be provided");return ut(a,t,e,n)}if(r)return lt(a,r,i,o);throw new Error("Either contract or extrinsic must be provided")}async function ut(a,t,e,n){let i=await y(t,n).getFee(a);return ct(i,18,e)}async function lt(a,t,e,n){try{return await e.getFee(n,t)}catch(r){if(a)throw r;return 0n}}function ft({balanceAmount:a,existentialDeposit:t,feeAmount:e,minAmount:n}){let r=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:r.lt(0)?0n:BigInt(r.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 h=class{api;chain;constructor(t,e){this.api=t,this.chain=e}static async create(t){return new h(await bt(t.ws),t)}static async createMulti(t){return Promise.all(t.map(h.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 r,i;let t=(r=this.api.consts.balances)==null?void 0:r.existentialDeposit,e=(i=this.api.consts.eqBalances)==null?void 0:i.existentialDeposit,n=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return Ct.fromAsset(this.asset,{amount:n,decimals:this.decimals})}async getAssetMeta(t){var i,o,m,c,s;let e=((i=this.api.query.assets)==null?void 0:i.metadata)||((o=this.api.query.assetRegistry)==null?void 0:o.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.currencyMetadatas)||((c=this.api.query.assetRegistry)==null?void 0:c.assetMetadatas)||((s=this.api.query.assetRegistry)==null?void 0:s.assetMetadataMap);if(!e)return;let n=await e(t),r="unwrapOrDefault"in n?n.unwrapOrDefault():n;return{decimals:r.decimals.toNumber(),symbol:r.symbol.toString()}}async getAssetDecimals(t){var e;return((e=await this.getAssetMeta(this.chain.getMetadataAssetId(t)))==null?void 0:e.decimals)||this.chain.getAssetDecimals(t)||this.decimals}async query(t){let e=await this.api.query[t.module][t.func](...t.args);return t.transform(e)}async getFee(t,e){let n=this.api.tx[e.module][e.func],r=e.getArgs(n);return(await n(...r).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,n){let r=this.api.tx[e.module][e.func],i=e.getArgs(r);return(await r(...i).signAndSend(this.#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:r,destination:{chain:i,config:o}}=a,m=X.fromAsset(r,{amount:0n,decimals:await C({address:t,config:o,evmSigner:e,polkadot:n})}),c=await v({address:t,config:o,evmSigner:e,polkadot:n}),s=await M(o,n),f=m.copyWith({amount:c}),{existentialDeposit:d}=n,p=await wt({config:a,polkadot:n}),u=m.copyWith({amount:s});return{balance:f,chain:i,existentialDeposit:d,fee:p,min:u}}async function wt({config:a,polkadot:t}){let{amount:e,asset:n}=a.source.config.destinationFee,r=await t.getAssetDecimals(n),i=X.fromAsset(n,{amount:0n,decimals:r});if(Number.isFinite(e))return i.copyWith({amount:At(e,r)});let o=e.build({api:t.api,asset:t.chain.getAssetId(n)});return i.copyWith({amount:await o.call()})}async function F({destinationAddress:a,evmSigner:t,polkadotSigner:e,sourceAddress:n,transferConfig:r}){if(!t)throw new Error("EVM Signer must be provided");let[i,o]=await h.createMulti([r.destination.chain,r.source.chain]),m=await j({destinationAddress:a,evmSigner:t,polkadot:i,transferConfig:r}),c=await xt(o,m.fee),s=await $({destinationAddress:a,destinationFee:c,evmSigner:t,polkadot:o,sourceAddress:n,transferConfig:r});return{destination:m,getEstimate(f){let p=b(L(f,s.balance.decimals).toString()).minus(s.balance.isSame(c)?c.toBig():b(0));return s.balance.copyWith({amount:p.lt(0)?0n:BigInt(p.toFixed())})},isSwapPossible:!0,max:s.max,min:St(m),source:s,async swap(){return F({destinationAddress:n,evmSigner:t,polkadotSigner:e,sourceAddress:a,transferConfig:{...r,destination:r.source,source:r.destination}})},async transfer(f){var S,x;let d=L(f,s.balance.decimals),{asset:p,source:{chain:u,config:l}}=r,w=(S=l.contract)==null?void 0:S.build({address:a,amount:d,asset:u.getAssetId(p),destination:m.chain,fee:c.amount,feeAsset:u.getAssetId(c)}),P=(x=l.extrinsic)==null?void 0:x.build({address:a,amount:d,asset:u.getAssetId(p),destination:m.chain,fee:c.amount,feeAsset:u.getAssetId(c),palletInstance:u.getAssetPalletInstance(p),source:u});if(w){if(!t)throw new Error("EVM Signer must be provided");return y(w,t).transfer().then(A=>typeof A=="object"?A.hash:A)}if(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 St({balance:a,existentialDeposit:t,fee:e,min:n}){let r=b(0).plus(a.isSame(e)?e.toBig():b(0)).plus(a.isSame(t)&&a.toBig().lt(t.toBig())?t.toBig():b(0)).plus(a.toBig().lt(n.toBig())?n.toBig():b(0));return a.copyWith({amount:BigInt(r.toFixed())})}async function xt(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:r}=H(t).assets(e);return{assets:n,asset(i){let{sourceChains:o,source:m}=r(i);return{sourceChains:o,source(c){let{destinationChains:s,destination:f}=m(c);return{destinationChains:s,destination(d){return{async accounts(p,u,l){return F({...a,destinationAddress:u,evmSigner:(l==null?void 0:l.evmSigner)??(l==null?void 0:l.ethersSigner),sourceAddress:p,transferConfig:f(d).build(),polkadotSigner:l==null?void 0:l.polkadotSigner})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:n,ethersSigner:r,evmSigner:i,keyOrAsset:o,polkadotSigner:m,sourceAddress:c,sourceKeyOrChain:s}){return F({destinationAddress:e,evmSigner:i??r,polkadotSigner:m,sourceAddress:c,transferConfig:H(t).assets().asset(o).source(s).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,withSignedTransaction:!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