@moonbeam-network/xcm-sdk 1.0.0-dev.18 → 1.0.0-dev.180

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,768 @@
1
- import{AssetAmount as G}from"@moonbeam-network/xcm-types";import{convertDecimals as ut}from"@moonbeam-network/xcm-utils";import V from"big.js";import{Contract as nt}from"ethers";import{createPublicClient as at,getContract as rt,http as st}from"viem";function S(a){return"provider"in a}function C(a){return"signer"in a}var k=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var I=class{address;#t;#e;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#t=t,this.#e=S(e)?new nt(this.address,k,e):rt({abi:k,address:this.address,publicClient:at({chain:e.chain,transport:st()}),walletClient:e})}async getBalance(){return C(this.#e)?(await this.#e.balanceOf(...this.#t.args)).toBigInt():this.#e.read.balanceOf(this.#t.args)}async getDecimals(){return C(this.#e)?await this.#e.decimals():this.#e.read.decimals()}};import{Contract as ot}from"ethers";import{createPublicClient as z,getContract as ct,http as K}from"viem";var F=[{inputs:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transfer",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Xtokens.Currency[]",name:"currencies",type:"tuple[]"},{internalType:"uint32",name:"feeItem",type:"uint32"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transferMultiCurrencies",outputs:[],stateMutability:"nonpayable",type:"function"}];var T=class{address="0x0000000000000000000000000000000000000804";#t;#e;#n;constructor(t,e){this.#t=t,this.#n=e,this.#e=S(e)?new ot(this.address,F,e):ct({abi:F,address:this.address,publicClient:z({chain:e.chain,transport:K()}),walletClient:e})}async transfer(){return C(this.#e)?this.#e[this.#t.func](...this.#t.args):this.#e.write[this.#t.func](this.#t.args)}async getEthersContractEstimatedGas(t){return(await t.estimateGas[this.#t.func](...this.#t.args)).toBigInt()}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=C(this.#e)?await this.getEthersContractEstimatedGas(this.#e):await this.getViemContractEstimatedGas(this.#e),n=await this.getGasPrice();return e*n}catch(e){throw console.error(e),new Error("Can't get a fee. Make sure that you have enough balance!")}}async getGasPrice(){if(S(this.#n)){let{gasPrice:e,maxPriorityFeePerGas:n}=await this.#n.getFeeData();return((e==null?void 0:e.toBigInt())||0n)+((n==null?void 0:n.toBigInt())||0n)}return z({chain:this.#n.chain,transport:K()}).getGasPrice()}};function g(a,t){if(a.module==="Erc20")return new I(a,t);if(a.module==="Xtokens")return new T(a,t);throw new Error(`Contract ${a.module} not found`)}import{CallType as N}from"@moonbeam-network/xcm-builder";import{toBigInt as mt}from"@moonbeam-network/xcm-utils";async function B({address:a,config:t,evmSigner:e,polkadot:n}){let r=t.balance.build({address:a,asset:n.chain.getBalanceAssetId(t.asset)});if(r.type===N.Substrate)return n.query(r);if(!e)throw new Error("Evm signer must be provided");return g(r,e).getBalance()}async function b({address:a,asset:t,config:e,evmSigner:n,polkadot:r}){let s=e.balance.build({address:a,asset:r.chain.getBalanceAssetId(t||e.asset)});if(s.type===N.Substrate)return r.getAssetDecimals(t||e.asset);if(!n)throw new Error("Evm signer must be provided");return g(s,n).getDecimals()}async function D(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 mt(e,n)}return 0n}async function j({transferConfig:a,destinationAddress:t,destinationFee:e,evmSigner:n,polkadot:r,sourceAddress:s}){var O,_,X,Q;let{asset:o,destination:f,source:{chain:c,config:i}}=a,p=G.fromAsset(o,{amount:0n,decimals:await b({address:t,config:i,evmSigner:n,polkadot:r})}),l=(O=i.fee)!=null&&O.asset?G.fromAsset(i.fee.asset,{amount:0n,decimals:await b({address:t,asset:i.fee.asset,config:i,evmSigner:n,polkadot:r})}):p,d=(_=i.destinationFee)!=null&&_.asset?G.fromAsset(i.destinationFee.asset,{amount:0n,decimals:await b({address:t,asset:i.destinationFee.asset,config:i,evmSigner:n,polkadot:r})}):p,m=await B({address:s,config:i,evmSigner:n,polkadot:r}),u=await $({address:s,balance:m,feeConfig:i.fee,polkadot:r}),w=i.asset.isEqual(i.destinationFee.asset)?m:await $({address:s,balance:m,feeConfig:i.destinationFee,polkadot:r}),P=await D(i,r),x=(X=i.extrinsic)==null?void 0:X.build({address:t,amount:m,asset:c.getAssetId(o),destination:f.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(o),source:c}),v=(Q=i.contract)==null?void 0:Q.build({address:t,amount:m,asset:c.getAssetId(o),destination:f.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),E=await ft({balance:m,contract:v,decimals:l.decimals,evmSigner:n,extrinsic:x,polkadot:r,sourceAddress:s}),h=p.copyWith({amount:m}),{existentialDeposit:W}=r,R=l.copyWith({amount:E}),Y=l.copyWith({amount:u}),Z=d.copyWith({amount:w}),q=p.copyWith({amount:P}),tt=dt({balanceAmount:h,existentialDeposit:W,feeAmount:R,minAmount:q});return{balance:h,chain:c,destinationFeeBalance:Z,existentialDeposit:W,fee:R,feeBalance:Y,max:tt,min:q}}async function $({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 ft({balance:a,contract:t,decimals:e,evmSigner:n,extrinsic:r,polkadot:s,sourceAddress:o}){if(t){if(!n)throw new Error("EVM Signer must be provided");return pt(a,t,e,n)}if(r)return lt(a,r,s,o);throw new Error("Either contract or extrinsic must be provided")}async function pt(a,t,e,n){let s=await g(t,n).getFee(a);return ut(s,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 dt({balanceAmount:a,existentialDeposit:t,feeAmount:e,minAmount:n}){let r=a.toBig().minus(n.toBig()).minus(a.isSame(t)?t.toBig():V(0)).minus(a.isSame(e)?e.toBig():V(0));return a.copyWith({amount:r.lt(0)?0n:BigInt(r.toFixed())})}import{ConfigBuilder as U,ConfigService as It}from"@moonbeam-network/xcm-config";import{convertDecimals as xt,toBigInt as J}from"@moonbeam-network/xcm-utils";import A from"big.js";import"@polkadot/api-augment";import{darwiniaPangoro as gt,eq as yt,equilibrium as ht,equilibriumAlphanet as Ct,paring as bt}from"@moonbeam-network/xcm-config";import{AssetAmount as At}from"@moonbeam-network/xcm-types";import{getPolkadotApi as St}from"@moonbeam-network/xcm-utils";var y=class{api;chain;configService;constructor(t,e,n){this.api=t,this.chain=e,this.configService=n}static async create(t,e){return new y(await St(t.ws),t,e)}static async createMulti(t,e){return Promise.all(t.map(n=>y.create(n,e)))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}get asset(){let t=this.api.registry.chainTokens.at(0),e=t==null?void 0:t.toString().toLowerCase();if(e==="token"&&[Ct.key,ht.key].includes(this.chain.key))return yt;if(e==="oring"&&this.chain.key===gt.key)return bt;if(!e)throw new Error("No native symbol key found");let n=this.configService.getAsset(e);if(!n)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return n}get existentialDeposit(){var r,s;let t=(r=this.api.consts.balances)==null?void 0:r.existentialDeposit,e=(s=this.api.consts.eqBalances)==null?void 0:s.existentialDeposit,n=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return At.fromAsset(this.asset,{amount:n,decimals:this.decimals})}async getAssetMeta(t){var s,o,f,c,i;let e=((s=this.api.query.assets)==null?void 0:s.metadata)||((o=this.api.query.assetRegistry)==null?void 0:o.metadata)||((f=this.api.query.assetRegistry)==null?void 0:f.currencyMetadatas)||((c=this.api.query.assetRegistry)==null?void 0:c.assetMetadatas)||((i=this.api.query.assetRegistry)==null?void 0:i.assetMetadataMap);if(!e)return;let n=await e(t),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 L}from"@moonbeam-network/xcm-types";import{toBigInt as wt}from"@moonbeam-network/xcm-utils";async function H({transferConfig:a,destinationAddress:t,evmSigner:e,polkadot:n}){let{asset:r,destination:{chain:s,config:o}}=a,f=L.fromAsset(r,{amount:0n,decimals:await b({address:t,config:o,evmSigner:e,polkadot:n})}),c=await B({address:t,config:o,evmSigner:e,polkadot:n}),i=await D(o,n),p=f.copyWith({amount:c}),{existentialDeposit:l}=n,d=await Pt({config:a,polkadot:n}),m=f.copyWith({amount:i});return{balance:p,chain:s,existentialDeposit:l,fee:d,min:m}}async function Pt({config:a,polkadot:t}){let{amount:e,asset:n}=a.source.config.destinationFee,r=await t.getAssetDecimals(n),s=L.fromAsset(n,{amount:0n,decimals:r});if(Number.isFinite(e))return s.copyWith({amount:wt(e,r)});let o=e.build({api:t.api,asset:t.chain.getAssetId(n)});return s.copyWith({amount:await o.call()})}async function M({configService:a,destinationAddress:t,evmSigner:e,polkadotSigner:n,sourceAddress:r,transferConfig:s}){let[o,f]=await y.createMulti([s.destination.chain,s.source.chain],a),c=await H({destinationAddress:t,evmSigner:e,polkadot:o,transferConfig:s}),i=await Et(f,c.fee),p=await j({destinationAddress:t,destinationFee:i,evmSigner:e,polkadot:f,sourceAddress:r,transferConfig:s});return{destination:c,getEstimate(l){let m=A(J(l,p.balance.decimals).toString()).minus(p.balance.isSame(i)?i.toBig():A(0));return p.balance.copyWith({amount:m.lt(0)?0n:BigInt(m.toFixed())})},isSwapPossible:!0,max:p.max,min:vt(c),source:p,async swap(){return M({configService:a,destinationAddress:r,evmSigner:e,polkadotSigner:n,sourceAddress:t,transferConfig:{...s,destination:s.source,source:s.destination}})},async transfer(l){var v,E;let d=J(l,p.balance.decimals),{asset:m,source:{chain:u,config:w}}=s,P=(v=w.contract)==null?void 0:v.build({address:t,amount:d,asset:u.getAssetId(m),destination:c.chain,fee:i.amount,feeAsset:u.getAssetId(i)}),x=(E=w.extrinsic)==null?void 0:E.build({address:t,amount:d,asset:u.getAssetId(m),destination:c.chain,fee:i.amount,feeAsset:u.getAssetId(i),palletInstance:u.getAssetPalletInstance(m),source:u});if(P){if(!e)throw new Error("EVM Signer must be provided");return g(P,e).transfer().then(h=>typeof h=="object"?h.hash:h)}if(x){if(!n)throw new Error("Polkadot signer must be provided");return f.transfer(r,x,n)}throw new Error("Either contract or extrinsic must be provided")}}}function vt({balance:a,existentialDeposit:t,fee:e,min:n}){let r=A(0).plus(a.isSame(e)?e.toBig():A(0)).plus(a.isSame(t)&&a.toBig().lt(t.toBig())?t.toBig():A(0)).plus(a.toBig().lt(n.toBig())?n.toBig():A(0));return a.copyWith({amount:BigInt(r.toFixed())})}async function Et(a,t){let e=await a.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:xt(t.amount,t.decimals,e),decimals:e})}function $e(a){let t=(a==null?void 0:a.configService)??new It;return{assets(e){let{assets:n,asset:r}=U(t).assets(e);return{assets:n,asset(s){let{sourceChains:o,source:f}=r(s);return{sourceChains:o,source(c){let{destinationChains:i,destination:p}=f(c);return{destinationChains:i,destination(l){return{async accounts(d,m,u){return M({...a,configService:t,destinationAddress:m,evmSigner:(u==null?void 0:u.evmSigner)??(u==null?void 0:u.ethersSigner),sourceAddress:d,transferConfig:p(l).build(),polkadotSigner:u==null?void 0:u.polkadotSigner})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:n,ethersSigner:r,evmSigner:s,keyOrAsset:o,polkadotSigner:f,sourceAddress:c,sourceKeyOrChain:i}){return M({configService:t,destinationAddress:e,evmSigner:s??r,polkadotSigner:f,sourceAddress:c,transferConfig:U(t).assets().asset(o).source(i).destination(n).build()})}}}export{$e as Sdk,pt as getContractFee,lt as getExtrinsicFee,ft as getFee,$ as getFeeBalances,dt as getMax,j 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
+ }) {
241
+ const zero = AssetAmount2.fromChainAsset(destination.getChainAsset(feeAsset), {
242
+ amount: 0n
243
+ });
244
+ if (Number.isFinite(fee)) {
245
+ return zero.copyWith({
246
+ amount: fee
247
+ });
248
+ }
249
+ if (EvmParachain.isAnyParachain(destination)) {
250
+ const polkadot = await PolkadotService.create(destination);
251
+ const cfg = fee.build({
252
+ address,
253
+ api: polkadot.api,
254
+ asset: destination.getChainAsset(asset),
255
+ destination,
256
+ feeAsset: destination.getChainAsset(feeAsset)
257
+ });
258
+ return zero.copyWith({
259
+ amount: await cfg.call()
260
+ });
261
+ }
262
+ return zero;
263
+ }
264
+ function convertToChainDecimals({
265
+ asset,
266
+ target
267
+ }) {
268
+ return AssetAmount2.fromChainAsset(target, {
269
+ amount: asset.convertDecimals(target.decimals).amount
270
+ });
271
+ }
272
+ async function getExistentialDeposit(chain) {
273
+ if (EvmParachain.isAnyParachain(chain)) {
274
+ const polkadot = await PolkadotService.create(chain);
275
+ return polkadot.existentialDeposit;
276
+ }
277
+ return void 0;
278
+ }
279
+ async function getDestinationFeeBalance({
280
+ balance,
281
+ feeBalance,
282
+ route,
283
+ sourceAddress
284
+ }) {
285
+ if (route.destination.fee.asset.isEqual(balance)) {
286
+ return balance;
287
+ }
288
+ if (route.destination.fee.asset.isEqual(feeBalance)) {
289
+ return feeBalance;
290
+ }
291
+ if (!route.source.destinationFee?.balance) {
292
+ throw new Error(
293
+ "BalanceBuilder must be defined for source.destinationFee.balance for AssetRoute"
294
+ );
295
+ }
296
+ return getBalance({
297
+ address: sourceAddress,
298
+ asset: route.getDestinationFeeAssetOnSource(),
299
+ builder: route.source.destinationFee?.balance,
300
+ chain: route.source.chain
301
+ });
302
+ }
303
+ async function getExtrinsicFee({
304
+ address,
305
+ balance,
306
+ chain,
307
+ extrinsic,
308
+ feeBalance,
309
+ feeConfig
310
+ }) {
311
+ try {
312
+ const polkadot = await PolkadotService.create(chain);
313
+ const fee = await polkadot.getFee(address, extrinsic);
314
+ const extra = feeConfig?.extra ? toBigInt(feeConfig.extra, feeBalance.decimals) : 0n;
315
+ const totalFee = fee + extra;
316
+ const converted = chain.usesChainDecimals ? convertDecimals(totalFee, polkadot.decimals, feeBalance.decimals) : totalFee;
317
+ return feeBalance.copyWith({ amount: converted });
318
+ } catch (error) {
319
+ if (balance.amount) {
320
+ throw error;
321
+ }
322
+ return feeBalance.copyWith({ amount: 0n });
323
+ }
324
+ }
325
+ async function getContractFee({
326
+ address,
327
+ balance,
328
+ chain,
329
+ contract,
330
+ destinationFee,
331
+ feeBalance,
332
+ feeConfig
333
+ }) {
334
+ try {
335
+ if (balance.amount === 0n) {
336
+ return feeBalance.copyWith({ amount: 0n });
337
+ }
338
+ const evm = EvmService.create(chain);
339
+ const fee = await evm.getFee(address, contract);
340
+ const extra = feeConfig?.extra ? toBigInt(feeConfig.extra, feeBalance.decimals) : 0n;
341
+ return feeBalance.copyWith({ amount: fee + extra });
342
+ } catch (error) {
343
+ throw new Error(
344
+ `Can't get a fee, make sure you have ${destinationFee.toDecimal()} ${destinationFee.getSymbol()} in your source balance, needed for destination fees`,
345
+ { cause: error }
346
+ );
347
+ }
348
+ }
349
+ function validateSovereignAccountBalances({
350
+ amount,
351
+ sourceData,
352
+ destinationData
353
+ }) {
354
+ if (!Parachain.is(destinationData.chain) || !destinationData.chain.checkSovereignAccountBalances || !destinationData.sovereignAccountBalances) {
355
+ return;
356
+ }
357
+ const { feeAssetBalance, transferAssetBalance } = destinationData.sovereignAccountBalances;
358
+ if (amount > transferAssetBalance) {
359
+ throw new Error(
360
+ `${sourceData.chain.name} Sovereign account in ${destinationData.chain.name} does not have enough balance for this transaction`
361
+ );
362
+ }
363
+ if (feeAssetBalance && sourceData.destinationFee.amount > feeAssetBalance) {
364
+ throw new Error(
365
+ `${sourceData.chain.name} Sovereign account in ${destinationData.chain.name} does not have enough balance to pay for fees for this transaction`
366
+ );
367
+ }
368
+ }
369
+
370
+ // src/getTransferData/getSourceData.ts
371
+ async function getSourceData({
372
+ route,
373
+ destinationAddress,
374
+ destinationFee,
375
+ sourceAddress
376
+ }) {
377
+ const source = route.source.chain;
378
+ const destination = route.destination.chain;
379
+ const [sourcePolkadot, destinationPolkadot] = await PolkadotService.createMulti([source, destination]);
380
+ const asset = source.getChainAsset(route.source.asset);
381
+ const feeAsset = route.source.fee ? source.getChainAsset(route.source.fee.asset) : asset;
382
+ const balance = await getBalance({
383
+ address: sourceAddress,
384
+ asset,
385
+ builder: route.source.balance,
386
+ chain: source
387
+ });
388
+ const feeBalance = route.source.fee ? await getBalance({
389
+ address: sourceAddress,
390
+ asset: feeAsset,
391
+ builder: route.source.fee.balance,
392
+ chain: source
393
+ }) : balance;
394
+ const destinationFeeBalance = await getDestinationFeeBalance({
395
+ balance,
396
+ feeBalance,
397
+ route,
398
+ sourceAddress
399
+ });
400
+ const existentialDeposit = await getExistentialDeposit(source);
401
+ const min = await getAssetMin({
402
+ asset,
403
+ builder: route.source.min,
404
+ chain: source
405
+ });
406
+ const extrinsic = route.extrinsic?.build({
407
+ asset: balance,
408
+ destination: route.destination.chain,
409
+ destinationAddress,
410
+ destinationApi: destinationPolkadot.api,
411
+ fee: destinationFee,
412
+ source,
413
+ sourceAddress,
414
+ sourceApi: sourcePolkadot.api
415
+ });
416
+ const contract = route.contract?.build({
417
+ asset: balance,
418
+ destination: route.destination.chain,
419
+ destinationAddress,
420
+ destinationApi: destinationPolkadot.api,
421
+ fee: destinationFee,
422
+ source,
423
+ sourceAddress,
424
+ sourceApi: sourcePolkadot.api
425
+ });
426
+ const fee = await getFee({
427
+ balance,
428
+ chain: source,
429
+ contract,
430
+ destinationFee,
431
+ extrinsic,
432
+ feeBalance,
433
+ feeConfig: route.source.fee,
434
+ sourceAddress
435
+ });
436
+ const max = getMax({
437
+ balance,
438
+ existentialDeposit,
439
+ fee,
440
+ min
441
+ });
442
+ return {
443
+ balance,
444
+ chain: source,
445
+ destinationFee,
446
+ destinationFeeBalance,
447
+ existentialDeposit,
448
+ fee,
449
+ feeBalance,
450
+ max,
451
+ min
452
+ };
453
+ }
454
+ async function getFee({
455
+ balance,
456
+ feeBalance,
457
+ chain,
458
+ contract,
459
+ destinationFee,
460
+ extrinsic,
461
+ feeConfig,
462
+ sourceAddress
463
+ }) {
464
+ if (!contract && !extrinsic) {
465
+ throw new Error("Either contract or extrinsic must be provided");
466
+ }
467
+ if (contract) {
468
+ return getContractFee({
469
+ address: sourceAddress,
470
+ balance,
471
+ chain,
472
+ contract,
473
+ destinationFee,
474
+ feeBalance,
475
+ feeConfig
476
+ });
477
+ }
478
+ return getExtrinsicFee({
479
+ address: sourceAddress,
480
+ balance,
481
+ chain,
482
+ extrinsic,
483
+ feeBalance,
484
+ feeConfig
485
+ });
486
+ }
487
+ async function getAssetsBalances({
488
+ address,
489
+ chain,
490
+ routes
491
+ }) {
492
+ const uniqueRoutes = routes.reduce((acc, route) => {
493
+ if (!acc.some((a) => a.source.asset.isEqual(route.source.asset))) {
494
+ return [route, ...acc];
495
+ }
496
+ return acc;
497
+ }, []);
498
+ const balances = await Promise.all(
499
+ uniqueRoutes.map(
500
+ async (route) => getBalance({
501
+ address,
502
+ asset: chain.getChainAsset(route.source.asset),
503
+ builder: route.source.balance,
504
+ chain
505
+ })
506
+ )
507
+ );
508
+ return balances;
509
+ }
510
+
511
+ // src/getTransferData/getDestinationData.ts
512
+ import { Parachain as Parachain2 } from "@moonbeam-network/xcm-types";
513
+ import { getSovereignAccountAddresses } from "@moonbeam-network/xcm-utils";
514
+ async function getDestinationData({
515
+ route,
516
+ destinationAddress
517
+ }) {
518
+ const destination = route.destination.chain;
519
+ const asset = destination.getChainAsset(route.destination.asset);
520
+ const balance = await getBalance({
521
+ address: destinationAddress,
522
+ asset,
523
+ builder: route.destination.balance,
524
+ chain: destination
525
+ });
526
+ const min = await getAssetMin({
527
+ asset,
528
+ builder: route.destination.min,
529
+ chain: destination
530
+ });
531
+ const fee = await getDestinationFee({
532
+ address: destinationAddress,
533
+ feeAsset: route.destination.fee.asset,
534
+ destination,
535
+ fee: route.destination.fee.amount,
536
+ asset: route.source.asset
537
+ });
538
+ const existentialDeposit = await getExistentialDeposit(destination);
539
+ return {
540
+ chain: destination,
541
+ balance,
542
+ existentialDeposit,
543
+ fee,
544
+ min,
545
+ sovereignAccountBalances: await getSovereignAccountBalances({
546
+ source: route.source,
547
+ destination: route.destination
548
+ })
549
+ };
550
+ }
551
+ async function getSovereignAccountBalances({
552
+ destination,
553
+ source
554
+ }) {
555
+ if (!Parachain2.is(source.chain) || !Parachain2.is(destination.chain) || !destination.chain.checkSovereignAccountBalances) {
556
+ return void 0;
557
+ }
558
+ const sovereignAccountAddresses = getSovereignAccountAddresses(
559
+ source.chain.parachainId
560
+ );
561
+ const destinationFeeAssetBalance = destination.fee.balance;
562
+ const sovereignAccountAddress = destination.chain.isRelay ? sovereignAccountAddresses.relay : sovereignAccountAddresses.generic;
563
+ const sovereignAccountBalance = await getBalance({
564
+ address: sovereignAccountAddress,
565
+ asset: destination.chain.getChainAsset(destination.asset),
566
+ builder: destination.balance,
567
+ chain: destination.chain
568
+ });
569
+ const sovereignAccountFeeAssetBalance = destinationFeeAssetBalance ? await getBalance({
570
+ address: sovereignAccountAddress,
571
+ asset: destination.chain.getChainAsset(destination.fee.asset),
572
+ builder: destinationFeeAssetBalance,
573
+ chain: destination.chain
574
+ }) : void 0;
575
+ return {
576
+ feeAssetBalance: sovereignAccountFeeAssetBalance?.amount,
577
+ transferAssetBalance: sovereignAccountBalance.amount
578
+ };
579
+ }
580
+
581
+ // src/sdk.ts
582
+ import { ConfigService, xcmRoutesMap } from "@moonbeam-network/xcm-config";
583
+ import {
584
+ EvmParachain as EvmParachain2
585
+ } from "@moonbeam-network/xcm-types";
586
+
587
+ // src/getTransferData/getTransferData.ts
588
+ import {
589
+ AssetAmount as AssetAmount3
590
+ } from "@moonbeam-network/xcm-types";
591
+ import { toBigInt as toBigInt2 } from "@moonbeam-network/xcm-utils";
592
+ import Big2 from "big.js";
593
+ async function getTransferData({
594
+ route,
595
+ sourceAddress,
596
+ destinationAddress
597
+ }) {
598
+ const destinationData = await getDestinationData({
599
+ route,
600
+ destinationAddress
601
+ });
602
+ const destinationFee = convertToChainDecimals({
603
+ asset: destinationData.fee,
604
+ target: route.getDestinationFeeAssetOnSource()
605
+ });
606
+ const sourceData = await getSourceData({
607
+ route,
608
+ destinationAddress,
609
+ destinationFee,
610
+ sourceAddress
611
+ });
612
+ return {
613
+ destination: destinationData,
614
+ getEstimate(amount) {
615
+ const bigAmount = Big2(
616
+ toBigInt2(amount, sourceData.balance.decimals).toString()
617
+ );
618
+ const result = bigAmount.minus(
619
+ sourceData.balance.isSame(destinationFee) ? destinationFee.toBig() : Big2(0)
620
+ );
621
+ return sourceData.balance.copyWith({
622
+ amount: result.lt(0) ? 0n : BigInt(result.toFixed())
623
+ });
624
+ },
625
+ max: sourceData.max,
626
+ min: getMin(destinationData),
627
+ source: sourceData,
628
+ async transfer(amount, { evmSigner, polkadotSigner }) {
629
+ const source = route.source.chain;
630
+ const destination = route.destination.chain;
631
+ const bigintAmount = toBigInt2(amount, sourceData.balance.decimals);
632
+ validateSovereignAccountBalances({
633
+ amount: bigintAmount,
634
+ destinationData,
635
+ sourceData
636
+ });
637
+ const asset = AssetAmount3.fromChainAsset(
638
+ route.source.chain.getChainAsset(route.source.asset),
639
+ { amount: bigintAmount }
640
+ );
641
+ const [sourcePolkadot, destinationPolkadot] = await PolkadotService.createMulti([source, destination]);
642
+ const contract = route.contract?.build({
643
+ asset,
644
+ destination,
645
+ destinationAddress,
646
+ destinationApi: destinationPolkadot.api,
647
+ fee: destinationFee,
648
+ source,
649
+ sourceAddress,
650
+ sourceApi: sourcePolkadot.api
651
+ });
652
+ const extrinsic = route.extrinsic?.build({
653
+ asset,
654
+ destination,
655
+ destinationAddress,
656
+ destinationApi: destinationPolkadot.api,
657
+ fee: destinationFee,
658
+ source,
659
+ sourceAddress,
660
+ sourceApi: sourcePolkadot.api
661
+ });
662
+ if (contract) {
663
+ if (!evmSigner) {
664
+ throw new Error("EVM Signer must be provided");
665
+ }
666
+ const evm = EvmService.create(source);
667
+ return evm.transfer(evmSigner, contract);
668
+ }
669
+ if (extrinsic) {
670
+ if (!polkadotSigner) {
671
+ throw new Error("Polkadot signer must be provided");
672
+ }
673
+ return sourcePolkadot.transfer(
674
+ sourceAddress,
675
+ extrinsic,
676
+ polkadotSigner
677
+ );
678
+ }
679
+ throw new Error("Either contract or extrinsic must be provided");
680
+ }
681
+ };
682
+ }
683
+
684
+ // src/sdk.ts
685
+ var DEFAULT_SERVICE = new ConfigService({ routes: xcmRoutesMap });
686
+ function Sdk({ configService, ecosystem } = {}) {
687
+ const service = configService ?? DEFAULT_SERVICE;
688
+ const assets = service.getEcosystemAssets(ecosystem);
689
+ return {
690
+ assets,
691
+ setAsset(asset) {
692
+ const sources = service.getSourceChains({ asset, ecosystem });
693
+ return {
694
+ sources,
695
+ setSource(source) {
696
+ const destinations = service.getDestinationChains({
697
+ asset,
698
+ source
699
+ });
700
+ return {
701
+ destinations,
702
+ setDestination(destination) {
703
+ const route = service.getAssetRoute({
704
+ asset,
705
+ source,
706
+ destination
707
+ });
708
+ return {
709
+ setAddresses({
710
+ sourceAddress,
711
+ destinationAddress
712
+ }) {
713
+ const sourceChain = service.getChain(source);
714
+ if (!EvmParachain2.isAnyParachain(sourceChain)) {
715
+ throw new Error(
716
+ "Source chain should be a Parachain or EvmParachain"
717
+ );
718
+ }
719
+ if (!EvmParachain2.isAnyParachain(route.destination.chain)) {
720
+ throw new Error(
721
+ "Destination chain should be a Parachain or EvmParachain"
722
+ );
723
+ }
724
+ return getTransferData({
725
+ route,
726
+ sourceAddress,
727
+ destinationAddress
728
+ });
729
+ }
730
+ };
731
+ }
732
+ };
733
+ }
734
+ };
735
+ }
736
+ };
737
+ }
738
+ async function getParachainBalances(chain, address, service = DEFAULT_SERVICE) {
739
+ const routes = service.getChainRoutes(chain).getRoutes();
740
+ const balances = await getAssetsBalances({
741
+ chain,
742
+ routes,
743
+ address
744
+ });
745
+ return balances;
746
+ }
747
+ export {
748
+ EvmService,
749
+ PolkadotService,
750
+ Sdk,
751
+ convertToChainDecimals,
752
+ getAssetMin,
753
+ getAssetsBalances,
754
+ getBalance,
755
+ getContractFee,
756
+ getDestinationData,
757
+ getDestinationFee,
758
+ getDestinationFeeBalance,
759
+ getExistentialDeposit,
760
+ getExtrinsicFee,
761
+ getFee,
762
+ getMax,
763
+ getMin,
764
+ getParachainBalances,
765
+ getSourceData,
766
+ validateSovereignAccountBalances
767
+ };
2
768
  //# sourceMappingURL=index.mjs.map