"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.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 __1 = require("..");
const ethers_contracts_1 = require("../ethers-contracts");
const solana_1 = require("../solana");
const wasm_1 = require("../solana/wasm");
function redeemOnEth(tokenBridgeAddress, signer, signedVAA, overrides = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const bridge = ethers_contracts_1.Bridge__factory.connect(tokenBridgeAddress, 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* () {
        const { parse_vaa } = yield wasm_1.importCoreWasm();
        const parsedVAA = parse_vaa(signedVAA);
        const isSolanaNative = Buffer.from(new Uint8Array(parsedVAA.payload)).readUInt16BE(33) ===
            __1.CHAIN_ID_SOLANA;
        return isSolanaNative;
    });
}
exports.isNFTVAASolanaNative = isNFTVAASolanaNative;
function redeemOnSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const isSolanaNative = yield isNFTVAASolanaNative(signedVAA);
        const { complete_transfer_wrapped_ix, complete_transfer_native_ix } = yield wasm_1.importNftWasm();
        const ixs = [];
        if (isSolanaNative) {
            ixs.push(solana_1.ixFromRust(complete_transfer_native_ix(tokenBridgeAddress, bridgeAddress, payerAddress, payerAddress, //TODO: allow for a different address than payer
            signedVAA)));
        }
        else {
            ixs.push(solana_1.ixFromRust(complete_transfer_wrapped_ix(tokenBridgeAddress, bridgeAddress, payerAddress, payerAddress, //TODO: allow for a different address than payer
            signedVAA)));
        }
        const transaction = new web3_js_1.Transaction().add(...ixs);
        const { blockhash } = yield connection.getRecentBlockhash();
        transaction.recentBlockhash = blockhash;
        transaction.feePayer = new web3_js_1.PublicKey(payerAddress);
        return transaction;
    });
}
exports.redeemOnSolana = redeemOnSolana;
function createMetaOnSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const { complete_transfer_wrapped_meta_ix } = yield wasm_1.importNftWasm();
        const ix = solana_1.ixFromRust(complete_transfer_wrapped_meta_ix(tokenBridgeAddress, bridgeAddress, payerAddress, signedVAA));
        const transaction = new web3_js_1.Transaction().add(ix);
        const { blockhash } = yield connection.getRecentBlockhash();
        transaction.recentBlockhash = blockhash;
        transaction.feePayer = new web3_js_1.PublicKey(payerAddress);
        return transaction;
    });
}
exports.createMetaOnSolana = createMetaOnSolana;
function redeemOnTerra(tokenBridgeAddress, walletAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        return new terra_js_1.MsgExecuteContract(walletAddress, tokenBridgeAddress, {
            submit_vaa: {
                data: js_base64_1.fromUint8Array(signedVAA),
            },
        });
    });
}
exports.redeemOnTerra = redeemOnTerra;
