"use strict";var u=Object.defineProperty;var d=(e,t,i)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i;var o=(e,t,i)=>d(e,typeof t!="symbol"?t+"":t,i);const n=require("./address-DKM11kf_.js");function f(e){return"getVersion"in e}const s={};function B(e,t,i){t in s||(s[t]={});const r=s[t];if(e in r)throw new Error(`Protocol ${e} for protocol ${t} has already registered`);s[t][e]=i}function m(e,t){const i=n.isChain(e)?n.chainToPlatform.get(e):e;return t in s?i in s[t]:!1}function l(e,t){if(t in s){const i=s[t];if(i&&e in i){const r=i[e];if(r)return r}}throw new Error(`No protocols registered for ${e}:${t}. This may be because the platform specific protocol implementation is not registered (by installing and importing it) or no implementation exists for this platform`)}const w=(e,t,i,r)=>l(e,t).fromRpc(i,r);class P{constructor(t,i){o(this,"network");o(this,"config");this.network=t,this.config=i}utils(){return this.constructor}getProtocol(t,i){return w(this.utils()._platform,t,i,this.config)}getProtocolInitializer(t){return l(this.utils()._platform,t)}async parseWormholeMessages(t,i,r){return(await this.getProtocol("WormholeCore",i)).parseTransaction(r)}}class k{constructor(t,i,r){o(this,"network");o(this,"chain");o(this,"config");o(this,"platform");o(this,"rpc");o(this,"protocols",new Map);o(this,"supportsWormholeCore",()=>this.supportsProtocol("WormholeCore"));o(this,"getWormholeCore",()=>this.getProtocol("WormholeCore"));o(this,"supportsTokenBridge",()=>this.supportsProtocol("TokenBridge"));o(this,"getTokenBridge",()=>this.getProtocol("TokenBridge"));o(this,"supportsAutomaticTokenBridge",()=>this.supportsProtocol("AutomaticTokenBridge"));o(this,"getAutomaticTokenBridge",()=>this.getProtocol("AutomaticTokenBridge"));o(this,"supportsCircleBridge",()=>this.supportsProtocol("CircleBridge"));o(this,"getCircleBridge",()=>this.getProtocol("CircleBridge"));o(this,"supportsAutomaticCircleBridge",()=>this.supportsProtocol("AutomaticCircleBridge"));o(this,"getAutomaticCircleBridge",()=>this.getProtocol("AutomaticCircleBridge"));o(this,"supportsIbcBridge",()=>this.supportsProtocol("IbcBridge"));o(this,"getIbcBridge",()=>this.getProtocol("IbcBridge"));o(this,"supportsPorticoBridge",()=>this.supportsProtocol("PorticoBridge"));o(this,"getPorticoBridge",()=>this.getProtocol("PorticoBridge"));o(this,"supportsTBTCBridge",()=>this.supportsProtocol("TBTCBridge"));o(this,"getTBTCBridge",()=>this.getProtocol("TBTCBridge"));this.config=i.config[t],this.platform=i,this.chain=this.config.key,this.network=this.config.network,this.rpc=r}getRpc(){return this.rpc=this.rpc?this.rpc:this.platform.getRpc(this.chain),this.rpc}async getDecimals(t){if(n.isNative(t))return this.config.nativeTokenDecimals;if(this.config.tokenMap){const i=n.canonicalAddress({chain:this.chain,address:t}),r=n.getTokenByAddress(this.network,this.chain,i);if(r)return r.decimals}return this.platform.utils().getDecimals(this.chain,await this.getRpc(),t)}async getBalance(t,i){return this.platform.utils().getBalance(this.chain,await this.getRpc(),t,i)}async getLatestBlock(){return this.platform.utils().getLatestBlock(await this.getRpc())}async getLatestFinalizedBlock(){return this.platform.utils().getLatestFinalizedBlock(await this.getRpc())}async parseTransaction(t){return this.platform.parseWormholeMessages(this.chain,await this.getRpc(),t)}async sendWait(t){return this.platform.utils().sendWait(this.chain,await this.getRpc(),t)}getToken(t){if(this.config.tokenMap&&t in this.config.tokenMap)return this.config.tokenMap[t]}async getNativeWrappedTokenId(){if(this.config.wrappedNative){const{address:i}=this.config.wrappedNative;return{chain:this.chain,address:n.toNative(this.chain,i)}}const t=await this.getTokenBridge();return{chain:this.chain,address:await t.getWrappedNative()}}async getTokenAccount(t,i){return{chain:this.chain,address:t}}supportsProtocol(t){return m(this.chain,t)}async getProtocol(t,i,r){if(!i&&this.protocols.has(t))return this.protocols.get(t);const c=i?{...this.config.contracts,...i}:this.config.contracts,h=r??await this.getRpc(),g=this.platform.getProtocolInitializer(t);let a;if(r){if(i)throw new Error("Custom contracts are currently not supported with custom rpc connection. Add the contracts to the base config.");a=await this.platform.getProtocol(t,h)}else if(f(g)){const p=await g.getVersion(h,c);a=new g(this.network,this.chain,h,c,p)}else a=new g(this.network,this.chain,h,c);return i||this.protocols.set(t,a),a}}exports.ChainContext=k;exports.PlatformContext=P;exports.registerProtocol=B;
