import{dh as h,aA as w,o as T,cU as l,aG as o,cS as p,cW as s,cY as g,c_ as m,c$ as y,ay as f}from"../main.mjs";import{E as u,a as b}from"./index-LvDsy6wR.js";import{E as c}from"./evm-DCydYqBI.js";import"./create-CbNCGANY.js";import"./provider-jsonrpc-DuMpLBV0.js";class i{network;chain;provider;contracts;chainId;core;gatewayAddress;gateway;tbtcTokenAddr;constructor(e,t,a,n){if(this.network=e,this.chain=t,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=h.getNativeTbtcToken(this.chain);if(!r)throw new Error("Native tbtc token not found");this.chainId=w.get(e,t),this.core=new u(e,t,a,n),this.gatewayAddress=this.contracts.tbtc,this.gateway=new T(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=l(r)}static async fromRpc(e,t){const[a,n]=await c.chainFromRpc(e),r=t[n];if(r.network!==a)throw new Error(`Network mismatch: ${r.network} != ${a}`);return new i(a,n,e,r.contracts)}async*transfer(e,t,a){const n=new o(e).toString(),r=await this.gateway.sendTbtc.populateTransaction(a,p(t.chain),t.address.toUniversalAddress().toUint8Array(),0n,0n);r.value=await this.core.getMessageFee(),yield*this.approve(n,a,this.gatewayAddress),yield this.createUnsignedTransaction(s(r,n),"TBTCBridge.Send")}async*redeem(e,t){if(t.payloadName!=="GatewayTransfer")throw new Error("Invalid VAA payload");const a=new o(e).toString(),n=await this.gateway.receiveTbtc.populateTransaction(g(t));yield this.createUnsignedTransaction(s(n,a),"TBTCBridge.Redeem")}async*approve(e,t,a){const n=c.getTokenImplementation(this.provider,this.tbtcTokenAddr);if(await n.allowance(e,a)<t){const d=await n.approve.populateTransaction(a,t);yield this.createUnsignedTransaction(s(d,e),"TBTC.Approve")}}createUnsignedTransaction(e,t){return new b(m(e,this.chainId),this.network,this.chain,t,!1)}}y(f,"TBTCBridge",i);export{i as EvmTBTCBridge};
