"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.getIsWrappedAssetSui = exports.getIsWrappedAssetAptos = exports.getIsWrappedAssetNear = exports.getIsWrappedAssetAlgorand = exports.getIsWrappedAssetSol = exports.getIsWrappedAssetSolana = exports.getIsWrappedAssetInjective = exports.getIsWrappedAssetTerra = exports.getIsWrappedAssetEth = void 0;
const algosdk_1 = require("algosdk");
const ethers_contracts_1 = require("../ethers-contracts");
const tokenBridge_1 = require("../solana/tokenBridge");
const sui_1 = require("../sui");
const utils_1 = require("../utils");
const bigint_1 = require("../utils/bigint");
const getForeignAsset_1 = require("./getForeignAsset");
/**
 * 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;
/**
 * Checks if the asset is a wrapped asset
 * @param tokenBridgeAddress The address of the Injective token bridge contract
 * @param client Connection/wallet information
 * @param assetAddress Address of the asset in Injective format
 * @returns true if asset is a wormhole wrapped asset
 */
function getIsWrappedAssetInjective(tokenBridgeAddress, client, assetAddress) {
    return __awaiter(this, void 0, void 0, function* () {
        const hexified = utils_1.tryNativeToHexString(assetAddress, "injective");
        const result = yield getForeignAsset_1.getForeignAssetInjective(tokenBridgeAddress, client, utils_1.CHAIN_ID_INJECTIVE, new Uint8Array(Buffer.from(hexified)));
        if (result === null) {
            return false;
        }
        return true;
    });
}
exports.getIsWrappedAssetInjective = getIsWrappedAssetInjective;
/**
 * Returns whether or not an asset on Solana is a wormhole wrapped asset
 * @param connection
 * @param tokenBridgeAddress
 * @param mintAddress
 * @param [commitment]
 * @returns
 */
function getIsWrappedAssetSolana(connection, tokenBridgeAddress, mintAddress, commitment) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!mintAddress) {
            return false;
        }
        return tokenBridge_1.getWrappedMeta(connection, tokenBridgeAddress, mintAddress, commitment)
            .catch((_) => null)
            .then((meta) => meta != null);
    });
}
exports.getIsWrappedAssetSolana = getIsWrappedAssetSolana;
exports.getIsWrappedAssetSol = getIsWrappedAssetSolana;
/**
 * 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;
function getIsWrappedAssetNear(tokenBridge, asset) {
    return asset.endsWith("." + tokenBridge);
}
exports.getIsWrappedAssetNear = getIsWrappedAssetNear;
/**
 * Determines whether or not given address is wrapped or native to Aptos.
 * @param client Client used to transfer data to/from Aptos node
 * @param tokenBridgeAddress Address of token bridge
 * @param assetFullyQualifiedType Fully qualified type of asset
 * @returns True if asset is wrapped
 */
function getIsWrappedAssetAptos(client, tokenBridgeAddress, assetFullyQualifiedType) {
    return __awaiter(this, void 0, void 0, function* () {
        assetFullyQualifiedType = utils_1.ensureHexPrefix(assetFullyQualifiedType);
        try {
            // get origin info from asset address
            yield client.getAccountResource(utils_1.coalesceModuleAddress(assetFullyQualifiedType), `${tokenBridgeAddress}::state::OriginInfo`);
            return true;
        }
        catch (_a) {
            return false;
        }
    });
}
exports.getIsWrappedAssetAptos = getIsWrappedAssetAptos;
function getIsWrappedAssetSui(provider, tokenBridgeStateObjectId, type) {
    return __awaiter(this, void 0, void 0, function* () {
        // An easy way to determine if given asset isn't a wrapped asset is to ensure
        // module name and struct name are coin and COIN respectively.
        if (!type.endsWith("::coin::COIN")) {
            return false;
        }
        try {
            // This call errors if the type doesn't exist in the TokenRegistry
            yield sui_1.getTokenFromTokenRegistry(provider, tokenBridgeStateObjectId, type);
            return true;
        }
        catch (e) {
            if (sui_1.isSuiError(e) && e.code === -32000 && e.message.includes("RPC Error")) {
                return false;
            }
            throw e;
        }
    });
}
exports.getIsWrappedAssetSui = getIsWrappedAssetSui;
