"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const utilities_1 = require("../test/utilities");
const logging_1 = require("../src/logging");
const log = logging_1.factory.getLogger("scripts.place_order");
const BatchExchange = artifacts.require("BatchExchange");
const argv = require("yargs")
    .option("accountId", {
    describe: "Account index of the order placer",
})
    .option("sellToken", {
    describe: "Token to be sold",
})
    .option("buyToken", {
    describe: "token to be bought",
})
    .option("minBuy", {
    describe: "minimum amount to be bought (in 10**18 WEI of buyToken, e.g. 1 = 1ETH)",
})
    .option("maxSell", {
    describe: "minimum amount to be sold (in 10**18 WEI of sellToken, e.g. 1 = 1ETH)",
})
    .option("validFor", {
    describe: "the number of auctions for which this orders is valid",
})
    .demand([
    "accountId",
    "sellToken",
    "buyToken",
    "minBuy",
    "maxSell",
    "validFor",
])
    .help(false)
    .version(false).argv;
module.exports = (callback) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const minBuy = web3.utils.toWei(String(argv.minBuy));
        const maxSell = web3.utils.toWei(String(argv.maxSell));
        const instance = yield BatchExchange.deployed();
        const accounts = yield web3.eth.getAccounts();
        const account = accounts[argv.accountId];
        const batch_index = (yield instance.getCurrentBatchId()).toNumber();
        const valid_until = batch_index + parseInt(argv.validFor);
        const id = yield utilities_1.sendTxAndGetReturnValue(instance.placeOrder, argv.buyToken, argv.sellToken, valid_until, minBuy, maxSell, {
            from: account,
        });
        log.info(`Limit Sell Order placed ID ${id}. Valid from batch ${batch_index} until ${valid_until}`);
        callback();
    }
    catch (error) {
        callback(error);
    }
});
