import { StargateClient } from '@cosmjs/stargate';
import { SecretNetworkClient } from 'secretjs';
import { C as CHAIN_IDS, f as formatBNToReadable } from './index-DSDEoKAQ.js';
import '@0xsquid/squid-types';
import 'viem';
import 'react';
import '@tanstack/react-query';
import '@cosmjs/encoding';
import 'bitcoinjs-lib';
import '@bitcoinerlab/secp256k1';
import 'axios';
import '@walletconnect/modal';
import '@walletconnect/universal-provider';
import 'ripple-address-codec';
import '@solana/spl-token';
import '@solana/wallet-standard-wallet-adapter-base';
import '@solana/web3.js';
import '@stellar/stellar-sdk';
import '@mysten/wallet-standard';
import 'ethers';
import 'bignumber.js';
import 'countries-list';
import 'currency-symbol-map';
import 'fuse.js';
import 'zustand';
import 'zustand/middleware';
import 'wagmi';
import '@safe-global/safe-apps-sdk';
import '@wallet-standard/core';
import '@mysten/slush-wallet';
import '@mysten/sui/client';
import 'wagmi/connectors';
import '@cosmjs/proto-signing';
import '@mysten/sui/transactions';
import '@cosmjs/cosmwasm-stargate';
import '@injectivelabs/sdk-ts/dist/cjs/core/stargate/index.js';
import '@0xsquid/sdk';
import 'viem/chains';

/**
 * Fetch secret network token balance
 * Using the permit signature, see permit function for more details
 * @param secretJS
 * @param contract
 * @param chainId
 * @param walletAddress
 * @param permit
 * @returns
 */
const getTokenBalance = async (secretJS, contract, permit) => {
    if (permit) {
        const msg = {
            balance: {},
        };
        const result = await secretJS.query.compute.queryContract({
            contract_address: contract.address,
            code_hash: contract.codeHash,
            query: {
                with_permit: {
                    query: msg,
                    permit,
                },
            },
        });
        return result;
    }
    return -1;
};
const getPermit = async (chainId, contracts, address) => {
    const contractsString = contracts.join("_");
    const permKey = `perm_${chainId}_${contractsString}_${address}`;
    let permit;
    const permitStored = window.localStorage.getItem(permKey);
    if (permitStored)
        permit = JSON.parse(permitStored);
    // Not able to fetch permit signature from local storage,
    // Ask user to sign message
    if (!permit) {
        try {
            const result = await window.keplr.signAmino(chainId, address, {
                chain_id: chainId,
                account_number: "0",
                sequence: "0",
                fee: {
                    amount: [{ denom: "uscrt", amount: "0" }],
                    gas: "1",
                },
                msgs: [
                    {
                        type: "query_permit",
                        value: {
                            permit_name: "secret-bridge-balance",
                            allowed_tokens: contracts,
                            permissions: ["balance"],
                        },
                    },
                ],
                memo: "",
            }, {
                preferNoSetFee: true,
                preferNoSetMemo: true,
            });
            permit = {
                params: {
                    permit_name: "secret-bridge-balance",
                    allowed_tokens: contracts,
                    chain_id: chainId,
                    permissions: ["balance"],
                },
                signature: result.signature,
            };
            window.localStorage.setItem(permKey, JSON.stringify(permit));
        }
        catch (err) {
            console.error("--- PERMIT ERROR ---");
            console.error(err);
        }
    }
    return permit;
};
/**
 * Fetches the secret balance of the user
 * This has a different logic than the other balances because Secret network hides the balance of the user by design
 * So we need to fetch the balance in a different way
 */
const fetchAllSecretBalances = async (chainData, userAddress, secretTokens, keplr) => {
    if (!keplr)
        return [];
    // Enables app to utilize keplr's secret utilities
    await keplr.enable(CHAIN_IDS.SECRET);
    // Create a client that handles the query encryption
    const client = new SecretNetworkClient({
        url: chainData.rest,
        chainId: CHAIN_IDS.SECRET,
        wallet: keplr.getOfflineSignerOnlyAmino(CHAIN_IDS.SECRET),
        encryptionUtils: keplr.getEnigmaUtils(CHAIN_IDS.SECRET),
        walletAddress: userAddress,
    });
    // Get secret tokens
    const permit = await getPermit(CHAIN_IDS.SECRET, secretTokens.map((st) => st.address), userAddress);
    // Fetching all balances in parallel
    const privateTokens = await Promise.all(secretTokens
        .filter((t) => !!t.codeHash)
        .map(async (token) => {
        const result = (await getTokenBalance(client, {
            address: token.address,
            codeHash: token.codeHash ?? "",
        }, permit));
        return {
            ...token,
            balance: formatBNToReadable(BigInt(result.balance.amount), token?.decimals),
        };
    }));
    // Use Stargate getBalance for SCRT
    const stargateClient = await StargateClient.connect(chainData.rpc);
    const nativeSecretToken = secretTokens.find((t) => t.address === "uscrt");
    const publicTokenBalance = await stargateClient.getBalance(userAddress, "uscrt");
    const publicTokenWithBalance = {
        ...nativeSecretToken,
        balance: formatBNToReadable(BigInt(publicTokenBalance.amount), nativeSecretToken?.decimals ?? 18),
    };
    return [...privateTokens, publicTokenWithBalance];
};

export { fetchAllSecretBalances, getPermit, getTokenBalance };
//# sourceMappingURL=secretService-L7xBX95y.js.map
