@moonbeam-network/xcm-sdk 2.7.10 → 3.0.1
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/LICENSE +1 -1
- package/README.md +37 -41
- package/build/index.d.ts +168 -301
- package/build/index.mjs +767 -1
- package/build/index.mjs.map +1 -1
- package/package.json +26 -34
- package/build/index.cjs +0 -2
- package/build/index.cjs.map +0 -1
- package/build/index.d.cts +0 -177
package/build/index.mjs
CHANGED
|
@@ -1,2 +1,768 @@
|
|
|
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 Et}from"@moonbeam-network/xcm-utils";import tt from"big.js";import{Contract as pt}from"ethers";import{createPublicClient as gt,getContract as yt,http as ht}from"viem";function B(n){return"provider"in n}function C(n){return!("abi"in n)}var A=[{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=B(e)?new pt(this.address,A,e):yt({abi:A,address:this.address,client:{public:gt({chain:e.chain,transport:ht()}),wallet: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 At}from"ethers";import{createPublicClient as J,getContract as bt,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=B(e)?new At(this.address,O,e):bt({abi:O,address:this.address,client:{public:J({chain:e.chain,transport:U()}),wallet: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 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=C(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(B(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 Pt,http as St}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=Pt({chain:e,transport:St()})}async getBalance(){return await this.#e.readContract({abi:A,address:this.#t.address,args:this.#t.args,functionName:"balanceOf"})}async getDecimals(){return await this.#e.readContract({abi:A,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 b(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):b(n,e)}import{CallType as Y}from"@moonbeam-network/xcm-builder";import{convertDecimals as vt,toBigInt as Bt}from"@moonbeam-network/xcm-utils";async function y({address:n,chain:t,balanceBuilder:e,asset:a,decimals:s,polkadot:o}){let i=e.build({address:n,asset:o.chain.getBalanceAssetId(a)});if(i.type===Y.Substrate){let r=await o.query(i);return t.usesChainDecimals?vt(r,o.decimals,s):r}return await b(i,t).getBalance()}async function h({address:n,asset:t,config:e,polkadot:a,chain:s,assetBuiltConfig:o}){let i=o||e.balance.build({address:n,asset:a.chain.getBalanceAssetId(t||e.asset)});return i.type===Y.Substrate?a.getAssetDecimals(t||e.asset):b(i,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 Bt(e,a)}return 0n}function Z({amount:n,source:t,destination:e}){if(!e.chain.checkSovereignAccountBalances||!e.sovereignAccountBalances)return;let{feeAssetBalance:a,transferAssetBalance:s}=e.sovereignAccountBalances;if(n>s)throw new Error(`${t.chain.name} Sovereign account in ${e.chain.name} does not have enough balance for this transaction`);if(a&&t.destinationFee.amount>a)throw new Error(`${t.chain.name} Sovereign account in ${e.chain.name} does not have enough balance to pay for fees for this transaction`)}async function at({transferConfig:n,destinationAddress:t,destinationFee:e,polkadot:a,sourceAddress:s,evmSigner:o}){var _,V,z,K,j,L,H;let{asset:i,destination:m,source:{chain:c,config:r}}=n,u=k.fromAsset(i,{amount:0n,decimals:await h({address:t,chain:c,config:r,polkadot:a})}),f=a.chain.getBalanceAssetId(((_=r==null?void 0:r.fee)==null?void 0:_.asset)||r.asset),p=(V=r.fee)==null?void 0:V.balance.build({address:t,asset:f}),d=(z=r.fee)!=null&&z.asset?await h({address:t,asset:r.fee.asset,assetBuiltConfig:p,chain:c,config:r,polkadot:a}):void 0,l=(K=r.fee)!=null&&K.asset?k.fromAsset(r.fee.asset,{amount:0n,decimals:d||0}):u,w=(j=r.destinationFee)!=null&&j.asset?k.fromAsset(r.destinationFee.asset,{amount:0n,decimals:await h({address:t,asset:r.destinationFee.asset,chain:c,config:r,polkadot:a})}):u,g=await y({address:s,asset:r.asset,balanceBuilder:r.balance,chain:c,decimals:u.decimals,polkadot:a}),D=await et({address:s,balance:g,chain:c,decimals:l.decimals,feeConfig:r.fee,polkadot:a}),E=r.asset.isEqual(r.destinationFee.asset)?g:await et({address:s,balance:g,chain:c,decimals:w.decimals,feeConfig:r.destinationFee,polkadot:a}),I=await G(r,a),v=(L=r.extrinsic)==null?void 0:L.build({address:t,amount:g,asset:c.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(i),source:c}),mt=(H=r.contract)==null?void 0:H.build({address:t,amount:g,asset:c.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),q=w.copyWith({amount:E}),ut=await It({balance:g,chain:c,contract:mt,decimals:l.decimals,destinationFeeBalanceAmount:q,destinationFeeConfig:r.destinationFee,evmSigner:o,extrinsic:v,feeConfig:r.fee,polkadot:a,sourceAddress:s}),$=u.copyWith({amount:g}),{existentialDeposit:N}=a,Q=l.copyWith({amount:ut}),lt=l.copyWith({amount:D}),X=u.copyWith({amount:I}),ft=Gt({balanceAmount:$,existentialDeposit:N,feeAmount:Q,minAmount:X});return{balance:$,chain:c,destinationFee:e,destinationFeeBalance:q,existentialDeposit:N,fee:Q,feeBalance:lt,max:ft,min:X}}async function et({address:n,balance:t,chain:e,decimals:a,feeConfig:s,polkadot:o}){if(!s)return t;let i=s.balance.build({address:n,asset:o.chain.getBalanceAssetId(s.asset)});if(i.type===xt.Evm){let c=b(i,e),r=await c.getDecimals(),u=await c.getBalance();return e.usesChainDecimals?W(u,o.decimals,r):u}let m=await o.query(i);return e.usesChainDecimals?W(m,o.decimals,a):m}async function It({balance:n,chain:t,contract:e,decimals:a,destinationFeeConfig:s,destinationFeeBalanceAmount:o,evmSigner:i,extrinsic:m,feeConfig:c,polkadot:r,sourceAddress:u}){if(e){if(!i)throw new Error("EVM Signer must be provided");if(s&&o&&typeof s.amount=="number"){let f=Number(Et(o.amount,o.decimals));if(f&&s.amount>f)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 Tt({balance:n,chain:t,config:e,decimals:a,evmSigner:i})}if(m){let f=await Ft(n,m,r,u),p=Mt(a,c),d=f+p;return t.usesChainDecimals?W(d,r.decimals,a):d}throw new Error("Either contract or extrinsic must be provided")}async function Tt({balance:n,config:t,decimals:e,evmSigner:a,chain:s}){let i=await M(t,a,s).getFee(n);return W(i,18,e)}async function Ft(n,t,e,a){try{return await e.getFee(a,t)}catch(s){if(n)throw s;return 0n}}function Mt(n,t){return t!=null&&t.xcmDeliveryFeeAmount?Dt(t.xcmDeliveryFeeAmount,n):0n}function Gt({balanceAmount:n,existentialDeposit:t,feeAmount:e,minAmount:a}){let s=n.toBig().minus(a.toBig()).minus(n.isSame(t)?t.toBig():tt(0)).minus(n.isSame(e)?e.toBig():tt(0));return n.copyWith({amount:s.lt(0)?0n:BigInt(s.toFixed())})}async function nt({address:n,chain:t,assets:e,polkadot:a}){let s=e.reduce((i,m)=>i.some(c=>c.asset.isEqual(m.asset))?i:[m,...i],[]);return await Promise.all(s.map(async i=>{let m=await h({address:n,asset:i.asset,chain:t,config:i,polkadot:a}),c=await y({address:n,asset:i.asset,balanceBuilder:i.balance,chain:t,decimals:m,polkadot:a});return k.fromAsset(i.asset,{amount:c,decimals:m})}))}import{ConfigBuilder as ot,ConfigService as ct}from"@moonbeam-network/xcm-config";import{convertDecimals as Nt,toBigInt as it}from"@moonbeam-network/xcm-utils";import S from"big.js";import"@polkadot/api-augment";import{AssetAmount as kt}from"@moonbeam-network/xcm-types";import{getPolkadotApi as Wt}from"@moonbeam-network/xcm-utils";var P=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 Wt(t.ws),t,e)}static async createMulti(t,e){return Promise.all(t.map(a=>n.create(a,e)))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}get asset(){let t=this.api.registry.chainTokens.at(0),e=t==null?void 0:t.toString().toLowerCase();if(!e)throw new Error("No native symbol key found");let a=this.configService.getAsset(e);if(!a)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return a}get existentialDeposit(){var s,o;let t=(s=this.api.consts.balances)==null?void 0:s.existentialDeposit,e=(o=this.api.consts.eqBalances)==null?void 0:o.existentialDeposit,a=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return kt.fromAsset(this.asset,{amount:a,decimals:this.decimals})}async getAssetMeta(t){var i,m,c,r,u,f;let e=((i=this.api.query.assets)==null?void 0:i.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.assets)||((c=this.api.query.assetRegistry)==null?void 0:c.metadata)||((r=this.api.query.assetRegistry)==null?void 0:r.currencyMetadatas)||((u=this.api.query.assetRegistry)==null?void 0:u.assetMetadatas)||((f=this.api.query.assetRegistry)==null?void 0:f.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],o=e.getArgs(s);return(await s(...o).signAndSend(this.#t(a)?t:a,{nonce:-1,signer:this.#t(a)?a:void 0,withSignedTransaction:!0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as st}from"@moonbeam-network/xcm-types";import{getSovereignAccountAddresses as Rt,toBigInt as Ot}from"@moonbeam-network/xcm-utils";async function rt({transferConfig:n,destinationAddress:t,polkadot:e}){let{asset:a,destination:{chain:s,config:o}}=n,i=st.fromAsset(a,{amount:0n,decimals:await h({address:t,chain:s,config:o,polkadot:e})}),m=await y({address:t,asset:o.asset,balanceBuilder:o.balance,chain:s,decimals:i.decimals,polkadot:e}),c=await G(o,e),r=i.copyWith({amount:m}),{existentialDeposit:u}=e,f=await qt({address:t,config:n,polkadot:e}),p=i.copyWith({amount:c});return{balance:r,chain:s,existentialDeposit:u,fee:f,min:p,sovereignAccountBalances:s.checkSovereignAccountBalances?await $t({decimals:i.decimals,polkadot:e,transferConfig:n}):void 0}}async function qt({address:n,config:t,polkadot:e}){let{amount:a,asset:s}=t.source.config.destinationFee,o=await e.getAssetDecimals(s),i=st.fromAsset(s,{amount:0n,decimals:o});if(Number.isFinite(a))return i.copyWith({amount:Ot(a,o)});let m=a.build({address:n,api:e.api,chain:e.chain,feeAsset:e.chain.getRegisteredAssetIdOrAddress(s),transferAsset:e.chain.getRegisteredAssetIdOrAddress(t.asset)});return i.copyWith({amount:await m.call()})}async function $t({transferConfig:n,decimals:t,polkadot:e}){var f;let{destination:{chain:a,config:s},source:{config:o}}=n,i=Rt(n.source.chain.parachainId),m=(f=o.destinationFee)==null?void 0:f.destinationBalance,c=a.isRelay?i.relay:i.generic,r=await y({address:c,asset:s.asset,balanceBuilder:s.balance,chain:a,decimals:t,polkadot:e});return{feeAssetBalance:m?await y({address:c,asset:o.destinationFee.asset,balanceBuilder:m,chain:a,decimals:t,polkadot:e}):void 0,transferAssetBalance:r}}async function R({configService:n,destinationAddress:t,evmSigner:e,polkadotSigner:a,sourceAddress:s,transferConfig:o}){let[i,m]=await P.createMulti([o.destination.chain,o.source.chain],n),c=await rt({destinationAddress:t,polkadot:i,transferConfig:o}),r=await Xt(m,c.fee),u=await at({destinationAddress:t,destinationFee:r,evmSigner:e,polkadot:m,sourceAddress:s,transferConfig:o});return{destination:c,getEstimate(f){let d=S(it(f,u.balance.decimals).toString()).minus(u.balance.isSame(r)?r.toBig():S(0));return u.balance.copyWith({amount:d.lt(0)?0n:BigInt(d.toFixed())})},isSwapPossible:!0,max:u.max,min:Qt(c),source:u,async swap(){return R({configService:n,destinationAddress:s,evmSigner:e,polkadotSigner:a,sourceAddress:t,transferConfig:{...o,destination:o.source,source:o.destination}})},async transfer(f){var E,I;let p=it(f,u.balance.decimals);Z({amount:p,destination:c,source:u});let{asset:d,source:{chain:l,config:w}}=o,g=(E=w.contract)==null?void 0:E.build({address:t,amount:p,asset:l.getAssetId(d),destination:c.chain,fee:r.amount,feeAsset:l.getAssetId(r)}),D=(I=w.extrinsic)==null?void 0:I.build({address:t,amount:p,asset:l.getAssetId(d),destination:c.chain,fee:r.amount,feeAsset:l.getAssetId(r),palletInstance:l.getAssetPalletInstance(d),source:l});if(g){if(!e)throw new Error("EVM Signer must be provided");return M(g,e,l).transfer().then(v=>typeof v=="object"?v.hash:v)}if(D){if(!a)throw new Error("Polkadot signer must be provided");return m.transfer(s,D,a)}throw new Error("Either contract or extrinsic must be provided")}}}function Qt({balance:n,existentialDeposit:t,fee:e,min:a}){let s=S(0).plus(n.isSame(e)?e.toBig():S(0)).plus(n.isSame(t)&&n.toBig().lt(t.toBig())?t.toBig():S(0)).plus(n.toBig().lt(a.toBig())?a.toBig():S(0));return n.copyWith({amount:BigInt(s.toFixed())})}async function Xt(n,t){let e=await n.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:Nt(t.amount,t.decimals,e),decimals:e})}function va(n){let t=(n==null?void 0:n.configService)??new ct;return{assets(e){let{assets:a,asset:s}=ot(t).assets(e);return{assets:a,asset(o){let{sourceChains:i,source:m}=s(o);return{sourceChains:i,source(c){let{destinationChains:r,destination:u}=m(c);return{destinationChains:r,destination(f){return{async accounts(p,d,l){return R({...n,configService:t,destinationAddress:d,evmSigner:(l==null?void 0:l.evmSigner)??(l==null?void 0:l.ethersSigner),sourceAddress:p,transferConfig:u(f).build(),polkadotSigner:l==null?void 0:l.polkadotSigner})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:a,ethersSigner:s,evmSigner:o,keyOrAsset:i,polkadotSigner:m,sourceAddress:c,sourceKeyOrChain:r}){return R({configService:t,destinationAddress:e,evmSigner:o??s,polkadotSigner:m,sourceAddress:c,transferConfig:ot(t).assets().asset(i).source(r).destination(a).build()})}}}async function Ba(n,t){let e=new ct,s=e.getChainConfig(n).getAssetsConfigs(),o=await P.create(n,e);return await nt({chain:n,assets:s,address:t,polkadot:o})}export{va as Sdk,nt as getAssetsBalances,y as getBalance,Tt as getContractFee,h as getDecimals,Ft as getExtrinsicFee,It as getFee,et as getFeeBalance,Gt as getMax,G as getMin,Ba as getParachainBalances,at as getSourceData,Z as validateSovereignAccountBalances};
|
|
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
|