@galacticcouncil/common 0.5.1 → 0.5.2-pr302-7a922f2
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.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var ce=Object.create;var w=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var fe=Object.getOwnPropertyNames;var le=Object.getPrototypeOf,de=Object.prototype.hasOwnProperty;var l=(e,t)=>{for(var r in t)w(e,r,{get:t[r],enumerable:!0})},q=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of fe(t))!de.call(e,o)&&o!==r&&w(e,o,{get:()=>t[o],enumerable:!(n=ue(t,o))||n.enumerable});return e};var pe=(e,t,r)=>(r=e!=null?ce(le(e)):{},q(t||!e||!e.__esModule?w(r,"default",{value:e,enumerable:!0}):r,e)),ge=e=>q(w({},"__esModule",{value:!0}),e);var Re={};l(Re,{HYDRATION_SS58_PREFIX:()=>S,RUNTIME_DECIMALS:()=>ke,SubstrateApis:()=>j,acc:()=>R,big:()=>H,blockProbe$:()=>X,encodeAssetId:()=>G,encodeLocation:()=>F,enums:()=>k,erc20:()=>L,h160:()=>K,hex:()=>N,log:()=>D,meta:()=>T,transform:()=>u,xc:()=>$,xcm:()=>W});module.exports=ge(Re);var ke=18,S=63;var R={};l(R,{getMultilocationDerivatedAccount:()=>me,getSovereignAccounts:()=>he});var d=require("@polkadot-api/substrate-bindings"),x=require("@polkadot-api/utils");function me(e,t,r,n=!1){let o=t.length===42,i=o?"AccountKey20":"AccountId32",s;if(o)s=(0,x.fromHex)(t.slice(2));else{let p=(0,d.getSs58AddressInfo)(t);if(!p.isValid)throw new Error("Invalid SS58 address");s=p.publicKey}let c="SiblingChain";r==0&&e?c="ChildChain":r==1&&!e&&(c="ParentChain");let f=d.compactNumber.enc,h=new Uint8Array([...new TextEncoder().encode(c),...e?f(e):[],...f(i.length+(o?20:32)),...new TextEncoder().encode(i),...s]),v=(0,d.Blake2256)(h);return n?(0,x.toHex)(v.slice(0,20)):(0,d.AccountId)().dec(v)}function he(e){let t=d.u32.enc(e),r=new TextEncoder().encode("para"),n=new TextEncoder().encode("sibl"),o=(0,x.toHex)(new Uint8Array([...r,...t])).padEnd(66,"0"),i=(0,x.toHex)(new Uint8Array([...n,...t])).padEnd(66,"0"),s=i.slice(0,42);return{generic:i,moonbeam:s,relay:o}}var H={};l(H,{asBigInt:()=>be,convertDecimals:()=>ye,pow10:()=>xe,toBigInt:()=>Y,toDecimal:()=>z});var b=pe(require("big.js"));b.default.NE=-18;function z(e,t,r){let n=(0,b.default)(e.toString().replace(/[^0-9]/g,"")),o=(0,b.default)(10).pow(t);return n.div(o).round(t,r).toString()}function Y(e,t){if(typeof e=="bigint")return e;let r=(0,b.default)(10).pow(t),n=(0,b.default)(e).mul(r);return BigInt(n.toFixed(0,b.default.roundDown))}function be(e){return BigInt(e.round(0,b.default.roundDown).toFixed(0))}function ye(e,t,r){let n=z(e,t);return Y(n.toString(),r)}function xe(e){if(e<0)throw new Error(`pow10: negative exp ${e}`);return 10n**BigInt(e)}var k={};l(k,{enumPath:()=>Ce});function Ce(e){let t=[],r=e;for(;r&&typeof r=="object"&&"type"in r;)t.push(r.type),r=r.value;return t.join(".")}var L={};l(L,{ERC20:()=>_});var _=class{static fromAssetId(t){let r=Buffer.alloc(20,0);return r[15]=1,r.writeUInt32BE(t,16),"0x"+r.toString("hex")}static toAssetId(t){let r=Buffer.from(t.replace("0x",""),"hex");return r.length!==20||!this.isAssetAddress(t)?null:r.readUInt32BE(16)}static isAssetAddress(t){let r=Buffer.from("0000000000000000000000000000000100000000","hex"),n=Buffer.from(t.replace("0x",""),"hex");return n.length!==20?!1:n.subarray(0,16).equals(r.subarray(0,16))}};var K={};l(K,{H160:()=>U,isEvmAccount:()=>J,isEvmAddress:()=>Q,isSs58Address:()=>Z});var C=require("polkadot-api"),M=require("@polkadot-api/utils"),g=require("buffer");var O="ETH\0";function J(e){if(!e)return!1;try{let t=(0,C.AccountId)().enc(e),r=g.Buffer.from(O);return g.Buffer.from(t.subarray(0,r.length)).equals(r)}catch{return!1}}function Q(e){return!!/^0x[a-fA-F0-9]{40}$/.test(e)}function Z(e){try{return(0,C.AccountId)(63).enc(e),!0}catch{return!1}}var U=class e{static toAccount=t=>{let r=g.Buffer.from(t.slice(2),"hex"),n=g.Buffer.from(O),o=Uint8Array.from(g.Buffer.concat([n,r,g.Buffer.alloc(8)])),i=(0,M.toHex)(o);return(0,C.AccountId)(63).dec(i)};static fromAccount=t=>{let r=(0,C.AccountId)().enc(t),n=g.Buffer.from(O),o=r.slice(n.length,-8);return"0x"+g.Buffer.from(o).toString("hex")};static fromSS58=t=>{let n=(0,C.AccountId)().enc(t).slice(0,20);return(0,M.toHex)(n)};static fromAny=t=>{if(Q(t))return t;if(J(t))return e.fromAccount(t);if(Z(t))return e.fromSS58(t);throw new Error("Unknown address type")}};var N={};l(N,{assertHexLen:()=>Ee,ensureHexPrefix:()=>te,hexNormalize:()=>Pe,isHex:()=>ee,stripHexPrefix:()=>Ae});var ve=/^0x[0-9a-fA-F]*$/;function ee(e,t=!1){return e==="0x"?!0:!ve.test(e)||!t&&e.length<4?!1:t||e.length%2===0}function Ae(e){return e.startsWith("0x")?e.slice(2):e}function te(e){return e.startsWith("0x")?e:`0x${e}`}function Pe(e){return te(e.toLowerCase())}function Ee(e,t){if(!ee(e)||e.length-2!==t*2)throw new Error(`Expected 0x hex of ${t} bytes`)}var D={};l(D,{logger:()=>Be});var B={all:0,trace:1,debug:2,info:3,warn:4,error:5,silent:6};function A(e){return e.padEnd(11)}var P={TRACE:A("[GC][TRACE]"),DEBUG:A("[GC][DEBUG]"),INFO:A("[GC][INFO]"),WARN:A("[GC][WARN]"),ERROR:A("[GC][ERROR]")};function we(){if(typeof window<"u")try{let e=window.localStorage.getItem("gc.log");if(e&&e in B)return e}catch{}if(typeof process<"u"){let e=process.env.GC_LOG;if(e&&e in B)return e}return"silent"}function E(e){let t=we();return B[e]>=B[t]}var Be={trace(...e){E("trace")&&console.debug(P.TRACE,...e)},debug(...e){E("debug")&&console.debug(P.DEBUG,...e)},info(...e){E("info")&&console.info(P.INFO,...e)},warn(...e){E("warn")&&console.warn(P.WARN,...e)},error(...e){if(!E("error"))return;e.some(r=>r instanceof Error)?console.error(P.ERROR,...e):this.warn(...e)}};var T={};l(T,{ErrorDecoder:()=>Ie});var I=require("@polkadot-api/substrate-bindings");function Ie(e){let{metadata:t}=I.metadata.dec(e),{pallets:r,lookup:n}=t.value,o=new Map(r.map(s=>[s.index,s])),i=new Map(n.map(s=>[s.id,s]));return{decode(s,c){let f=o.get(s);if(!f?.errors)return;let h=i.get(f.errors);if(!h||h.def.tag!=="variant")return;let v=I.u32.dec(c),p=h.def.value.find(V=>V.index===v);if(p)return{pallet:f.name,error:p.name,desc:p.docs[0]}}}}var W={};l(W,{encodeAssetId:()=>G,encodeLocation:()=>F,toAccountId32:()=>re,toAccountKey20:()=>ne,transform:()=>u});var y=require("polkadot-api"),re=e=>{let t=(0,y.AccountId)().enc(e);return{type:"AccountId32",value:{network:void 0,id:y.FixedSizeBinary.fromBytes(t)}}},ne=e=>({type:"AccountKey20",value:{network:void 0,key:y.FixedSizeBinary.fromHex(e)}}),u=e=>{if(Array.isArray(e))return e.map(u);if(typeof e=="object"&&e!==null){let t=Object.keys(e);if(t.length===1){let r=t[0],n=e[r];if(r==="AccountKey20"&&typeof n=="object"){let o=ne(n.key);return n.network&&(o.value.network=u(n.network)),o}if(r==="AccountId32"&&typeof n=="object"){let o=re(n.id);return n.network&&(o.value.network=u(n.network)),o}return r==="GeneralKey"&&typeof n=="object"&&"data"in n?{type:r,value:{length:n.length,data:typeof n.data=="string"?y.Binary.fromHex(n.data):n.data}}:r==="GlobalConsensus"&&typeof n=="object"&&("Polkadot"in n||"Kusama"in n)?{type:r,value:n}:r==="Ethereum"?{type:r,value:{chain_id:BigInt("chainId"in n?n.chainId:n.chain_id)}}:r==="PalletInstance"?{type:r,value:n}:r==="GeneralIndex"?{type:r,value:BigInt(n)}:r==="X1"&&Array.isArray(n)?{type:r,value:n.length===1?u(n[0]):n.map(u)}:typeof n=="string"?{type:r,value:{type:n}}:typeof n=="number"?{type:r,value:n}:{type:r,value:u(n)}}else{let r={};for(let n of t){let o=e[n];r[n]=u(o)}return r}}else return e};function F(e){if(!e||typeof e!="object")return e;let{parents:t,interior:r}=e;return!r||r==="Here"?{parents:t,interior:{type:"Here"}}:{parents:t,interior:u(r)}}function G(e){return e==null?e:typeof e=="string"?BigInt(e):typeof e=="object"&&!Array.isArray(e)?u(e):e}var $={};l($,{convertToId:()=>Se});var oe=require("buffer");function Se(e){let r=oe.Buffer.from(e.replace("0x",""),"hex").subarray(16);return r.readUIntBE(0,r.length)}var ie=require("polkadot-api"),se=require("polkadot-api/ws-provider"),ae=require("lru-cache");var m=require("rxjs"),a=require("rxjs/operators");function X(e,{intervalMs:t=5e3,timeoutMs:r=5e3,staleThreshold:n=3,initialDelayMs:o=0}={}){let i=c=>(0,m.defer)(()=>(0,m.from)(e._request("chain_getHeader",[]))).pipe((0,a.timeout)({first:r}),(0,a.map)(f=>{let h=parseInt(f.number,16),p=c.blockNumber!==void 0&&h<=c.blockNumber?c.staleCount+1:0;return{state:p>=n?"stale":"healthy",blockNumber:h,staleCount:p,delayMs:t}}),(0,a.catchError)(()=>{let f=c.staleCount+1;return(0,m.of)({state:f>=n?"stale":"healthy",staleCount:f,delayMs:t})}));return(0,m.of)({state:"healthy",staleCount:0,delayMs:o}).pipe((0,a.expand)(c=>(0,m.timer)(c.delayMs).pipe((0,a.switchMap)(()=>i(c)))),(0,a.skip)(1),(0,a.map)(c=>c.state),(0,a.distinctUntilChanged)(),(0,a.shareReplay)({bufferSize:1,refCount:!0}))}var j=class e{static _instance=new e;_cache=new ae.LRUCache({max:40});_metadataCache;constructor(){if(e._instance)throw new Error("Use SubstrateApis.getInstance() instead of new.");e._instance=this}static getInstance(){return e._instance}configureMetadataCache(t){this._metadataCache=t}createCacheKey(t){return t.join(",")}findCacheKey(t){let r=t.find(o=>this._cache.has(o));if(r)return r;let n=this.createCacheKey(t);return this._cache.has(n)?n:null}createClient(t,r){let n=(0,se.getWsProvider)(t,r?.wsProviderOpts),o=(0,ie.createClient)(n,this._metadataCache),i=this.createCacheKey(t);console.log(`Created PAPI client for ${i}`);let s={client:o,provider:n,endpoints:t,currentEndpointIndex:0};return this._cache.set(i,s,{noDisposeOnSet:!0}),r?.probeConfig?.enabled!==!1&&this.startHealthProbe(s,r?.probeConfig),o}api(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);return o?this._cache.get(o).client:this.createClient(n,r)}release(){for(let[t,r]of this._cache.entries())this.stopHealthProbe(r),console.log("Releasing "+t),r.client.destroy();this._cache.clear()}configureHealthProbe(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);if(o){let i=this._cache.get(o);this.stopHealthProbe(i),r.enabled!==!1&&this.startHealthProbe(i,r)}}startHealthProbe(t,r={}){let n=o=>{if(t.endpoints.length>1){let i=(t.currentEndpointIndex+1)%t.endpoints.length,s=t.endpoints[i];console.log(`${o} on ${t.endpoints[t.currentEndpointIndex]}, switching to ${s}`),t.provider.switch(s),t.currentEndpointIndex=i,this.stopHealthProbe(t),this.startHealthProbe(t,{...r,initialDelayMs:r.intervalMs??5e3})}};t.probeSubscription=X(t.client,r).subscribe({next:o=>{o==="stale"&&n("RPC stale")}})}stopHealthProbe(t){t.probeSubscription&&(t.probeSubscription.unsubscribe(),t.probeSubscription=void 0)}getWs(t){let r=typeof t=="string"?t.split(","):t,n=this.findCacheKey(r);if(n)return this._cache.get(n)?.provider}};0&&(module.exports={HYDRATION_SS58_PREFIX,RUNTIME_DECIMALS,SubstrateApis,acc,big,blockProbe$,encodeAssetId,encodeLocation,enums,erc20,h160,hex,log,meta,transform,xc,xcm});
|
|
1
|
+
"use strict";var ue=Object.create;var w=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var le=Object.getOwnPropertyNames;var de=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var l=(e,t)=>{for(var r in t)w(e,r,{get:t[r],enumerable:!0})},z=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of le(t))!pe.call(e,o)&&o!==r&&w(e,o,{get:()=>t[o],enumerable:!(n=fe(t,o))||n.enumerable});return e};var ge=(e,t,r)=>(r=e!=null?ue(de(e)):{},z(t||!e||!e.__esModule?w(r,"default",{value:e,enumerable:!0}):r,e)),me=e=>z(w({},"__esModule",{value:!0}),e);var _e={};l(_e,{HYDRATION_SS58_PREFIX:()=>S,RUNTIME_DECIMALS:()=>Oe,SubstrateApis:()=>X,acc:()=>R,big:()=>H,blockProbe$:()=>j,detectLegacyRpc:()=>ke,encodeAssetId:()=>G,encodeLocation:()=>F,enums:()=>k,erc20:()=>L,h160:()=>K,hex:()=>T,log:()=>D,meta:()=>U,transform:()=>u,wsToHttp:()=>He,xc:()=>$,xcm:()=>W});module.exports=me(_e);var Oe=18,S=63;var R={};l(R,{getMultilocationDerivatedAccount:()=>he,getSovereignAccounts:()=>ye});var d=require("@polkadot-api/substrate-bindings"),x=require("@polkadot-api/utils");function he(e,t,r,n=!1){let o=t.length===42,i=o?"AccountKey20":"AccountId32",s;if(o)s=(0,x.fromHex)(t.slice(2));else{let p=(0,d.getSs58AddressInfo)(t);if(!p.isValid)throw new Error("Invalid SS58 address");s=p.publicKey}let c="SiblingChain";r==0&&e?c="ChildChain":r==1&&!e&&(c="ParentChain");let f=d.compactNumber.enc,h=new Uint8Array([...new TextEncoder().encode(c),...e?f(e):[],...f(i.length+(o?20:32)),...new TextEncoder().encode(i),...s]),v=(0,d.Blake2256)(h);return n?(0,x.toHex)(v.slice(0,20)):(0,d.AccountId)().dec(v)}function ye(e){let t=d.u32.enc(e),r=new TextEncoder().encode("para"),n=new TextEncoder().encode("sibl"),o=(0,x.toHex)(new Uint8Array([...r,...t])).padEnd(66,"0"),i=(0,x.toHex)(new Uint8Array([...n,...t])).padEnd(66,"0"),s=i.slice(0,42);return{generic:i,moonbeam:s,relay:o}}var H={};l(H,{asBigInt:()=>be,convertDecimals:()=>xe,pow10:()=>Ce,toBigInt:()=>J,toDecimal:()=>Y});var y=ge(require("big.js"));y.default.NE=-18;function Y(e,t,r){let n=(0,y.default)(e.toString().replace(/[^0-9]/g,"")),o=(0,y.default)(10).pow(t);return n.div(o).round(t,r).toString()}function J(e,t){if(typeof e=="bigint")return e;let r=(0,y.default)(10).pow(t),n=(0,y.default)(e).mul(r);return BigInt(n.toFixed(0,y.default.roundDown))}function be(e){return BigInt(e.round(0,y.default.roundDown).toFixed(0))}function xe(e,t,r){let n=Y(e,t);return J(n.toString(),r)}function Ce(e){if(e<0)throw new Error(`pow10: negative exp ${e}`);return 10n**BigInt(e)}var k={};l(k,{enumPath:()=>ve});function ve(e){let t=[],r=e;for(;r&&typeof r=="object"&&"type"in r;)t.push(r.type),r=r.value;return t.join(".")}var L={};l(L,{ERC20:()=>_});var _=class{static fromAssetId(t){let r=Buffer.alloc(20,0);return r[15]=1,r.writeUInt32BE(t,16),"0x"+r.toString("hex")}static toAssetId(t){let r=Buffer.from(t.replace("0x",""),"hex");return r.length!==20||!this.isAssetAddress(t)?null:r.readUInt32BE(16)}static isAssetAddress(t){let r=Buffer.from("0000000000000000000000000000000100000000","hex"),n=Buffer.from(t.replace("0x",""),"hex");return n.length!==20?!1:n.subarray(0,16).equals(r.subarray(0,16))}};var K={};l(K,{H160:()=>N,isEvmAccount:()=>Q,isEvmAddress:()=>Z,isSs58Address:()=>ee});var C=require("polkadot-api"),O=require("@polkadot-api/utils"),g=require("buffer");var M="ETH\0";function Q(e){if(!e)return!1;try{let t=(0,C.AccountId)().enc(e),r=g.Buffer.from(M);return g.Buffer.from(t.subarray(0,r.length)).equals(r)}catch{return!1}}function Z(e){return!!/^0x[a-fA-F0-9]{40}$/.test(e)}function ee(e){try{return(0,C.AccountId)(63).enc(e),!0}catch{return!1}}var N=class e{static toAccount=t=>{let r=g.Buffer.from(t.slice(2),"hex"),n=g.Buffer.from(M),o=Uint8Array.from(g.Buffer.concat([n,r,g.Buffer.alloc(8)])),i=(0,O.toHex)(o);return(0,C.AccountId)(63).dec(i)};static fromAccount=t=>{let r=(0,C.AccountId)().enc(t),n=g.Buffer.from(M),o=r.slice(n.length,-8);return"0x"+g.Buffer.from(o).toString("hex")};static fromSS58=t=>{let n=(0,C.AccountId)().enc(t).slice(0,20);return(0,O.toHex)(n)};static fromAny=t=>{if(Z(t))return t;if(Q(t))return e.fromAccount(t);if(ee(t))return e.fromSS58(t);throw new Error("Unknown address type")}};var T={};l(T,{assertHexLen:()=>we,ensureHexPrefix:()=>re,hexNormalize:()=>Ee,isHex:()=>te,stripHexPrefix:()=>Pe});var Ae=/^0x[0-9a-fA-F]*$/;function te(e,t=!1){return e==="0x"?!0:!Ae.test(e)||!t&&e.length<4?!1:t||e.length%2===0}function Pe(e){return e.startsWith("0x")?e.slice(2):e}function re(e){return e.startsWith("0x")?e:`0x${e}`}function Ee(e){return re(e.toLowerCase())}function we(e,t){if(!te(e)||e.length-2!==t*2)throw new Error(`Expected 0x hex of ${t} bytes`)}var D={};l(D,{logger:()=>Ie});var B={all:0,trace:1,debug:2,info:3,warn:4,error:5,silent:6};function A(e){return e.padEnd(11)}var P={TRACE:A("[GC][TRACE]"),DEBUG:A("[GC][DEBUG]"),INFO:A("[GC][INFO]"),WARN:A("[GC][WARN]"),ERROR:A("[GC][ERROR]")};function Be(){if(typeof window<"u")try{let e=window.localStorage.getItem("gc.log");if(e&&e in B)return e}catch{}if(typeof process<"u"){let e=process.env.GC_LOG;if(e&&e in B)return e}return"silent"}function E(e){let t=Be();return B[e]>=B[t]}var Ie={trace(...e){E("trace")&&console.debug(P.TRACE,...e)},debug(...e){E("debug")&&console.debug(P.DEBUG,...e)},info(...e){E("info")&&console.info(P.INFO,...e)},warn(...e){E("warn")&&console.warn(P.WARN,...e)},error(...e){if(!E("error"))return;e.some(r=>r instanceof Error)?console.error(P.ERROR,...e):this.warn(...e)}};var U={};l(U,{ErrorDecoder:()=>Se});var I=require("@polkadot-api/substrate-bindings");function Se(e){let{metadata:t}=I.metadata.dec(e),{pallets:r,lookup:n}=t.value,o=new Map(r.map(s=>[s.index,s])),i=new Map(n.map(s=>[s.id,s]));return{decode(s,c){let f=o.get(s);if(!f?.errors)return;let h=i.get(f.errors);if(!h||h.def.tag!=="variant")return;let v=I.u32.dec(c),p=h.def.value.find(q=>q.index===v);if(p)return{pallet:f.name,error:p.name,desc:p.docs[0]}}}}var W={};l(W,{encodeAssetId:()=>G,encodeLocation:()=>F,toAccountId32:()=>ne,toAccountKey20:()=>oe,transform:()=>u});var b=require("polkadot-api"),ne=e=>{let t=(0,b.AccountId)().enc(e);return{type:"AccountId32",value:{network:void 0,id:b.FixedSizeBinary.fromBytes(t)}}},oe=e=>({type:"AccountKey20",value:{network:void 0,key:b.FixedSizeBinary.fromHex(e)}}),u=e=>{if(Array.isArray(e))return e.map(u);if(typeof e=="object"&&e!==null){let t=Object.keys(e);if(t.length===1){let r=t[0],n=e[r];if(r==="AccountKey20"&&typeof n=="object"){let o=oe(n.key);return n.network&&(o.value.network=u(n.network)),o}if(r==="AccountId32"&&typeof n=="object"){let o=ne(n.id);return n.network&&(o.value.network=u(n.network)),o}return r==="GeneralKey"&&typeof n=="object"&&"data"in n?{type:r,value:{length:n.length,data:typeof n.data=="string"?b.Binary.fromHex(n.data):n.data}}:r==="GlobalConsensus"&&typeof n=="object"&&("Polkadot"in n||"Kusama"in n)?{type:r,value:n}:r==="Ethereum"?{type:r,value:{chain_id:BigInt("chainId"in n?n.chainId:n.chain_id)}}:r==="PalletInstance"?{type:r,value:n}:r==="GeneralIndex"?{type:r,value:BigInt(n)}:r==="X1"&&Array.isArray(n)?{type:r,value:n.length===1?u(n[0]):n.map(u)}:typeof n=="string"?{type:r,value:{type:n}}:typeof n=="number"?{type:r,value:n}:{type:r,value:u(n)}}else{let r={};for(let n of t){let o=e[n];r[n]=u(o)}return r}}else return e};function F(e){if(!e||typeof e!="object")return e;let{parents:t,interior:r}=e;return!r||r==="Here"?{parents:t,interior:{type:"Here"}}:{parents:t,interior:u(r)}}function G(e){return e==null?e:typeof e=="string"?BigInt(e):typeof e=="object"&&!Array.isArray(e)?u(e):e}var $={};l($,{convertToId:()=>Re});var ie=require("buffer");function Re(e){let r=ie.Buffer.from(e.replace("0x",""),"hex").subarray(16);return r.readUIntBE(0,r.length)}var se=require("polkadot-api"),ae=require("polkadot-api/ws-provider"),ce=require("lru-cache");var m=require("rxjs"),a=require("rxjs/operators");function j(e,{intervalMs:t=5e3,timeoutMs:r=5e3,staleThreshold:n=3,initialDelayMs:o=0}={}){let i=c=>(0,m.defer)(()=>(0,m.from)(e._request("chain_getHeader",[]))).pipe((0,a.timeout)({first:r}),(0,a.map)(f=>{let h=parseInt(f.number,16),p=c.blockNumber!==void 0&&h<=c.blockNumber?c.staleCount+1:0;return{state:p>=n?"stale":"healthy",blockNumber:h,staleCount:p,delayMs:t}}),(0,a.catchError)(()=>{let f=c.staleCount+1;return(0,m.of)({state:f>=n?"stale":"healthy",staleCount:f,delayMs:t})}));return(0,m.of)({state:"healthy",staleCount:0,delayMs:o}).pipe((0,a.expand)(c=>(0,m.timer)(c.delayMs).pipe((0,a.switchMap)(()=>i(c)))),(0,a.skip)(1),(0,a.map)(c=>c.state),(0,a.distinctUntilChanged)(),(0,a.shareReplay)({bufferSize:1,refCount:!0}))}var X=class e{static _instance=new e;_cache=new ce.LRUCache({max:40});_metadataCache;constructor(){if(e._instance)throw new Error("Use SubstrateApis.getInstance() instead of new.");e._instance=this}static getInstance(){return e._instance}configureMetadataCache(t){this._metadataCache=t}createCacheKey(t){return t.join(",")}findCacheKey(t){let r=t.find(o=>this._cache.has(o));if(r)return r;let n=this.createCacheKey(t);return this._cache.has(n)?n:null}createClient(t,r){let n=(0,ae.getWsProvider)(t,r?.wsProviderOpts),o=(0,se.createClient)(n,this._metadataCache),i=this.createCacheKey(t);console.log(`Created PAPI client for ${i}`);let s={client:o,provider:n,endpoints:t,currentEndpointIndex:0};return this._cache.set(i,s,{noDisposeOnSet:!0}),r?.probeConfig?.enabled!==!1&&this.startHealthProbe(s,r?.probeConfig),o}api(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);return o?this._cache.get(o).client:this.createClient(n,r)}release(){for(let[t,r]of this._cache.entries())this.stopHealthProbe(r),console.log("Releasing "+t),r.client.destroy();this._cache.clear()}configureHealthProbe(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);if(o){let i=this._cache.get(o);this.stopHealthProbe(i),r.enabled!==!1&&this.startHealthProbe(i,r)}}startHealthProbe(t,r={}){let n=o=>{if(t.endpoints.length>1){let i=(t.currentEndpointIndex+1)%t.endpoints.length,s=t.endpoints[i];console.log(`${o} on ${t.endpoints[t.currentEndpointIndex]}, switching to ${s}`),t.provider.switch(s),t.currentEndpointIndex=i,this.stopHealthProbe(t),this.startHealthProbe(t,{...r,initialDelayMs:r.intervalMs??5e3})}};t.probeSubscription=j(t.client,r).subscribe({next:o=>{o==="stale"&&n("RPC stale")}})}stopHealthProbe(t){t.probeSubscription&&(t.probeSubscription.unsubscribe(),t.probeSubscription=void 0)}getWs(t){let r=typeof t=="string"?t.split(","):t,n=this.findCacheKey(r);if(n)return this._cache.get(n)?.provider}};function He(e){return e.replace(/^(ws)(s)?:\/\//,(t,r,n)=>n?"https://":"http://")}var V=new Map;async function ke(e,t){let r=V.get(e);if(r!==void 0)return r;try{let n=await fetch(e,{signal:t,method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:1,jsonrpc:"2.0",method:"rpc_methods",params:[]})});if(!n.ok)return!1;let i=(await n.json())?.result?.methods??[];if(i.includes("dev_newBlock"))return V.set(e,!0),!0;let s=!i.includes("chainHead_v1_follow");return V.set(e,s),s}catch{return!1}}0&&(module.exports={HYDRATION_SS58_PREFIX,RUNTIME_DECIMALS,SubstrateApis,acc,big,blockProbe$,detectLegacyRpc,encodeAssetId,encodeLocation,enums,erc20,h160,hex,log,meta,transform,wsToHttp,xc,xcm});
|
package/build/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var ee=Object.defineProperty;var f=(e,t)=>{for(var r in t)ee(e,r,{get:t[r],enumerable:!0})};var Te=18,v=63;var I={};f(I,{getMultilocationDerivatedAccount:()=>ae,getSovereignAccounts:()=>ce});import{AccountId as te,Blake2256 as re,compactNumber as ne,getSs58AddressInfo as oe,u32 as ie}from"@polkadot-api/substrate-bindings";import{toHex as A,fromHex as se}from"@polkadot-api/utils";function ae(e,t,r,n=!1){let o=t.length===42,i=o?"AccountKey20":"AccountId32",s;if(o)s=se(t.slice(2));else{let l=oe(t);if(!l.isValid)throw new Error("Invalid SS58 address");s=l.publicKey}let a="SiblingChain";r==0&&e?a="ChildChain":r==1&&!e&&(a="ParentChain");let c=ne.enc,d=new Uint8Array([...new TextEncoder().encode(a),...e?c(e):[],...c(i.length+(o?20:32)),...new TextEncoder().encode(i),...s]),m=re(d);return n?A(m.slice(0,20)):te().dec(m)}function ce(e){let t=ie.enc(e),r=new TextEncoder().encode("para"),n=new TextEncoder().encode("sibl"),o=A(new Uint8Array([...r,...t])).padEnd(66,"0"),i=A(new Uint8Array([...n,...t])).padEnd(66,"0"),s=i.slice(0,42);return{generic:i,moonbeam:s,relay:o}}var H={};f(H,{asBigInt:()=>ue,convertDecimals:()=>fe,pow10:()=>le,toBigInt:()=>R,toDecimal:()=>S});import g from"big.js";g.NE=-18;function S(e,t,r){let n=g(e.toString().replace(/[^0-9]/g,"")),o=g(10).pow(t);return n.div(o).round(t,r).toString()}function R(e,t){if(typeof e=="bigint")return e;let r=g(10).pow(t),n=g(e).mul(r);return BigInt(n.toFixed(0,g.roundDown))}function ue(e){return BigInt(e.round(0,g.roundDown).toFixed(0))}function fe(e,t,r){let n=S(e,t);return R(n.toString(),r)}function le(e){if(e<0)throw new Error(`pow10: negative exp ${e}`);return 10n**BigInt(e)}var k={};f(k,{enumPath:()=>de});function de(e){let t=[],r=e;for(;r&&typeof r=="object"&&"type"in r;)t.push(r.type),r=r.value;return t.join(".")}var _={};f(_,{ERC20:()=>P});var P=class{static fromAssetId(t){let r=Buffer.alloc(20,0);return r[15]=1,r.writeUInt32BE(t,16),"0x"+r.toString("hex")}static toAssetId(t){let r=Buffer.from(t.replace("0x",""),"hex");return r.length!==20||!this.isAssetAddress(t)?null:r.readUInt32BE(16)}static isAssetAddress(t){let r=Buffer.from("0000000000000000000000000000000100000000","hex"),n=Buffer.from(t.replace("0x",""),"hex");return n.length!==20?!1:n.subarray(0,16).equals(r.subarray(0,16))}};var K={};f(K,{H160:()=>w,isEvmAccount:()=>M,isEvmAddress:()=>O,isSs58Address:()=>U});import{AccountId as h}from"polkadot-api";import{toHex as L}from"@polkadot-api/utils";import{Buffer as p}from"buffer";var E="ETH\0";function M(e){if(!e)return!1;try{let t=h().enc(e),r=p.from(E);return p.from(t.subarray(0,r.length)).equals(r)}catch{return!1}}function O(e){return!!/^0x[a-fA-F0-9]{40}$/.test(e)}function U(e){try{return h(63).enc(e),!0}catch{return!1}}var w=class e{static toAccount=t=>{let r=p.from(t.slice(2),"hex"),n=p.from(E),o=Uint8Array.from(p.concat([n,r,p.alloc(8)])),i=L(o);return h(63).dec(i)};static fromAccount=t=>{let r=h().enc(t),n=p.from(E),o=r.slice(n.length,-8);return"0x"+p.from(o).toString("hex")};static fromSS58=t=>{let n=h().enc(t).slice(0,20);return L(n)};static fromAny=t=>{if(O(t))return t;if(M(t))return e.fromAccount(t);if(U(t))return e.fromSS58(t);throw new Error("Unknown address type")}};var T={};f(T,{assertHexLen:()=>he,ensureHexPrefix:()=>D,hexNormalize:()=>me,isHex:()=>N,stripHexPrefix:()=>ge});var pe=/^0x[0-9a-fA-F]*$/;function N(e,t=!1){return e==="0x"?!0:!pe.test(e)||!t&&e.length<4?!1:t||e.length%2===0}function ge(e){return e.startsWith("0x")?e.slice(2):e}function D(e){return e.startsWith("0x")?e:`0x${e}`}function me(e){return D(e.toLowerCase())}function he(e,t){if(!N(e)||e.length-2!==t*2)throw new Error(`Expected 0x hex of ${t} bytes`)}var F={};f(F,{logger:()=>ye});var C={all:0,trace:1,debug:2,info:3,warn:4,error:5,silent:6};function b(e){return e.padEnd(11)}var y={TRACE:b("[GC][TRACE]"),DEBUG:b("[GC][DEBUG]"),INFO:b("[GC][INFO]"),WARN:b("[GC][WARN]"),ERROR:b("[GC][ERROR]")};function be(){if(typeof window<"u")try{let e=window.localStorage.getItem("gc.log");if(e&&e in C)return e}catch{}if(typeof process<"u"){let e=process.env.GC_LOG;if(e&&e in C)return e}return"silent"}function x(e){let t=be();return C[e]>=C[t]}var ye={trace(...e){x("trace")&&console.debug(y.TRACE,...e)},debug(...e){x("debug")&&console.debug(y.DEBUG,...e)},info(...e){x("info")&&console.info(y.INFO,...e)},warn(...e){x("warn")&&console.warn(y.WARN,...e)},error(...e){if(!x("error"))return;e.some(r=>r instanceof Error)?console.error(y.ERROR,...e):this.warn(...e)}};var G={};f(G,{ErrorDecoder:()=>ve});import{metadata as xe,u32 as Ce}from"@polkadot-api/substrate-bindings";function ve(e){let{metadata:t}=xe.dec(e),{pallets:r,lookup:n}=t.value,o=new Map(r.map(s=>[s.index,s])),i=new Map(n.map(s=>[s.id,s]));return{decode(s,a){let c=o.get(s);if(!c?.errors)return;let d=i.get(c.errors);if(!d||d.def.tag!=="variant")return;let m=Ce.dec(a),l=d.def.value.find(B=>B.index===m);if(l)return{pallet:c.name,error:l.name,desc:l.docs[0]}}}}var q={};f(q,{encodeAssetId:()=>V,encodeLocation:()=>j,toAccountId32:()=>$,toAccountKey20:()=>X,transform:()=>u});import{AccountId as Ae,Binary as Pe,FixedSizeBinary as W}from"polkadot-api";var $=e=>{let t=Ae().enc(e);return{type:"AccountId32",value:{network:void 0,id:W.fromBytes(t)}}},X=e=>({type:"AccountKey20",value:{network:void 0,key:W.fromHex(e)}}),u=e=>{if(Array.isArray(e))return e.map(u);if(typeof e=="object"&&e!==null){let t=Object.keys(e);if(t.length===1){let r=t[0],n=e[r];if(r==="AccountKey20"&&typeof n=="object"){let o=X(n.key);return n.network&&(o.value.network=u(n.network)),o}if(r==="AccountId32"&&typeof n=="object"){let o=$(n.id);return n.network&&(o.value.network=u(n.network)),o}return r==="GeneralKey"&&typeof n=="object"&&"data"in n?{type:r,value:{length:n.length,data:typeof n.data=="string"?Pe.fromHex(n.data):n.data}}:r==="GlobalConsensus"&&typeof n=="object"&&("Polkadot"in n||"Kusama"in n)?{type:r,value:n}:r==="Ethereum"?{type:r,value:{chain_id:BigInt("chainId"in n?n.chainId:n.chain_id)}}:r==="PalletInstance"?{type:r,value:n}:r==="GeneralIndex"?{type:r,value:BigInt(n)}:r==="X1"&&Array.isArray(n)?{type:r,value:n.length===1?u(n[0]):n.map(u)}:typeof n=="string"?{type:r,value:{type:n}}:typeof n=="number"?{type:r,value:n}:{type:r,value:u(n)}}else{let r={};for(let n of t){let o=e[n];r[n]=u(o)}return r}}else return e};function j(e){if(!e||typeof e!="object")return e;let{parents:t,interior:r}=e;return!r||r==="Here"?{parents:t,interior:{type:"Here"}}:{parents:t,interior:u(r)}}function V(e){return e==null?e:typeof e=="string"?BigInt(e):typeof e=="object"&&!Array.isArray(e)?u(e):e}var z={};f(z,{convertToId:()=>we});import{Buffer as Ee}from"buffer";function we(e){let r=Ee.from(e.replace("0x",""),"hex").subarray(16);return r.readUIntBE(0,r.length)}import{createClient as Ue}from"polkadot-api";import{getWsProvider as Ke}from"polkadot-api/ws-provider";import{LRUCache as Ne}from"lru-cache";import{defer as Be,from as Ie,of as Y,timer as Se}from"rxjs";import{catchError as Re,distinctUntilChanged as He,expand as ke,map as J,shareReplay as _e,skip as Le,switchMap as Me,timeout as Oe}from"rxjs/operators";function Q(e,{intervalMs:t=5e3,timeoutMs:r=5e3,staleThreshold:n=3,initialDelayMs:o=0}={}){let i=a=>Be(()=>Ie(e._request("chain_getHeader",[]))).pipe(Oe({first:r}),J(c=>{let d=parseInt(c.number,16),l=a.blockNumber!==void 0&&d<=a.blockNumber?a.staleCount+1:0;return{state:l>=n?"stale":"healthy",blockNumber:d,staleCount:l,delayMs:t}}),Re(()=>{let c=a.staleCount+1;return Y({state:c>=n?"stale":"healthy",staleCount:c,delayMs:t})}));return Y({state:"healthy",staleCount:0,delayMs:o}).pipe(ke(a=>Se(a.delayMs).pipe(Me(()=>i(a)))),Le(1),J(a=>a.state),He(),_e({bufferSize:1,refCount:!0}))}var Z=class e{static _instance=new e;_cache=new Ne({max:40});_metadataCache;constructor(){if(e._instance)throw new Error("Use SubstrateApis.getInstance() instead of new.");e._instance=this}static getInstance(){return e._instance}configureMetadataCache(t){this._metadataCache=t}createCacheKey(t){return t.join(",")}findCacheKey(t){let r=t.find(o=>this._cache.has(o));if(r)return r;let n=this.createCacheKey(t);return this._cache.has(n)?n:null}createClient(t,r){let n=Ke(t,r?.wsProviderOpts),o=Ue(n,this._metadataCache),i=this.createCacheKey(t);console.log(`Created PAPI client for ${i}`);let s={client:o,provider:n,endpoints:t,currentEndpointIndex:0};return this._cache.set(i,s,{noDisposeOnSet:!0}),r?.probeConfig?.enabled!==!1&&this.startHealthProbe(s,r?.probeConfig),o}api(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);return o?this._cache.get(o).client:this.createClient(n,r)}release(){for(let[t,r]of this._cache.entries())this.stopHealthProbe(r),console.log("Releasing "+t),r.client.destroy();this._cache.clear()}configureHealthProbe(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);if(o){let i=this._cache.get(o);this.stopHealthProbe(i),r.enabled!==!1&&this.startHealthProbe(i,r)}}startHealthProbe(t,r={}){let n=o=>{if(t.endpoints.length>1){let i=(t.currentEndpointIndex+1)%t.endpoints.length,s=t.endpoints[i];console.log(`${o} on ${t.endpoints[t.currentEndpointIndex]}, switching to ${s}`),t.provider.switch(s),t.currentEndpointIndex=i,this.stopHealthProbe(t),this.startHealthProbe(t,{...r,initialDelayMs:r.intervalMs??5e3})}};t.probeSubscription=Q(t.client,r).subscribe({next:o=>{o==="stale"&&n("RPC stale")}})}stopHealthProbe(t){t.probeSubscription&&(t.probeSubscription.unsubscribe(),t.probeSubscription=void 0)}getWs(t){let r=typeof t=="string"?t.split(","):t,n=this.findCacheKey(r);if(n)return this._cache.get(n)?.provider}};export{v as HYDRATION_SS58_PREFIX,Te as RUNTIME_DECIMALS,Z as SubstrateApis,I as acc,H as big,Q as blockProbe$,V as encodeAssetId,j as encodeLocation,k as enums,_ as erc20,K as h160,T as hex,F as log,G as meta,u as transform,z as xc,q as xcm};
|
|
1
|
+
var te=Object.defineProperty;var f=(e,t)=>{for(var r in t)te(e,r,{get:t[r],enumerable:!0})};var Fe=18,v=63;var S={};f(S,{getMultilocationDerivatedAccount:()=>ce,getSovereignAccounts:()=>ue});import{AccountId as re,Blake2256 as ne,compactNumber as oe,getSs58AddressInfo as ie,u32 as se}from"@polkadot-api/substrate-bindings";import{toHex as A,fromHex as ae}from"@polkadot-api/utils";function ce(e,t,r,n=!1){let o=t.length===42,i=o?"AccountKey20":"AccountId32",s;if(o)s=ae(t.slice(2));else{let l=ie(t);if(!l.isValid)throw new Error("Invalid SS58 address");s=l.publicKey}let a="SiblingChain";r==0&&e?a="ChildChain":r==1&&!e&&(a="ParentChain");let c=oe.enc,d=new Uint8Array([...new TextEncoder().encode(a),...e?c(e):[],...c(i.length+(o?20:32)),...new TextEncoder().encode(i),...s]),m=ne(d);return n?A(m.slice(0,20)):re().dec(m)}function ue(e){let t=se.enc(e),r=new TextEncoder().encode("para"),n=new TextEncoder().encode("sibl"),o=A(new Uint8Array([...r,...t])).padEnd(66,"0"),i=A(new Uint8Array([...n,...t])).padEnd(66,"0"),s=i.slice(0,42);return{generic:i,moonbeam:s,relay:o}}var k={};f(k,{asBigInt:()=>fe,convertDecimals:()=>le,pow10:()=>de,toBigInt:()=>H,toDecimal:()=>R});import g from"big.js";g.NE=-18;function R(e,t,r){let n=g(e.toString().replace(/[^0-9]/g,"")),o=g(10).pow(t);return n.div(o).round(t,r).toString()}function H(e,t){if(typeof e=="bigint")return e;let r=g(10).pow(t),n=g(e).mul(r);return BigInt(n.toFixed(0,g.roundDown))}function fe(e){return BigInt(e.round(0,g.roundDown).toFixed(0))}function le(e,t,r){let n=R(e,t);return H(n.toString(),r)}function de(e){if(e<0)throw new Error(`pow10: negative exp ${e}`);return 10n**BigInt(e)}var _={};f(_,{enumPath:()=>pe});function pe(e){let t=[],r=e;for(;r&&typeof r=="object"&&"type"in r;)t.push(r.type),r=r.value;return t.join(".")}var L={};f(L,{ERC20:()=>P});var P=class{static fromAssetId(t){let r=Buffer.alloc(20,0);return r[15]=1,r.writeUInt32BE(t,16),"0x"+r.toString("hex")}static toAssetId(t){let r=Buffer.from(t.replace("0x",""),"hex");return r.length!==20||!this.isAssetAddress(t)?null:r.readUInt32BE(16)}static isAssetAddress(t){let r=Buffer.from("0000000000000000000000000000000100000000","hex"),n=Buffer.from(t.replace("0x",""),"hex");return n.length!==20?!1:n.subarray(0,16).equals(r.subarray(0,16))}};var T={};f(T,{H160:()=>w,isEvmAccount:()=>M,isEvmAddress:()=>N,isSs58Address:()=>K});import{AccountId as h}from"polkadot-api";import{toHex as O}from"@polkadot-api/utils";import{Buffer as p}from"buffer";var E="ETH\0";function M(e){if(!e)return!1;try{let t=h().enc(e),r=p.from(E);return p.from(t.subarray(0,r.length)).equals(r)}catch{return!1}}function N(e){return!!/^0x[a-fA-F0-9]{40}$/.test(e)}function K(e){try{return h(63).enc(e),!0}catch{return!1}}var w=class e{static toAccount=t=>{let r=p.from(t.slice(2),"hex"),n=p.from(E),o=Uint8Array.from(p.concat([n,r,p.alloc(8)])),i=O(o);return h(63).dec(i)};static fromAccount=t=>{let r=h().enc(t),n=p.from(E),o=r.slice(n.length,-8);return"0x"+p.from(o).toString("hex")};static fromSS58=t=>{let n=h().enc(t).slice(0,20);return O(n)};static fromAny=t=>{if(N(t))return t;if(M(t))return e.fromAccount(t);if(K(t))return e.fromSS58(t);throw new Error("Unknown address type")}};var F={};f(F,{assertHexLen:()=>ye,ensureHexPrefix:()=>U,hexNormalize:()=>he,isHex:()=>D,stripHexPrefix:()=>me});var ge=/^0x[0-9a-fA-F]*$/;function D(e,t=!1){return e==="0x"?!0:!ge.test(e)||!t&&e.length<4?!1:t||e.length%2===0}function me(e){return e.startsWith("0x")?e.slice(2):e}function U(e){return e.startsWith("0x")?e:`0x${e}`}function he(e){return U(e.toLowerCase())}function ye(e,t){if(!D(e)||e.length-2!==t*2)throw new Error(`Expected 0x hex of ${t} bytes`)}var G={};f(G,{logger:()=>xe});var C={all:0,trace:1,debug:2,info:3,warn:4,error:5,silent:6};function y(e){return e.padEnd(11)}var b={TRACE:y("[GC][TRACE]"),DEBUG:y("[GC][DEBUG]"),INFO:y("[GC][INFO]"),WARN:y("[GC][WARN]"),ERROR:y("[GC][ERROR]")};function be(){if(typeof window<"u")try{let e=window.localStorage.getItem("gc.log");if(e&&e in C)return e}catch{}if(typeof process<"u"){let e=process.env.GC_LOG;if(e&&e in C)return e}return"silent"}function x(e){let t=be();return C[e]>=C[t]}var xe={trace(...e){x("trace")&&console.debug(b.TRACE,...e)},debug(...e){x("debug")&&console.debug(b.DEBUG,...e)},info(...e){x("info")&&console.info(b.INFO,...e)},warn(...e){x("warn")&&console.warn(b.WARN,...e)},error(...e){if(!x("error"))return;e.some(r=>r instanceof Error)?console.error(b.ERROR,...e):this.warn(...e)}};var W={};f(W,{ErrorDecoder:()=>Ae});import{metadata as Ce,u32 as ve}from"@polkadot-api/substrate-bindings";function Ae(e){let{metadata:t}=Ce.dec(e),{pallets:r,lookup:n}=t.value,o=new Map(r.map(s=>[s.index,s])),i=new Map(n.map(s=>[s.id,s]));return{decode(s,a){let c=o.get(s);if(!c?.errors)return;let d=i.get(c.errors);if(!d||d.def.tag!=="variant")return;let m=ve.dec(a),l=d.def.value.find(I=>I.index===m);if(l)return{pallet:c.name,error:l.name,desc:l.docs[0]}}}}var z={};f(z,{encodeAssetId:()=>q,encodeLocation:()=>V,toAccountId32:()=>j,toAccountKey20:()=>X,transform:()=>u});import{AccountId as Pe,Binary as Ee,FixedSizeBinary as $}from"polkadot-api";var j=e=>{let t=Pe().enc(e);return{type:"AccountId32",value:{network:void 0,id:$.fromBytes(t)}}},X=e=>({type:"AccountKey20",value:{network:void 0,key:$.fromHex(e)}}),u=e=>{if(Array.isArray(e))return e.map(u);if(typeof e=="object"&&e!==null){let t=Object.keys(e);if(t.length===1){let r=t[0],n=e[r];if(r==="AccountKey20"&&typeof n=="object"){let o=X(n.key);return n.network&&(o.value.network=u(n.network)),o}if(r==="AccountId32"&&typeof n=="object"){let o=j(n.id);return n.network&&(o.value.network=u(n.network)),o}return r==="GeneralKey"&&typeof n=="object"&&"data"in n?{type:r,value:{length:n.length,data:typeof n.data=="string"?Ee.fromHex(n.data):n.data}}:r==="GlobalConsensus"&&typeof n=="object"&&("Polkadot"in n||"Kusama"in n)?{type:r,value:n}:r==="Ethereum"?{type:r,value:{chain_id:BigInt("chainId"in n?n.chainId:n.chain_id)}}:r==="PalletInstance"?{type:r,value:n}:r==="GeneralIndex"?{type:r,value:BigInt(n)}:r==="X1"&&Array.isArray(n)?{type:r,value:n.length===1?u(n[0]):n.map(u)}:typeof n=="string"?{type:r,value:{type:n}}:typeof n=="number"?{type:r,value:n}:{type:r,value:u(n)}}else{let r={};for(let n of t){let o=e[n];r[n]=u(o)}return r}}else return e};function V(e){if(!e||typeof e!="object")return e;let{parents:t,interior:r}=e;return!r||r==="Here"?{parents:t,interior:{type:"Here"}}:{parents:t,interior:u(r)}}function q(e){return e==null?e:typeof e=="string"?BigInt(e):typeof e=="object"&&!Array.isArray(e)?u(e):e}var Y={};f(Y,{convertToId:()=>Be});import{Buffer as we}from"buffer";function Be(e){let r=we.from(e.replace("0x",""),"hex").subarray(16);return r.readUIntBE(0,r.length)}import{createClient as Ke}from"polkadot-api";import{getWsProvider as Te}from"polkadot-api/ws-provider";import{LRUCache as De}from"lru-cache";import{defer as Ie,from as Se,of as J,timer as Re}from"rxjs";import{catchError as He,distinctUntilChanged as ke,expand as _e,map as Q,shareReplay as Le,skip as Oe,switchMap as Me,timeout as Ne}from"rxjs/operators";function Z(e,{intervalMs:t=5e3,timeoutMs:r=5e3,staleThreshold:n=3,initialDelayMs:o=0}={}){let i=a=>Ie(()=>Se(e._request("chain_getHeader",[]))).pipe(Ne({first:r}),Q(c=>{let d=parseInt(c.number,16),l=a.blockNumber!==void 0&&d<=a.blockNumber?a.staleCount+1:0;return{state:l>=n?"stale":"healthy",blockNumber:d,staleCount:l,delayMs:t}}),He(()=>{let c=a.staleCount+1;return J({state:c>=n?"stale":"healthy",staleCount:c,delayMs:t})}));return J({state:"healthy",staleCount:0,delayMs:o}).pipe(_e(a=>Re(a.delayMs).pipe(Me(()=>i(a)))),Oe(1),Q(a=>a.state),ke(),Le({bufferSize:1,refCount:!0}))}var ee=class e{static _instance=new e;_cache=new De({max:40});_metadataCache;constructor(){if(e._instance)throw new Error("Use SubstrateApis.getInstance() instead of new.");e._instance=this}static getInstance(){return e._instance}configureMetadataCache(t){this._metadataCache=t}createCacheKey(t){return t.join(",")}findCacheKey(t){let r=t.find(o=>this._cache.has(o));if(r)return r;let n=this.createCacheKey(t);return this._cache.has(n)?n:null}createClient(t,r){let n=Te(t,r?.wsProviderOpts),o=Ke(n,this._metadataCache),i=this.createCacheKey(t);console.log(`Created PAPI client for ${i}`);let s={client:o,provider:n,endpoints:t,currentEndpointIndex:0};return this._cache.set(i,s,{noDisposeOnSet:!0}),r?.probeConfig?.enabled!==!1&&this.startHealthProbe(s,r?.probeConfig),o}api(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);return o?this._cache.get(o).client:this.createClient(n,r)}release(){for(let[t,r]of this._cache.entries())this.stopHealthProbe(r),console.log("Releasing "+t),r.client.destroy();this._cache.clear()}configureHealthProbe(t,r){let n=typeof t=="string"?t.split(","):t,o=this.findCacheKey(n);if(o){let i=this._cache.get(o);this.stopHealthProbe(i),r.enabled!==!1&&this.startHealthProbe(i,r)}}startHealthProbe(t,r={}){let n=o=>{if(t.endpoints.length>1){let i=(t.currentEndpointIndex+1)%t.endpoints.length,s=t.endpoints[i];console.log(`${o} on ${t.endpoints[t.currentEndpointIndex]}, switching to ${s}`),t.provider.switch(s),t.currentEndpointIndex=i,this.stopHealthProbe(t),this.startHealthProbe(t,{...r,initialDelayMs:r.intervalMs??5e3})}};t.probeSubscription=Z(t.client,r).subscribe({next:o=>{o==="stale"&&n("RPC stale")}})}stopHealthProbe(t){t.probeSubscription&&(t.probeSubscription.unsubscribe(),t.probeSubscription=void 0)}getWs(t){let r=typeof t=="string"?t.split(","):t,n=this.findCacheKey(r);if(n)return this._cache.get(n)?.provider}};function mt(e){return e.replace(/^(ws)(s)?:\/\//,(t,r,n)=>n?"https://":"http://")}var B=new Map;async function ht(e,t){let r=B.get(e);if(r!==void 0)return r;try{let n=await fetch(e,{signal:t,method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({id:1,jsonrpc:"2.0",method:"rpc_methods",params:[]})});if(!n.ok)return!1;let i=(await n.json())?.result?.methods??[];if(i.includes("dev_newBlock"))return B.set(e,!0),!0;let s=!i.includes("chainHead_v1_follow");return B.set(e,s),s}catch{return!1}}export{v as HYDRATION_SS58_PREFIX,Fe as RUNTIME_DECIMALS,ee as SubstrateApis,S as acc,k as big,Z as blockProbe$,ht as detectLegacyRpc,q as encodeAssetId,V as encodeLocation,_ as enums,L as erc20,T as h160,F as hex,G as log,W as meta,u as transform,mt as wsToHttp,Y as xc,z as xcm};
|