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