"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIsTransferCompletedAptos = exports.getIsTransferCompletedSolana = exports.getIsTransferCompletedTerra = exports.getIsTransferCompletedEth = void 0;
const axios_1 = __importDefault(require("axios"));
const _1 = require(".");
const __1 = require("..");
const bridge_1 = require("../bridge");
const ethers_contracts_1 = require("../ethers-contracts");
const wormhole_1 = require("../solana/wormhole");
const wormhole_2 = require("../vaa/wormhole");
function getIsTransferCompletedEth(nftBridgeAddress, provider, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const nftBridge = ethers_contracts_1.NFTBridge__factory.connect(nftBridgeAddress, provider);
        const signedVAAHash = bridge_1.getSignedVAAHash(signedVAA);
        return yield nftBridge.isTransferCompleted(signedVAAHash);
    });
}
exports.getIsTransferCompletedEth = getIsTransferCompletedEth;
function getIsTransferCompletedTerra(nftBridgeAddress, signedVAA, client, gasPriceUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        const msg = yield _1.redeemOnTerra(nftBridgeAddress, __1.TERRA_REDEEMED_CHECK_WALLET_ADDRESS, signedVAA);
        // TODO: remove gasPriceUrl and just use the client's gas prices
        const gasPrices = yield axios_1.default.get(gasPriceUrl).then((result) => result.data);
        const account = yield client.auth.accountInfo(__1.TERRA_REDEEMED_CHECK_WALLET_ADDRESS);
        try {
            yield client.tx.estimateFee([
                {
                    sequenceNumber: account.getSequenceNumber(),
                    publicKey: account.getPublicKey(),
                },
            ], {
                msgs: [msg],
                memo: "already redeemed calculation",
                feeDenoms: ["uluna"],
                gasPrices,
            });
        }
        catch (e) {
            // redeemed if the VAA was already executed
            return e.response.data.message.includes("VaaAlreadyExecuted");
        }
        return false;
    });
}
exports.getIsTransferCompletedTerra = getIsTransferCompletedTerra;
function getIsTransferCompletedSolana(nftBridgeAddress, signedVAA, connection, commitment) {
    return __awaiter(this, void 0, void 0, function* () {
        const parsed = wormhole_2.parseVaa(signedVAA);
        return wormhole_1.getClaim(connection, nftBridgeAddress, parsed.emitterAddress, parsed.emitterChain, parsed.sequence, commitment).catch((e) => false);
    });
}
exports.getIsTransferCompletedSolana = getIsTransferCompletedSolana;
function getIsTransferCompletedAptos(client, nftBridgeAddress, transferVaa) {
    return __awaiter(this, void 0, void 0, function* () {
        // get handle
        nftBridgeAddress = __1.ensureHexPrefix(nftBridgeAddress);
        const state = (yield client.getAccountResource(nftBridgeAddress, `${nftBridgeAddress}::state::State`)).data;
        const handle = state.consumed_vaas.elems.handle;
        // check if vaa hash is in consumed_vaas
        const transferVaaHash = bridge_1.getSignedVAAHash(transferVaa);
        try {
            // when accessing Set<T>, key is type T and value is 0
            yield client.getTableItem(handle, {
                key_type: "vector<u8>",
                value_type: "u8",
                key: transferVaaHash,
            });
            return true;
        }
        catch (_a) {
            return false;
        }
    });
}
exports.getIsTransferCompletedAptos = getIsTransferCompletedAptos;
