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 { ALPH_TOKEN_ID, binToHex, DUST_AMOUNT } from "@alephium/web3";
import { PublicKey, Transaction } from "@solana/web3.js";
import { MsgExecuteContract } from "@terra-money/terra.js";
import { fromUint8Array } from "js-base64";
import { _submitVAAAlgorand } from "../algorand";
import { Bridge__factory } from "../ethers-contracts";
import { ixFromRust } from "../solana";
import { importTokenWasm } from "../solana/wasm";
import { CreateRemoteTokenPool, CreateLocalTokenPool } from "../alephium-contracts/ts/scripts";
export function createRemoteTokenPoolOnAlph(signerProvider, attestTokenHandlerId, signedVAA, payer, alphAmount) {
    return __awaiter(this, void 0, void 0, function* () {
        return CreateRemoteTokenPool.execute(signerProvider, {
            initialFields: {
                payer: payer,
                attestTokenHandler: attestTokenHandlerId,
                vaa: binToHex(signedVAA),
                alphAmount: alphAmount
            },
            attoAlphAmount: alphAmount
        });
    });
}
export function createLocalTokenPoolOnAlph(signerProvider, attestTokenHandlerId, localTokenId, signedVAA, payer, alphAmount) {
    return __awaiter(this, void 0, void 0, function* () {
        return CreateLocalTokenPool.execute(signerProvider, {
            initialFields: {
                payer: payer,
                attestTokenHandler: attestTokenHandlerId,
                localTokenId: localTokenId,
                vaa: binToHex(signedVAA),
                alphAmount: alphAmount
            },
            attoAlphAmount: alphAmount,
            tokens: localTokenId === ALPH_TOKEN_ID
                ? []
                : [{ id: localTokenId, amount: BigInt(1) }, { id: ALPH_TOKEN_ID, amount: DUST_AMOUNT * BigInt(2) }]
        });
    });
}
export function createWrappedOnEth(tokenBridgeAddress, signer, signedVAA, overrides = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
        const v = yield bridge.createWrapped(signedVAA, overrides);
        const receipt = yield v.wait();
        return receipt;
    });
}
export function createWrappedOnTerra(tokenBridgeAddress, walletAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        return new MsgExecuteContract(walletAddress, tokenBridgeAddress, {
            submit_vaa: {
                data: fromUint8Array(signedVAA),
            },
        });
    });
}
export function createWrappedOnSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, signedVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        const { create_wrapped_ix } = yield importTokenWasm();
        const ix = ixFromRust(create_wrapped_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 createWrappedOnAlgorand(client, tokenBridgeId, bridgeId, senderAddr, attestVAA) {
    return __awaiter(this, void 0, void 0, function* () {
        return yield _submitVAAAlgorand(client, tokenBridgeId, bridgeId, attestVAA, senderAddr);
    });
}
