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{cI as l,ao as m,ch as g,at as d,cr as y,cu as o,cw as f,cy as u,am as b}from"../main.js";import{E as C,a as k}from"./index-CYFq9U58.js";import{C as v}from"./contract-bGGxC9_m.js";import{E as h}from"./evm-DwqzhXfk.js";import{r as A}from"./chain-DF9mIQM9.js";import"./create-CbRd6EbF.js";import"./provider-jsonrpc-DTnJKzTS.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 C(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 k(u(t,this.chainId),this.network,this.chain,e,!1)}}A(b,"TBTCBridge",c);export{c as EvmTBTCBridge};
