"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transactionStrategy = exports.hederaTransaction = exports.cosmosTransaction = exports.elrondTransaction = exports.solanaTransaction = exports.bitcoinTransaction = exports.evmTransaction = exports.rippleTransaction = exports.stellarTransaction = exports.tronTransaction = exports.tezosTransaction = exports.nearTransaction = exports.cardanoTransaction = exports.defaultTransaction = void 0;
const bignumber_js_1 = require("bignumber.js");
const Exchange_1 = require("../../wallet-api/Exchange");
const featureFlags_1 = require("../../featureFlags");
function defaultTransaction({ family, amount, recipient, customFeeConfig, }) {
    // Type assertion needed because each Transaction family has different required properties
    // This function provides base properties that are then extended by family-specific functions
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
    return {
        family,
        amount,
        recipient,
        ...customFeeConfig,
    };
}
exports.defaultTransaction = defaultTransaction;
function cardanoTransaction({ amount, recipient, customFeeConfig, }) {
    return {
        family: "cardano",
        amount,
        recipient,
        mode: "send",
        ...customFeeConfig,
    };
}
exports.cardanoTransaction = cardanoTransaction;
function nearTransaction({ amount, recipient, customFeeConfig, }) {
    return {
        family: "near",
        amount,
        recipient,
        mode: "send",
        ...customFeeConfig,
    };
}
exports.nearTransaction = nearTransaction;
function tezosTransaction({ amount, recipient, customFeeConfig, }) {
    return {
        family: "tezos",
        amount,
        recipient,
        mode: "send",
        ...customFeeConfig,
    };
}
exports.tezosTransaction = tezosTransaction;
function tronTransaction({ amount, recipient, customFeeConfig, }) {
    return {
        family: "tron",
        amount,
        recipient,
        mode: "send",
        ...customFeeConfig,
    };
}
exports.tronTransaction = tronTransaction;
function stellarTransaction({ amount, recipient, customFeeConfig, payinExtraId, customErrorType, }) {
    if (!payinExtraId)
        throw (0, Exchange_1.createStepError)({
            error: new Error("Missing payinExtraId"),
            step: Exchange_1.StepError.PAYIN_EXTRA_ID,
            customErrorType,
        });
    return {
        family: "stellar",
        mode: "send",
        amount,
        recipient,
        ...customFeeConfig,
        memoValue: payinExtraId,
        memoType: "MEMO_TEXT",
    };
}
exports.stellarTransaction = stellarTransaction;
function rippleTransaction({ amount, recipient, customFeeConfig, payinExtraId, customErrorType, }) {
    if (!payinExtraId)
        throw (0, Exchange_1.createStepError)({
            error: new Error("Missing payinExtraId"),
            step: Exchange_1.StepError.PAYIN_EXTRA_ID,
            customErrorType,
        });
    return {
        family: "xrp",
        amount,
        recipient,
        ...customFeeConfig,
        tag: new bignumber_js_1.BigNumber(payinExtraId).toNumber(),
    };
}
exports.rippleTransaction = rippleTransaction;
function evmTransaction({ amount, recipient, customFeeConfig, extraTransactionParameters, sponsored, }) {
    if (customFeeConfig?.gasLimit) {
        delete customFeeConfig.gasLimit;
    }
    if (extraTransactionParameters) {
        return {
            family: "evm",
            amount,
            recipient,
            ...customFeeConfig,
            data: Buffer.from(extraTransactionParameters, "hex"),
            sponsored,
        };
    }
    return {
        family: "evm",
        amount,
        recipient,
        ...customFeeConfig,
        sponsored,
    };
}
exports.evmTransaction = evmTransaction;
function bitcoinTransaction({ amount, recipient, customFeeConfig, extraTransactionParameters, }) {
    const baseTransaction = {
        family: "bitcoin",
        amount,
        recipient,
        ...customFeeConfig,
    };
    if (extraTransactionParameters) {
        return {
            ...baseTransaction,
            opReturnData: Buffer.from(extraTransactionParameters, "utf-8"),
        };
    }
    return baseTransaction;
}
exports.bitcoinTransaction = bitcoinTransaction;
function solanaTransaction({ amount, recipient, customFeeConfig: _customFeeConfig, extraTransactionParameters, }) {
    let templateId = undefined;
    const lifiSolanaFeature = (0, featureFlags_1.getFeature)({ key: "lifiSolana" });
    if (lifiSolanaFeature?.enabled && extraTransactionParameters) {
        try {
            const parsed = JSON.parse(extraTransactionParameters);
            if (typeof parsed?.solanaTransaction?.templateId === "string") {
                templateId = parsed.solanaTransaction.templateId;
            }
            else {
                console.warn(`Template id "${templateId}" found in extraTransactionParameters for solana transaction is not a string, ignored`);
            }
        }
        catch (e) {
            console.warn("Failed to parse extraTransactionParameters", e);
        }
    }
    return {
        family: "solana",
        amount,
        recipient,
        model: { kind: "transfer", uiState: {} },
        ...(templateId && { templateId }),
    };
}
exports.solanaTransaction = solanaTransaction;
function elrondTransaction({ amount, recipient, customFeeConfig, }) {
    return {
        family: "multiversx",
        amount,
        recipient,
        mode: "send",
        fees: customFeeConfig.fees || null,
        gasLimit: customFeeConfig.gasLimit ? Number(customFeeConfig.gasLimit) : 0,
    };
}
exports.elrondTransaction = elrondTransaction;
function tonTransaction({ amount, recipient, customFeeConfig: _customFeeConfig, }) {
    return {
        family: "ton",
        amount,
        recipient,
        comment: { isEncrypted: false, text: "" },
        fees: new bignumber_js_1.BigNumber(0), // Set default value as completeExchange call prepareTransaction, which set again fees.
    };
}
function cosmosTransaction({ amount, recipient, customFeeConfig, payinExtraId, }) {
    return {
        family: "cosmos",
        amount,
        recipient,
        mode: "send",
        networkInfo: null,
        ...customFeeConfig,
        memo: payinExtraId ?? undefined,
    };
}
exports.cosmosTransaction = cosmosTransaction;
function hederaTransaction({ amount, recipient, customFeeConfig, payinExtraId, }) {
    return {
        family: "hedera",
        amount,
        recipient,
        ...customFeeConfig,
        memo: payinExtraId ?? undefined,
    };
}
exports.hederaTransaction = hederaTransaction;
exports.transactionStrategy = {
    algorand: defaultTransaction,
    aptos: defaultTransaction,
    bitcoin: bitcoinTransaction,
    canton: defaultTransaction,
    cardano: cardanoTransaction,
    casper: defaultTransaction,
    celo: defaultTransaction,
    cosmos: cosmosTransaction,
    evm: evmTransaction,
    filecoin: defaultTransaction,
    hedera: hederaTransaction,
    icon: defaultTransaction,
    internet_computer: defaultTransaction,
    mina: defaultTransaction,
    multiversx: elrondTransaction,
    near: nearTransaction,
    polkadot: defaultTransaction,
    solana: solanaTransaction,
    stacks: defaultTransaction,
    stellar: stellarTransaction,
    sui: defaultTransaction,
    tezos: tezosTransaction,
    ton: tonTransaction,
    tron: tronTransaction,
    vechain: defaultTransaction,
    kaspa: defaultTransaction,
    xrp: rippleTransaction,
};
//# sourceMappingURL=transactionStrategies.js.map