"use strict";var w=Object.defineProperty;var T=(o,t,e)=>t in o?w(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var s=(o,t,e)=>T(o,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./address-DKM11kf_.js"),l=require("./index-DflOc1lz.js"),g=require("./contract-C2wJ6LwN.js"),d=require("./platform-DHBUZeaO.js"),u=require("./unsignedTransaction-B7AZXsTD.js"),p=require("./chain-CJuQGMF5.js");class c{constructor(t,e,n,a){s(this,"network");s(this,"chain");s(this,"provider");s(this,"contracts");s(this,"chainId");s(this,"core");s(this,"gatewayAddress");s(this,"gateway");s(this,"tbtcTokenAddr");if(this.network=t,this.chain=e,this.provider=n,this.contracts=a,this.network!=="Mainnet")throw new Error("TBTC is only supported on Mainnet");if(!this.contracts.tbtc)throw new Error("TBTC contract address is required");const i=r.TBTCBridge.getNativeTbtcToken(this.chain);if(!i)throw new Error("Native tbtc token not found");this.chainId=r.networkChainToNativeChainId.get(t,e),this.core=new l.EvmWormholeCore(t,e,n,a),this.gatewayAddress=this.contracts.tbtc,this.gateway=new g.Contract(this.gatewayAddress,["function sendTbtc(uint256 amount, uint16 recipientChain, bytes32 recipient, uint256 arbiterFee, uint32 nonce) payable returns (uint64)","function receiveTbtc(bytes calldata encodedVm)"],n),this.tbtcTokenAddr=r.canonicalAddress(i)}static async fromRpc(t,e){const[n,a]=await d.EvmPlatform.chainFromRpc(t),i=e[a];if(i.network!==n)throw new Error(`Network mismatch: ${i.network} != ${n}`);return new c(n,a,t,i.contracts)}async*transfer(t,e,n){const a=new r.EvmAddress(t).toString(),i=await this.gateway.sendTbtc.populateTransaction(n,r.toChainId(e.chain),e.address.toUniversalAddress().toUint8Array(),0n,0n);i.value=await this.core.getMessageFee(),yield*this.approve(a,n,this.gatewayAddress),yield this.createUnsignedTransaction(r.addFrom(i,a),"TBTCBridge.Send")}async*redeem(t,e){if(e.payloadName!=="GatewayTransfer")throw new Error("Invalid VAA payload");const n=new r.EvmAddress(t).toString(),a=await this.gateway.receiveTbtc.populateTransaction(r.serialize(e));yield this.createUnsignedTransaction(r.addFrom(a,n),"TBTCBridge.Redeem")}async*approve(t,e,n){const a=d.EvmPlatform.getTokenImplementation(this.provider,this.tbtcTokenAddr);if(await a.allowance(t,n)<e){const h=await a.approve.populateTransaction(n,e);yield this.createUnsignedTransaction(r.addFrom(h,t),"TBTC.Approve")}}createUnsignedTransaction(t,e){return new u.EvmUnsignedTransaction(r.addChainId(t,this.chainId),this.network,this.chain,e,!1)}}p.registerProtocol(r._platform,"TBTCBridge",c);exports.EvmTBTCBridge=c;
