"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getForeignAssetAlgorand = exports.getForeignAssetSolana = exports.getForeignAssetTerra = exports.getForeignAssetEth = exports.getForeignAssetAlephium = void 0;
const web3_1 = require("@alephium/web3");
const web3_js_1 = require("@solana/web3.js");
const js_base64_1 = require("js-base64");
const algorand_1 = require("../algorand");
const ethers_contracts_1 = require("../ethers-contracts");
const wasm_1 = require("../solana/wasm");
const utils_1 = require("../utils");
const alephium_1 = require("./alephium");
function getForeignAssetAlephium(tokenBridgeId, provider, originChain, originAsset, groupIndex) {
    return __awaiter(this, void 0, void 0, function* () {
        const remoteTokenPoolId = alephium_1.getTokenPoolId(tokenBridgeId, utils_1.coalesceChainId(originChain), web3_1.binToHex(originAsset), groupIndex);
        try {
            const exists = yield alephium_1.contractExists(remoteTokenPoolId, provider);
            return exists ? remoteTokenPoolId : null;
        }
        catch (e) {
            return null;
        }
    });
}
exports.getForeignAssetAlephium = getForeignAssetAlephium;
/**
 * Returns a foreign asset address on Ethereum for a provided native chain and asset address, AddressZero if it does not exist
 * @param tokenBridgeAddress
 * @param provider
 * @param originChain
 * @param originAsset zero pad to 32 bytes
 * @returns
 */
function getForeignAssetEth(tokenBridgeAddress, provider, originChain, originAsset) {
    return __awaiter(this, void 0, void 0, function* () {
        const tokenBridge = ethers_contracts_1.Bridge__factory.connect(tokenBridgeAddress, provider);
        try {
            return yield tokenBridge.wrappedAsset(utils_1.coalesceChainId(originChain), originAsset);
        }
        catch (e) {
            return null;
        }
    });
}
exports.getForeignAssetEth = getForeignAssetEth;
function getForeignAssetTerra(tokenBridgeAddress, client, originChain, originAsset) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const result = yield client.wasm.contractQuery(tokenBridgeAddress, {
                wrapped_registry: {
                    chain: utils_1.coalesceChainId(originChain),
                    address: js_base64_1.fromUint8Array(originAsset),
                },
            });
            return result.address;
        }
        catch (e) {
            return null;
        }
    });
}
exports.getForeignAssetTerra = getForeignAssetTerra;
/**
 * Returns a foreign asset address on Solana for a provided native chain and asset address
 * @param connection
 * @param tokenBridgeAddress
 * @param originChain
 * @param originAsset zero pad to 32 bytes
 * @returns
 */
function getForeignAssetSolana(connection, tokenBridgeAddress, originChain, originAsset) {
    return __awaiter(this, void 0, void 0, function* () {
        const { wrapped_address } = yield wasm_1.importTokenWasm();
        const wrappedAddress = wrapped_address(tokenBridgeAddress, originAsset, utils_1.coalesceChainId(originChain));
        const wrappedAddressPK = new web3_js_1.PublicKey(wrappedAddress);
        const wrappedAssetAccountInfo = yield connection.getAccountInfo(wrappedAddressPK);
        return wrappedAssetAccountInfo ? wrappedAddressPK.toString() : null;
    });
}
exports.getForeignAssetSolana = getForeignAssetSolana;
function getForeignAssetAlgorand(client, tokenBridgeId, chain, contract) {
    return __awaiter(this, void 0, void 0, function* () {
        const chainId = utils_1.coalesceChainId(chain);
        if (chainId === utils_1.CHAIN_ID_ALGORAND) {
            return algorand_1.hexToNativeAssetBigIntAlgorand(contract);
        }
        else {
            let { lsa, doesExist } = yield algorand_1.calcLogicSigAccount(client, tokenBridgeId, BigInt(chainId), contract);
            if (!doesExist) {
                return null;
            }
            let asset = yield algorand_1.decodeLocalState(client, tokenBridgeId, lsa.address());
            if (asset.length > 8) {
                const tmp = Buffer.from(asset.slice(0, 8));
                return tmp.readBigUInt64BE(0);
            }
            else
                return null;
        }
    });
}
exports.getForeignAssetAlgorand = getForeignAssetAlgorand;
