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());
    });
};
import { binToHex } from "@alephium/web3";
import { PublicKey } from "@solana/web3.js";
import { fromUint8Array } from "js-base64";
import { calcLogicSigAccount, decodeLocalState, hexToNativeAssetBigIntAlgorand, } from "../algorand";
import { Bridge__factory } from "../ethers-contracts";
import { importTokenWasm } from "../solana/wasm";
import { CHAIN_ID_ALGORAND, coalesceChainId, } from "../utils";
import { contractExists, getTokenPoolId } from "./alephium";
export function getForeignAssetAlephium(tokenBridgeId, provider, originChain, originAsset, groupIndex) {
    return __awaiter(this, void 0, void 0, function* () {
        const remoteTokenPoolId = getTokenPoolId(tokenBridgeId, coalesceChainId(originChain), binToHex(originAsset), groupIndex);
        try {
            const exists = yield contractExists(remoteTokenPoolId, provider);
            return exists ? remoteTokenPoolId : null;
        }
        catch (e) {
            return null;
        }
    });
}
/**
 * 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
 */
export function getForeignAssetEth(tokenBridgeAddress, provider, originChain, originAsset) {
    return __awaiter(this, void 0, void 0, function* () {
        const tokenBridge = Bridge__factory.connect(tokenBridgeAddress, provider);
        try {
            return yield tokenBridge.wrappedAsset(coalesceChainId(originChain), originAsset);
        }
        catch (e) {
            return null;
        }
    });
}
export 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: coalesceChainId(originChain),
                    address: fromUint8Array(originAsset),
                },
            });
            return result.address;
        }
        catch (e) {
            return null;
        }
    });
}
/**
 * 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
 */
export function getForeignAssetSolana(connection, tokenBridgeAddress, originChain, originAsset) {
    return __awaiter(this, void 0, void 0, function* () {
        const { wrapped_address } = yield importTokenWasm();
        const wrappedAddress = wrapped_address(tokenBridgeAddress, originAsset, coalesceChainId(originChain));
        const wrappedAddressPK = new PublicKey(wrappedAddress);
        const wrappedAssetAccountInfo = yield connection.getAccountInfo(wrappedAddressPK);
        return wrappedAssetAccountInfo ? wrappedAddressPK.toString() : null;
    });
}
export function getForeignAssetAlgorand(client, tokenBridgeId, chain, contract) {
    return __awaiter(this, void 0, void 0, function* () {
        const chainId = coalesceChainId(chain);
        if (chainId === CHAIN_ID_ALGORAND) {
            return hexToNativeAssetBigIntAlgorand(contract);
        }
        else {
            let { lsa, doesExist } = yield calcLogicSigAccount(client, tokenBridgeId, BigInt(chainId), contract);
            if (!doesExist) {
                return null;
            }
            let asset = yield decodeLocalState(client, tokenBridgeId, lsa.address());
            if (asset.length > 8) {
                const tmp = Buffer.from(asset.slice(0, 8));
                return tmp.readBigUInt64BE(0);
            }
            else
                return null;
        }
    });
}
