"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.setup_environment");
const BatchExchange = artifacts.require("BatchExchange");
const ERC20Mintable = artifacts.require("ERC20Mintable.sol");
const argv = require("yargs")
    .option("numAccounts", {
    describe: "Number of accounts to register with exchange",
    default: 3,
})
    .option("numTokens", {
    describe: "Number of tokens to create, fund and add to exchange",
    default: 3,
})
    .help()
    .version(false).argv;
module.exports = function (callback) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const instance = yield BatchExchange.deployed();
            const TokenOWL = artifacts.require("TokenOWL");
            const TokenOWLProxy = artifacts.require("TokenOWLProxy");
            const owlProxyContract = yield TokenOWLProxy.deployed();
            const owlProxy = yield TokenOWL.at(owlProxyContract.address);
            const accounts = yield web3.eth.getAccounts();
            yield owlProxy.setMinter(accounts[0]);
            const amount = web3.utils.toWei("3000");
            yield owlProxy.mintOWL(accounts[0], amount);
            yield owlProxy.approve(instance.address, amount);
            log.info(`Beginning setup with ${argv.numAccounts} accounts and ${argv.numTokens} tokens`);
            // Create and register tokens (feeToken is already registered)
            const tokens = [];
            for (let i = 1; i < argv.numTokens; i++) {
                const token = yield ERC20Mintable.new();
                yield instance.addToken(token.address);
                tokens.push(token);
            }
            // Create balance and approval for tokens
            for (let account = 0; account < argv.numAccounts; account++) {
                // Mint and approve OWL
                yield owlProxy.mintOWL(accounts[account], amount);
                yield owlProxy.approve(instance.address, amount, {
                    from: accounts[account],
                });
                // Mint and approve other tokens.
                for (const token of tokens) {
                    yield token.mint(accounts[account], amount);
                    yield token.approve(instance.address, amount, {
                        from: accounts[account],
                    });
                }
            }
            log.info("Environment setup complete!");
            callback();
        }
        catch (error) {
            callback(error);
        }
    });
};
