"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 logging_1 = require("../src/logging");
const log = logging_1.factory.getLogger("scripts.request_withdraw");
const BatchExchange = artifacts.require("BatchExchange");
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
const argv = require("yargs")
    .option("accountId", {
    describe: "Withdrawers's account index",
})
    .option("tokenId", {
    describe: "Token to withdraw",
})
    .option("amount", {
    describe: "Amount in to withdraw (in 10**18 WEI, e.g. 1 = 1 ETH)",
})
    .demand(["accountId", "tokenId", "amount"])
    .help(false)
    .version(false).argv;
module.exports = (callback) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const amount = web3.utils.toWei(String(argv.amount));
        const instance = yield BatchExchange.deployed();
        const accounts = yield web3.eth.getAccounts();
        const withdrawer = accounts[argv.accountId];
        log.info(`Using account ${withdrawer}`);
        const token_address = yield instance.tokenIdToAddressMap(argv.tokenId);
        if (token_address === ZERO_ADDRESS) {
            callback(`Error: No token registered at index ${argv.tokenId}`);
        }
        yield instance.requestWithdraw(token_address, amount, { from: withdrawer });
        const [, claimable_at] = yield instance.getPendingWithdraw(withdrawer, token_address);
        log.info(`Withdraw request successful. Will be claimable in batch ${claimable_at}`);
        callback();
    }
    catch (error) {
        callback(error);
    }
});
