"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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIsTransferCompletedAptos = exports.getIsTransferCompletedNear = exports.getIsTransferCompletedAlgorand = exports.getIsTransferCompletedSolana = exports.getIsTransferCompletedXpla = exports.getIsTransferCompletedInjective = exports.getIsTransferCompletedTerra2 = exports.getIsTransferCompletedTerra = exports.getIsTransferCompletedEth = void 0;
var algosdk_1 = require("algosdk");
var axios_1 = __importDefault(require("axios"));
var js_base64_1 = require("js-base64");
var _1 = require(".");
var __1 = require("..");
var wormhole_1 = require("../solana/wormhole");
var algorand_1 = require("../algorand");
var near_1 = require("../utils/near");
var bridge_1 = require("../bridge");
var ethers_contracts_1 = require("../ethers-contracts");
var wormhole_2 = require("../vaa/wormhole");
var bigint_1 = require("../utils/bigint");
function getIsTransferCompletedEth(tokenBridgeAddress, provider, signedVAA) {
    return __awaiter(this, void 0, void 0, function () {
        var tokenBridge, signedVAAHash;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    tokenBridge = ethers_contracts_1.Bridge__factory.connect(tokenBridgeAddress, provider);
                    signedVAAHash = bridge_1.getSignedVAAHash(signedVAA);
                    return [4 /*yield*/, tokenBridge.isTransferCompleted(signedVAAHash)];
                case 1: return [2 /*return*/, _a.sent()];
            }
        });
    });
}
exports.getIsTransferCompletedEth = getIsTransferCompletedEth;
// Note: this function is the legacy implementation for terra classic.  New
// cosmwasm sdk functions should instead be based on
// `getIsTransferCompletedTerra2`.
function getIsTransferCompletedTerra(tokenBridgeAddress, signedVAA, client, gasPriceUrl) {
    return __awaiter(this, void 0, void 0, function () {
        var msg, gasPrices, account, e_1;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, _1.redeemOnTerra(tokenBridgeAddress, __1.TERRA_REDEEMED_CHECK_WALLET_ADDRESS, signedVAA)];
                case 1:
                    msg = _a.sent();
                    return [4 /*yield*/, axios_1.default.get(gasPriceUrl).then(function (result) { return result.data; })];
                case 2:
                    gasPrices = _a.sent();
                    return [4 /*yield*/, client.auth.accountInfo(__1.TERRA_REDEEMED_CHECK_WALLET_ADDRESS)];
                case 3:
                    account = _a.sent();
                    _a.label = 4;
                case 4:
                    _a.trys.push([4, 6, , 7]);
                    return [4 /*yield*/, client.tx.estimateFee([
                            {
                                sequenceNumber: account.getSequenceNumber(),
                                publicKey: account.getPublicKey(),
                            },
                        ], {
                            msgs: [msg],
                            memo: "already redeemed calculation",
                            feeDenoms: ["uluna"],
                            gasPrices: gasPrices,
                        })];
                case 5:
                    _a.sent();
                    return [3 /*break*/, 7];
                case 6:
                    e_1 = _a.sent();
                    // redeemed if the VAA was already executed
                    if (e_1.response.data.message.includes("VaaAlreadyExecuted")) {
                        return [2 /*return*/, true];
                    }
                    else {
                        throw e_1;
                    }
                    return [3 /*break*/, 7];
                case 7: return [2 /*return*/, false];
            }
        });
    });
}
exports.getIsTransferCompletedTerra = getIsTransferCompletedTerra;
/**
 * This function is used to check if a VAA has been redeemed on terra2 by
 * querying the token bridge contract.
 * @param tokenBridgeAddress The token bridge address (bech32)
 * @param signedVAA The signed VAA byte array
 * @param client The LCD client. Only used for querying, not transactions will
 * be signed
 */
function getIsTransferCompletedTerra2(tokenBridgeAddress, signedVAA, client) {
    return __awaiter(this, void 0, void 0, function () {
        var result;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, client.wasm.contractQuery(tokenBridgeAddress, {
                        is_vaa_redeemed: {
                            vaa: js_base64_1.fromUint8Array(signedVAA),
                        },
                    })];
                case 1:
                    result = _a.sent();
                    return [2 /*return*/, result.is_redeemed];
            }
        });
    });
}
exports.getIsTransferCompletedTerra2 = getIsTransferCompletedTerra2;
/**
 * Return if the VAA has been redeemed or not
 * @param tokenBridgeAddress The Injective token bridge contract address
 * @param signedVAA The signed VAA byte array
 * @param client Holds the wallet and signing information
 * @returns true if the VAA has been redeemed.
 */
function getIsTransferCompletedInjective(tokenBridgeAddress, signedVAA, client) {
    return __awaiter(this, void 0, void 0, function () {
        var queryResult, parsed;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, client.fetchSmartContractState(tokenBridgeAddress, Buffer.from(JSON.stringify({
                        is_vaa_redeemed: {
                            vaa: js_base64_1.fromUint8Array(signedVAA),
                        },
                    })).toString("base64"))];
                case 1:
                    queryResult = _a.sent();
                    parsed = __1.parseSmartContractStateResponse(queryResult);
                    return [2 /*return*/, parsed.is_redeemed];
            }
        });
    });
}
exports.getIsTransferCompletedInjective = getIsTransferCompletedInjective;
function getIsTransferCompletedXpla(tokenBridgeAddress, signedVAA, client) {
    return __awaiter(this, void 0, void 0, function () {
        var result;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, client.wasm.contractQuery(tokenBridgeAddress, {
                        is_vaa_redeemed: {
                            vaa: js_base64_1.fromUint8Array(signedVAA),
                        },
                    })];
                case 1:
                    result = _a.sent();
                    return [2 /*return*/, result.is_redeemed];
            }
        });
    });
}
exports.getIsTransferCompletedXpla = getIsTransferCompletedXpla;
function getIsTransferCompletedSolana(tokenBridgeAddress, signedVAA, connection, commitment) {
    return __awaiter(this, void 0, void 0, function () {
        var parsed;
        return __generator(this, function (_a) {
            parsed = wormhole_2.parseVaa(signedVAA);
            return [2 /*return*/, wormhole_1.getClaim(connection, tokenBridgeAddress, parsed.emitterAddress, parsed.emitterChain, parsed.sequence, commitment).catch(function (e) { return false; })];
        });
    });
}
exports.getIsTransferCompletedSolana = getIsTransferCompletedSolana;
// Algorand
/**
 * This function is used to check if a VAA has been redeemed by looking at a specific bit.
 * @param client AlgodV2 client
 * @param appId Application Id
 * @param addr Wallet address. Someone has to pay for this.
 * @param seq The sequence number of the redemption
 * @returns true, if the bit was set and VAA was redeemed, false otherwise.
 */
function checkBitsSet(client, appId, addr, seq) {
    return __awaiter(this, void 0, void 0, function () {
        var retval, appState, acctInfo, als, BIG_MAX_BITS, BIG_EIGHT, start, beg, s, b, key;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    retval = false;
                    appState = [];
                    return [4 /*yield*/, client.accountInformation(addr).do()];
                case 1:
                    acctInfo = _a.sent();
                    als = acctInfo["apps-local-state"];
                    als.forEach(function (app) {
                        if (BigInt(app["id"]) === appId) {
                            appState = app["key-value"];
                        }
                    });
                    if (appState.length === 0) {
                        return [2 /*return*/, retval];
                    }
                    BIG_MAX_BITS = BigInt(algorand_1.MAX_BITS);
                    BIG_EIGHT = BigInt(8);
                    start = (seq / BIG_MAX_BITS) * BIG_MAX_BITS;
                    beg = bigint_1.safeBigIntToNumber(seq - start);
                    s = Math.floor(beg / algorand_1.BITS_PER_KEY);
                    b = Math.floor((beg - s * algorand_1.BITS_PER_KEY) / 8);
                    key = Buffer.from(algosdk_1.bigIntToBytes(s, 1)).toString("base64");
                    appState.forEach(function (kv) {
                        if (kv["key"] === key) {
                            var v = Buffer.from(kv["value"]["bytes"], "base64");
                            var bt = 1 << bigint_1.safeBigIntToNumber(seq % BIG_EIGHT);
                            retval = (v[b] & bt) != 0;
                            return;
                        }
                    });
                    return [2 /*return*/, retval];
            }
        });
    });
}
/**
 * <p>Returns true if this transfer was completed on Algorand</p>
 * @param client AlgodV2 client
 * @param appId Most likely the Token bridge ID
 * @param signedVAA VAA to check
 * @returns true if VAA has been redeemed, false otherwise
 */
function getIsTransferCompletedAlgorand(client, appId, signedVAA) {
    return __awaiter(this, void 0, void 0, function () {
        var parsedVAA, seq, chainRaw, em, _a, doesExist, lsa, seqAddr, retVal;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    parsedVAA = algorand_1._parseVAAAlgorand(signedVAA);
                    seq = parsedVAA.sequence;
                    chainRaw = parsedVAA.chainRaw;
                    em = parsedVAA.emitter;
                    return [4 /*yield*/, algorand_1.calcLogicSigAccount(client, appId, seq / BigInt(algorand_1.MAX_BITS), chainRaw + em)];
                case 1:
                    _a = _b.sent(), doesExist = _a.doesExist, lsa = _a.lsa;
                    if (!doesExist) {
                        return [2 /*return*/, false];
                    }
                    seqAddr = lsa.address();
                    return [4 /*yield*/, checkBitsSet(client, appId, seqAddr, seq)];
                case 2:
                    retVal = _b.sent();
                    return [2 /*return*/, retVal];
            }
        });
    });
}
exports.getIsTransferCompletedAlgorand = getIsTransferCompletedAlgorand;
function getIsTransferCompletedNear(provider, tokenBridge, signedVAA) {
    return __awaiter(this, void 0, void 0, function () {
        var vaa;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    vaa = Buffer.from(signedVAA).toString("hex");
                    return [4 /*yield*/, near_1.callFunctionNear(provider, tokenBridge, "is_transfer_completed", {
                            vaa: vaa,
                        })];
                case 1: return [2 /*return*/, (_a.sent())[1]];
            }
        });
    });
}
exports.getIsTransferCompletedNear = getIsTransferCompletedNear;
/**
 * Determine whether or not the transfer in the given VAA has completed on Aptos.
 * @param client Client used to transfer data to/from Aptos node
 * @param tokenBridgeAddress Address of token bridge
 * @param transferVAA Bytes of transfer VAA
 * @returns True if transfer is completed
 */
function getIsTransferCompletedAptos(client, tokenBridgeAddress, transferVAA) {
    return __awaiter(this, void 0, void 0, function () {
        var state, handle, transferVAAHash, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    // get handle
                    tokenBridgeAddress = __1.ensureHexPrefix(tokenBridgeAddress);
                    return [4 /*yield*/, client.getAccountResource(tokenBridgeAddress, tokenBridgeAddress + "::state::State")];
                case 1:
                    state = (_b.sent()).data;
                    handle = state.consumed_vaas.elems.handle;
                    transferVAAHash = bridge_1.getSignedVAAHash(transferVAA);
                    _b.label = 2;
                case 2:
                    _b.trys.push([2, 4, , 5]);
                    // when accessing Set<T>, key is type T and value is 0
                    return [4 /*yield*/, client.getTableItem(handle, {
                            key_type: "vector<u8>",
                            value_type: "u8",
                            key: transferVAAHash,
                        })];
                case 3:
                    // when accessing Set<T>, key is type T and value is 0
                    _b.sent();
                    return [2 /*return*/, true];
                case 4:
                    _a = _b.sent();
                    return [2 /*return*/, false];
                case 5: return [2 /*return*/];
            }
        });
    });
}
exports.getIsTransferCompletedAptos = getIsTransferCompletedAptos;
