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 { NFTBridge__factory } from "../ethers-contracts";
import { getSignedVAAHash } from "../bridge";
import { importCoreWasm } from "../solana/wasm";
import { PublicKey } from "@solana/web3.js";
import axios from "axios";
import { redeemOnTerra } from ".";
import { TERRA_REDEEMED_CHECK_WALLET_ADDRESS } from "..";
export function getIsTransferCompletedEth(nftBridgeAddress, provider, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const nftBridge = NFTBridge__factory.connect(nftBridgeAddress, provider);
        const signedVAAHash = yield getSignedVAAHash(signedVAA);
        return yield nftBridge.isTransferCompleted(signedVAAHash);
    });
}
export function getIsTransferCompletedTerra(nftBridgeAddress, signedVAA, client, gasPriceUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        const msg = yield redeemOnTerra(nftBridgeAddress, TERRA_REDEEMED_CHECK_WALLET_ADDRESS, signedVAA);
        // TODO: remove gasPriceUrl and just use the client's gas prices
        const gasPrices = yield axios.get(gasPriceUrl).then((result) => result.data);
        const account = yield client.auth.accountInfo(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;
    });
}
export function getIsTransferCompletedSolana(nftBridgeAddress, signedVAA, connection) {
    return __awaiter(this, void 0, void 0, function* () {
        const { claim_address } = yield importCoreWasm();
        const claimAddress = yield claim_address(nftBridgeAddress, signedVAA);
        const claimInfo = yield connection.getAccountInfo(new PublicKey(claimAddress), "confirmed");
        return !!claimInfo;
    });
}
