"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.getIsWrappedAssetAlgorand = exports.getIsWrappedAssetSol = exports.getIsWrappedAssetTerra = exports.getIsWrappedAssetEth = void 0;
const web3_js_1 = require("@solana/web3.js");
const algosdk_1 = require("algosdk");
const ethers_contracts_1 = require("../ethers-contracts");
const wasm_1 = require("../solana/wasm");
const bigint_1 = require("../utils/bigint");
/**
 * Returns whether or not an asset address on Ethereum is a wormhole wrapped asset
 * @param tokenBridgeAddress
 * @param provider
 * @param assetAddress
 * @returns
 */
function getIsWrappedAssetEth(tokenBridgeAddress, provider, assetAddress) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!assetAddress)
            return false;
        const tokenBridge = ethers_contracts_1.Bridge__factory.connect(tokenBridgeAddress, provider);
        return yield tokenBridge.isWrappedAsset(assetAddress);
    });
}
exports.getIsWrappedAssetEth = getIsWrappedAssetEth;
// TODO: this doesn't seem right
function getIsWrappedAssetTerra(tokenBridgeAddress, client, assetAddress) {
    return __awaiter(this, void 0, void 0, function* () {
        return false;
    });
}
exports.getIsWrappedAssetTerra = getIsWrappedAssetTerra;
/**
 * Returns whether or not an asset on Solana is a wormhole wrapped asset
 * @param connection
 * @param tokenBridgeAddress
 * @param mintAddress
 * @returns
 */
function getIsWrappedAssetSol(connection, tokenBridgeAddress, mintAddress) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!mintAddress)
            return false;
        const { wrapped_meta_address } = yield wasm_1.importTokenWasm();
        const wrappedMetaAddress = wrapped_meta_address(tokenBridgeAddress, new web3_js_1.PublicKey(mintAddress).toBytes());
        const wrappedMetaAddressPK = new web3_js_1.PublicKey(wrappedMetaAddress);
        const wrappedMetaAccountInfo = yield connection.getAccountInfo(wrappedMetaAddressPK);
        return !!wrappedMetaAccountInfo;
    });
}
exports.getIsWrappedAssetSol = getIsWrappedAssetSol;
/**
 * Returns whethor or not an asset on Algorand is a wormhole wrapped asset
 * @param client Algodv2 client
 * @param tokenBridgeId token bridge ID
 * @param assetId Algorand asset index
 * @returns true if the asset is wrapped
 */
function getIsWrappedAssetAlgorand(client, tokenBridgeId, assetId) {
    return __awaiter(this, void 0, void 0, function* () {
        if (assetId === BigInt(0)) {
            return false;
        }
        const tbAddr = algosdk_1.getApplicationAddress(tokenBridgeId);
        const assetInfo = yield client.getAssetByID(bigint_1.safeBigIntToNumber(assetId)).do();
        const creatorAddr = assetInfo.params.creator;
        const creatorAcctInfo = yield client.accountInformation(creatorAddr).do();
        const wormhole = creatorAcctInfo["auth-addr"] === tbAddr;
        return wormhole;
    });
}
exports.getIsWrappedAssetAlgorand = getIsWrappedAssetAlgorand;
