@moonbeam-network/xcm-sdk 1.0.0-dev.2 → 1.0.0-dev.200

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.mjs CHANGED
@@ -1,2 +1,771 @@
1
- import{AssetAmount as q}from"@moonbeam-network/xcm-types";import{convertDecimals as U}from"@moonbeam-network/xcm-utils";import W from"big.js";import{Contract as $}from"ethers";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 x=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 $(this.address,F,e)}async getBalance(){return(await this.#e.balanceOf(...this.#t.args)).toBigInt()}};import{Contract as L}from"ethers";var G=[{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 S=class{address="0x0000000000000000000000000000000000000804";#t;#e;#n;constructor(t,e){this.#t=t,this.#n=e,this.#e=new L(this.address,G,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){return console.error(e),0n}}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 g(a,t){if(a.module==="Erc20")return new x(a,t);if(a.module==="Xtokens")return new S(a,t);throw new Error(`Contract ${a.module} not found`)}import{CallType as H}from"@moonbeam-network/xcm-builder";import{toBigInt as J}from"@moonbeam-network/xcm-utils";async function B({address:a,config:t,ethersSigner:e,polkadot:n}){let r=t.balance.build({address:a,asset:n.chain.getBalanceAssetId(t.asset)});return r.type===H.Substrate?n.query(r):g(r,e).getBalance()}async function T(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 J(e,n)}return 0n}async function O({transferConfig:a,destinationAddress:t,destinationFee:e,ethersSigner:n,polkadot:r,sourceAddress:s}){var M,E,k;let{asset:i,destination:m,source:{chain:o,config:c}}=a,f=q.fromAsset(i,{amount:0n,decimals:await r.getAssetDecimals(i)}),l=(M=c.fee)!=null&&M.asset?q.fromAsset(c.fee.asset,{amount:0n,decimals:await r.getAssetDecimals(c.fee.asset)}):f,u=await B({address:s,config:c,ethersSigner:n,polkadot:r}),p=await V({address:s,balance:u,config:c,polkadot:r}),A=await T(c,r),b=(E=c.extrinsic)==null?void 0:E.build({address:t,amount:u,asset:o.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:o.getAssetId(e),palletInstance:o.getAssetPalletInstance(i),source:o}),C=(k=c.contract)==null?void 0:k.build({address:t,amount:u,asset:o.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:o.getAssetId(e)}),P=await Y({balance:u,contract:C,decimals:l.decimals,ethersSigner:n,extrinsic:b,polkadot:r,sourceAddress:s}),h=f.copyWith({amount:u}),{existentialDeposit:w}=r,D=l.copyWith({amount:P}),z=l.copyWith({amount:p}),v=f.copyWith({amount:A}),N=et({balanceAmount:h,existentialDeposit:w,feeAmount:D,minAmount:v});return{balance:h,chain:o,existentialDeposit:w,fee:D,feeBalance:z,max:N,min:v}}async function V({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 Y({balance:a,contract:t,decimals:e,ethersSigner:n,extrinsic:r,polkadot:s,sourceAddress:i}){if(t){if(!n)throw new Error("Ethers signer must be provided");return Z(a,t,e,n)}if(r)return tt(a,r,s,i);throw new Error("Either contract or extrinsic must be provided")}async function Z(a,t,e,n){let s=await g(t,n).getFee(a);return U(s,18,e)}async function tt(a,t,e,n){try{return await e.getFee(n,t)}catch(r){if(a)throw r;return 0n}}function et({balanceAmount:a,existentialDeposit:t,feeAmount:e,minAmount:n}){let r=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:r.lt(0)?0n:BigInt(r.toFixed())})}import{ConfigBuilder as K}from"@moonbeam-network/xcm-config";import{convertDecimals as pt,toBigInt as R}from"@moonbeam-network/xcm-utils";import y from"big.js";import"@polkadot/api-augment";import{assetsMap as nt,darwiniaPangoro as at,eq as rt,equilibriumAlphanet as st,paring as it}from"@moonbeam-network/xcm-config";import{AssetAmount as ot}from"@moonbeam-network/xcm-types";import{getPolkadotApi as ct}from"@moonbeam-network/xcm-utils";var d=class{api;chain;constructor(t,e){this.api=t,this.chain=e}static async create(t){return new d(await ct(t.ws),t)}static async createMulti(t){return Promise.all(t.map(d.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===st.key)return rt;if(e==="oring"&&this.chain.key===at.key)return it;if(!e)throw new Error("No native symbol key found");let n=nt.get(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 ot.fromAsset(this.asset,{amount:n,decimals:this.decimals})}async getAssetMeta(t){var s,i,m,o,c;let e=((s=this.api.query.assets)==null?void 0:s.metadata)||((i=this.api.query.assetRegistry)==null?void 0:i.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.currencyMetadatas)||((o=this.api.query.assetRegistry)==null?void 0:o.assetMetadatas)||((c=this.api.query.assetRegistry)==null?void 0:c.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],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 _}from"@moonbeam-network/xcm-types";import{toBigInt as mt}from"@moonbeam-network/xcm-utils";async function Q({transferConfig:a,destinationAddress:t,ethersSigner:e,polkadot:n}){let{asset:r,destination:{chain:s,config:i}}=a,m=_.fromAsset(r,{amount:0n,decimals:await n.getAssetDecimals(r)}),o=await B({address:t,config:i,ethersSigner:e,polkadot:n}),c=await T(i,n),f=m.copyWith({amount:o}),{existentialDeposit:l}=n,u=await ut({config:a,polkadot:n}),p=m.copyWith({amount:c});return{balance:f,chain:s,existentialDeposit:l,fee:u,min:p}}async function ut({config:a,polkadot:t}){let{amount:e,asset:n}=a.source.config.destinationFee,r=await t.getAssetDecimals(n),s=_.fromAsset(n,{amount:0n,decimals:r});if(Number.isFinite(e))return s.copyWith({amount:mt(e,r)});let i=e.build({api:t.api,asset:t.chain.getAssetId(n)});return s.copyWith({amount:await i.call()})}async function I({destinationAddress:a,ethersSigner:t,polkadotSigner:e,sourceAddress:n,transferConfig:r}){if(!t)throw new Error("Ethers signer must be provided");let[s,i]=await d.createMulti([r.destination.chain,r.source.chain]),m=await Q({destinationAddress:a,ethersSigner:t,polkadot:s,transferConfig:r}),o=await lt(i,m.fee),c=await O({destinationAddress:a,destinationFee:o,ethersSigner:t,polkadot:i,sourceAddress:n,transferConfig:r});return{destination:m,getEstimate(f){let u=y(R(f,c.balance.decimals).toString()).minus(c.balance.isSame(o)?o.toBig():y(0));return c.balance.copyWith({amount:u.lt(0)?0n:BigInt(u.toFixed())})},isSwapPossible:!0,max:c.max,min:ft(m),source:c,async swap(){return I({destinationAddress:n,ethersSigner:t,polkadotSigner:e,sourceAddress:a,transferConfig:{...r,destination:r.source,source:r.destination}})},async transfer(f){var P,h;let l=R(f,c.balance.decimals),{asset:u,source:{chain:p,config:A}}=r,b=(P=A.contract)==null?void 0:P.build({address:a,amount:l,asset:p.getAssetId(u),destination:m.chain,fee:o.amount,feeAsset:p.getAssetId(o)}),C=(h=A.extrinsic)==null?void 0:h.build({address:a,amount:l,asset:p.getAssetId(u),destination:m.chain,fee:o.amount,feeAsset:p.getAssetId(o),palletInstance:p.getAssetPalletInstance(u),source:p});if(b){if(!t)throw new Error("Ethers signer must be provided");return g(b,t).transfer().then(w=>w.hash)}if(C){if(!e)throw new Error("Polkadot signer must be provided");return i.transfer(n,C,e)}throw new Error("Either contract or extrinsic must be provided")}}}function ft({balance:a,existentialDeposit:t,fee:e,min:n}){let r=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(r.toFixed())})}async function lt(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 Ce(a){return{assets(t){let{assets:e,asset:n}=K().assets(t);return{assets:e,asset(r){let{sourceChains:s,source:i}=n(r);return{sourceChains:s,source(m){let{destinationChains:o,destination:c}=i(m);return{destinationChains:o,destination(f){return{async accounts(l,u,p){return I({...a,...p,destinationAddress:u,sourceAddress:l,transferConfig:c(f).build()})}}}}}}}}},async getTransferData({destinationAddress:t,destinationKeyOrChain:e,ethersSigner:n,keyOrAsset:r,polkadotSigner:s,sourceAddress:i,sourceKeyOrChain:m}){return I({destinationAddress:t,ethersSigner:n,polkadotSigner:s,sourceAddress:i,transferConfig:K().assets().asset(r).source(m).destination(e).build()})}}}export{Ce as Sdk,Z as getContractFee,tt as getExtrinsicFee,Y as getFee,V as getFeeBalances,et as getMax,O as getSourceData};
1
+ // src/services/polkadot/PolkadotService.ts
2
+ import "@polkadot/api-augment";
3
+ import { AssetAmount } from "@moonbeam-network/xcm-types";
4
+ import { getPolkadotApi } from "@moonbeam-network/xcm-utils";
5
+ var PolkadotService = class _PolkadotService {
6
+ api;
7
+ chain;
8
+ constructor(api, chain) {
9
+ this.api = api;
10
+ this.chain = chain;
11
+ }
12
+ static async create(chain) {
13
+ return new _PolkadotService(await getPolkadotApi(chain.ws), chain);
14
+ }
15
+ static async createMulti(chains) {
16
+ return Promise.all(
17
+ chains.map((chain) => _PolkadotService.create(chain))
18
+ );
19
+ }
20
+ get decimals() {
21
+ return this.api.registry.chainDecimals.at(0) || 12;
22
+ }
23
+ get existentialDeposit() {
24
+ const existentialDeposit = this.api.consts.balances?.existentialDeposit;
25
+ const eqExistentialDeposit = this.api.consts.eqBalances?.existentialDeposit;
26
+ const amount = existentialDeposit?.toBigInt() || eqExistentialDeposit?.toBigInt() || 0n;
27
+ return AssetAmount.fromChainAsset(this.chain.nativeAsset, { amount });
28
+ }
29
+ async query(config) {
30
+ const response = await this.api.query[config.module][config.func](
31
+ ...config.args
32
+ );
33
+ return config.transform(response);
34
+ }
35
+ getExtrinsic(config) {
36
+ const fn = this.api.tx[config.module][config.func];
37
+ const args = config.getArgs(fn);
38
+ return fn(...args);
39
+ }
40
+ getExtrinsicCallHash(config) {
41
+ return this.getExtrinsic(config).method.toHex();
42
+ }
43
+ async getPaymentInfo(account, config) {
44
+ const extrinsic = this.getExtrinsic(config);
45
+ return extrinsic.paymentInfo(account, { nonce: -1 });
46
+ }
47
+ async getFee(account, config) {
48
+ const info = await this.getPaymentInfo(account, config);
49
+ return info.partialFee.toBigInt();
50
+ }
51
+ async transfer(account, config, signer, statusCallback) {
52
+ const extrinsic = this.getExtrinsic(config);
53
+ const isSigner = this.#isSigner(signer);
54
+ const signOptions = {
55
+ nonce: -1,
56
+ signer: isSigner ? signer : void 0,
57
+ withSignedTransaction: true
58
+ };
59
+ const hash = await new Promise((resolve, reject) => {
60
+ extrinsic.signAndSend(isSigner ? account : signer, signOptions, (result) => {
61
+ if (result.isError || result.dispatchError) {
62
+ reject(
63
+ new Error(
64
+ result.dispatchError?.toString() || "Transaction failed"
65
+ )
66
+ );
67
+ }
68
+ if (result.txHash) {
69
+ resolve(result.txHash.toString());
70
+ }
71
+ statusCallback?.(result);
72
+ }).catch(reject);
73
+ });
74
+ return hash;
75
+ }
76
+ #isSigner(signer) {
77
+ return "signPayload" in signer;
78
+ }
79
+ };
80
+
81
+ // src/getTransferData/getTransferData.utils.ts
82
+ import {
83
+ ContractConfig,
84
+ EvmQueryConfig,
85
+ SubstrateQueryConfig
86
+ } from "@moonbeam-network/xcm-builder";
87
+ import {
88
+ AssetAmount as AssetAmount2,
89
+ EvmChain,
90
+ EvmParachain,
91
+ Parachain
92
+ } from "@moonbeam-network/xcm-types";
93
+ import { convertDecimals, toBigInt } from "@moonbeam-network/xcm-utils";
94
+ import Big from "big.js";
95
+
96
+ // src/services/evm/EvmService.ts
97
+ import {
98
+ http,
99
+ createPublicClient
100
+ } from "viem";
101
+ var EvmService = class _EvmService {
102
+ chain;
103
+ client;
104
+ static create(chain) {
105
+ return new _EvmService(chain);
106
+ }
107
+ constructor(chain) {
108
+ this.chain = chain;
109
+ this.client = createPublicClient({
110
+ chain: chain.getViemChain(),
111
+ transport: http()
112
+ });
113
+ }
114
+ async query(query) {
115
+ return this.client[query.func](...query.args);
116
+ }
117
+ async getFee(address, contract) {
118
+ const gas = await this.client.estimateContractGas({
119
+ abi: contract.abi,
120
+ account: address,
121
+ address: contract.address,
122
+ args: contract.args,
123
+ functionName: contract.func
124
+ });
125
+ const gasPrice = await this.client.getGasPrice();
126
+ return gas * gasPrice;
127
+ }
128
+ async getBalance(address, contract) {
129
+ const balance = await this.client.readContract({
130
+ abi: contract.abi,
131
+ address: contract.address,
132
+ args: [address],
133
+ functionName: "balanceOf"
134
+ });
135
+ if (typeof balance !== "bigint") {
136
+ throw new Error(
137
+ `Could not get balance on ${this.chain.name} for ${address} from contract ${contract.address}`
138
+ );
139
+ }
140
+ return balance;
141
+ }
142
+ async transfer(signer, contract) {
143
+ const { request } = await this.client.simulateContract({
144
+ abi: contract.abi,
145
+ account: signer.account,
146
+ address: contract.address,
147
+ args: contract.args,
148
+ functionName: contract.func
149
+ });
150
+ const hash = await signer.writeContract(request);
151
+ return hash;
152
+ }
153
+ };
154
+
155
+ // src/getTransferData/getTransferData.utils.ts
156
+ async function getBalance({
157
+ address,
158
+ asset,
159
+ builder,
160
+ chain
161
+ }) {
162
+ const config = builder.build({
163
+ address,
164
+ asset
165
+ });
166
+ const amount = AssetAmount2.fromChainAsset(asset, { amount: 0n });
167
+ if (SubstrateQueryConfig.is(config) && EvmParachain.isAnyParachain(chain)) {
168
+ const polkadot = await PolkadotService.create(chain);
169
+ const balance = await polkadot.query(config);
170
+ const converted = chain.usesChainDecimals ? convertDecimals(balance, polkadot.decimals, asset.decimals) : balance;
171
+ return amount.copyWith({ amount: converted });
172
+ }
173
+ if (EvmChain.is(chain) || EvmParachain.is(chain)) {
174
+ const evm = EvmService.create(chain);
175
+ if (ContractConfig.is(config)) {
176
+ const balance = await evm.getBalance(address, config);
177
+ return amount.copyWith({ amount: balance });
178
+ }
179
+ if (EvmQueryConfig.is(config)) {
180
+ const balance = await evm.query(config);
181
+ return amount.copyWith({ amount: balance });
182
+ }
183
+ }
184
+ throw new Error(
185
+ `Can't get balance for ${address} on chain ${chain.name} and asset ${asset.symbol}`
186
+ );
187
+ }
188
+ async function getAssetMin({
189
+ asset,
190
+ builder,
191
+ chain
192
+ }) {
193
+ const zero = AssetAmount2.fromChainAsset(chain.getChainAsset(asset), {
194
+ amount: 0n
195
+ });
196
+ if (builder && EvmParachain.isAnyParachain(chain)) {
197
+ const polkadot = await PolkadotService.create(chain);
198
+ const min = await polkadot.query(
199
+ builder.build({ asset: zero.getMinAssetId(), address: zero.address })
200
+ );
201
+ return zero.copyWith({ amount: min });
202
+ }
203
+ if (zero.min) {
204
+ return zero.copyWith({ amount: zero.min });
205
+ }
206
+ return zero;
207
+ }
208
+ function getMin({
209
+ balance,
210
+ existentialDeposit,
211
+ fee,
212
+ min
213
+ }) {
214
+ const result = Big(0).plus(balance.isSame(fee) ? fee.toBig() : Big(0)).plus(
215
+ existentialDeposit && balance.isSame(existentialDeposit) && balance.toBig().lt(existentialDeposit.toBig()) ? existentialDeposit.toBig() : Big(0)
216
+ ).plus(balance.toBig().lt(min.toBig()) ? min.toBig() : Big(0));
217
+ return balance.copyWith({
218
+ amount: BigInt(result.toFixed())
219
+ });
220
+ }
221
+ function getMax({
222
+ balance,
223
+ existentialDeposit,
224
+ fee,
225
+ min
226
+ }) {
227
+ const result = balance.toBig().minus(min.toBig()).minus(
228
+ existentialDeposit && balance.isSame(existentialDeposit) ? existentialDeposit.toBig() : Big(0)
229
+ ).minus(balance.isSame(fee) ? fee.toBig() : Big(0));
230
+ return balance.copyWith({
231
+ amount: result.lt(0) ? 0n : BigInt(result.toFixed())
232
+ });
233
+ }
234
+ async function getDestinationFee({
235
+ address,
236
+ asset,
237
+ destination,
238
+ fee,
239
+ feeAsset,
240
+ source
241
+ }) {
242
+ const zero = AssetAmount2.fromChainAsset(destination.getChainAsset(feeAsset), {
243
+ amount: 0n
244
+ });
245
+ if (typeof fee === "number") {
246
+ return zero.copyWith({
247
+ amount: fee
248
+ });
249
+ }
250
+ if (EvmParachain.isAnyParachain(destination)) {
251
+ const polkadot = await PolkadotService.create(destination);
252
+ const cfg = fee.build({
253
+ address,
254
+ api: polkadot.api,
255
+ asset: destination.getChainAsset(asset),
256
+ destination,
257
+ feeAsset: destination.getChainAsset(feeAsset),
258
+ source
259
+ });
260
+ return zero.copyWith({
261
+ amount: await cfg.call()
262
+ });
263
+ }
264
+ return zero;
265
+ }
266
+ function convertToChainDecimals({
267
+ asset,
268
+ target
269
+ }) {
270
+ return AssetAmount2.fromChainAsset(target, {
271
+ amount: asset.convertDecimals(target.decimals).amount
272
+ });
273
+ }
274
+ async function getExistentialDeposit(chain) {
275
+ if (EvmParachain.isAnyParachain(chain)) {
276
+ const polkadot = await PolkadotService.create(chain);
277
+ return polkadot.existentialDeposit;
278
+ }
279
+ return void 0;
280
+ }
281
+ async function getDestinationFeeBalance({
282
+ balance,
283
+ feeBalance,
284
+ route,
285
+ sourceAddress
286
+ }) {
287
+ if (route.destination.fee.asset.isEqual(balance)) {
288
+ return balance;
289
+ }
290
+ if (route.destination.fee.asset.isEqual(feeBalance)) {
291
+ return feeBalance;
292
+ }
293
+ if (!route.source.destinationFee?.balance) {
294
+ throw new Error(
295
+ "BalanceBuilder must be defined for source.destinationFee.balance for AssetRoute"
296
+ );
297
+ }
298
+ return getBalance({
299
+ address: sourceAddress,
300
+ asset: route.getDestinationFeeAssetOnSource(),
301
+ builder: route.source.destinationFee?.balance,
302
+ chain: route.source.chain
303
+ });
304
+ }
305
+ async function getExtrinsicFee({
306
+ address,
307
+ balance,
308
+ chain,
309
+ extrinsic,
310
+ feeBalance,
311
+ feeConfig
312
+ }) {
313
+ try {
314
+ const polkadot = await PolkadotService.create(chain);
315
+ const fee = await polkadot.getFee(address, extrinsic);
316
+ const extra = feeConfig?.extra ? toBigInt(feeConfig.extra, feeBalance.decimals) : 0n;
317
+ const totalFee = fee + extra;
318
+ const converted = chain.usesChainDecimals ? convertDecimals(totalFee, polkadot.decimals, feeBalance.decimals) : totalFee;
319
+ return feeBalance.copyWith({ amount: converted });
320
+ } catch (error) {
321
+ if (balance.amount) {
322
+ throw error;
323
+ }
324
+ return feeBalance.copyWith({ amount: 0n });
325
+ }
326
+ }
327
+ async function getContractFee({
328
+ address,
329
+ balance,
330
+ chain,
331
+ contract,
332
+ destinationFee,
333
+ feeBalance,
334
+ feeConfig
335
+ }) {
336
+ try {
337
+ if (balance.amount === 0n) {
338
+ return feeBalance.copyWith({ amount: 0n });
339
+ }
340
+ const evm = EvmService.create(chain);
341
+ const fee = await evm.getFee(address, contract);
342
+ const extra = feeConfig?.extra ? toBigInt(feeConfig.extra, feeBalance.decimals) : 0n;
343
+ return feeBalance.copyWith({ amount: fee + extra });
344
+ } catch (error) {
345
+ throw new Error(
346
+ `Can't get a fee, make sure you have ${destinationFee.toDecimal()} ${destinationFee.getSymbol()} in your source balance, needed for destination fees`,
347
+ { cause: error }
348
+ );
349
+ }
350
+ }
351
+ function validateSovereignAccountBalances({
352
+ amount,
353
+ sourceData,
354
+ destinationData
355
+ }) {
356
+ if (!Parachain.is(destinationData.chain) || !destinationData.chain.checkSovereignAccountBalances || !destinationData.sovereignAccountBalances) {
357
+ return;
358
+ }
359
+ const { feeAssetBalance, transferAssetBalance } = destinationData.sovereignAccountBalances;
360
+ if (amount > transferAssetBalance) {
361
+ throw new Error(
362
+ `${sourceData.chain.name} Sovereign account in ${destinationData.chain.name} does not have enough balance for this transaction`
363
+ );
364
+ }
365
+ if (feeAssetBalance && sourceData.destinationFee.amount > feeAssetBalance) {
366
+ throw new Error(
367
+ `${sourceData.chain.name} Sovereign account in ${destinationData.chain.name} does not have enough balance to pay for fees for this transaction`
368
+ );
369
+ }
370
+ }
371
+
372
+ // src/getTransferData/getSourceData.ts
373
+ async function getSourceData({
374
+ route,
375
+ destinationAddress,
376
+ destinationFee,
377
+ sourceAddress
378
+ }) {
379
+ const source = route.source.chain;
380
+ const destination = route.destination.chain;
381
+ const [sourcePolkadot, destinationPolkadot] = await PolkadotService.createMulti([source, destination]);
382
+ const asset = source.getChainAsset(route.source.asset);
383
+ const feeAsset = route.source.fee ? source.getChainAsset(route.source.fee.asset) : asset;
384
+ const balance = await getBalance({
385
+ address: sourceAddress,
386
+ asset,
387
+ builder: route.source.balance,
388
+ chain: source
389
+ });
390
+ const feeBalance = await getBalance({
391
+ address: sourceAddress,
392
+ asset: feeAsset,
393
+ builder: route.source.fee.balance,
394
+ chain: source
395
+ });
396
+ const destinationFeeBalance = await getDestinationFeeBalance({
397
+ balance,
398
+ feeBalance,
399
+ route,
400
+ sourceAddress
401
+ });
402
+ const existentialDeposit = await getExistentialDeposit(source);
403
+ const min = await getAssetMin({
404
+ asset,
405
+ builder: route.source.min,
406
+ chain: source
407
+ });
408
+ const extrinsic = route.extrinsic?.build({
409
+ asset: balance,
410
+ destination: route.destination.chain,
411
+ destinationAddress,
412
+ destinationApi: destinationPolkadot.api,
413
+ fee: destinationFee,
414
+ source,
415
+ sourceAddress,
416
+ sourceApi: sourcePolkadot.api
417
+ });
418
+ const contract = route.contract?.build({
419
+ asset: balance,
420
+ destination: route.destination.chain,
421
+ destinationAddress,
422
+ destinationApi: destinationPolkadot.api,
423
+ fee: destinationFee,
424
+ source,
425
+ sourceAddress,
426
+ sourceApi: sourcePolkadot.api
427
+ });
428
+ const fee = await getFee({
429
+ balance,
430
+ chain: source,
431
+ contract,
432
+ destinationFee,
433
+ extrinsic,
434
+ feeBalance,
435
+ feeConfig: route.source.fee,
436
+ sourceAddress
437
+ });
438
+ const max = getMax({
439
+ balance,
440
+ existentialDeposit,
441
+ fee,
442
+ min
443
+ });
444
+ return {
445
+ balance,
446
+ chain: source,
447
+ destinationFee,
448
+ destinationFeeBalance,
449
+ existentialDeposit,
450
+ fee,
451
+ feeBalance,
452
+ max,
453
+ min
454
+ };
455
+ }
456
+ async function getFee({
457
+ balance,
458
+ feeBalance,
459
+ chain,
460
+ contract,
461
+ destinationFee,
462
+ extrinsic,
463
+ feeConfig,
464
+ sourceAddress
465
+ }) {
466
+ if (!contract && !extrinsic) {
467
+ throw new Error("Either contract or extrinsic must be provided");
468
+ }
469
+ if (contract) {
470
+ return getContractFee({
471
+ address: sourceAddress,
472
+ balance,
473
+ chain,
474
+ contract,
475
+ destinationFee,
476
+ feeBalance,
477
+ feeConfig
478
+ });
479
+ }
480
+ return getExtrinsicFee({
481
+ address: sourceAddress,
482
+ balance,
483
+ chain,
484
+ extrinsic,
485
+ feeBalance,
486
+ feeConfig
487
+ });
488
+ }
489
+ async function getAssetsBalances({
490
+ address,
491
+ chain,
492
+ routes
493
+ }) {
494
+ const uniqueRoutes = routes.reduce((acc, route) => {
495
+ if (!acc.some((a) => a.source.asset.isEqual(route.source.asset))) {
496
+ return [route, ...acc];
497
+ }
498
+ return acc;
499
+ }, []);
500
+ const balances = await Promise.all(
501
+ uniqueRoutes.map(
502
+ async (route) => getBalance({
503
+ address,
504
+ asset: chain.getChainAsset(route.source.asset),
505
+ builder: route.source.balance,
506
+ chain
507
+ })
508
+ )
509
+ );
510
+ return balances;
511
+ }
512
+
513
+ // src/getTransferData/getDestinationData.ts
514
+ import { Parachain as Parachain2 } from "@moonbeam-network/xcm-types";
515
+ import { getSovereignAccountAddresses } from "@moonbeam-network/xcm-utils";
516
+ async function getDestinationData({
517
+ route,
518
+ destinationAddress
519
+ }) {
520
+ const destination = route.destination.chain;
521
+ const asset = destination.getChainAsset(route.destination.asset);
522
+ const balance = await getBalance({
523
+ address: destinationAddress,
524
+ asset,
525
+ builder: route.destination.balance,
526
+ chain: destination
527
+ });
528
+ const min = await getAssetMin({
529
+ asset,
530
+ builder: route.destination.min,
531
+ chain: destination
532
+ });
533
+ const fee = await getDestinationFee({
534
+ address: destinationAddress,
535
+ feeAsset: route.destination.fee.asset,
536
+ destination,
537
+ fee: route.destination.fee.amount,
538
+ source: route.source.chain,
539
+ asset: route.source.asset
540
+ });
541
+ const existentialDeposit = await getExistentialDeposit(destination);
542
+ return {
543
+ chain: destination,
544
+ balance,
545
+ existentialDeposit,
546
+ fee,
547
+ min,
548
+ sovereignAccountBalances: await getSovereignAccountBalances({
549
+ source: route.source,
550
+ destination: route.destination
551
+ })
552
+ };
553
+ }
554
+ async function getSovereignAccountBalances({
555
+ destination,
556
+ source
557
+ }) {
558
+ if (!Parachain2.is(source.chain) || !Parachain2.is(destination.chain) || !destination.chain.checkSovereignAccountBalances) {
559
+ return void 0;
560
+ }
561
+ const sovereignAccountAddresses = getSovereignAccountAddresses(
562
+ source.chain.parachainId
563
+ );
564
+ const destinationFeeAssetBalance = destination.fee.balance;
565
+ const sovereignAccountAddress = destination.chain.isRelay ? sovereignAccountAddresses.relay : sovereignAccountAddresses.generic;
566
+ const sovereignAccountBalance = await getBalance({
567
+ address: sovereignAccountAddress,
568
+ asset: destination.chain.getChainAsset(destination.asset),
569
+ builder: destination.balance,
570
+ chain: destination.chain
571
+ });
572
+ const sovereignAccountFeeAssetBalance = destinationFeeAssetBalance ? await getBalance({
573
+ address: sovereignAccountAddress,
574
+ asset: destination.chain.getChainAsset(destination.fee.asset),
575
+ builder: destinationFeeAssetBalance,
576
+ chain: destination.chain
577
+ }) : void 0;
578
+ return {
579
+ feeAssetBalance: sovereignAccountFeeAssetBalance?.amount,
580
+ transferAssetBalance: sovereignAccountBalance.amount
581
+ };
582
+ }
583
+
584
+ // src/sdk.ts
585
+ import { ConfigService, xcmRoutesMap } from "@moonbeam-network/xcm-config";
586
+ import {
587
+ EvmParachain as EvmParachain2
588
+ } from "@moonbeam-network/xcm-types";
589
+
590
+ // src/getTransferData/getTransferData.ts
591
+ import {
592
+ AssetAmount as AssetAmount3
593
+ } from "@moonbeam-network/xcm-types";
594
+ import { toBigInt as toBigInt2 } from "@moonbeam-network/xcm-utils";
595
+ import Big2 from "big.js";
596
+ async function getTransferData({
597
+ route,
598
+ sourceAddress,
599
+ destinationAddress
600
+ }) {
601
+ const destinationData = await getDestinationData({
602
+ route,
603
+ destinationAddress
604
+ });
605
+ const destinationFee = convertToChainDecimals({
606
+ asset: destinationData.fee,
607
+ target: route.getDestinationFeeAssetOnSource()
608
+ });
609
+ const sourceData = await getSourceData({
610
+ route,
611
+ destinationAddress,
612
+ destinationFee,
613
+ sourceAddress
614
+ });
615
+ return {
616
+ destination: destinationData,
617
+ getEstimate(amount) {
618
+ const bigAmount = Big2(
619
+ toBigInt2(amount, sourceData.balance.decimals).toString()
620
+ );
621
+ const result = bigAmount.minus(
622
+ sourceData.balance.isSame(destinationFee) ? destinationFee.toBig() : Big2(0)
623
+ );
624
+ return sourceData.balance.copyWith({
625
+ amount: result.lt(0) ? 0n : BigInt(result.toFixed())
626
+ });
627
+ },
628
+ max: sourceData.max,
629
+ min: getMin(destinationData),
630
+ source: sourceData,
631
+ async transfer(amount, { evmSigner, polkadotSigner }) {
632
+ const source = route.source.chain;
633
+ const destination = route.destination.chain;
634
+ const bigintAmount = toBigInt2(amount, sourceData.balance.decimals);
635
+ validateSovereignAccountBalances({
636
+ amount: bigintAmount,
637
+ destinationData,
638
+ sourceData
639
+ });
640
+ const asset = AssetAmount3.fromChainAsset(
641
+ route.source.chain.getChainAsset(route.source.asset),
642
+ { amount: bigintAmount }
643
+ );
644
+ const [sourcePolkadot, destinationPolkadot] = await PolkadotService.createMulti([source, destination]);
645
+ const contract = route.contract?.build({
646
+ asset,
647
+ destination,
648
+ destinationAddress,
649
+ destinationApi: destinationPolkadot.api,
650
+ fee: destinationFee,
651
+ source,
652
+ sourceAddress,
653
+ sourceApi: sourcePolkadot.api
654
+ });
655
+ const extrinsic = route.extrinsic?.build({
656
+ asset,
657
+ destination,
658
+ destinationAddress,
659
+ destinationApi: destinationPolkadot.api,
660
+ fee: destinationFee,
661
+ source,
662
+ sourceAddress,
663
+ sourceApi: sourcePolkadot.api
664
+ });
665
+ if (contract) {
666
+ if (!evmSigner) {
667
+ throw new Error("EVM Signer must be provided");
668
+ }
669
+ const evm = EvmService.create(source);
670
+ return evm.transfer(evmSigner, contract);
671
+ }
672
+ if (extrinsic) {
673
+ if (!polkadotSigner) {
674
+ throw new Error("Polkadot signer must be provided");
675
+ }
676
+ return sourcePolkadot.transfer(
677
+ sourceAddress,
678
+ extrinsic,
679
+ polkadotSigner
680
+ );
681
+ }
682
+ throw new Error("Either contract or extrinsic must be provided");
683
+ }
684
+ };
685
+ }
686
+
687
+ // src/sdk.ts
688
+ var DEFAULT_SERVICE = new ConfigService({ routes: xcmRoutesMap });
689
+ function Sdk({ configService, ecosystem } = {}) {
690
+ const service = configService ?? DEFAULT_SERVICE;
691
+ const assets = service.getEcosystemAssets(ecosystem);
692
+ return {
693
+ assets,
694
+ setAsset(asset) {
695
+ const sources = service.getSourceChains({ asset, ecosystem });
696
+ return {
697
+ sources,
698
+ setSource(source) {
699
+ const destinations = service.getDestinationChains({
700
+ asset,
701
+ source
702
+ });
703
+ return {
704
+ destinations,
705
+ setDestination(destination) {
706
+ const route = service.getAssetRoute({
707
+ asset,
708
+ source,
709
+ destination
710
+ });
711
+ return {
712
+ setAddresses({
713
+ sourceAddress,
714
+ destinationAddress
715
+ }) {
716
+ const sourceChain = service.getChain(source);
717
+ if (!EvmParachain2.isAnyParachain(sourceChain)) {
718
+ throw new Error(
719
+ "Source chain should be a Parachain or EvmParachain"
720
+ );
721
+ }
722
+ if (!EvmParachain2.isAnyParachain(route.destination.chain)) {
723
+ throw new Error(
724
+ "Destination chain should be a Parachain or EvmParachain"
725
+ );
726
+ }
727
+ return getTransferData({
728
+ route,
729
+ sourceAddress,
730
+ destinationAddress
731
+ });
732
+ }
733
+ };
734
+ }
735
+ };
736
+ }
737
+ };
738
+ }
739
+ };
740
+ }
741
+ async function getParachainBalances(chain, address, service = DEFAULT_SERVICE) {
742
+ const routes = service.getChainRoutes(chain).getRoutes();
743
+ const balances = await getAssetsBalances({
744
+ chain,
745
+ routes,
746
+ address
747
+ });
748
+ return balances;
749
+ }
750
+ export {
751
+ EvmService,
752
+ PolkadotService,
753
+ Sdk,
754
+ convertToChainDecimals,
755
+ getAssetMin,
756
+ getAssetsBalances,
757
+ getBalance,
758
+ getContractFee,
759
+ getDestinationData,
760
+ getDestinationFee,
761
+ getDestinationFeeBalance,
762
+ getExistentialDeposit,
763
+ getExtrinsicFee,
764
+ getFee,
765
+ getMax,
766
+ getMin,
767
+ getParachainBalances,
768
+ getSourceData,
769
+ validateSovereignAccountBalances
770
+ };
2
771
  //# sourceMappingURL=index.mjs.map