"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.getIsTransferCompletedSolana = exports.getIsTransferCompletedTerra = exports.getIsTransferCompletedEth = void 0;
const ethers_contracts_1 = require("../ethers-contracts");
const bridge_1 = require("../bridge");
const wasm_1 = require("../solana/wasm");
const web3_js_1 = require("@solana/web3.js");
const axios_1 = __importDefault(require("axios"));
const _1 = require(".");
const __1 = require("..");
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 = yield 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) {
    return __awaiter(this, void 0, void 0, function* () {
        const { claim_address } = yield wasm_1.importCoreWasm();
        const claimAddress = yield claim_address(nftBridgeAddress, signedVAA);
        const claimInfo = yield connection.getAccountInfo(new web3_js_1.PublicKey(claimAddress), "confirmed");
        return !!claimInfo;
    });
}
exports.getIsTransferCompletedSolana = getIsTransferCompletedSolana;
