var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
import { ACCOUNT_SIZE, createCloseAccountInstruction, createInitializeAccountInstruction, getMinimumBalanceForRentExemptAccount, NATIVE_MINT, TOKEN_PROGRAM_ID, } from "@solana/spl-token";
import { Keypair, PublicKey, SystemProgram, Transaction as SolanaTransaction, } from "@solana/web3.js";
import { MsgExecuteContract } from "@terra-money/terra.js";
import { MsgExecuteContract as MsgExecuteContractInjective } from "@injectivelabs/sdk-ts";
import { bigIntToBytes, getApplicationAddress, makeApplicationCallTxnFromObject, makeAssetTransferTxnWithSuggestedParamsFromObject, makePaymentTxnWithSuggestedParamsFromObject, OnApplicationComplete, } from "algosdk";
import BN from "bn.js";
import { isNativeDenom } from "../terra";
import { getIsWrappedAssetNear } from "..";
import { assetOptinCheck, getMessageFee, optin, } from "../algorand";
import { getEmitterAddressAlgorand } from "../bridge";
import { Bridge__factory, TokenImplementation__factory, } from "../ethers-contracts";
import { createApproveAuthoritySignerInstruction, createTransferNativeInstruction, createTransferNativeWithPayloadInstruction, createTransferWrappedInstruction, createTransferWrappedWithPayloadInstruction, } from "../solana/tokenBridge";
import { coalesceChainId, createNonce, hexToUint8Array, safeBigIntToNumber, textToUint8Array, uint8ArrayToHex, CHAIN_ID_SOLANA, callFunctionNear, } from "../utils";
import { isNativeDenomInjective, isNativeDenomXpla } from "../cosmwasm";
import { MsgExecuteContract as XplaMsgExecuteContract } from "@xpla/xpla.js";
import { transferTokens as transferTokensAptos, transferTokensWithPayload, } from "../aptos";
export function getAllowanceEth(tokenBridgeAddress, tokenAddress, signer) {
    return __awaiter(this, void 0, void 0, function () {
        var token, signerAddress, allowance;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    token = TokenImplementation__factory.connect(tokenAddress, signer);
                    return [4 /*yield*/, signer.getAddress()];
                case 1:
                    signerAddress = _a.sent();
                    return [4 /*yield*/, token.allowance(signerAddress, tokenBridgeAddress)];
                case 2:
                    allowance = _a.sent();
                    return [2 /*return*/, allowance];
            }
        });
    });
}
export function approveEth(tokenBridgeAddress, tokenAddress, signer, amount, overrides) {
    if (overrides === void 0) { overrides = {}; }
    return __awaiter(this, void 0, void 0, function () {
        var token;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    token = TokenImplementation__factory.connect(tokenAddress, signer);
                    return [4 /*yield*/, token.approve(tokenBridgeAddress, amount, overrides)];
                case 1: return [4 /*yield*/, (_a.sent()).wait()];
                case 2: return [2 /*return*/, _a.sent()];
            }
        });
    });
}
export function transferFromEth(tokenBridgeAddress, signer, tokenAddress, amount, recipientChain, recipientAddress, relayerFee, overrides, payload) {
    if (relayerFee === void 0) { relayerFee = 0; }
    if (overrides === void 0) { overrides = {}; }
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var recipientChainId, bridge, v, _a, receipt;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    recipientChainId = coalesceChainId(recipientChain);
                    bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
                    if (!(payload === null)) return [3 /*break*/, 2];
                    return [4 /*yield*/, bridge.transferTokens(tokenAddress, amount, recipientChainId, recipientAddress, relayerFee, createNonce(), overrides)];
                case 1:
                    _a = _b.sent();
                    return [3 /*break*/, 4];
                case 2: return [4 /*yield*/, bridge.transferTokensWithPayload(tokenAddress, amount, recipientChainId, recipientAddress, createNonce(), payload, overrides)];
                case 3:
                    _a = _b.sent();
                    _b.label = 4;
                case 4:
                    v = _a;
                    return [4 /*yield*/, v.wait()];
                case 5:
                    receipt = _b.sent();
                    return [2 /*return*/, receipt];
            }
        });
    });
}
export function transferFromEthNative(tokenBridgeAddress, signer, amount, recipientChain, recipientAddress, relayerFee, overrides, payload) {
    if (relayerFee === void 0) { relayerFee = 0; }
    if (overrides === void 0) { overrides = {}; }
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var recipientChainId, bridge, v, _a, receipt;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    recipientChainId = coalesceChainId(recipientChain);
                    bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
                    if (!(payload === null)) return [3 /*break*/, 2];
                    return [4 /*yield*/, bridge.wrapAndTransferETH(recipientChainId, recipientAddress, relayerFee, createNonce(), __assign(__assign({}, overrides), { value: amount }))];
                case 1:
                    _a = _b.sent();
                    return [3 /*break*/, 4];
                case 2: return [4 /*yield*/, bridge.wrapAndTransferETHWithPayload(recipientChainId, recipientAddress, createNonce(), payload, __assign(__assign({}, overrides), { value: amount }))];
                case 3:
                    _a = _b.sent();
                    _b.label = 4;
                case 4:
                    v = _a;
                    return [4 /*yield*/, v.wait()];
                case 5:
                    receipt = _b.sent();
                    return [2 /*return*/, receipt];
            }
        });
    });
}
export function transferFromTerra(walletAddress, tokenBridgeAddress, tokenAddress, amount, recipientChain, recipientAddress, relayerFee, payload) {
    if (relayerFee === void 0) { relayerFee = "0"; }
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var recipientChainId, nonce, isNativeAsset, mk_initiate_transfer;
        var _a;
        return __generator(this, function (_b) {
            recipientChainId = coalesceChainId(recipientChain);
            nonce = Math.round(Math.random() * 100000);
            isNativeAsset = isNativeDenom(tokenAddress);
            mk_initiate_transfer = function (info) {
                return payload
                    ? {
                        initiate_transfer_with_payload: {
                            asset: {
                                amount: amount,
                                info: info,
                            },
                            recipient_chain: recipientChainId,
                            recipient: Buffer.from(recipientAddress).toString("base64"),
                            fee: relayerFee,
                            nonce: nonce,
                            payload: payload,
                        },
                    }
                    : {
                        initiate_transfer: {
                            asset: {
                                amount: amount,
                                info: info,
                            },
                            recipient_chain: recipientChainId,
                            recipient: Buffer.from(recipientAddress).toString("base64"),
                            fee: relayerFee,
                            nonce: nonce,
                        },
                    };
            };
            return [2 /*return*/, isNativeAsset
                    ? [
                        new MsgExecuteContract(walletAddress, tokenBridgeAddress, {
                            deposit_tokens: {},
                        }, (_a = {}, _a[tokenAddress] = amount, _a)),
                        new MsgExecuteContract(walletAddress, tokenBridgeAddress, mk_initiate_transfer({
                            native_token: {
                                denom: tokenAddress,
                            },
                        }), {}),
                    ]
                    : [
                        new MsgExecuteContract(walletAddress, tokenAddress, {
                            increase_allowance: {
                                spender: tokenBridgeAddress,
                                amount: amount,
                                expires: {
                                    never: {},
                                },
                            },
                        }, {}),
                        new MsgExecuteContract(walletAddress, tokenBridgeAddress, mk_initiate_transfer({
                            token: {
                                contract_addr: tokenAddress,
                            },
                        }), {}),
                    ]];
        });
    });
}
/**
 * Creates the necessary messages to transfer an asset
 * @param walletAddress Address of the Inj wallet
 * @param tokenBridgeAddress Address of the token bridge contract
 * @param tokenAddress Address of the token being transferred
 * @param amount Amount of token to be transferred
 * @param recipientChain Destination chain
 * @param recipientAddress Destination wallet address
 * @param relayerFee Relayer fee
 * @param payload Optional payload
 * @returns Transfer messages to be sent on chain
 */
export function transferFromInjective(walletAddress, tokenBridgeAddress, tokenAddress, amount, recipientChain, recipientAddress, relayerFee, payload) {
    if (relayerFee === void 0) { relayerFee = "0"; }
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var recipientChainId, nonce, isNativeAsset, mk_action, mk_initiate_transfer;
        return __generator(this, function (_a) {
            recipientChainId = coalesceChainId(recipientChain);
            nonce = Math.round(Math.random() * 100000);
            isNativeAsset = isNativeDenomInjective(tokenAddress);
            mk_action = payload
                ? "initiate_transfer_with_payload"
                : "initiate_transfer";
            mk_initiate_transfer = function (info) {
                return payload
                    ? {
                        asset: {
                            amount: amount,
                            info: info,
                        },
                        recipient_chain: recipientChainId,
                        recipient: Buffer.from(recipientAddress).toString("base64"),
                        fee: relayerFee,
                        nonce: nonce,
                        payload: payload,
                    }
                    : {
                        asset: {
                            amount: amount,
                            info: info,
                        },
                        recipient_chain: recipientChainId,
                        recipient: Buffer.from(recipientAddress).toString("base64"),
                        fee: relayerFee,
                        nonce: nonce,
                    };
            };
            return [2 /*return*/, isNativeAsset
                    ? [
                        MsgExecuteContractInjective.fromJSON({
                            contractAddress: tokenBridgeAddress,
                            sender: walletAddress,
                            msg: {},
                            action: "deposit_tokens",
                            funds: { denom: tokenAddress, amount: amount },
                        }),
                        MsgExecuteContractInjective.fromJSON({
                            contractAddress: tokenBridgeAddress,
                            sender: walletAddress,
                            msg: mk_initiate_transfer({ native_token: { denom: tokenAddress } }),
                            action: mk_action,
                        }),
                    ]
                    : [
                        MsgExecuteContractInjective.fromJSON({
                            contractAddress: tokenAddress,
                            sender: walletAddress,
                            msg: {
                                spender: tokenBridgeAddress,
                                amount: amount,
                                expires: {
                                    never: {},
                                },
                            },
                            action: "increase_allowance",
                        }),
                        MsgExecuteContractInjective.fromJSON({
                            contractAddress: tokenBridgeAddress,
                            sender: walletAddress,
                            msg: mk_initiate_transfer({ token: { contract_addr: tokenAddress } }),
                            action: mk_action,
                        }),
                    ]];
        });
    });
}
export function transferFromXpla(walletAddress, tokenBridgeAddress, tokenAddress, amount, recipientChain, recipientAddress, relayerFee, payload) {
    var _a;
    if (relayerFee === void 0) { relayerFee = "0"; }
    if (payload === void 0) { payload = null; }
    var recipientChainId = coalesceChainId(recipientChain);
    var nonce = Math.round(Math.random() * 100000);
    var isNativeAsset = isNativeDenomXpla(tokenAddress);
    var createInitiateTransfer = function (info) {
        return payload
            ? {
                initiate_transfer_with_payload: {
                    asset: {
                        amount: amount,
                        info: info,
                    },
                    recipient_chain: recipientChainId,
                    recipient: Buffer.from(recipientAddress).toString("base64"),
                    fee: relayerFee,
                    nonce: nonce,
                    payload: payload,
                },
            }
            : {
                initiate_transfer: {
                    asset: {
                        amount: amount,
                        info: info,
                    },
                    recipient_chain: recipientChainId,
                    recipient: Buffer.from(recipientAddress).toString("base64"),
                    fee: relayerFee,
                    nonce: nonce,
                },
            };
    };
    return isNativeAsset
        ? [
            new XplaMsgExecuteContract(walletAddress, tokenBridgeAddress, {
                deposit_tokens: {},
            }, (_a = {}, _a[tokenAddress] = amount, _a)),
            new XplaMsgExecuteContract(walletAddress, tokenBridgeAddress, createInitiateTransfer({
                native_token: {
                    denom: tokenAddress,
                },
            }), {}),
        ]
        : [
            new XplaMsgExecuteContract(walletAddress, tokenAddress, {
                increase_allowance: {
                    spender: tokenBridgeAddress,
                    amount: amount,
                    expires: {
                        never: {},
                    },
                },
            }, {}),
            new XplaMsgExecuteContract(walletAddress, tokenBridgeAddress, createInitiateTransfer({
                token: {
                    contract_addr: tokenAddress,
                },
            }), {}),
        ];
}
export function transferNativeSol(connection, bridgeAddress, tokenBridgeAddress, payerAddress, amount, targetAddress, targetChain, relayerFee, payload, commitment) {
    if (relayerFee === void 0) { relayerFee = BigInt(0); }
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var rentBalance, payerPublicKey, ancillaryKeypair, createAncillaryAccountIx, initialBalanceTransferIx, initAccountIx, approvalIx, message, nonce, tokenBridgeTransferIx, closeAccountIx, blockhash, transaction;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, getMinimumBalanceForRentExemptAccount(connection, commitment)];
                case 1:
                    rentBalance = _a.sent();
                    payerPublicKey = new PublicKey(payerAddress);
                    ancillaryKeypair = Keypair.generate();
                    createAncillaryAccountIx = SystemProgram.createAccount({
                        fromPubkey: payerPublicKey,
                        newAccountPubkey: ancillaryKeypair.publicKey,
                        lamports: rentBalance,
                        space: ACCOUNT_SIZE,
                        programId: TOKEN_PROGRAM_ID,
                    });
                    initialBalanceTransferIx = SystemProgram.transfer({
                        fromPubkey: payerPublicKey,
                        lamports: amount,
                        toPubkey: ancillaryKeypair.publicKey,
                    });
                    initAccountIx = createInitializeAccountInstruction(ancillaryKeypair.publicKey, NATIVE_MINT, payerPublicKey);
                    approvalIx = createApproveAuthoritySignerInstruction(tokenBridgeAddress, ancillaryKeypair.publicKey, payerPublicKey, amount);
                    message = Keypair.generate();
                    nonce = createNonce().readUInt32LE(0);
                    tokenBridgeTransferIx = payload !== null
                        ? createTransferNativeWithPayloadInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, message.publicKey, ancillaryKeypair.publicKey, NATIVE_MINT, nonce, amount, Buffer.from(targetAddress), coalesceChainId(targetChain), payload)
                        : createTransferNativeInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, message.publicKey, ancillaryKeypair.publicKey, NATIVE_MINT, nonce, amount, relayerFee, Buffer.from(targetAddress), coalesceChainId(targetChain));
                    closeAccountIx = createCloseAccountInstruction(ancillaryKeypair.publicKey, //account to close
                    payerPublicKey, //Remaining funds destination
                    payerPublicKey //authority
                    );
                    return [4 /*yield*/, connection.getLatestBlockhash(commitment)];
                case 2:
                    blockhash = (_a.sent()).blockhash;
                    transaction = new SolanaTransaction();
                    transaction.recentBlockhash = blockhash;
                    transaction.feePayer = payerPublicKey;
                    transaction.add(createAncillaryAccountIx, initialBalanceTransferIx, initAccountIx, approvalIx, tokenBridgeTransferIx, closeAccountIx);
                    transaction.partialSign(message, ancillaryKeypair);
                    return [2 /*return*/, transaction];
            }
        });
    });
}
export function transferFromSolana(connection, bridgeAddress, tokenBridgeAddress, payerAddress, fromAddress, mintAddress, amount, targetAddress, targetChain, originAddress, originChain, fromOwnerAddress, relayerFee, payload, commitment) {
    if (relayerFee === void 0) { relayerFee = BigInt(0); }
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var originChainId, nonce, approvalIx, message, isSolanaNative, tokenBridgeTransferIx, transaction, blockhash;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    originChainId = originChain
                        ? coalesceChainId(originChain)
                        : undefined;
                    if (fromOwnerAddress === undefined) {
                        fromOwnerAddress = payerAddress;
                    }
                    nonce = createNonce().readUInt32LE(0);
                    approvalIx = createApproveAuthoritySignerInstruction(tokenBridgeAddress, fromAddress, fromOwnerAddress, amount);
                    message = Keypair.generate();
                    isSolanaNative = originChainId === undefined || originChainId === CHAIN_ID_SOLANA;
                    if (!isSolanaNative && !originAddress) {
                        return [2 /*return*/, Promise.reject("originAddress is required when specifying originChain")];
                    }
                    tokenBridgeTransferIx = isSolanaNative
                        ? payload !== null
                            ? createTransferNativeWithPayloadInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, message.publicKey, fromAddress, mintAddress, nonce, amount, targetAddress, coalesceChainId(targetChain), payload)
                            : createTransferNativeInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, message.publicKey, fromAddress, mintAddress, nonce, amount, relayerFee, targetAddress, coalesceChainId(targetChain))
                        : payload !== null
                            ? createTransferWrappedWithPayloadInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, message.publicKey, fromAddress, fromOwnerAddress, originChainId, originAddress, nonce, amount, targetAddress, coalesceChainId(targetChain), payload)
                            : createTransferWrappedInstruction(tokenBridgeAddress, bridgeAddress, payerAddress, message.publicKey, fromAddress, fromOwnerAddress, originChainId, originAddress, nonce, amount, relayerFee, targetAddress, coalesceChainId(targetChain));
                    transaction = new SolanaTransaction().add(approvalIx, tokenBridgeTransferIx);
                    return [4 /*yield*/, connection.getLatestBlockhash(commitment)];
                case 1:
                    blockhash = (_a.sent()).blockhash;
                    transaction.recentBlockhash = blockhash;
                    transaction.feePayer = new PublicKey(payerAddress);
                    transaction.partialSign(message);
                    return [2 /*return*/, transaction];
            }
        });
    });
}
/**
 * Transfers an asset from Algorand to a receiver on another chain
 * @param client AlgodV2 client
 * @param tokenBridgeId Application ID of the token bridge
 * @param bridgeId Application ID of the core bridge
 * @param sender Sending account
 * @param assetId Asset index
 * @param qty Quantity to transfer
 * @param receiver Receiving account
 * @param chain Reeiving chain
 * @param fee Transfer fee
 * @param payload payload for payload3 transfers
 * @returns Sequence number of confirmation
 */
export function transferFromAlgorand(client, tokenBridgeId, bridgeId, senderAddr, assetId, qty, receiver, chain, fee, payload) {
    if (payload === void 0) { payload = null; }
    return __awaiter(this, void 0, void 0, function () {
        var recipientChainId, tokenAddr, applAddr, txs, _a, emitterAddr, emitterOptInTxs, creator, creatorAcctInfo, wormhole, assetInfo, authAddr, params, msgFee, payTxn, bNat, result, _b, payTxn, bOptin, txn, t, accounts, t_1, t_2, args, acTxn;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    recipientChainId = coalesceChainId(chain);
                    tokenAddr = getApplicationAddress(tokenBridgeId);
                    applAddr = getEmitterAddressAlgorand(tokenBridgeId);
                    txs = [];
                    return [4 /*yield*/, optin(client, senderAddr, bridgeId, BigInt(0), applAddr)];
                case 1:
                    _a = _c.sent(), emitterAddr = _a.addr, emitterOptInTxs = _a.txs;
                    txs.push.apply(txs, __spreadArray([], __read(emitterOptInTxs)));
                    wormhole = false;
                    if (!(assetId !== BigInt(0))) return [3 /*break*/, 4];
                    return [4 /*yield*/, client
                            .getAssetByID(safeBigIntToNumber(assetId))
                            .do()];
                case 2:
                    assetInfo = _c.sent();
                    creator = assetInfo["params"]["creator"];
                    return [4 /*yield*/, client.accountInformation(creator).do()];
                case 3:
                    creatorAcctInfo = _c.sent();
                    authAddr = creatorAcctInfo["auth-addr"];
                    if (authAddr === tokenAddr) {
                        wormhole = true;
                    }
                    _c.label = 4;
                case 4: return [4 /*yield*/, client.getTransactionParams().do()];
                case 5:
                    params = _c.sent();
                    return [4 /*yield*/, getMessageFee(client, bridgeId)];
                case 6:
                    msgFee = _c.sent();
                    if (msgFee > 0) {
                        payTxn = makePaymentTxnWithSuggestedParamsFromObject({
                            from: senderAddr,
                            suggestedParams: params,
                            to: getApplicationAddress(tokenBridgeId),
                            amount: msgFee,
                        });
                        txs.push({ tx: payTxn, signer: null });
                    }
                    if (!!wormhole) return [3 /*break*/, 8];
                    bNat = Buffer.from("native", "binary").toString("hex");
                    return [4 /*yield*/, optin(client, senderAddr, tokenBridgeId, assetId, bNat)];
                case 7:
                    result = _c.sent();
                    creator = result.addr;
                    txs.push.apply(txs, __spreadArray([], __read(result.txs)));
                    _c.label = 8;
                case 8:
                    _b = assetId !== BigInt(0);
                    if (!_b) return [3 /*break*/, 10];
                    return [4 /*yield*/, assetOptinCheck(client, assetId, creator)];
                case 9:
                    _b = !(_c.sent());
                    _c.label = 10;
                case 10:
                    if (_b) {
                        payTxn = makePaymentTxnWithSuggestedParamsFromObject({
                            from: senderAddr,
                            to: creator,
                            amount: 100000,
                            suggestedParams: params,
                        });
                        txs.push({ tx: payTxn, signer: null });
                        bOptin = textToUint8Array("optin");
                        txn = makeApplicationCallTxnFromObject({
                            from: senderAddr,
                            appIndex: safeBigIntToNumber(tokenBridgeId),
                            onComplete: OnApplicationComplete.NoOpOC,
                            appArgs: [bOptin, bigIntToBytes(assetId, 8)],
                            foreignAssets: [safeBigIntToNumber(assetId)],
                            accounts: [creator],
                            suggestedParams: params,
                        });
                        txn.fee *= 2;
                        txs.push({ tx: txn, signer: null });
                    }
                    t = makeApplicationCallTxnFromObject({
                        from: senderAddr,
                        appIndex: safeBigIntToNumber(tokenBridgeId),
                        onComplete: OnApplicationComplete.NoOpOC,
                        appArgs: [textToUint8Array("nop")],
                        suggestedParams: params,
                    });
                    txs.push({ tx: t, signer: null });
                    accounts = [];
                    if (assetId === BigInt(0)) {
                        t_1 = makePaymentTxnWithSuggestedParamsFromObject({
                            from: senderAddr,
                            to: creator,
                            amount: qty,
                            suggestedParams: params,
                        });
                        txs.push({ tx: t_1, signer: null });
                        accounts = [emitterAddr, creator, creator];
                    }
                    else {
                        t_2 = makeAssetTransferTxnWithSuggestedParamsFromObject({
                            from: senderAddr,
                            to: creator,
                            suggestedParams: params,
                            amount: qty,
                            assetIndex: safeBigIntToNumber(assetId),
                        });
                        txs.push({ tx: t_2, signer: null });
                        accounts = [emitterAddr, creator, creatorAcctInfo["address"]];
                    }
                    args = [
                        textToUint8Array("sendTransfer"),
                        bigIntToBytes(assetId, 8),
                        bigIntToBytes(qty, 8),
                        hexToUint8Array(receiver),
                        bigIntToBytes(recipientChainId, 8),
                        bigIntToBytes(fee, 8),
                    ];
                    if (payload !== null) {
                        args.push(payload);
                    }
                    acTxn = makeApplicationCallTxnFromObject({
                        from: senderAddr,
                        appIndex: safeBigIntToNumber(tokenBridgeId),
                        onComplete: OnApplicationComplete.NoOpOC,
                        appArgs: args,
                        foreignApps: [safeBigIntToNumber(bridgeId)],
                        foreignAssets: [safeBigIntToNumber(assetId)],
                        accounts: accounts,
                        suggestedParams: params,
                    });
                    acTxn.fee *= 2;
                    txs.push({ tx: acTxn, signer: null });
                    return [2 /*return*/, txs];
            }
        });
    });
}
export function transferTokenFromNear(provider, account, coreBridge, tokenBridge, assetId, qty, receiver, chain, fee, payload) {
    if (payload === void 0) { payload = ""; }
    return __awaiter(this, void 0, void 0, function () {
        var isWrapped, messageFee, options, bal, bank;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    isWrapped = getIsWrappedAssetNear(tokenBridge, assetId);
                    return [4 /*yield*/, callFunctionNear(provider, coreBridge, "message_fee", {})];
                case 1:
                    messageFee = _a.sent();
                    chain = coalesceChainId(chain);
                    if (!isWrapped) return [3 /*break*/, 2];
                    return [2 /*return*/, [
                            {
                                contractId: tokenBridge,
                                methodName: "send_transfer_wormhole_token",
                                args: {
                                    token: assetId,
                                    amount: qty.toString(10),
                                    receiver: uint8ArrayToHex(receiver),
                                    chain: chain,
                                    fee: fee.toString(10),
                                    payload: payload,
                                    message_fee: messageFee,
                                },
                                attachedDeposit: new BN(messageFee + 1),
                                gas: new BN("100000000000000"),
                            },
                        ]];
                case 2:
                    options = [];
                    return [4 /*yield*/, callFunctionNear(provider, assetId, "storage_balance_of", {
                            account_id: tokenBridge,
                        })];
                case 3:
                    bal = _a.sent();
                    if (bal === null) {
                        // Looks like we have to stake some storage for this asset
                        // for the token bridge...
                        options.push({
                            contractId: assetId,
                            methodName: "storage_deposit",
                            args: { account_id: tokenBridge, registration_only: true },
                            gas: new BN("100000000000000"),
                            attachedDeposit: new BN("2000000000000000000000"), // 0.002 NEAR
                        });
                    }
                    if (!(messageFee > 0)) return [3 /*break*/, 5];
                    return [4 /*yield*/, callFunctionNear(provider, tokenBridge, "bank_balance", {
                            acct: account,
                        })];
                case 4:
                    bank = _a.sent();
                    if (!bank[0]) {
                        options.push({
                            contractId: tokenBridge,
                            methodName: "register_bank",
                            args: {},
                            gas: new BN("100000000000000"),
                            attachedDeposit: new BN("2000000000000000000000"), // 0.002 NEAR
                        });
                    }
                    if (bank[1] < messageFee) {
                        options.push({
                            contractId: tokenBridge,
                            methodName: "fill_bank",
                            args: {},
                            gas: new BN("100000000000000"),
                            attachedDeposit: new BN(messageFee),
                        });
                    }
                    _a.label = 5;
                case 5:
                    options.push({
                        contractId: assetId,
                        methodName: "ft_transfer_call",
                        args: {
                            receiver_id: tokenBridge,
                            amount: qty.toString(10),
                            msg: JSON.stringify({
                                receiver: uint8ArrayToHex(receiver),
                                chain: chain,
                                fee: fee.toString(10),
                                payload: payload,
                                message_fee: messageFee,
                            }),
                        },
                        attachedDeposit: new BN(1),
                        gas: new BN("100000000000000"),
                    });
                    return [2 /*return*/, options];
            }
        });
    });
}
export function transferNearFromNear(provider, coreBridge, tokenBridge, qty, receiver, chain, fee, payload) {
    if (payload === void 0) { payload = ""; }
    return __awaiter(this, void 0, void 0, function () {
        var messageFee;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, callFunctionNear(provider, coreBridge, "message_fee", {})];
                case 1:
                    messageFee = _a.sent();
                    return [2 /*return*/, {
                            contractId: tokenBridge,
                            methodName: "send_transfer_near",
                            args: {
                                receiver: uint8ArrayToHex(receiver),
                                chain: coalesceChainId(chain),
                                fee: fee.toString(10),
                                payload: payload,
                                message_fee: messageFee,
                            },
                            attachedDeposit: new BN(qty.toString(10)).add(new BN(messageFee)),
                            gas: new BN("100000000000000"),
                        }];
            }
        });
    });
}
/**
 * Transfer an asset on Aptos to another chain.
 * @param tokenBridgeAddress Address of token bridge
 * @param fullyQualifiedType Full qualified type of asset to transfer
 * @param amount Amount to send to recipient
 * @param recipientChain Target chain
 * @param recipient Recipient's address on target chain
 * @param relayerFee Fee to pay relayer
 * @param payload Payload3 data, leave undefined for basic token transfers
 * @returns Transaction payload
 */
export function transferFromAptos(tokenBridgeAddress, fullyQualifiedType, amount, recipientChain, recipient, relayerFee, payload) {
    if (relayerFee === void 0) { relayerFee = "0"; }
    if (payload === void 0) { payload = ""; }
    if (payload) {
        // Currently unsupported
        return transferTokensWithPayload(tokenBridgeAddress, fullyQualifiedType, amount, recipientChain, recipient, relayerFee, createNonce().readUInt32LE(0), payload);
    }
    return transferTokensAptos(tokenBridgeAddress, fullyQualifiedType, amount, recipientChain, recipient, relayerFee, createNonce().readUInt32LE(0));
}
