"use strict";var w=Object.defineProperty;var ct=Object.getOwnPropertyDescriptor;var ut=Object.getOwnPropertyNames;var mt=Object.prototype.hasOwnProperty;var lt=(e,t)=>{for(var r in t)w(e,r,{get:t[r],enumerable:!0})},ft=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of ut(t))!mt.call(e,i)&&i!==r&&w(e,i,{get:()=>t[i],enumerable:!(n=ct(t,i))||n.enumerable});return e};var gt=e=>ft(w({},"__esModule",{value:!0}),e);var Rt={};lt(Rt,{AssetMinBuilder:()=>dt,BalanceBuilder:()=>bt,BaseConfig:()=>l,CallType:()=>x,ContractBuilder:()=>yt,ContractConfig:()=>f,ExtrinsicBuilder:()=>Tt,ExtrinsicConfig:()=>a,FeeBuilder:()=>Nt,SubstrateQueryConfig:()=>u,XcmVersion:()=>p,calculateSystemAccountBalance:()=>v,evm:()=>U,substrate:()=>H});module.exports=gt(Rt);var Dr=require("@polkadot/api-augment");var N=require("@polkadot/types"),M=require("@polkadot/util"),R=require("@polkadot/util-crypto");var p=(i=>(i.v1="V1",i.v2="V2",i.v3="V3",i.v4="V4",i))(p||{});function A(e,t=0){if(!e)return"V1";let{type:r}=e.meta.args[t],n=e.meta.registry.createType(r.toString()),i=(0,N.getTypeDef)(n==null?void 0:n.toRawType());if(!i.sub)return"V1";let o=Array.isArray(i.sub)?i.sub.map(s=>s.name):[i.sub.name];if(o.includes("V1"))return"V1";if(o.includes("V2"))return"V2";if(o.includes("V3"))return"V3";throw new Error("Can't find Xcm version")}function c(e){return e.length===42&&e.startsWith("0x")?{AccountKey20:{key:e}}:{AccountId32:{id:(0,M.u8aToHex)((0,R.decodeAddress)(e)),network:null}}}var x=(r=>(r.Evm="Evm",r.Substrate="Substrate",r))(x||{});var l=class{module;func;type;constructor({module:t,func:r,type:n}){this.module=t,this.func=r,this.type=n}};var u=class extends l{args;transform;constructor({args:t=[],transform:r,type:n="Substrate",...i}){super({...i,type:n}),this.args=t,this.transform=r}};function dt(){return{assetRegistry:pt,assets:At,foreignAssets:xt}}function pt(){let e="assetRegistry";return{assetMetadatas:()=>({build:({asset:t})=>new u({module:e,func:"assetMetadatas",args:[t],transform:async r=>r.unwrapOrDefault().minimalBalance.toBigInt()})}),currencyMetadatas:()=>({build:({asset:t})=>new u({module:e,func:"currencyMetadatas",args:[t],transform:async r=>r.unwrapOrDefault().minimalBalance.toBigInt()})})}}function At(){return{asset:()=>({build:({asset:e})=>new u({module:"assets",func:"asset",args:[e],transform:async t=>t.unwrapOrDefault().minBalance.toBigInt()})})}}function xt(){return{asset:()=>({build:({asset:e})=>{let t={parents:2,interior:{X2:[{globalconsensus:{ethereum:{chainId:1}}},c(e)]}};return new u({module:"foreignAssets",func:"asset",args:[t],transform:async r=>r.unwrapOrDefault().minBalance.toBigInt()})}})}}var L=require("@polkadot/util"),V=require("@polkadot/util-crypto");var P=require("@moonbeam-network/xcm-utils"),g=require("@polkadot/util"),E=require("@polkadot/util-crypto");var f=class extends l{args;address;constructor({args:t,address:r,...n}){super({...n,type:"Evm"}),this.args=t,this.address=r}};var X=18446744073709551615n;function _(){return{transfer:(e=X)=>({build:({address:t,amount:r,asset:n,destination:i})=>new f({args:[(0,g.isString)(n)?(0,P.formatAssetIdToERC20)(n):n,r,W(t,i),e],func:"transfer",module:"Xtokens"})}),transferMultiCurrencies:(e=X)=>({build:({address:t,amount:r,asset:n,destination:i,fee:o,feeAsset:s})=>new f({args:[[[(0,g.isString)(n)?(0,P.formatAssetIdToERC20)(n):n,r],[(0,g.isString)(s)?(0,P.formatAssetIdToERC20)(s):s,o]],1,W(t,i),e],func:"transferMultiCurrencies",module:"Xtokens"})}),transferWithEvmTo32:(e=X)=>({build:({address:t,amount:r,asset:n,destination:i})=>{let o=Ct(t,i);return new f({args:[(0,g.isString)(n)?(0,P.formatAssetIdToERC20)(n):n,r,o,e],func:"transfer",module:"Xtokens"})}})}}function Ct(e,t){let r="01",n=(0,E.evmToAddress)(e),i=`0x${r}${(0,g.u8aToHex)((0,E.decodeAddress)(n),-1,!1)}00`;return[1,t.parachainId?[`0x0000000${t.parachainId.toString(16)}`,i]:[i]]}function W(e,t){let n=`0x${t.isEvmParachain()?"03":"01"}${(0,g.u8aToHex)((0,E.decodeAddress)(e),-1,!1)}00`;return[1,t.parachainId?[`0x0000000${t.parachainId.toString(16)}`,n]:[n]]}function yt(){return{Xtokens:_}}function bt(){return{evm:U,substrate:H}}function U(){return{erc20:ht}}function H(){return{assets:Pt,foreignAssets:Et,system:Bt,tokens:It}}function ht(){return{build:({address:e,asset:t})=>{if(!t||!(0,L.isString)(t))throw new Error(`Invalid contract address: ${t}`);return new f({address:t,args:[e],func:"balanceOf",module:"Erc20"})}}}function Pt(){return{account:()=>({build:({address:e,asset:t})=>new u({module:"assets",func:"account",args:[t,e],transform:async r=>r.unwrapOrDefault().balance.toBigInt()})})}}function Et(){return{account:()=>({build:({address:e,asset:t})=>{let r={parents:2,interior:{X2:[{GlobalConsensus:{ethereum:{chainId:1}}},c(t)]}};return new u({module:"foreignAssets",func:"account",args:[r,e],transform:async n=>n.unwrapOrDefault().balance.toBigInt()})}})}}function Bt(){return{account:()=>({build:({address:e})=>new u({module:"system",func:"account",args:[e],transform:async t=>v(t)})}),accountEquilibrium:()=>({build:({address:e,asset:t})=>new u({module:"system",func:"account",args:[e],transform:async r=>{var s;if(r.data.isEmpty)return 0n;let n=r.data.toJSON(),i;if(Array.isArray(n)&&(i=n),Array.isArray((s=n==null?void 0:n.v0)==null?void 0:s.balance)&&(i=n.v0.balance),!i)throw new Error("Can't get balance from Equilibrium chain");let o=i.find(([m])=>m===t);return o?BigInt(o[1].positive):0n}})}),accountEvmTo32:()=>({build:({address:e})=>{let t=(0,V.evmToAddress)(e);return new u({module:"system",func:"account",args:[t],transform:async r=>v(r)})}})}}function It(){return{accounts:()=>({build:({address:e,asset:t})=>new u({module:"tokens",func:"accounts",args:[e,t],transform:async({free:r,frozen:n})=>BigInt(r.sub(n).toString())})})}}async function v(e){let t=e.data,r=BigInt(t.free.toString()),n=t.miscFrozen??t.frozen,i=BigInt(n.sub(t.reserved).toString()),o=i<0n?0n:i;return r-o}var a=class extends l{getArgs;constructor({getArgs:t,...r}){super({...r,type:"Substrate"}),this.getArgs=t}};var j="eqBalances";function q(){return{xcmTransfer:()=>({build:({address:e,amount:t,asset:r,destination:n})=>new a({module:j,func:"xcmTransfer",getArgs:()=>[r,t,{parents:1,interior:{X2:[{Parachain:n.parachainId},c(e)]}},"ThisAccWillPay"]})}),transferXcm:()=>({build:({address:e,amount:t,asset:r,destination:n,fee:i,feeAsset:o})=>new a({module:j,func:"transferXcm",getArgs:()=>{let s=t-i>0n?t-i:0n;return[[r,r===o?s:t],[o,i],{parents:1,interior:{X2:[{Parachain:n.parachainId},c(e)]}}]}})})}}function d({asset:e,address:t,destination:r,func:n,parents:i=1,feeIndex:o=0}){let s=A(n);return[{[s]:{parents:i,interior:{X1:{Parachain:r.parachainId}}}},{[s]:{parents:0,interior:{X1:c(t)}}},{[s]:e},o,"Unlimited"]}function z({asset:e,feeAsset:t}){let r=Number(e),n=Number(t);return Number.isNaN(r)||Number.isNaN(n)?!1:r>n}var C="polkadotXcm";function $(){return{limitedReserveTransferAssets:()=>{let e="limitedReserveTransferAssets";return{here:()=>({build:t=>new a({module:C,func:e,getArgs:r=>d({...t,func:r,asset:[{id:{Concrete:{parents:0,interior:"Here"}},fun:{Fungible:t.amount}}]})})}),X1:()=>({build:t=>new a({module:C,func:e,getArgs:r=>d({...t,func:r,asset:[{id:{Concrete:{parents:0,interior:{X1:{PalletInstance:t.palletInstance}}}},fun:{Fungible:t.amount}}]})})}),X2:()=>({build:t=>new a({module:C,func:e,getArgs:r=>{let n=!!t.feeAsset&&t.asset!==t.feeAsset,i=[{id:{Concrete:{parents:0,interior:{X2:[{PalletInstance:t.palletInstance},{GeneralIndex:t.asset}]}}},fun:{Fungible:t.amount}}],o=z(t);if(n){let s={id:{Concrete:{parents:0,interior:{X2:[{PalletInstance:t.palletInstance},{GeneralIndex:t.feeAsset}]}}},fun:{Fungible:t.fee}};o?i.unshift(s):i.push(s)}return d({...t,func:r,asset:i,feeIndex:n&&!o?1:0})}})})}},limitedReserveWithdrawAssets:()=>{let e="limitedReserveWithdrawAssets";return{X2:()=>({build:t=>new a({module:C,func:e,getArgs:r=>d({...t,func:r,asset:[{id:{Concrete:{parents:1,interior:{X2:[{Parachain:t.destination.parachainId},{PalletInstance:t.palletInstance}]}}},fun:{Fungible:t.amount}}]})})})}},trasferAssets:()=>{let e="transferAssets";return{here:()=>({build:t=>new a({module:C,func:e,getArgs:r=>d({...t,func:r,asset:[{id:{Concrete:{parents:1,interior:"Here"}},fun:{Fungible:t.amount}}]})})})}},transferAssetsUsingTypeAndThen:()=>{let e="transferAssetsUsingTypeAndThen";return{globalConcensusEthereum:()=>({build:({destination:t,address:r,amount:n,asset:i})=>{let o="V4";return new a({module:C,func:e,getArgs:()=>[{[o]:{parents:1,interior:{X1:[{Parachain:t.parachainId}]}}},{[o]:[{id:{parents:2,interior:{X2:[{globalConsensus:{Ethereum:{ChainId:1}}},c(i)]}},fun:{fungible:n}}]},"LocalReserve",{[o]:{parents:2,interior:{X2:[{globalConsensus:{Ethereum:{ChainId:1}}},c(i)]}}},"LocalReserve",{[o]:[{DepositAsset:{assets:{Wild:{AllCounted:1}},beneficiary:{parents:0,interior:{X1:[c(r)]}}}}]},"Unlimited"]})}})}}}}function y(){return"Unlimited"}function b(e,t,r){return{[e]:{parents:1,interior:{X2:[{Parachain:r.parachainId},c(t)]}}}}var B="xTokens";function G(){return{transfer:()=>({build:({address:e,amount:t,asset:r,destination:n})=>new a({module:B,func:"transfer",getArgs:i=>{let o=A(i,2);return[r,t,b(o,e,n),y()]}})}),transferMultiAsset:e=>{let t="transferMultiasset";return{here:()=>({build:({address:r,amount:n,destination:i})=>new a({module:B,func:t,getArgs:o=>{let s=A(o,1);return[{[s]:{id:{Concrete:{parents:0,interior:"Here"}},fun:{Fungible:n}}},b(s,r,i),y()]}})}),X1:()=>({build:({address:r,amount:n,destination:i})=>new a({module:B,func:t,getArgs:o=>{let s=A(o,1);return[{[s]:{id:{Concrete:{parents:1,interior:{X1:{Parachain:e}}}},fun:{Fungible:n}}},b(s,r,i),y()]}})}),X2:()=>({build:({address:r,amount:n,asset:i,destination:o})=>new a({module:B,func:t,getArgs:s=>{let m=A(s,1);return[{[m]:{id:{Concrete:{parents:1,interior:{X2:[{Parachain:e},{GeneralKey:i}]}}},fun:{Fungible:n}}},b(m,r,o),y()]}})})}},transferMultiCurrencies:()=>({build:({address:e,amount:t,asset:r,destination:n,fee:i,feeAsset:o})=>new a({module:B,func:"transferMulticurrencies",getArgs:()=>[[[r,t],[o,i]],1,b("V3",e,n),y()]})})}}var K="xTransfer";function J(){return{transfer:()=>{let e="transfer";return{here:()=>({build:({address:t,amount:r,destination:n})=>new a({module:K,func:e,getArgs:()=>[{id:{Concrete:{parents:0,interior:"Here"}},fun:{Fungible:r}},{parents:1,interior:{X2:[{Parachain:n.parachainId},c(t)]}},{refTime:5e9,proofSize:0}]})}),X2:()=>({build:({address:t,amount:r,destination:n,palletInstance:i})=>new a({module:K,func:e,getArgs:()=>[{id:{Concrete:{parents:1,interior:{X2:[{Parachain:n.parachainId},{PalletInstance:i}]}}},fun:{Fungible:r||1n}},{parents:1,interior:{X2:[{Parachain:n.parachainId},c(t)]}},{refTime:5e9,proofSize:0}]})})}}}}var Q="xcmPallet";function Y(){return{limitedReserveTransferAssets:(e=1)=>{let t="limitedReserveTransferAssets";return{here:()=>({build:r=>new a({module:Q,func:t,getArgs:n=>d({...r,parents:e,func:n,asset:[{id:{Concrete:{parents:0,interior:"Here"}},fun:{Fungible:r.amount}}]})})})}},transferAssetsUsingTypeAndThen:()=>{let e="transferAssetsUsingTypeAndThen";return{here:()=>({build:t=>new a({module:Q,func:e,getArgs:()=>{let r="V4";return[{[r]:{parents:0,interior:{X1:[{Parachain:t.destination.parachainId}]}}},{[r]:[{id:{parents:0,interior:"Here"},fun:{Fungible:t.amount}}]},"LocalReserve",{[r]:{parents:0,interior:"Here"}},"LocalReserve",{[r]:[{DepositAsset:{assets:{Wild:{AllCounted:1}},beneficiary:{parents:0,interior:{X1:[c(t.address)]}}}}]},"Unlimited"]}})})}}}}function Tt(){return{eqBalances:q,xTokens:G,xTransfer:J,polkadotXcm:$,xcmPallet:Y}}var I=class{api;call;type="Substrate";constructor({api:t,call:r}){this.api=t,this.call=r}};var Z=require("@moonbeam-network/xcm-utils");var F=10**6,wt="0xabcdef1234567890fedcba0987654321abcdef1234567890fedcba0987654321",Xt="0x0000000000000000000000000000000000000802",vt={moonbeam:"10",moonriver:"10","moonbase-alpha":"3"},St={moonbeam:"110",moonriver:"110","moonbase-alpha":"48"},S="V4";function h(){return S==="V4"}function Ft(e){if(!h())return e;let t={...e};return t.interior&&typeof t.interior=="object"&&"x1"in t.interior&&(Array.isArray(t.interior.x1)||(t.interior.x1=[t.interior.x1])),t}function tt(e){return{WithdrawAsset:e.map(t=>({id:{...t},fun:{Fungible:F}}))}}function et(e){return{ReserveAssetDeposited:e.map(t=>({id:{...t},fun:{Fungible:F}}))}}function rt(){return{ClearOrigin:"NULL"}}function nt(e){return{BuyExecution:{fees:{id:{...e},fun:{Fungible:F}},weight_limit:{Unlimited:"NULL"}}}}function it(e,t){let r={AccountKey20:{key:e,network:null}};return{DepositAsset:{assets:{Wild:{AllCounted:t.length}},beneficiary:{interior:{X1:h()?[r]:r},parents:0},max_assets:0}}}function ot(){return{SetTopic:wt}}function O(e){return{Concrete:{...e}}}function Ot(e){let t={PalletInstance:vt[e]},r={interior:{X1:h()?[t]:t},parents:"0"};return h()?r:O(r)}function Dt(e,t){let r={interior:{X2:[{PalletInstance:St[t]},{AccountKey20:{key:e,network:null}}]},parents:"0"};return h()?r:O(r)}async function kt(e,t){let r=await e.query.assetManager.assetIdType(t);if(r.isNone||!r.unwrap().isXcm)throw new Error(`No asset type found for asset ${t}`);return r}async function D(e,t,r){if(t===Xt)return Ot(r.key);if((0,Z.isHexString)(t))return Dt(t,r.key);let i=(await kt(e,t)).unwrap().asXcm.toJSON(),o=Ft(i);return h()?o:O(o)}async function st(e,t,r){let n=await e.call.xcmPaymentApi.queryXcmWeight({[S]:t});if(!n.isOk)throw new Error("There was an error trying to get the weight for the xcm instructions (queryXcmWeight)");let i=n.asOk,o=await e.call.xcmPaymentApi.queryWeightToAssetFee(i,{[S]:{...r}});if(!o.isOk)throw new Error("There was an error trying to get the fee with the weight and asset (weightToForeingAssets)");return o.asOk.toBigInt()}function Nt(){return{xcmPaymentApi:Mt}}function Mt(){return{xcmPaymentFee:({isAssetReserveChain:e,shouldTransferAssetPrecedeAsset:t=!1})=>({build:({address:r,api:n,feeAsset:i,chain:o,transferAsset:s})=>new I({api:n,call:async()=>{let m=await D(n,i,o),k=await D(n,s,o),T=i===s?[m]:t?[k,m]:[m,k],at=[e?tt(T):et(T),rt(),nt(m),it(r,T),ot()];return st(n,at,m)}})})}}0&&(module.exports={AssetMinBuilder,BalanceBuilder,BaseConfig,CallType,ContractBuilder,ContractConfig,ExtrinsicBuilder,ExtrinsicConfig,FeeBuilder,SubstrateQueryConfig,XcmVersion,calculateSystemAccountBalance,evm,substrate}); //# sourceMappingURL=index.cjs.map