var T=Object.defineProperty;var p=(i,t,e)=>t in i?T(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var s=(i,t,e)=>p(i,typeof t!="symbol"?t+"":t,e);import{cm as l,af as m,bG as g,ak as d,c4 as y,c7 as o,c9 as f,cb as b,ad as u}from"../main.js";import{E as k,a as C}from"./index-B8PSBQZ9.js";import{C as v}from"./contract-CiHLuMan.js";import{E as h}from"./evm-PXwgtnG4.js";import{r as A}from"./chain-CxM3TvYC.js";import"./create-CPwTP7kz.js";import"./provider-jsonrpc-B1nSkRdE.js";class c{constructor(t,e,a,n){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=a,this.contracts=n,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 r=l.getNativeTbtcToken(this.chain);if(!r)throw new Error("Native tbtc token not found");this.chainId=m.get(t,e),this.core=new k(t,e,a,n),this.gatewayAddress=this.contracts.tbtc,this.gateway=new v(this.gatewayAddress,["function sendTbtc(uint256 amount, uint16 recipientChain, bytes32 recipient, uint256 arbiterFee, uint32 nonce) payable returns (uint64)","function receiveTbtc(bytes calldata encodedVm)"],a),this.tbtcTokenAddr=g(r)}static async fromRpc(t,e){const[a,n]=await h.chainFromRpc(t),r=e[n];if(r.network!==a)throw new Error(`Network mismatch: ${r.network} != ${a}`);return new c(a,n,t,r.contracts)}async*transfer(t,e,a){const n=new d(t).toString(),r=await this.gateway.sendTbtc.populateTransaction(a,y(e.chain),e.address.toUniversalAddress().toUint8Array(),0n,0n);r.value=await this.core.getMessageFee(),yield*this.approve(n,a,this.gatewayAddress),yield this.createUnsignedTransaction(o(r,n),"TBTCBridge.Send")}async*redeem(t,e){if(e.payloadName!=="GatewayTransfer")throw new Error("Invalid VAA payload");const a=new d(t).toString(),n=await this.gateway.receiveTbtc.populateTransaction(f(e));yield this.createUnsignedTransaction(o(n,a),"TBTCBridge.Redeem")}async*approve(t,e,a){const n=h.getTokenImplementation(this.provider,this.tbtcTokenAddr);if(await n.allowance(t,a)<e){const w=await n.approve.populateTransaction(a,e);yield this.createUnsignedTransaction(o(w,t),"TBTC.Approve")}}createUnsignedTransaction(t,e){return new C(b(t,this.chainId),this.network,this.chain,e,!1)}}A(u,"TBTCBridge",c);export{c as EvmTBTCBridge};
