import { T as h, l as w, E as T, N as l, V as o, aK as c, ah as p, aL as r, at as g, X as y, aM as m, au as f, aN as u } from "./platform-Cil_UtAQ.mjs";
import { EvmWormholeCore as b } from "./index-BKgliEPZ.mjs";
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 s = h.getNativeTbtcToken(this.chain);
    if (!s)
      throw new Error("Native tbtc token not found");
    this.chainId = w.get(e, t), this.core = new b(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(s);
  }
  static async fromRpc(e, t) {
    const [a, n] = await o.chainFromRpc(e), s = t[n];
    if (s.network !== a)
      throw new Error(`Network mismatch: ${s.network} != ${a}`);
    return new i(a, n, e, s.contracts);
  }
  async *transfer(e, t, a) {
    const n = new c(e).toString(), s = await this.gateway.sendTbtc.populateTransaction(a, p(t.chain), t.address.toUniversalAddress().toUint8Array(), 0n, 0n);
    s.value = await this.core.getMessageFee(), yield* this.approve(n, a, this.gatewayAddress), yield this.createUnsignedTransaction(r(s, n), "TBTCBridge.Send");
  }
  async *redeem(e, t) {
    if (t.payloadName !== "GatewayTransfer")
      throw new Error("Invalid VAA payload");
    const a = new c(e).toString(), n = await this.gateway.receiveTbtc.populateTransaction(g(t));
    yield this.createUnsignedTransaction(r(n, a), "TBTCBridge.Redeem");
  }
  async *approve(e, t, a) {
    const n = o.getTokenImplementation(this.provider, this.tbtcTokenAddr);
    if (await n.allowance(e, a) < t) {
      const d = await n.approve.populateTransaction(a, t);
      yield this.createUnsignedTransaction(r(d, e), "TBTC.Approve");
    }
  }
  createUnsignedTransaction(e, t) {
    return new y(m(e, this.chainId), this.network, this.chain, t, !1);
  }
}
f(u, "TBTCBridge", i);
export {
  i as EvmTBTCBridge
};
