@galacticcouncil/common 0.5.0 → 0.5.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/build/index.cjs +1 -1
- package/build/index.mjs +1 -1
- package/build/types/substrate/probe.d.ts +2 -1
- package/package.json +1 -1
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 u=(e,t)=>{for(var r in t)w(e,r,{get:t[r],enumerable:!0})},V=(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)):{},V(t||!e||!e.__esModule?w(r,"default",{value:e,enumerable:!0}):r,e)),ge=e=>V(w({},"__esModule",{value:!0}),e);var Re={};u(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:()=>M,hex:()=>N,log:()=>T,meta:()=>D,transform:()=>c,xc:()=>$,xcm:()=>W});module.exports=ge(Re);var ke=18,S=63;var R={};u(R,{getMultilocationDerivatedAccount:()=>me,getSovereignAccounts:()=>he});var f=require("@polkadot-api/substrate-bindings"),C=require("@polkadot-api/utils");function me(e,t,r,n=!1){let o=t.length===42,s=o?"AccountKey20":"AccountId32",i;if(o)i=(0,C.fromHex)(t.slice(2));else{let b=(0,f.getSs58AddressInfo)(t);if(!b.isValid)throw new Error("Invalid SS58 address");i=b.publicKey}let m="SiblingChain";r==0&&e?m="ChildChain":r==1&&!e&&(m="ParentChain");let l=f.compactNumber.enc,x=new Uint8Array([...new TextEncoder().encode(m),...e?l(e):[],...l(s.length+(o?20:32)),...new TextEncoder().encode(s),...i]),h=(0,f.Blake2256)(x);return n?(0,C.toHex)(h.slice(0,20)):(0,f.AccountId)().dec(h)}function he(e){let t=f.u32.enc(e),r=new TextEncoder().encode("para"),n=new TextEncoder().encode("sibl"),o=(0,C.toHex)(new Uint8Array([...r,...t])).padEnd(66,"0"),s=(0,C.toHex)(new Uint8Array([...n,...t])).padEnd(66,"0"),i=s.slice(0,42);return{generic:s,moonbeam:i,relay:o}}var H={};u(H,{asBigInt:()=>be,convertDecimals:()=>ye,pow10:()=>xe,toBigInt:()=>z,toDecimal:()=>q});var g=pe(require("big.js"));g.default.NE=-18;function q(e,t,r){let n=(0,g.default)(e.toString().replace(/[^0-9]/g,"")),o=(0,g.default)(10).pow(t);return n.div(o).round(t,r).toString()}function z(e,t){if(typeof e=="bigint")return e;let r=(0,g.default)(10).pow(t),n=(0,g.default)(e).mul(r);return BigInt(n.toFixed(0,g.default.roundDown))}function be(e){return BigInt(e.round(0,g.default.roundDown).toFixed(0))}function ye(e,t,r){let n=q(e,t);return z(n.toString(),r)}function xe(e){if(e<0)throw new Error(`pow10: negative exp ${e}`);return 10n**BigInt(e)}var k={};u(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={};u(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 M={};u(M,{H160:()=>K,isEvmAccount:()=>Y,isEvmAddress:()=>J,isSs58Address:()=>Q});var v=require("polkadot-api"),O=require("@polkadot-api/utils"),d=require("buffer");var U="ETH\0";function Y(e){if(!e)return!1;try{let t=(0,v.AccountId)().enc(e),r=d.Buffer.from(U);return d.Buffer.from(t.subarray(0,r.length)).equals(r)}catch{return!1}}function J(e){return!!/^0x[a-fA-F0-9]{40}$/.test(e)}function Q(e){try{return(0,v.AccountId)(63).enc(e),!0}catch{return!1}}var K=class e{static toAccount=t=>{let r=d.Buffer.from(t.slice(2),"hex"),n=d.Buffer.from(U),o=Uint8Array.from(d.Buffer.concat([n,r,d.Buffer.alloc(8)])),s=(0,O.toHex)(o);return(0,v.AccountId)(63).dec(s)};static fromAccount=t=>{let r=(0,v.AccountId)().enc(t),n=d.Buffer.from(U),o=r.slice(n.length,-8);return"0x"+d.Buffer.from(o).toString("hex")};static fromSS58=t=>{let n=(0,v.AccountId)().enc(t).slice(0,20);return(0,O.toHex)(n)};static fromAny=t=>{if(J(t))return t;if(Y(t))return e.fromAccount(t);if(Q(t))return e.fromSS58(t);throw new Error("Unknown address type")}};var N={};u(N,{assertHexLen:()=>Ee,ensureHexPrefix:()=>ee,hexNormalize:()=>Pe,isHex:()=>Z,stripHexPrefix:()=>Ae});var ve=/^0x[0-9a-fA-F]*$/;function Z(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 ee(e){return e.startsWith("0x")?e:`0x${e}`}function Pe(e){return ee(e.toLowerCase())}function Ee(e,t){if(!Z(e)||e.length-2!==t*2)throw new Error(`Expected 0x hex of ${t} bytes`)}var T={};u(T,{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 D={};u(D,{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(i=>[i.index,i])),s=new Map(n.map(i=>[i.id,i]));return{decode(i,m){let l=o.get(i);if(!l?.errors)return;let x=s.get(l.errors);if(!x||x.def.tag!=="variant")return;let h=I.u32.dec(m),b=x.def.value.find(ae=>ae.index===h);if(b)return{pallet:l.name,error:b.name,desc:b.docs[0]}}}}var W={};u(W,{encodeAssetId:()=>G,encodeLocation:()=>F,toAccountId32:()=>te,toAccountKey20:()=>re,transform:()=>c});var y=require("polkadot-api"),te=e=>{let t=(0,y.AccountId)().enc(e);return{type:"AccountId32",value:{network:void 0,id:y.FixedSizeBinary.fromBytes(t)}}},re=e=>({type:"AccountKey20",value:{network:void 0,key:y.FixedSizeBinary.fromHex(e)}}),c=e=>{if(Array.isArray(e))return e.map(c);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=re(n.key);return n.network&&(o.value.network=c(n.network)),o}if(r==="AccountId32"&&typeof n=="object"){let o=te(n.id);return n.network&&(o.value.network=c(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?c(n[0]):n.map(c)}:typeof n=="string"?{type:r,value:{type:n}}:typeof n=="number"?{type:r,value:n}:{type:r,value:c(n)}}else{let r={};for(let n of t){let o=e[n];r[n]=c(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:c(r)}}function G(e){return e==null?e:typeof e=="string"?BigInt(e):typeof e=="object"&&!Array.isArray(e)?c(e):e}var $={};u($,{convertToId:()=>Se});var ne=require("buffer");function Se(e){let r=ne.Buffer.from(e.replace("0x",""),"hex").subarray(16);return r.readUIntBE(0,r.length)}var oe=require("polkadot-api"),ie=require("polkadot-api/ws-provider"),se=require("lru-cache");var p=require("rxjs"),a=require("rxjs/operators");function X(e,{intervalMs:t=5e3,timeoutMs:r=5e3,staleThreshold:n=3}={}){let o=i=>(0,p.defer)(()=>(0,p.from)(e._request("chain_getHeader",[]))).pipe((0,a.timeout)({first:r}),(0,a.map)(m=>{let l=parseInt(m.number,16),h=i.blockNumber!==void 0&&l<=i.blockNumber?i.staleCount+1:0;return{state:h>=n?"stale":"healthy",blockNumber:l,staleCount:h,delayMs:t}}),(0,a.catchError)(()=>(0,p.of)({state:"stale",staleCount:0,delayMs:t})));return(0,p.of)({state:"healthy",staleCount:0,delayMs:0}).pipe((0,a.expand)(i=>(0,p.timer)(i.delayMs).pipe((0,a.switchMap)(()=>o(i)))),(0,a.skip)(1),(0,a.map)(i=>i.state),(0,a.distinctUntilChanged)(),(0,a.shareReplay)({bufferSize:1,refCount:!0}))}var j=class e{static _instance=new e;_cache=new se.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,ie.getWsProvider)(t,r?.wsProviderOpts),o=(0,oe.createClient)(n,this._metadataCache),s=this.createCacheKey(t);console.log(`Created PAPI client for ${s}`);let i={client:o,provider:n,endpoints:t,currentEndpointIndex:0};return this._cache.set(s,i,{noDisposeOnSet:!0}),r?.probeConfig?.enabled!==!1&&this.startHealthProbe(i,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 s=this._cache.get(o);this.stopHealthProbe(s),r.enabled!==!1&&this.startHealthProbe(s,r)}}startHealthProbe(t,r={}){let n=o=>{if(t.endpoints.length>1){let s=(t.currentEndpointIndex+1)%t.endpoints.length,i=t.endpoints[s];console.log(`${o} on ${t.endpoints[t.currentEndpointIndex]}, switching to ${i}`),t.provider.switch(i),t.currentEndpointIndex=s,this.stopHealthProbe(t),this.startHealthProbe(t,r)}};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 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});
|
package/build/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var ee=Object.defineProperty;var
|
|
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};
|
|
@@ -5,8 +5,9 @@ export type ProbeConfig = {
|
|
|
5
5
|
intervalMs?: number;
|
|
6
6
|
timeoutMs?: number;
|
|
7
7
|
staleThreshold?: number;
|
|
8
|
+
initialDelayMs?: number;
|
|
8
9
|
};
|
|
9
10
|
export interface HealthProbeConfig extends ProbeConfig {
|
|
10
11
|
enabled?: boolean;
|
|
11
12
|
}
|
|
12
|
-
export declare function blockProbe$(client: PolkadotClient, { intervalMs, timeoutMs, staleThreshold, }?: ProbeConfig): Observable<ProbeState>;
|
|
13
|
+
export declare function blockProbe$(client: PolkadotClient, { intervalMs, timeoutMs, staleThreshold, initialDelayMs, }?: ProbeConfig): Observable<ProbeState>;
|