"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const transactionFactory_1 = require("../../../../../utils/solidity/transactionFactory");
const Contracts_1 = require("../../../../../Contracts");
const token_math_1 = require("@melonproject/token-math");
const evm_1 = require("../../../../../utils/evm");
const Environment_1 = require("../../../../../utils/environment/Environment");
const isBuySell = (prices) => {
    if (!prices.length)
        return false;
    const first = prices[0];
    return (first.buy !== undefined && first.sell !== undefined);
};
const prepareArgs = (environment, { prices, blockNumber: givenBlockNumber }) => __awaiter(this, void 0, void 0, function* () {
    environment.logger('debug', Environment_1.LogLevels.DEBUG, prices);
    const tokens = isBuySell(prices)
        ? prices.map(p => p.buy.base.token.address.toString())
        : prices.map(p => p.base.token.address.toString());
    const baseBuy = isBuySell(prices)
        ? prices.map(p => token_math_1.toAtomic(p.buy))
        : prices.map(p => token_math_1.toAtomic(p));
    const baseSell = isBuySell(prices)
        ? prices.map(p => token_math_1.toAtomic(p.sell))
        : prices.map(p => token_math_1.toAtomic(p));
    const blockNumber = givenBlockNumber || (yield evm_1.getLatestBlock(environment)).number;
    const zeroExArray = isBuySell(prices)
        ? prices.map(p => '0x')
        : prices.map(p => '0x');
    const zeroArray = isBuySell(prices) ? prices.map(p => 0) : prices.map(p => 0);
    return [
        tokens,
        baseBuy,
        baseSell,
        zeroExArray,
        zeroExArray,
        blockNumber,
        zeroArray,
    ];
});
const setBaseRate = transactionFactory_1.transactionFactory('setBaseRate', Contracts_1.Contracts.ConversionRates, undefined, prepareArgs);
exports.setBaseRate = setBaseRate;
