"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());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const R = __importStar(require("ramda"));
const Contracts_1 = require("../../../Contracts");
const deployContract_1 = require("../../../utils/solidity/deployContract");
const promisesSerial_1 = require("../../../utils/helpers/promisesSerial");
const ensure_1 = require("../../../utils/guards/ensure");
const deployErc20Proxy_1 = require("./deployErc20Proxy");
const addAuthorizedAddress_1 = require("../third-party/0x/transactions/addAuthorizedAddress");
const registerAssetProxy_1 = require("../third-party/0x/transactions/registerAssetProxy");
const changeZRXAsset_1 = require("../third-party/0x/transactions/changeZRXAsset");
const addNewWrapperPair_1 = require("../third-party/ethfinex/transactions/addNewWrapperPair");
exports.deployEthfinex = (environment, { tokens, erc20Proxy: givenErc20Proxy }) => __awaiter(this, void 0, void 0, function* () {
    const ethfinex = yield deployContract_1.deployContract(environment, Contracts_1.Contracts.EthfinexExchangeEfx, []);
    const wrapperRegistryEFX = yield deployContract_1.deployContract(environment, Contracts_1.Contracts.WrapperRegistryEFX, []);
    const erc20Proxy = givenErc20Proxy || (yield deployErc20Proxy_1.deployErc20Proxy(environment));
    const tokenWrappersPromises = tokens.map(token => () => __awaiter(this, void 0, void 0, function* () {
        if (token.symbol === 'WETH') {
            return deployContract_1.deployContract(environment, Contracts_1.Contracts.WrapperLockEth, [
                'WETH',
                'WETH token',
                18,
                ethfinex.toString(),
                erc20Proxy.toString(),
            ]);
        }
        else {
            return deployContract_1.deployContract(environment, Contracts_1.Contracts.WrapperLock, [
                token.address.toString(),
                `W-${token.symbol}`,
                `${token.symbol} Token`,
                token.decimals,
                false,
                ethfinex.toString(),
                erc20Proxy.toString(),
            ]);
        }
    }));
    const tokenWrappers = yield promisesSerial_1.promisesSerial(tokenWrappersPromises);
    yield addAuthorizedAddress_1.addAuthorizedAddress(environment, erc20Proxy, { exchange: ethfinex });
    yield registerAssetProxy_1.registerAssetProxy(environment, ethfinex, { assetProxy: erc20Proxy });
    const zrxToken = tokens.find(R.propEq('symbol', 'ZRX'));
    ensure_1.ensure(!!zrxToken, `No ZRX token found in provided tokens: ${tokens
        .map(R.prop('symbol'))
        .join(', ')}`);
    yield changeZRXAsset_1.changeZRXAsset(environment, ethfinex, { zrxToken });
    const wrapperPairs = yield addNewWrapperPair_1.addNewWrapperPair(environment, wrapperRegistryEFX, {
        tokens,
        wrappers: tokenWrappers,
    });
    return {
        erc20Proxy,
        exchange: ethfinex,
        wrapperPairs,
        wrapperRegistryEFX,
    };
});
