import { ag as D, aD as x, aE as ne, aF as Q, aG as N, aH as S, aI as E, k as m, aJ as y, G as _, e as v, h as B, aK as se, aL as oe, aM as F, aN as ae, f as R, H as Y, aO as ie, aP as P, aQ as ce, aR as de, m as X, aS as le, aT as ge, aU as ue, aV as K } from "./unsignedTransaction-DCDxTFq8.mjs";
import "./index-zIzzPp6D.mjs";
import { l as fe, di as M, ai as H, L as he, ah as I, i as b, N as pe, t as q, ap as we, at as V, ar as ye, m as $, an as me, ao as G, au as Z } from "./platform-Cil_UtAQ.mjs";
const L = async (A, t, e) => {
  if (!D(e))
    throw new Error(`Invalid Sui type: ${e}`);
  const a = await x(A, t);
  if (!a)
    throw new Error(`Unable to fetch object fields from token bridge state. Object ID: ${t}`);
  const n = a.token_registry.fields?.id?.id;
  if (!n)
    throw new Error("Unable to fetch token registry object ID");
  const o = ne(a.token_registry?.type);
  if (!o)
    throw new Error("Unable to fetch token registry package ID");
  return A.getDynamicFieldObject({
    parentId: n,
    name: {
      type: `${o}::token_registry::Key<${e}>`,
      value: {
        dummy_field: !1
      }
    }
  });
}, U = async (A, t, e, a) => {
  const n = await x(A, t);
  if (!n)
    throw new Error("Unable to fetch object fields from token bridge state");
  const o = n.token_registry?.fields?.coin_types, d = o?.fields?.id?.id;
  if (!d)
    throw new Error("Unable to fetch coin types");
  const s = Q(o?.type);
  if (!s)
    throw new Error("Unable to get key type");
  const r = await A.getDynamicFieldObject({
    parentId: d,
    name: {
      type: s,
      value: {
        addr: [...e],
        chain: a
      }
    }
  });
  if (r.error) {
    if (r.error.code === "dynamicFieldNotFound")
      return null;
    throw new Error(`Unexpected getDynamicFieldObject response ${r.error}`);
  }
  const l = N(r);
  if (!l)
    return null;
  if (!S(l))
    throw new Error("What?");
  return "value" in l ? E(l.value) : null;
};
class W {
  network;
  chain;
  provider;
  contracts;
  coreBridgeObjectId;
  tokenBridgeObjectId;
  chainId;
  constructor(t, e, a, n) {
    this.network = t, this.chain = e, this.provider = a, this.contracts = n, this.chainId = fe.get(t, e);
    const o = this.contracts.tokenBridge;
    if (!o)
      throw new Error(`Wormhole Token Bridge contract for domain ${e} not found`);
    const d = this.contracts.coreBridge;
    if (!d)
      throw new Error(`Wormhole Token Bridge contract for domain ${e} not found`);
    this.tokenBridgeObjectId = o, this.coreBridgeObjectId = d;
  }
  static async fromRpc(t, e) {
    const [a, n] = await m.chainFromRpc(t), o = e[n];
    if (o.network !== a)
      throw new Error(`Network mismatch: ${o.network} != ${a}`);
    return new W(a, n, t, o.contracts);
  }
  async isWrappedAsset(t) {
    try {
      return await this.getOriginalAsset(t), !0;
    } catch {
      return !1;
    }
  }
  async getOriginalAsset(t) {
    let e = t.getCoinType();
    if (!D(e))
      throw new Error(`Invalid Sui type: ${e}`);
    const a = await L(this.provider, this.tokenBridgeObjectId, e), n = N(a);
    if (!n)
      throw M(e);
    if (!S(n))
      throw new Error("Expected fields to be a MoveStruct");
    if (!("value" in n))
      throw new Error("Expected a `value` key in fields of MoveStruct");
    const o = n.value;
    if (!y(o))
      throw new Error("Expected fields to be a MoveStruct");
    const d = E(o.type);
    if (e = E(e), d.includes(`wrapped_asset::WrappedAsset<${e}>`)) {
      const s = o.fields.info;
      if (!y(s))
        throw new Error("Expected fields to be a MoveStruct");
      const r = s.fields.token_address;
      if (!y(r))
        throw new Error("Expected fields to be a MoveStruct");
      if (!S(r.fields))
        throw new Error("Expected address data to be a MoveObject");
      if (!("value" in r.fields))
        throw new Error("Expected a `value` key in fields of MoveStruct");
      const l = r.fields.value;
      if (!y(l))
        throw new Error("Expected fields to be a MoveStruct");
      const c = new Uint8Array(l.fields.data);
      return {
        chain: he(Number(s.fields.token_chain)),
        address: new H(c)
      };
    }
    throw M(e);
  }
  async getTokenUniversalAddress(t) {
    let e = t.getCoinType();
    if (!D(e))
      throw new Error(`Invalid Sui type: ${e}`);
    const a = await L(this.provider, this.tokenBridgeObjectId, e), n = N(a);
    if (!n)
      throw new Error(`Token of type ${e} has not been registered with the token bridge. Has it been attested?`);
    if (!S(n))
      throw new Error("Expected fields to be a MoveStruct");
    if (!("value" in n))
      throw new Error("Expected a `value` key in fields of MoveStruct");
    const o = n.value;
    if (!y(o))
      throw new Error("Expected fields to be a MoveStruct");
    const d = E(o.type);
    if (e = E(e), d.includes(`native_asset::NativeAsset<${e}>`)) {
      const s = o.fields.token_address;
      if (!y(s))
        throw new Error("Expected fields to be a MoveStruct");
      if (!("value" in s.fields))
        throw new Error("Expected a `value` key in fields of MoveStruct");
      const r = s.fields.value;
      if (!y(r))
        throw new Error("Expected fields to be a MoveStruct");
      const l = new Uint8Array(r.fields.data);
      return new H(l);
    }
    throw new Error(`Token of type ${e} is not a native asset`);
  }
  async getTokenNativeAddress(t, e) {
    const a = await U(this.provider, this.tokenBridgeObjectId, e.toUint8Array(), I(t));
    if (!a)
      throw new Error(`Token ${e.toString()} not found in token registry`);
    return new _(a);
  }
  async hasWrappedAsset(t) {
    try {
      return await this.getWrappedAsset(t), !0;
    } catch {
    }
    return !1;
  }
  async getWrappedAsset(t) {
    if (b(t.address))
      throw new Error("Token Address required, 'native' literal not supported");
    const e = await U(this.provider, this.tokenBridgeObjectId, t.address.toUniversalAddress().toUint8Array(), I(t.chain));
    if (!e)
      throw M(pe(t));
    return q(this.chain, e);
  }
  async isTransferCompleted(t) {
    const e = await x(this.provider, this.tokenBridgeObjectId);
    if (!e)
      throw new Error("Unable to fetch object fields from token bridge state");
    const a = e.consumed_vaas?.fields?.hashes, n = Q(a?.fields?.items?.type);
    if (!n)
      throw new Error("Unable to get key type");
    const o = a?.fields?.items?.fields?.id?.id;
    if (!o)
      throw new Error("Unable to fetch consumed VAAs table");
    const d = await this.provider.getDynamicFieldObject({
      parentId: o,
      name: {
        type: n,
        value: {
          data: [...we(t.hash)]
        }
      }
    });
    if (!d.error)
      return !0;
    if (d.error.code === "dynamicFieldNotFound")
      return !1;
    throw new Error(`Unexpected getDynamicFieldObject response ${d.error}`);
  }
  async *createAttestation(t) {
    const e = 0n, n = t.toString(), o = await this.provider.getCoinMetadata({ coinType: n });
    if (o === null || o.id === null)
      throw new Error(`Coin metadata ID for type ${n} not found`);
    const [d, s] = await this.getPackageIds(), r = new v(), [l] = r.splitCoins(r.gas, [r.pure.u64(e)]), [c] = r.moveCall({
      target: `${s}::attest_token::attest_token`,
      arguments: [r.object(this.tokenBridgeObjectId), r.object(o.id), r.pure.u32(0)],
      typeArguments: [n]
    });
    r.moveCall({
      target: `${d}::publish_message::publish_message`,
      arguments: [
        r.object(this.coreBridgeObjectId),
        l,
        c,
        r.object(B)
      ]
    }), yield this.createUnsignedTx(r, "Sui.TokenBridge.CreateAttestation");
  }
  async *submitAttestation(t, e) {
    const [a, n] = await this.getPackageIds(), o = e.toString(), d = Math.min(t.payload.decimals, 8), s = await this.getCoinBuildOutput(a, n, d), r = await se(s, o);
    yield this.createUnsignedTx(r, "Sui.TokenBridge.PrepareCreateWrapped");
    let l = "", c = "", f = "", h = "", k = "", p = !1;
    for (; !p; ) {
      await new Promise((u) => setTimeout(u, 500));
      const j = await this.provider.queryTransactionBlocks({
        filter: { FromAddress: o },
        options: { showObjectChanges: !0 },
        limit: 3
      });
      for (const u of j.data)
        if ("objectChanges" in u) {
          for (const g of u.objectChanges)
            oe(g) && g.packageId !== void 0 ? l = g.packageId : F(g) && g.objectType.includes("WrappedAssetSetup") ? (c = g.objectId, k = g.objectType.split(", ")[1].replace(">", "")) : F(g) && g.objectType.includes("UpgradeCap") ? f = g.objectId : F(g) && g.objectType.includes("CoinMetadata") && (h = g.objectId);
          if (l !== "" && c !== "" && f !== "" && h !== "") {
            p = !0;
            break;
          } else
            l = "", c = "", f = "", h = "";
        }
    }
    const w = ae(l), i = new v(), [O] = i.moveCall({
      target: `${a}::vaa::parse_and_verify`,
      arguments: [
        i.object(this.coreBridgeObjectId),
        i.pure.vector("u8", V(t)),
        i.object(B)
      ]
    }), [C] = i.moveCall({
      target: `${n}::vaa::verify_only_once`,
      arguments: [i.object(this.tokenBridgeObjectId), O]
    });
    i.moveCall({
      target: `${n}::create_wrapped::complete_registration`,
      arguments: [
        i.object(this.tokenBridgeObjectId),
        i.object(h),
        i.object(c),
        i.object(f),
        C
      ],
      typeArguments: [w, k]
    }), yield this.createUnsignedTx(i, "Sui.TokenBridge.SubmitAttestation");
  }
  async *transfer(t, e, a, n, o) {
    const d = 0n, s = 0n, l = t.toString(), c = (b(a) ? R : a).toString(), f = await m.getCoins(this.provider, t, c), [h, ...k] = f.filter((u) => Y(u.coinType, c));
    if (h === void 0)
      throw new Error(`Coins array doesn't contain any coins of type ${c}`);
    const [p, w] = await this.getPackageIds(), i = new v(), [O] = (() => {
      if (c === R)
        return i.splitCoins(i.gas, [i.pure.u64(n)]);
      {
        const u = i.object(h.coinObjectId);
        return k.length && i.mergeCoins(u, k.map((g) => i.object(g.coinObjectId))), i.splitCoins(u, [i.pure.u64(n)]);
      }
    })(), [C] = i.splitCoins(i.gas, [i.pure.u64(d)]), [j] = i.moveCall({
      target: `${w}::state::verified_asset`,
      arguments: [i.object(this.tokenBridgeObjectId)],
      typeArguments: [c]
    });
    if (o) {
      if (!l)
        throw new Error("senderAddress is required for transfer with payload");
      let u = !1;
      const g = await (async () => {
        const J = await ie(this.provider, p, l);
        if (J !== null)
          return i.object(J);
        {
          const [re] = i.moveCall({
            target: `${p}::emitter::new`,
            arguments: [i.object(this.coreBridgeObjectId)]
          });
          return u = !0, re;
        }
      })(), [T, ee] = i.moveCall({
        target: `${w}::transfer_tokens_with_payload::prepare_transfer`,
        arguments: [
          g,
          j,
          O,
          i.pure.u16(I(e.chain)),
          i.pure.vector("u8", e.address.toUint8Array()),
          i.pure.vector("u8", o),
          i.pure.u32(0)
        ],
        typeArguments: [c]
      });
      i.moveCall({
        target: `${w}::coin_utils::return_nonzero`,
        arguments: [ee],
        typeArguments: [c]
      });
      const [te] = i.moveCall({
        target: `${w}::transfer_tokens_with_payload::transfer_tokens_with_payload`,
        arguments: [i.object(this.tokenBridgeObjectId), T],
        typeArguments: [c]
      });
      i.moveCall({
        target: `${p}::publish_message::publish_message`,
        arguments: [
          i.object(this.coreBridgeObjectId),
          C,
          te,
          i.object(B)
        ]
      }), u && i.transferObjects([g], i.pure.address(l)), yield this.createUnsignedTx(i, "Sui.TokenBridge.TransferWithPayload");
    } else {
      const [u, g] = i.moveCall({
        target: `${w}::transfer_tokens::prepare_transfer`,
        arguments: [
          j,
          O,
          i.pure.u16(I(e.chain)),
          i.pure.vector("u8", e.address.toUint8Array()),
          i.pure.u64(s),
          i.pure.u32(0)
        ],
        typeArguments: [c]
      });
      i.moveCall({
        target: `${w}::coin_utils::return_nonzero`,
        arguments: [g],
        typeArguments: [c]
      });
      const [T] = i.moveCall({
        target: `${w}::transfer_tokens::transfer_tokens`,
        arguments: [i.object(this.tokenBridgeObjectId), u],
        typeArguments: [c]
      });
      i.moveCall({
        target: `${p}::publish_message::publish_message`,
        arguments: [
          i.object(this.coreBridgeObjectId),
          C,
          T,
          i.object(B)
        ]
      }), yield this.createUnsignedTx(i, "Sui.TokenBridge.Transfer");
    }
  }
  async *redeem(t, e, a = !0) {
    const n = await U(this.provider, this.tokenBridgeObjectId, e.payload.token.address.toUint8Array(), I(e.payload.token.chain));
    if (!n)
      throw new Error("Unable to fetch token coinType");
    const [o, d] = await this.getPackageIds(), s = new v(), [r] = s.moveCall({
      target: `${o}::vaa::parse_and_verify`,
      arguments: [
        s.object(this.coreBridgeObjectId),
        s.pure.vector("u8", V(e)),
        s.object(B)
      ]
    }), [l] = s.moveCall({
      target: `${d}::vaa::verify_only_once`,
      arguments: [s.object(this.tokenBridgeObjectId), r]
    }), [c] = s.moveCall({
      target: `${d}::complete_transfer::authorize_transfer`,
      arguments: [s.object(this.tokenBridgeObjectId), l],
      typeArguments: [n]
    }), [f] = s.moveCall({
      target: `${d}::complete_transfer::redeem_relayer_payout`,
      arguments: [c],
      typeArguments: [n]
    });
    s.moveCall({
      target: `${d}::coin_utils::return_nonzero`,
      arguments: [f],
      typeArguments: [n]
    }), yield this.createUnsignedTx(s, "Sui.TokenBridge.Redeem");
  }
  async getWrappedNative() {
    return q(this.chain, R);
  }
  async getPackageIds() {
    return Promise.all([
      P(this.provider, this.coreBridgeObjectId),
      P(this.provider, this.tokenBridgeObjectId)
    ]);
  }
  async getCoinBuildOutput(t, e, a) {
    if (a > 8)
      throw new Error("Decimals is capped at 8");
    const n = (await ce(this.provider, this.tokenBridgeObjectId))?.replace("0x", "");
    if (!n)
      throw new Error(`Original token bridge package ID not found for object ID ${this.tokenBridgeObjectId}`);
    const o = "a11ceb0b060000000901000a020a14031e1704350405392d07669f01088502600ae502050cea02160004010b010c0205020d000002000201020003030c020001000104020700000700010001090801010c020a050600030803040202000302010702080007080100020800080303090002070801010b020209000901010608010105010b0202080008030209000504434f494e095478436f6e7465787408565f5f305f325f3011577261707065644173736574536574757004636f696e0e6372656174655f777261707065640b64756d6d795f6669656c6404696e697414707265706172655f726567697374726174696f6e0f7075626c69635f7472616e736665720673656e646572087472616e736665720a74785f636f6e746578740f76657273696f6e5f636f6e74726f6c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002" + n + "00020106010000000001090b0031" + a.toString(16).padStart(2, "0") + "0a0138000b012e110238010200";
    return {
      modules: [ye.encode($.decode(o))],
      dependencies: ["0x1", "0x2", e, t].map((s) => de(s))
    };
  }
  createUnsignedTx(t, e, a = !1) {
    return new X(t, this.network, this.chain, e, a);
  }
}
class z {
  network;
  chain;
  connection;
  contracts;
  tokenBridgeRelayerObjectId;
  coreBridgeObjectId;
  tokenBridgeObjectId;
  fields;
  constructor(t, e, a, n) {
    this.network = t, this.chain = e, this.connection = a, this.contracts = n;
    const { tokenBridge: o, tokenBridgeRelayer: d, coreBridge: s } = n;
    if (!o || !d || !s)
      throw new Error(`Some object IDs for ${e} Automatic Token Bridge not found`);
    this.tokenBridgeRelayerObjectId = d, this.tokenBridgeObjectId = o, this.coreBridgeObjectId = s;
  }
  static async fromRpc(t, e) {
    const [a, n] = await m.chainFromRpc(t), o = e[n];
    if (o.network !== a)
      throw new Error(`Network mismatch for chain ${n}: ${o.network} != ${a}`);
    return new z(a, n, t, o.contracts);
  }
  async *transfer(t, e, a, n, o) {
    const s = new _(b(a) ? m.nativeTokenId(this.network, this.chain).address : a).getCoinType(), { coreBridge: r, tokenBridge: l } = await this.getPackageIds(), c = new v(), f = BigInt(0), [h] = c.splitCoins(c.gas, [c.pure.u64(f)]), [k] = await (async () => {
      if (b(a))
        return c.splitCoins(c.gas, [c.pure.u64(n)]);
      {
        const C = await m.getCoins(this.connection, t, s), [j, ...u] = C.filter((T) => T.coinType === s);
        if (j === void 0)
          throw new Error(`Coins array doesn't contain any coins of type ${s}`);
        const g = c.object(j.coinObjectId);
        return u.length && c.mergeCoins(g, u.map((T) => c.object(T.coinObjectId))), c.splitCoins(g, [c.pure.u64(n)]);
      }
    })(), [p] = c.moveCall({
      target: `${l}::state::verified_asset`,
      arguments: [c.object(this.tokenBridgeObjectId)],
      typeArguments: [s]
    }), w = await this.getPackageId(), [i] = c.moveCall({
      target: `${w}::transfer::transfer_tokens_with_relay`,
      arguments: [
        c.object(this.tokenBridgeRelayerObjectId),
        k,
        p,
        c.pure.u64(o ?? 0n),
        c.pure.u16(I(e.chain)),
        c.pure.address($.encode(e.address.toUint8Array(), !0)),
        c.pure.u32(123)
      ],
      typeArguments: [s]
    }), [O] = c.moveCall({
      target: `${l}::transfer_tokens_with_payload::transfer_tokens_with_payload`,
      arguments: [c.object(this.tokenBridgeObjectId), i],
      typeArguments: [s]
    });
    c.moveCall({
      target: `${r}::publish_message::publish_message`,
      arguments: [
        c.object(this.coreBridgeObjectId),
        h,
        O,
        c.object(B)
      ]
    }), yield this.createUnsignedTx(c, "AutomaticTokenBridge.transfer");
  }
  async *redeem(t, e) {
    const { coreBridge: a, tokenBridge: n } = await this.getPackageIds(), { address: o, chain: d } = e.payload.token, s = await U(this.connection, this.tokenBridgeObjectId, o.toUniversalAddress().toUint8Array(), I(d));
    if (!s)
      throw new Error("Unable to fetch token coinType");
    const r = new v(), [l] = r.moveCall({
      target: `${a}::vaa::parse_and_verify`,
      arguments: [
        r.object(this.coreBridgeObjectId),
        r.pure.vector("u8", V(e)),
        r.object(B)
      ]
    }), [c] = r.moveCall({
      target: `${n}::vaa::verify_only_once`,
      arguments: [r.object(this.tokenBridgeObjectId), l]
    }), [f] = r.moveCall({
      target: `${n}::complete_transfer_with_payload::authorize_transfer`,
      arguments: [r.object(this.tokenBridgeObjectId), c],
      typeArguments: [s]
    }), h = await this.getPackageId();
    r.moveCall({
      target: `${h}::redeem::complete_transfer`,
      arguments: [r.object(this.tokenBridgeRelayerObjectId), f],
      typeArguments: [s]
    }), yield this.createUnsignedTx(r, "AutomaticTokenBridge.redeem");
  }
  async getRelayerFee(t, e) {
    const a = b(e) ? m.nativeTokenId(this.network, this.chain) : e, n = await this.getTokenInfo(a.toString());
    if (n === null)
      throw new Error("Unsupported token for relay");
    const o = await this.getFields(), d = await this.connection.getDynamicFieldObject({
      parentId: this.tokenBridgeRelayerObjectId,
      name: { type: "vector<u8>", value: Array.from(me.encode("relayer_fees")) }
    });
    if (!d.data || !d.data.content)
      throw d.error ? new Error("Failed to get relayer fees: " + JSON.stringify(d.error)) : new Error("Unable to compute relayer fee");
    const { content: s } = d.data;
    if (!y(s) || !le(s.fields.id))
      throw new Error("Unable to compute relayer fee");
    const r = await this.connection.getDynamicFieldObject({
      parentId: s.fields.id.id,
      name: { type: "u16", value: I(t) }
    });
    if (!r.data || !r.data.content)
      throw r.error ? new Error("Failed to get relayer fees: " + JSON.stringify(d.error)) : new Error("Unable to compute relayer fee");
    const { content: l } = r.data;
    if (!y(l))
      throw new Error("Unable to compute relayer fee");
    const c = await m.getDecimals(this.network, this.chain, this.connection, e.toString()), f = n.swap_rate, h = o.relayer_fee_precision, k = o.swap_rate_precision, p = l.fields.value;
    return 10n ** BigInt(c) * BigInt(p) * BigInt(k) / (BigInt(f) * BigInt(h));
  }
  async maxSwapAmount(t) {
    const a = (b(t) ? m.nativeTokenId(this.network, this.chain) : t).toString(), n = await this.connection.getCoinMetadata({ coinType: a });
    if (!n)
      throw new Error("metadata is null");
    const o = await this.getPackageId(), d = new v();
    d.moveCall({
      // Calculates the max number of tokens the recipient can convert to native
      // Sui. The max amount of native assets the contract will swap with the
      // recipient is governed by the `max_native_swap_amount` variable.
      target: `${o}::redeem::calculate_max_swap_amount_in`,
      arguments: [d.object(this.tokenBridgeRelayerObjectId), d.pure.u8(n.decimals)],
      typeArguments: [a]
    });
    const s = await this.connection.devInspectTransactionBlock({
      transactionBlock: d,
      sender: $.encode(new Uint8Array(32))
    });
    if (!s.results || s.results.length == 0 || !s.results[0]?.returnValues || s.results[0]?.returnValues.length !== 1)
      throw Error("swap rate not set");
    return G.decode(new Uint8Array(s.results[0].returnValues[0][0].toReversed()));
  }
  async nativeTokenAmount(t, e) {
    const n = (b(t) ? m.nativeTokenId(this.network, this.chain) : t).toString(), o = await this.connection.getCoinMetadata({ coinType: n });
    if (!o)
      throw new Error("metadata is null");
    const d = await this.getPackageId(), s = new v();
    s.moveCall({
      // Calculates the amount of native Sui that the recipient will receive
      // for swapping the `to_native_amount` of tokens.
      target: `${d}::redeem::calculate_native_swap_amount_out`,
      arguments: [
        s.object(this.tokenBridgeRelayerObjectId),
        s.pure.u64(e),
        s.pure.u8(o.decimals)
      ],
      typeArguments: [n]
    });
    const r = await this.connection.devInspectTransactionBlock({
      transactionBlock: s,
      sender: $.encode(new Uint8Array(32))
    });
    if (!r.results || r.results.length == 0 || !r.results[0]?.returnValues || r.results[0]?.returnValues.length !== 1)
      throw Error("swap rate not set");
    return G.decode(new Uint8Array(r.results[0].returnValues[0][0].toReversed()));
  }
  async getRegisteredTokens() {
    const e = (await this.getFields()).registered_tokens.fields.id.id;
    return (await this.connection.getDynamicFields({
      parentId: e
    })).data.map((o) => {
      const { address: d, module: s, name: r } = ge(o.objectType);
      return new _([d, s, r].join(ue));
    });
  }
  async isRegisteredToken(t) {
    const e = new _(b(t) ? m.nativeTokenId(this.network, this.chain).address : t).unwrap();
    try {
      return await this.getTokenInfo(e) !== null;
    } catch (a) {
      console.error(a);
    }
    return !1;
  }
  async getTokenInfo(t) {
    const e = await this.getFields(), n = new _(e.registered_tokens.type).getPackageId(), o = e.registered_tokens.fields.id.id, d = new _(t), s = Y(K, d.unwrap()) ? K : d.getCoinType();
    try {
      const r = await this.connection.getDynamicFieldObject({
        parentId: o,
        name: {
          type: `${n}::registered_tokens::Key<${s}>`,
          value: { dummy_field: !1 }
        }
      });
      if (r.error)
        throw new Error("Failed to get token info: " + JSON.stringify(r.error));
      if (!r.data || !r.data.content)
        throw new Error("Failed to get token info: " + JSON.stringify(r));
      const { content: l } = r.data;
      return y(l) && y(l.fields.value) ? l.fields.value.fields : null;
    } catch (r) {
      if (r?.code === -32e3 && r.message?.includes("RPC Error"))
        return console.error(r), null;
      throw r;
    }
  }
  async getFields() {
    if (!this.fields) {
      const t = await x(this.connection, this.tokenBridgeRelayerObjectId);
      if (t === null)
        throw new Error("Failed to get fields from token bridge relayer state");
      this.fields = t;
    }
    return this.fields;
  }
  async getPackageId() {
    const t = await this.getFields();
    return new _(t.registered_tokens.type).getPackageId();
  }
  async getPackageIds() {
    const [t, e] = await Promise.all([
      P(this.connection, this.coreBridgeObjectId),
      P(this.connection, this.tokenBridgeObjectId)
    ]);
    return { coreBridge: t, tokenBridge: e };
  }
  createUnsignedTx(t, e, a = !1) {
    return new X(t, this.network, this.chain, e, a);
  }
}
Z("Sui", "TokenBridge", W);
Z("Sui", "AutomaticTokenBridge", z);
export {
  z as SuiAutomaticTokenBridge,
  W as SuiTokenBridge,
  U as getTokenCoinType,
  L as getTokenFromTokenRegistry
};
