@moonbeam-network/xcm-sdk 1.0.0-dev.117 → 1.0.0-dev.118

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.
package/build/index.d.ts CHANGED
@@ -2,59 +2,36 @@ import {
2
2
  SubstrateQueryConfig,
3
3
  ExtrinsicConfig,
4
4
  ContractConfig,
5
+ BalanceConfigBuilder,
5
6
  } from '@moonbeam-network/xcm-builder';
6
7
  import {
7
- IConfigService,
8
- AssetConfig,
9
8
  TransferConfig,
10
9
  FeeAssetConfig,
11
- DestinationFeeConfig,
10
+ AssetConfig,
11
+ IConfigService,
12
12
  } from '@moonbeam-network/xcm-config';
13
13
  import {
14
14
  AnyParachain,
15
- Asset,
16
15
  AssetAmount,
17
- ChainAssetId,
18
16
  AnyChain,
19
- EvmParachain,
17
+ ChainAsset,
20
18
  Ecosystem,
19
+ Asset,
21
20
  } from '@moonbeam-network/xcm-types';
22
21
  import { ApiPromise } from '@polkadot/api';
23
22
  import { Signer } from '@polkadot/api/types';
24
23
  import { IKeyringPair } from '@polkadot/types/types';
25
- import { Signer as Signer$1 } from 'ethers';
26
24
  import { WalletClient } from 'viem';
27
25
 
28
26
  declare class PolkadotService {
29
27
  #private;
30
28
  readonly api: ApiPromise;
31
29
  readonly chain: AnyParachain;
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[]>;
30
+ constructor(api: ApiPromise, chain: AnyParachain);
31
+ static create(chain: AnyParachain): Promise<PolkadotService>;
32
+ static createMulti(chains: AnyParachain[]): Promise<PolkadotService[]>;
46
33
  get decimals(): number;
47
- get asset(): Asset;
48
34
  get existentialDeposit(): AssetAmount;
49
- getAssetMeta(asset: ChainAssetId): Promise<
50
- | {
51
- symbol: string;
52
- decimals: number;
53
- }
54
- | undefined
55
- >;
56
- getAssetDecimalsFromQuery(asset: ChainAssetId): Promise<number | undefined>;
57
- getAssetDecimals(asset: Asset): Promise<number>;
58
35
  query(config: SubstrateQueryConfig): Promise<bigint>;
59
36
  getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
60
37
  transfer(
@@ -64,14 +41,10 @@ declare class PolkadotService {
64
41
  ): Promise<string>;
65
42
  }
66
43
 
67
- type EvmSigner = Signer$1 | WalletClient;
44
+ type EvmSigner = WalletClient;
68
45
  interface Signers {
69
- /**
70
- * @deprecated ethersSigner - is deprecated and will be removed in v2, use evmSigner instead
71
- */
72
- ethersSigner?: Signer$1;
73
46
  evmSigner?: EvmSigner;
74
- polkadotSigner: Signer | IKeyringPair;
47
+ polkadotSigner?: Signer | IKeyringPair;
75
48
  }
76
49
  interface TransferData {
77
50
  destination: DestinationChainTransferData;
@@ -81,7 +54,10 @@ interface TransferData {
81
54
  min: AssetAmount;
82
55
  source: SourceChainTransferData;
83
56
  swap(): Promise<TransferData | undefined>;
84
- transfer(amount: bigint | number | string): Promise<string>;
57
+ transfer(
58
+ amount: bigint | number | string,
59
+ signers?: Signers,
60
+ ): Promise<string>;
85
61
  }
86
62
  interface SourceChainTransferData extends ChainTransferData {
87
63
  destinationFeeBalance: AssetAmount;
@@ -97,42 +73,10 @@ interface ChainTransferData {
97
73
  min: AssetAmount;
98
74
  }
99
75
 
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
-
131
76
  interface GetSourceDataParams {
132
77
  transferConfig: TransferConfig;
133
78
  destinationAddress: string;
134
79
  destinationFee: AssetAmount;
135
- evmSigner?: EvmSigner;
136
80
  polkadot: PolkadotService;
137
81
  sourceAddress: string;
138
82
  }
@@ -142,77 +86,46 @@ declare function getSourceData({
142
86
  destinationFee,
143
87
  polkadot,
144
88
  sourceAddress,
145
- evmSigner,
146
89
  }: GetSourceDataParams): Promise<SourceChainTransferData>;
147
- interface GetFeeBalanceParams
148
- extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
149
- balance: bigint;
150
- feeConfig: FeeAssetConfig | undefined;
151
- }
152
- declare function getFeeBalance({
153
- address,
154
- balance,
155
- chain,
156
- decimals,
157
- feeConfig,
158
- polkadot,
159
- }: GetFeeBalanceParams): Promise<bigint>;
160
90
  interface GetFeeParams {
161
- balance: bigint;
91
+ balance: AssetAmount;
92
+ feeBalance: AssetAmount;
162
93
  contract?: ContractConfig;
163
94
  chain: AnyChain;
164
- decimals: number;
165
- evmSigner?: EvmSigner;
95
+ destinationFee: AssetAmount;
166
96
  extrinsic?: ExtrinsicConfig;
167
97
  feeConfig?: FeeAssetConfig;
168
- destinationFeeConfig?: DestinationFeeConfig;
169
- destinationFeeBalanceAmount?: AssetAmount;
170
98
  polkadot: PolkadotService;
171
99
  sourceAddress: string;
172
100
  }
173
101
  declare function getFee({
174
102
  balance,
103
+ feeBalance,
175
104
  chain,
176
- contract,
177
- decimals,
178
- destinationFeeConfig,
179
- destinationFeeBalanceAmount,
180
- evmSigner,
105
+ contract: contractConfig,
106
+ destinationFee,
181
107
  extrinsic,
182
108
  feeConfig,
183
109
  polkadot,
184
110
  sourceAddress,
185
- }: GetFeeParams): 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>;
111
+ }: GetFeeParams): Promise<AssetAmount>;
199
112
  declare function getExtrinsicFee(
200
- balance: bigint,
113
+ balance: AssetAmount,
201
114
  extrinsic: ExtrinsicConfig,
202
115
  polkadot: PolkadotService,
203
116
  sourceAddress: string,
204
117
  ): Promise<bigint>;
205
118
  interface GetMaxParams {
206
- balanceAmount: AssetAmount;
119
+ balance: AssetAmount;
207
120
  existentialDeposit: AssetAmount;
208
- feeAmount: AssetAmount;
209
- minAmount: AssetAmount;
121
+ fee: AssetAmount;
122
+ min: AssetAmount;
210
123
  }
211
124
  declare function getMax({
212
- balanceAmount,
125
+ balance,
213
126
  existentialDeposit,
214
- feeAmount,
215
- minAmount,
127
+ fee,
128
+ min,
216
129
  }: GetMaxParams): AssetAmount;
217
130
  interface GetAssetsBalancesParams {
218
131
  address: string;
@@ -228,6 +141,25 @@ declare function getAssetsBalances({
228
141
  polkadot,
229
142
  }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
230
143
 
144
+ interface GetBalancesParams {
145
+ address: string;
146
+ asset: ChainAsset;
147
+ builder: BalanceConfigBuilder;
148
+ chain: AnyChain;
149
+ polkadot: PolkadotService;
150
+ }
151
+ declare function getBalance({
152
+ address,
153
+ asset,
154
+ builder,
155
+ chain,
156
+ polkadot,
157
+ }: GetBalancesParams): Promise<AssetAmount>;
158
+ declare function getMin(
159
+ config: AssetConfig,
160
+ polkadot: PolkadotService,
161
+ ): Promise<AssetAmount>;
162
+
231
163
  interface SdkOptions extends Partial<Signers> {
232
164
  configService?: IConfigService;
233
165
  }
@@ -242,7 +174,6 @@ declare function Sdk(options?: SdkOptions): {
242
174
  accounts(
243
175
  sourceAddress: string,
244
176
  destinationAddress: string,
245
- signers?: Partial<Signers>,
246
177
  ): Promise<TransferData>;
247
178
  };
248
179
  };
@@ -251,7 +182,6 @@ declare function Sdk(options?: SdkOptions): {
251
182
  getTransferData({
252
183
  destinationAddress,
253
184
  destinationKeyOrChain,
254
- ethersSigner,
255
185
  evmSigner,
256
186
  keyOrAsset,
257
187
  polkadotSigner,
@@ -277,8 +207,6 @@ export {
277
207
  type EvmSigner,
278
208
  type GetAssetsBalancesParams,
279
209
  type GetBalancesParams,
280
- type GetDecimalsParams,
281
- type GetFeeBalanceParams,
282
210
  type GetFeeParams,
283
211
  type GetMaxParams,
284
212
  type GetSourceDataParams,
@@ -290,11 +218,8 @@ export {
290
218
  type TransferData,
291
219
  getAssetsBalances,
292
220
  getBalance,
293
- getContractFee,
294
- getDecimals,
295
221
  getExtrinsicFee,
296
222
  getFee,
297
- getFeeBalance,
298
223
  getMax,
299
224
  getMin,
300
225
  getParachainBalances,
package/build/index.mjs CHANGED
@@ -1,2 +1,2 @@
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 O=[{inputs:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transfer",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Xtokens.Currency[]",name:"currencies",type:"tuple[]"},{internalType:"uint32",name:"feeItem",type:"uint32"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transferMultiCurrencies",outputs:[],stateMutability:"nonpayable",type:"function"}];var x=class{defaultMoonChainAddress="0x0000000000000000000000000000000000000804";address;#t;#e;#a;constructor(t,e,a){this.#t=t,this.#a=e,this.address=a??this.defaultMoonChainAddress,this.#e=v(e)?new Ct(this.address,O,e):At({abi:O,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)}),R=w.copyWith({amount:E}),mt=await Et({balance:g,chain:c,contract:ct,decimals:f.decimals,destinationFeeBalanceAmount:R,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:R,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 $t,toBigInt as rt}from"@moonbeam-network/xcm-utils";import P from"big.js";import"@polkadot/api-augment";import{eq as Gt,equilibrium as kt}from"@moonbeam-network/xcm-config";import{AssetAmount as Wt}from"@moonbeam-network/xcm-types";import{getPolkadotApi as qt}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 qt(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==="token"&&[kt.key].includes(this.chain.key))return Gt;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 Wt.fromAsset(this.asset,{amount:a,decimals:this.decimals})}async getAssetMeta(t){var o,m,c,r,u,l;let e=((o=this.api.query.assets)==null?void 0:o.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.assets)||((c=this.api.query.assetRegistry)==null?void 0:c.metadata)||((r=this.api.query.assetRegistry)==null?void 0:r.currencyMetadatas)||((u=this.api.query.assetRegistry)==null?void 0:u.assetMetadatas)||((l=this.api.query.assetRegistry)==null?void 0:l.assetMetadataMap);if(!e)return;let a=await e(t),s="unwrapOrDefault"in a?a.unwrapOrDefault():a;return{decimals:("unwrapOrDefault"in s.decimals?s.decimals.unwrapOrDefault():s.decimals).toNumber(),symbol:s.symbol.toString()}}async getAssetDecimalsFromQuery(t){var s;let e=(s=this.api.query.assetsRegistry)==null?void 0:s.assetDecimals;return e?(await e(t)).unwrapOrDefault().toNumber():void 0}async getAssetDecimals(t){var s;let e=this.chain.getMetadataAssetId(t),a=this.chain.getAssetDecimals(t)||await this.getAssetDecimalsFromQuery(e)||((s=await this.getAssetMeta(e))==null?void 0:s.decimals)||this.decimals;if(!a)throw new Error(`No decimals found for asset ${t.originSymbol}`);return a}async query(t){let e=await this.api.query[t.module][t.func](...t.args);return t.transform(e)}async getFee(t,e){let a=this.api.tx[e.module][e.func],s=e.getArgs(a);return(await a(...s).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,a){let s=this.api.tx[e.module][e.func],i=e.getArgs(s);return(await s(...i).signAndSend(this.#t(a)?t:a,{nonce:-1,signer:this.#t(a)?a:void 0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as nt}from"@moonbeam-network/xcm-types";import{toBigInt as Ot}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 Rt({address:t,config:n,polkadot:e}),d=o.copyWith({amount:c});return{balance:r,chain:s,existentialDeposit:u,fee:l,min:d}}async function Rt({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:Ot(e,s)});let o=e.build({api:t.api,asset:t.chain.getAssetId(a)});return i.copyWith({amount:await o.call()})}async function q({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 Qt(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:Nt(c),source:u,async swap(){return q({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 Nt({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 Qt(n,t){let e=await n.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:$t(t.amount,t.decimals,e),decimals:e})}function Sa(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 q({...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 q({configService:t,destinationAddress:e,evmSigner:i??s,polkadotSigner:m,sourceAddress:c,transferConfig:it(t).assets().asset(o).source(r).destination(a).build()})}}}async function va(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{Sa 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,va as getParachainBalances,et as getSourceData};
1
+ import{convertDecimals as z,toBigInt as J}from"@moonbeam-network/xcm-utils";import W from"big.js";import{createPublicClient as H,http as Q}from"viem";var F=[{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 B=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=H({chain:e.getViemChain(),transport:Q()})}async getBalance(){let t=this.#t.args[0];return this.#e.readContract({abi:F,address:this.address,args:[t],functionName:"balanceOf"})}};import{createPublicClient as V,http as $}from"viem";var E=[{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{defaultMoonChainAddress="0x0000000000000000000000000000000000000804";address;#t;#e;constructor(t,e,n){this.#t=t,this.address=n??this.defaultMoonChainAddress,this.#e=V({chain:e.getViemChain(),transport:$()})}async transfer(t){let{request:e}=await this.#e.simulateContract({abi:E,account:t.account,address:this.address,args:this.#t.args,functionName:this.#t.func});return await t.writeContract(e)}async getFee(t,e){if(t===0n)return 0n;let n=await this.#e.estimateContractGas({abi:E,account:e,address:this.address,args:this.#t.args,functionName:this.#t.func}),s=await this.getGasPrice();return n*s}async getGasPrice(){return this.#e.getGasPrice()}};function h(a,t){var e;if(!a.isEvmParachain())throw new Error("Chain is not an EVM parachain");if(t.module==="Erc20")return new B(t,a);if(t.module==="Xtokens"){let n=(e=a==null?void 0:a.contracts)==null?void 0:e.Xtokens;return new T(t,a,n)}throw new Error(`Contract ${t.module} not found`)}import{CallType as R}from"@moonbeam-network/xcm-builder";import{AssetAmount as G}from"@moonbeam-network/xcm-types";import{convertDecimals as j}from"@moonbeam-network/xcm-utils";async function y({address:a,asset:t,builder:e,chain:n,polkadot:s}){let r=e.build({address:a,asset:t.getBalanceAssetId(),constractAddress:t.address}),i=G.fromChainAsset(t,{amount:0n});if(r.type===R.Substrate){let p=await s.query(r),m=n.usesChainDecimals?j(p,s.decimals,t.decimals):p;return i.copyWith({amount:m})}let c=await h(n,r).getBalance();return i.copyWith({amount:c})}async function I(a,t){let e=G.fromChainAsset(t.chain.getChainAsset(a.asset),{amount:0n});if(a.min){let n=await t.query(a.min.build({asset:e.getMinAssetId()}));return e.copyWith({amount:n})}return e.min?e.copyWith({amount:e.min}):e.copyWith({amount:0n})}async function _({transferConfig:a,destinationAddress:t,destinationFee:e,polkadot:n,sourceAddress:s}){var x,S;let{destination:r,source:{chain:i,config:o}}=a,c=i.getChainAsset(a.asset),p=o.fee?i.getChainAsset(o.fee.asset):c,m=await y({address:s,asset:c,builder:o.balance,chain:i,polkadot:n}),u=o.fee?await y({address:s,asset:p,builder:o.fee.balance,chain:i,polkadot:n}):m,l=o.destinationFee.asset.isEqual(c)?m:o.destinationFee.asset.isEqual(p)?u:await y({address:s,asset:i.getChainAsset(o.destinationFee.asset),builder:o.destinationFee.balance,chain:i,polkadot:n}),d=await I(o,n),{existentialDeposit:f}=n,A=(x=o.extrinsic)==null?void 0:x.build({address:t,amount:m.amount,asset:c.getAssetId(),destination:r.chain,fee:e.amount,feeAsset:e.getAssetId(),palletInstance:c.getAssetPalletInstance(),source:i}),v=(S=o.contract)==null?void 0:S.build({address:t,amount:m.amount,asset:c.address||c.getAssetId(),destination:r.chain,fee:e.amount,feeAsset:e.address||e.getAssetId()}),g=await L({balance:m,chain:i,contract:v,destinationFee:e,extrinsic:A,feeBalance:u,feeConfig:o.fee,polkadot:n,sourceAddress:s}),P=Y({balance:m,existentialDeposit:f,fee:g,min:d});return{balance:m,chain:i,destinationFeeBalance:l,existentialDeposit:f,fee:g,feeBalance:u,max:P,min:d}}async function L({balance:a,feeBalance:t,chain:e,contract:n,destinationFee:s,extrinsic:r,feeConfig:i,polkadot:o,sourceAddress:c}){if(!n&&!r)throw new Error("Either contract or extrinsic must be provided");if(n){let d=h(e,n);try{let f=await d.getFee(a.amount,c);return t.copyWith({amount:f})}catch(f){throw new Error(`Can't get a fee, make sure you have ${s.toDecimal()} ${s.getSymbol()} in your source balance, needed for destination fees`,{cause:f})}}let p=await U(a,r,o,c),m=i!=null&&i.extra?J(i.extra,t.decimals):0n,u=p+m,l=e.usesChainDecimals?z(u,o.decimals,t.decimals):u;return t.copyWith({amount:l})}async function U(a,t,e,n){try{return await e.getFee(n,t)}catch(s){if(a)throw s;return 0n}}function Y({balance:a,existentialDeposit:t,fee:e,min:n}){let s=a.toBig().minus(n.toBig()).minus(a.isSame(t)?t.toBig():W(0)).minus(a.isSame(e)?e.toBig():W(0));return a.copyWith({amount:s.lt(0)?0n:BigInt(s.toFixed())})}async function q({address:a,chain:t,assets:e,polkadot:n}){let s=e.reduce((i,o)=>i.some(c=>c.asset.isEqual(o.asset))?i:[o,...i],[]);return await Promise.all(s.map(async i=>y({address:a,asset:t.getChainAsset(i.asset),builder:i.balance,chain:t,polkadot:n})))}import{ConfigBuilder as X,ConfigService as N}from"@moonbeam-network/xcm-config";import{toBigInt as K}from"@moonbeam-network/xcm-utils";import b from"big.js";import{AssetAmount as at}from"@moonbeam-network/xcm-types";import"@polkadot/api-augment";import{AssetAmount as Z}from"@moonbeam-network/xcm-types";import{getPolkadotApi as tt}from"@moonbeam-network/xcm-utils";var C=class a{api;chain;constructor(t,e){this.api=t,this.chain=e}static async create(t){return new a(await tt(t.ws),t)}static async createMulti(t){return Promise.all(t.map(e=>a.create(e)))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}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 Z.fromChainAsset(this.chain.nativeAsset,{amount: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],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 et}from"@moonbeam-network/xcm-types";async function O({transferConfig:a,destinationAddress:t,polkadot:e}){let{destination:{chain:n,config:s}}=a,r=n.getChainAsset(s.asset),i=await y({address:t,asset:r,builder:s.balance,chain:n,polkadot:e}),o=await I(s,e),c=await nt({address:t,polkadot:e,transferConfig:a});return{balance:i,chain:n,existentialDeposit:e.existentialDeposit,fee:c,min:o}}async function nt({transferConfig:a,polkadot:t}){let{amount:e}=a.source.config.destinationFee,n=et.fromChainAsset(a.destination.chain.getChainAsset(a.source.config.destinationFee.asset),{amount:0n});if(Number.isFinite(e))return n.copyWith({amount:e});let s=e.build({api:t.api,asset:n.getAssetId()});return n.copyWith({amount:await s.call()})}async function D({configService:a,destinationAddress:t,evmSigner:e,polkadotSigner:n,sourceAddress:s,transferConfig:r}){let[i,o]=await C.createMulti([r.destination.chain,r.source.chain]),c=await O({destinationAddress:t,polkadot:i,transferConfig:r}),p=r.source.chain.getChainAsset(c.fee),m=at.fromChainAsset(p,{amount:c.fee.convertDecimals(p.decimals).amount}),u=await _({destinationAddress:t,destinationFee:m,polkadot:o,sourceAddress:s,transferConfig:r});return{destination:c,getEstimate(l){let f=b(K(l,u.balance.decimals).toString()).minus(u.balance.isSame(m)?m.toBig():b(0));return u.balance.copyWith({amount:f.lt(0)?0n:BigInt(f.toFixed())})},isSwapPossible:!0,max:u.max,min:st(c),source:u,async swap(){return D({configService:a,destinationAddress:s,evmSigner:e,polkadotSigner:n,sourceAddress:t,transferConfig:{...r,destination:r.source,source:r.destination}})},async transfer(l,d){var k,M;let f=K(l,u.balance.decimals),{source:{chain:A,config:v}}=r,g=A.getChainAsset(r.asset),P=A.getChainAsset(m.key),x=(k=v.contract)==null?void 0:k.build({address:t,amount:f,asset:g.address||g.getAssetId(),destination:c.chain,fee:m.amount,feeAsset:P.address||P.getAssetId()}),S=(M=v.extrinsic)==null?void 0:M.build({address:t,amount:f,asset:g.getAssetId(),destination:c.chain,fee:m.amount,feeAsset:P.getAssetId(),palletInstance:g.getAssetPalletInstance(),source:A});if(x){let w=e||d.evmSigner;if(!w)throw new Error("EVM Signer must be provided");return h(A,x).transfer(w)}if(S){let w=n||d.polkadotSigner;if(!w)throw new Error("Polkadot signer must be provided");return o.transfer(s,S,w)}throw new Error("Either contract or extrinsic must be provided")}}}function st({balance:a,existentialDeposit:t,fee:e,min:n}){let s=b(0).plus(a.isSame(e)?e.toBig():b(0)).plus(a.isSame(t)&&a.toBig().lt(t.toBig())?t.toBig():b(0)).plus(a.toBig().lt(n.toBig())?n.toBig():b(0));return a.copyWith({amount:BigInt(s.toFixed())})}function he(a){let t=(a==null?void 0:a.configService)??new N;return{assets(e){let{assets:n,asset:s}=X(t).assets(e);return{assets:n,asset(r){let{sourceChains:i,source:o}=s(r);return{sourceChains:i,source(c){let{destinationChains:p,destination:m}=o(c);return{destinationChains:p,destination(u){return{async accounts(l,d){return D({...a,configService:t,destinationAddress:d,sourceAddress:l,transferConfig:m(u).build()})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:n,evmSigner:s,keyOrAsset:r,polkadotSigner:i,sourceAddress:o,sourceKeyOrChain:c}){return D({configService:t,destinationAddress:e,evmSigner:s,polkadotSigner:i,sourceAddress:o,transferConfig:X(t).assets().asset(r).source(c).destination(n).build()})}}}async function Ce(a,t){let s=new N().getChainConfig(a).getAssetsConfigs(),r=await C.create(a);return await q({chain:a,assets:s,address:t,polkadot:r})}export{he as Sdk,q as getAssetsBalances,y as getBalance,U as getExtrinsicFee,L as getFee,Y as getMax,I as getMin,Ce as getParachainBalances,_ as getSourceData};
2
2
  //# sourceMappingURL=index.mjs.map