var S = Object.defineProperty;
var b = (a, e, t) => e in a ? S(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
var i = (a, e, t) => b(a, typeof e != "symbol" ? e + "" : e, t);
import { ch as v, ci as f, cj as x, cf as I, ck as A, b2 as w, bw as U, b9 as $, bx as m, r as o, ay as k, p as c, m as h, cl as C, bC as T, O as E, bz as F, k as _, ba as u, cm as O, bf as W } from "./index-6NRj6D5S.mjs";
import { A as N } from "./unsignedTransaction-BDOQhqcZ.mjs";
import { A as Q } from "./aptos-Bm_dl-Sm.mjs";
const R = [
  { name: "tokenBridgeAddress", ...f },
  { name: "chain", ...x() },
  {
    name: "domainSeparator",
    binary: "bytes",
    custom: I.encode(A),
    omit: !0
  },
  { name: "tokenId", ...f },
  // from https://github.com/aptos-labs/aptos-core/blob/25696fd266498d81d346fe86e01c330705a71465/aptos-move/framework/aptos-framework/sources/account.move#L90-L95
  { name: "domainSeparator", binary: "bytes", custom: new Uint8Array([255]), omit: !0 }
], P = (a) => v(R, a);
class d {
  constructor(e, t, s, r) {
    i(this, "network");
    i(this, "chain");
    i(this, "connection");
    i(this, "contracts");
    i(this, "chainId");
    i(this, "tokenBridgeAddress");
    this.network = e, this.chain = t, this.connection = s, this.contracts = r, this.chainId = w(t);
    const n = r.tokenBridge;
    if (!n)
      throw new Error(`TokenBridge contract Address for chain ${t} not found`);
    this.tokenBridgeAddress = n;
  }
  static async fromRpc(e, t) {
    const [s, r] = await Q.chainFromRpc(e), n = t[r];
    if (n.network !== s)
      throw new Error("Network mismatch " + n.network + " !== " + s);
    return new d(s, r, e, n.contracts);
  }
  async isWrappedAsset(e) {
    try {
      return await this.getOriginalAsset(e), !0;
    } catch {
      return !1;
    }
  }
  async getOriginalAsset(e) {
    const t = e.toString().split(A), s = await this.connection.getAccountResource({
      accountAddress: t[0],
      resourceType: `${this.tokenBridgeAddress}::state::OriginInfo`
    });
    if (!s)
      throw U(e.toString());
    const r = $(parseInt(s.token_chain.number)), n = new m(s.token_address.external_address);
    return { chain: r, address: n };
  }
  async getTokenUniversalAddress(e) {
    return new m(o.encode(k(e.toString()), !0));
  }
  async getTokenNativeAddress(e, t) {
    const s = e === this.chain ? await this.getTypeFromExternalAddress(t.toString()) : await this.getAssetFullyQualifiedType({ chain: e, address: t });
    if (!s)
      throw new Error("Invalid asset address.");
    return new c(s);
  }
  async hasWrappedAsset(e) {
    try {
      return await this.getWrappedAsset(e), !0;
    } catch {
    }
    return !1;
  }
  async getWrappedAsset(e) {
    if (h(e.address))
      throw new Error("native asset cannot be a wrapped asset");
    const t = await this.getAssetFullyQualifiedType(e);
    if (!t)
      throw new Error("Invalid asset address.");
    return await this.connection.getAccountResource({
      accountAddress: C(t),
      resourceType: `${this.tokenBridgeAddress}::state::OriginInfo`
    }), T(this.chain, t);
  }
  async isTransferCompleted(e) {
    const s = (await this.connection.getAccountResource({
      accountAddress: this.tokenBridgeAddress,
      resourceType: `${this.tokenBridgeAddress}::state::State`
    })).consumed_vaas.elems.handle;
    try {
      return await this.connection.getTableItem({
        handle: s,
        data: {
          key_type: "vector<u8>",
          value_type: "u8",
          key: `0x${E.Buffer.from(F(e.hash)).toString("hex")}`
        }
      }), !0;
    } catch {
      return !1;
    }
  }
  async getWrappedNative() {
    return T(this.chain, _);
  }
  async *createAttestation(e, t) {
    const s = { chain: this.chain, address: new c(e) }, r = await this.getAssetFullyQualifiedType(s);
    if (!r)
      throw new Error("Invalid asset address.");
    yield this.createUnsignedTx({
      function: `${this.tokenBridgeAddress}::attest_token::attest_token_entry`,
      typeArguments: [r],
      functionArguments: []
    }, "Aptos.AttestToken");
  }
  async *submitAttestation(e, t) {
    yield this.createUnsignedTx({
      function: `${this.tokenBridgeAddress}::wrapped::create_wrapped_coin_type`,
      typeArguments: [],
      functionArguments: [u(e)]
    }, "Aptos.CreateWrappedCoinType");
    const s = await this.getAssetFullyQualifiedType(e.payload.token);
    if (!s)
      throw new Error("Invalid asset address.");
    yield this.createUnsignedTx({
      function: `${this.tokenBridgeAddress}::wrapped::create_wrapped_coin`,
      typeArguments: [s],
      functionArguments: [u(e)]
    }, "Aptos.CreateWrappedCoin");
  }
  async *transfer(e, t, s, r, n) {
    const B = 0n, p = 0n, y = h(s) ? _ : s.toString(), l = t.address.toUniversalAddress().toUint8Array(), g = w(t.chain);
    n ? yield this.createUnsignedTx({
      function: `${this.tokenBridgeAddress}::transfer_tokens::transfer_tokens_with_payload_entry`,
      typeArguments: [y],
      functionArguments: [r, g, l, p, n]
    }, "Aptos.TransferTokensWithPayload") : yield this.createUnsignedTx({
      function: `${this.tokenBridgeAddress}::transfer_tokens::transfer_tokens_entry`,
      typeArguments: [y],
      functionArguments: [r, g, l, B, p]
    }, "Aptos.TransferTokens");
  }
  async *redeem(e, t, s = !0) {
    const r = t.payload.token.chain === this.chain ? await this.getTypeFromExternalAddress(t.payload.token.address.toString()) : await this.getAssetFullyQualifiedType(t.payload.token);
    if (!r)
      throw new Error("Invalid asset address.");
    yield this.createUnsignedTx({
      function: `${this.tokenBridgeAddress}::complete_transfer::submit_vaa_and_register_entry`,
      typeArguments: [r],
      functionArguments: [u(t)]
    }, "Aptos.CompleteTransfer");
  }
  async getAssetFullyQualifiedType(e) {
    return e.chain === this.chain ? O(e.address.toString()) ? e.address.toString() : null : `${d.getForeignAssetAddress(this.chain, this.tokenBridgeAddress, e)}::coin::T`;
  }
  /**
   * Given a hash, returns the fully qualified type by querying the corresponding TypeInfo.
   * @param address Hash of fully qualified type
   * @returns The fully qualified type associated with the given hash
   */
  async getTypeFromExternalAddress(e) {
    try {
      const t = await this.connection.getAccountResource({
        accountAddress: this.tokenBridgeAddress,
        resourceType: `${this.tokenBridgeAddress}::state::State`
      }), { handle: s } = t.native_infos, r = await this.connection.getTableItem({
        handle: s,
        data: {
          key_type: `${this.tokenBridgeAddress}::token_hash::TokenHash`,
          value_type: "0x1::type_info::TypeInfo",
          key: { hash: e }
        }
      });
      return r ? [
        r.account_address,
        String.fromCharCode(...o.decode(r.module_name)),
        String.fromCharCode(...o.decode(r.struct_name))
      ].join(A) : null;
    } catch {
      return null;
    }
  }
  /**
   * Derive the module address for an asset defined by the given origin chain and address.
   * @param tokenBridgeAddress Address of token bridge (32 bytes)
   * @param originChain Chain ID of chain that original asset is from
   * @param originAddress Native address of asset
   * @returns The module address for the given asset
   */
  static getForeignAssetAddress(e, t, s) {
    if (h(s.address))
      throw new Error("Invalid token address");
    const r = P({
      chain: s.chain,
      tokenBridgeAddress: new c(t).toUniversalAddress(),
      tokenId: s.address.toUniversalAddress()
    });
    return o.encode(k(r), !0);
  }
  createUnsignedTx(e, t, s = !1) {
    return new N(e, this.network, this.chain, t, s);
  }
}
W("Aptos", "TokenBridge", d);
export {
  d as AptosTokenBridge
};
