"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.createWrappedOnAlgorand = exports.createWrappedOnSolana = exports.createWrappedOnTerra = exports.createWrappedOnEth = exports.createLocalTokenPoolOnAlph = exports.createRemoteTokenPoolOnAlph = void 0;
const web3_1 = require("@alephium/web3");
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 algorand_1 = require("../algorand");
const ethers_contracts_1 = require("../ethers-contracts");
const solana_1 = require("../solana");
const wasm_1 = require("../solana/wasm");
const scripts_1 = require("../alephium-contracts/ts/scripts");
function createRemoteTokenPoolOnAlph(signerProvider, attestTokenHandlerId, signedVAA, payer, alphAmount) {
    return __awaiter(this, void 0, void 0, function* () {
        return scripts_1.CreateRemoteTokenPool.execute(signerProvider, {
            initialFields: {
                payer: payer,
                attestTokenHandler: attestTokenHandlerId,
                vaa: web3_1.binToHex(signedVAA),
                alphAmount: alphAmount
            },
            attoAlphAmount: alphAmount
        });
    });
}
exports.createRemoteTokenPoolOnAlph = createRemoteTokenPoolOnAlph;
function createLocalTokenPoolOnAlph(signerProvider, attestTokenHandlerId, localTokenId, signedVAA, payer, alphAmount) {
    return __awaiter(this, void 0, void 0, function* () {
        return scripts_1.CreateLocalTokenPool.execute(signerProvider, {
            initialFields: {
                payer: payer,
                attestTokenHandler: attestTokenHandlerId,
                localTokenId: localTokenId,
                vaa: web3_1.binToHex(signedVAA),
                alphAmount: alphAmount
            },
            attoAlphAmount: alphAmount + (localTokenId === web3_1.ALPH_TOKEN_ID ? web3_1.DUST_AMOUNT : web3_1.DUST_AMOUNT * BigInt(2)),
            tokens: [{ id: localTokenId, amount: BigInt(1) }]
        });
    });
}
exports.createLocalTokenPoolOnAlph = createLocalTokenPoolOnAlph;
function createWrappedOnEth(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.createWrapped(signedVAA, overrides);
        const receipt = yield v.wait();
        return receipt;
    });
}
exports.createWrappedOnEth = createWrappedOnEth;
function createWrappedOnTerra(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.createWrappedOnTerra = createWrappedOnTerra;
function createWrappedOnSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const { create_wrapped_ix } = yield wasm_1.importTokenWasm();
        const ix = solana_1.ixFromRust(create_wrapped_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.createWrappedOnSolana = createWrappedOnSolana;
function createWrappedOnAlgorand(client, tokenBridgeId, bridgeId, senderAddr, attestVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        return yield algorand_1._submitVAAAlgorand(client, tokenBridgeId, bridgeId, attestVAA, senderAddr);
    });
}
exports.createWrappedOnAlgorand = createWrappedOnAlgorand;
