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