import { isSolanaAddress as c } from "../chains/isSolanaAddress.js";
const s = "https://solana-mainnet.g.alchemy.com/v2", u = async (n, t) => {
  if (!t)
    return null;
  const r = `${s}/${t}`;
  try {
    const e = await (await fetch(r, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        jsonrpc: "2.0",
        id: 1,
        method: "getBalance",
        params: [n]
      })
    })).json();
    return e.error ? null : e.result?.value?.toString() ?? "0";
  } catch {
    return null;
  }
}, l = async (n, t, r) => {
  if (!n.contractAddress || !r)
    return null;
  const a = `${s}/${r}`;
  try {
    const o = await (await fetch(a, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        jsonrpc: "2.0",
        id: 1,
        method: "getTokenAccountsByOwner",
        params: [
          t,
          {
            mint: n.contractAddress
          },
          {
            encoding: "jsonParsed"
          }
        ]
      })
    })).json();
    return o.error ? null : !o.result?.value || o.result.value.length === 0 ? "0" : o.result.value[0]?.account?.data?.parsed?.info?.tokenAmount?.amount ?? "0";
  } catch {
    return null;
  }
}, p = (n, t, r) => c(t) ? n.symbol === "SOL" && !n.contractAddress ? u(t, r) : l(n, t, r) : null;
export {
  p as getSolanaTokenBalance
};
//# sourceMappingURL=getSolanaTokenBalance.js.map
