"use strict";var p=Object.defineProperty;var l=(r,t,o)=>t in r?p(r,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):r[t]=o;var i=(r,t,o)=>l(r,typeof t!="symbol"?t+"":t,o);const s=require("./address-XxuWo72f.js");class d{constructor(t,o,e){i(this,"network");i(this,"chain");i(this,"config");i(this,"platform");i(this,"rpc");i(this,"protocols",new Map);i(this,"supportsWormholeCore",()=>this.supportsProtocol("WormholeCore"));i(this,"getWormholeCore",()=>this.getProtocol("WormholeCore"));i(this,"supportsTokenBridge",()=>this.supportsProtocol("TokenBridge"));i(this,"getTokenBridge",()=>this.getProtocol("TokenBridge"));i(this,"supportsAutomaticTokenBridge",()=>this.supportsProtocol("AutomaticTokenBridge"));i(this,"getAutomaticTokenBridge",()=>this.getProtocol("AutomaticTokenBridge"));i(this,"supportsExecutorTokenBridge",()=>this.supportsProtocol("ExecutorTokenBridge"));i(this,"getExecutorTokenBridge",()=>this.getProtocol("ExecutorTokenBridge"));i(this,"supportsCircleBridge",()=>this.supportsProtocol("CircleBridge"));i(this,"getCircleBridge",()=>this.getProtocol("CircleBridge"));i(this,"supportsAutomaticCircleBridge",()=>this.supportsProtocol("AutomaticCircleBridge"));i(this,"getAutomaticCircleBridge",()=>this.getProtocol("AutomaticCircleBridge"));i(this,"supportsIbcBridge",()=>this.supportsProtocol("IbcBridge"));i(this,"getIbcBridge",()=>this.getProtocol("IbcBridge"));i(this,"supportsPorticoBridge",()=>this.supportsProtocol("PorticoBridge"));i(this,"getPorticoBridge",()=>this.getProtocol("PorticoBridge"));i(this,"supportsTBTCBridge",()=>this.supportsProtocol("TBTCBridge"));i(this,"getTBTCBridge",()=>this.getProtocol("TBTCBridge"));this.config=o.config[t],this.platform=o,this.chain=this.config.key,this.network=this.config.network,this.rpc=e}getRpc(){return this.rpc=this.rpc?this.rpc:this.platform.getRpc(this.chain),this.rpc}async getDecimals(t){if(s.isNative(t))return this.config.nativeTokenDecimals;if(this.config.tokenMap){const o=s.canonicalAddress({chain:this.chain,address:t}),e=s.getTokenByAddress(this.network,this.chain,o);if(e)return e.decimals}return this.platform.utils().getDecimals(this.network,this.chain,await this.getRpc(),t)}async getBalance(t,o){return this.platform.utils().getBalance(this.network,this.chain,await this.getRpc(),t,o)}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:o}=this.config.wrappedNative;return{chain:this.chain,address:s.toNative(this.chain,o)}}const t=await this.getTokenBridge();return{chain:this.chain,address:await t.getWrappedNative()}}async getTokenAccount(t,o){return{chain:this.chain,address:t}}supportsProtocol(t){return s.protocolIsRegistered(this.chain,t)}async getProtocol(t,o,e){if(!o&&this.protocols.has(t))return this.protocols.get(t);const h=o?{...this.config.contracts,...o}:this.config.contracts,n=e??await this.getRpc(),a=this.platform.getProtocolInitializer(t);let c;if(e){if(o)throw new Error("Custom contracts are currently not supported with custom rpc connection. Add the contracts to the base config.");c=await this.platform.getProtocol(t,n)}else if(s.isVersionedProtocolInitializer(a)){const g=await a.getVersion(n,h);c=new a(this.network,this.chain,n,h,g)}else c=new a(this.network,this.chain,n,h);return o||this.protocols.set(t,c),c}}exports.ChainContext=d;
