"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const token_math_1 = require("@melonproject/token-math");
const getContract_1 = require("./getContract");
const prepareTransaction_1 = require("./prepareTransaction");
const ensure_1 = require("../guards/ensure");
const signTransaction_1 = require("../environment/signTransaction");
const EnsureError_1 = require("../guards/EnsureError");
const getBalance_1 = require("../evm/getBalance");
const getLogCurried_1 = require("../environment/getLogCurried");
const parseReceiptLogs_1 = require("./parseReceiptLogs");
const getLog = getLogCurried_1.getLogCurried('melon:protocol:utils:solidity:transactionFactory');
const TRANSACTION_POLL_INTERVAL = 15 * 1000;
const TRANSACTION_TIMEOUT = 10 * 60 * 1000;
exports.defaultGuard = () => __awaiter(this, void 0, void 0, function* () { });
exports.defaultPrepareArgs = (environment, params, contractAddress) => __awaiter(this, void 0, void 0, function* () { return Object.values(params || {}).map(v => v.toString()); });
exports.defaultPostProcess = () => __awaiter(this, void 0, void 0, function* () { return true; });
/**
 * If the TX is signed, it comes as the raw signed string
 * (result.rawTransaction) as in:
 * https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#id5
 *
 * Otherwise, we assume it is a tx-object like:
 * https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#id4
 * ```
 *  {
 *    to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
 *    value: '1000000000',
 *    gas: 2000000
 *  }
 * ```
 */
const isSignedTx = signedOrNotTx => typeof signedOrNotTx === 'string';
/**
 * The transaction factory returns a function "execute" (You have to rename it
 * to the actual name of the transaction, for example: "transfer"). As a
 * minimum, one needs to provide the transaction name and the contract path:
 *
 * ```typescript
 * const transfer = transactionFactory('transfer', Contract.Token);
 * ```
 *
 * This transfer function can then be executed directly:
 *
 * ```typescript
 * await transfer(new Address('0xdeadbeef'));
 * ```
 *
 * Or sliced into a prepare and a send part:
 * ```typescript
 * const preparedTransaction: PreparedTransaction =
 *    await transfer.prepare(new Address('0xdeadbeef'));
 *
 * // pass that prepared transaction to the signer
 * const result = await transfer.send(new Address('0xdeadbeef'),
 *    preparedTransaction);
 * ```
 */
const transactionFactory = (name, contract, guard = exports.defaultGuard, prepareArgs = exports.defaultPrepareArgs, postProcess = exports.defaultPostProcess, defaultOptions = {}) => {
    const prepare = (environment, contractAddress, params, optionsOrCallback = defaultOptions) => __awaiter(this, void 0, void 0, function* () {
        const log = getLog(environment);
        const options = typeof optionsOrCallback === 'function'
            ? optionsOrCallback(environment)
            : optionsOrCallback;
        if (!options.skipGuards) {
            yield guard(environment, params, contractAddress, options);
        }
        const args = yield prepareArgs(environment, params, contractAddress);
        const txId = `${contract}@${contractAddress}.${name}(${args
            .map(JSON.stringify)
            .join(',')})`;
        log.info('Prepare transaction', txId);
        try {
            const contractInstance = getContract_1.getContract(environment, contract, contractAddress);
            ensure_1.ensure(!!contractInstance.methods[name], `Method ${name} does not exist on contract ${contract}`);
            const transaction = contractInstance.methods[name](...args);
            transaction.name = name;
            const prepared = yield prepareTransaction_1.prepareTransaction(environment, transaction, options);
            // HACK: To avoid circular dependencies (?)
            const { calcAmguInEth, } = yield Promise.resolve().then(() => __importStar(require('../../contracts/engine/calls/calcAmguInEth')));
            const amguInEth = options.amguPayable
                ? yield calcAmguInEth(environment, contractAddress, prepared.gasEstimation)
                : token_math_1.createQuantity('eth', '0'); /*;*/
            const incentiveInEth = options.incentive
                ? token_math_1.createQuantity('eth', '10000000000000000')
                : token_math_1.createQuantity('eth', '0');
            const melonTransaction = {
                amguInEth,
                contract,
                incentiveInEth,
                name,
                params,
                rawTransaction: {
                    data: prepared.encoded,
                    from: `${options.from || environment.wallet.address}`,
                    gas: `${options.gas || prepared.gasEstimation}`,
                    gasPrice: `${options.gasPrice || environment.options.gasPrice}`,
                    to: `${contractAddress}`,
                    value: `${options.value ||
                        token_math_1.add(amguInEth.quantity, incentiveInEth.quantity)}`,
                },
                transactionArgs: prepared.transaction.arguments,
            };
            const totalCost = token_math_1.createQuantity('ETH', token_math_1.add(token_math_1.toBI(melonTransaction.rawTransaction.value), token_math_1.multiply(token_math_1.toBI(melonTransaction.rawTransaction.gas), token_math_1.toBI(melonTransaction.rawTransaction.gasPrice))));
            const balance = yield getBalance_1.getBalance(environment);
            ensure_1.ensure(token_math_1.greaterThan(balance, totalCost), `Insufficent balance. Got: ${token_math_1.toFixed(balance)}, need: ${token_math_1.toFixed(totalCost)}`);
            log.debug('Transaction prepared', melonTransaction);
            return melonTransaction;
        }
        catch (e) {
            log.error(txId, e, args);
            if (e instanceof EnsureError_1.EnsureError) {
                throw e;
            }
            else {
                throw new Error(
                // tslint:disable-next-line:max-line-length
                `Error in prepare transaction ${txId}): ${e.message}`);
            }
        }
    });
    /**
     * - Wait for receipt
     * - Poll for receipt
     * - Reject after 10 minutes
     */
    const send = (environment, contractAddress, signedOrNotTx, 
    // prepared,
    params) => new Promise((resolve, reject) => {
        let transactionHash;
        let pollInterval;
        const log = getLog(environment);
        const receiptPromiEvent = isSignedTx(signedOrNotTx)
            ? environment.eth.sendSignedTransaction(signedOrNotTx)
            : environment.eth.sendTransaction(signedOrNotTx);
        const transactionTimeout = setTimeout(() => {
            if (pollInterval)
                clearInterval(pollInterval);
            log.error('Transaction timed out', name);
            reject(
            // tslint:disable-next-line: max-line-length
            `Transaction ${name} with transaction hash: ${transactionHash} not mined after ${TRANSACTION_TIMEOUT /
                1000 /
                60} minutes. It might get mined eventually.`);
        }, TRANSACTION_TIMEOUT);
        const processReceipt = (receipt) => __awaiter(this, void 0, void 0, function* () {
            try {
                const receiptWithEvents = parseReceiptLogs_1.parseReceiptLogs(receipt, log);
                log.debug(`Receipt for ${name}`, receiptWithEvents);
                const postprocessed = yield postProcess(environment, receiptWithEvents, params, contractAddress);
                clearTimeout(transactionTimeout);
                if (pollInterval)
                    clearInterval(pollInterval);
                resolve(postprocessed);
            }
            catch (error) {
                reject('Transaction error in postprocess ***');
            }
        });
        receiptPromiEvent.on('receipt', receipt => {
            processReceipt(receipt);
        });
        receiptPromiEvent.on('transactionHash', txHash => {
            transactionHash = txHash;
            pollInterval = setInterval(() => __awaiter(this, void 0, void 0, function* () {
                const receipt = yield environment.eth.getTransactionReceipt(transactionHash);
                if (receipt) {
                    log.debug('Got TX hash from polling');
                    yield processReceipt(receipt);
                }
            }), TRANSACTION_POLL_INTERVAL);
        });
        receiptPromiEvent.on('error', error => {
            log.error('Transaction error', name, error);
            // throw error;
            // new Error(`Transaction error ${name}: ${error.message}`);
            reject(`Transaction error ${name}: ${error.message}`);
        });
    });
    const execute = (environment, contractAddress, params, options = defaultOptions) => __awaiter(this, void 0, void 0, function* () {
        const prepared = yield prepare(environment, contractAddress, params, options);
        const signedTransactionData = yield signTransaction_1.signTransaction(environment, prepared.rawTransaction);
        const result = yield send(environment, contractAddress, signedTransactionData, 
        // prepared,
        params, options);
        return result;
    });
    execute.prepare = prepare;
    execute.send = send;
    return execute;
};
exports.transactionFactory = transactionFactory;
