"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.redeemOnAptos = exports.redeemOnTerra = exports.createMetaOnSolana = exports.redeemOnSolana = exports.isNFTVAASolanaNative = exports.redeemOnEth = void 0;
const web3_js_1 = require("@solana/web3.js");
const terra_js_1 = require("@terra-money/terra.js");
const js_base64_1 = require("js-base64");
const ethers_contracts_1 = require("../ethers-contracts");
const nftBridge_1 = require("../solana/nftBridge");
const utils_1 = require("../utils");
const vaa_1 = require("../vaa");
function redeemOnEth(nftBridgeAddress, signer, signedVAA, overrides = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const bridge = ethers_contracts_1.Bridge__factory.connect(nftBridgeAddress, signer);
        const v = yield bridge.completeTransfer(signedVAA, overrides);
        const receipt = yield v.wait();
        return receipt;
    });
}
exports.redeemOnEth = redeemOnEth;
function isNFTVAASolanaNative(signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        return vaa_1.parseVaa(signedVAA).payload.readUInt16BE(33) === utils_1.CHAIN_ID_SOLANA;
    });
}
exports.isNFTVAASolanaNative = isNFTVAASolanaNative;
function redeemOnSolana(connection, bridgeAddress, nftBridgeAddress, payerAddress, signedVaa, toAuthorityAddress, commitment) {
    return __awaiter(this, void 0, void 0, function* () {
        const parsed = vaa_1.parseNftTransferVaa(signedVaa);
        const createCompleteTransferInstruction = parsed.tokenChain == utils_1.CHAIN_ID_SOLANA
            ? nftBridge_1.createCompleteTransferNativeInstruction
            : nftBridge_1.createCompleteTransferWrappedInstruction;
        const transaction = new web3_js_1.Transaction().add(createCompleteTransferInstruction(nftBridgeAddress, bridgeAddress, payerAddress, parsed, toAuthorityAddress));
        const { blockhash } = yield connection.getLatestBlockhash(commitment);
        transaction.recentBlockhash = blockhash;
        transaction.feePayer = new web3_js_1.PublicKey(payerAddress);
        return transaction;
    });
}
exports.redeemOnSolana = redeemOnSolana;
function createMetaOnSolana(connection, bridgeAddress, nftBridgeAddress, payerAddress, signedVaa, commitment) {
    return __awaiter(this, void 0, void 0, function* () {
        const parsed = vaa_1.parseNftTransferVaa(signedVaa);
        if (parsed.tokenChain == utils_1.CHAIN_ID_SOLANA) {
            return Promise.reject("parsed.tokenChain == CHAIN_ID_SOLANA");
        }
        const transaction = new web3_js_1.Transaction().add(nftBridge_1.createCompleteWrappedMetaInstruction(nftBridgeAddress, bridgeAddress, payerAddress, parsed));
        const { blockhash } = yield connection.getLatestBlockhash(commitment);
        transaction.recentBlockhash = blockhash;
        transaction.feePayer = new web3_js_1.PublicKey(payerAddress);
        return transaction;
    });
}
exports.createMetaOnSolana = createMetaOnSolana;
function redeemOnTerra(nftBridgeAddress, walletAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        return new terra_js_1.MsgExecuteContract(walletAddress, nftBridgeAddress, {
            submit_vaa: {
                data: js_base64_1.fromUint8Array(signedVAA),
            },
        });
    });
}
exports.redeemOnTerra = redeemOnTerra;
function redeemOnAptos(nftBridgeAddress, transferVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const parsedVAA = vaa_1.parseNftTransferVaa(transferVAA);
        if (parsedVAA.toChain !== utils_1.CHAIN_ID_APTOS) {
            throw new Error("Transfer is not destined for Aptos.");
        }
        return {
            function: `${nftBridgeAddress}::complete_transfer::submit_vaa_and_register_entry`,
            type_arguments: [],
            arguments: [transferVAA],
        };
    });
}
exports.redeemOnAptos = redeemOnAptos;
