"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.getForeignAssetSol = exports.getForeignAssetTerra = exports.getForeignAssetEth = void 0;
const web3_js_1 = require("@solana/web3.js");
const js_base64_1 = require("js-base64");
const __1 = require("..");
const ethers_contracts_1 = require("../ethers-contracts");
const wasm_1 = require("../solana/wasm");
const utils_1 = require("../utils");
/**
 * 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 originChainId = utils_1.coalesceChainId(originChain);
        const tokenBridge = ethers_contracts_1.NFTBridge__factory.connect(tokenBridgeAddress, provider);
        try {
            if (originChainId === __1.CHAIN_ID_SOLANA) {
                // All NFTs from Solana are minted to the same address, the originAsset is encoded as the tokenId as
                // BigNumber.from(new PublicKey(originAsset).toBytes()).toString()
                const addr = yield tokenBridge.wrappedAsset(originChain, "0x0101010101010101010101010101010101010101010101010101010101010101");
                return addr;
            }
            return yield tokenBridge.wrappedAsset(originChainId, originAsset);
        }
        catch (e) {
            return null;
        }
    });
}
exports.getForeignAssetEth = getForeignAssetEth;
/**
 * Returns a foreign asset address on Terra for a provided native chain and asset address
 * @param tokenBridgeAddress
 * @param client
 * @param originChain
 * @param originAsset
 * @returns
 */
function getForeignAssetTerra(tokenBridgeAddress, client, originChain, originAsset) {
    return __awaiter(this, void 0, void 0, function* () {
        const originChainId = utils_1.coalesceChainId(originChain);
        try {
            const address = originChain == __1.CHAIN_ID_SOLANA
                ? "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
                : js_base64_1.fromUint8Array(originAsset);
            const result = yield client.wasm.contractQuery(tokenBridgeAddress, {
                wrapped_registry: {
                    chain: originChainId,
                    address,
                },
            });
            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 tokenBridgeAddress
 * @param originChain
 * @param originAsset zero pad to 32 bytes
 * @returns
 */
function getForeignAssetSol(tokenBridgeAddress, originChain, originAsset, tokenId) {
    return __awaiter(this, void 0, void 0, function* () {
        const originChainId = utils_1.coalesceChainId(originChain);
        const { wrapped_address } = yield wasm_1.importNftWasm();
        const wrappedAddress = wrapped_address(tokenBridgeAddress, originAsset, originChainId, tokenId);
        const wrappedAddressPK = new web3_js_1.PublicKey(wrappedAddress);
        // we don't require NFT accounts to exist, so don't check them.
        return wrappedAddressPK.toString();
    });
}
exports.getForeignAssetSol = getForeignAssetSol;
