"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.claim_withdraw");
const BatchExchange = artifacts.require("BatchExchange");
const ERC20 = artifacts.require("ERC20");
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
const argv = require("yargs")
    .option("accountId", {
    describe: "Claimer's account index",
})
    .option("tokenId", {
    describe: "Token to claim",
})
    .demand(["accountId", "tokenId"])
    .help(false)
    .version(false).argv;
module.exports = (callback) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        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}`);
        }
        const token = yield ERC20.at(token_address);
        const balance_before = yield token.balanceOf(withdrawer);
        yield instance.withdraw(withdrawer, token_address, { from: withdrawer });
        const balance_after = yield token.balanceOf(withdrawer);
        log.info(`Success! Balance of token ${argv.tokenId} before claim: ${balance_before}, after claim: ${balance_after}`);
        callback();
    }
    catch (error) {
        callback(error);
    }
});
