"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const token_math_1 = require("@melonproject/token-math");
const setupInvestedTestFund_1 = require("../../../../tests/utils/setupInvestedTestFund");
const deployAndInitTestEnv_1 = require("../../../../tests/utils/deployAndInitTestEnv");
const getHub_1 = require("../../hub/calls/getHub");
const getRoutes_1 = require("../../hub/calls/getRoutes");
const transfer_1 = require("../../../dependencies/token/transactions/transfer");
const getWrapperLock_1 = require("../../../exchanges/third-party/ethfinex/calls/getWrapperLock");
const setEthfinexWrapperRegistry_1 = require("../../../version/transactions/setEthfinexWrapperRegistry");
const Contracts_1 = require("../../../../Contracts");
const getTokenBySymbol_1 = require("../../../../utils/environment/getTokenBySymbol");
const makeEthfinexOrder_1 = require("./makeEthfinexOrder");
const cancelEthfinexOrder_1 = require("./cancelEthfinexOrder");
const createOrder_1 = require("../../../exchanges/third-party/0x/utils/createOrder");
const signOrder_1 = require("../../../exchanges/third-party/0x/utils/signOrder");
const fillOrder_1 = require("../../../exchanges/third-party/0x/transactions/fillOrder");
const shared = {};
beforeAll(() => __awaiter(this, void 0, void 0, function* () {
    const env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
    const registry = env.deployment.melonContracts.registry;
    const wrapperRegistryEFX = env.deployment.thirdPartyContracts.exchanges.ethfinex.wrapperRegistryEFX;
    shared.env = env;
    shared.accounts = yield env.eth.getAccounts();
    shared.routes = yield setupInvestedTestFund_1.setupInvestedTestFund(env);
    shared.ethfinexAddress =
        env.deployment.exchangeConfigs[Contracts_1.Exchanges.Ethfinex].exchange;
    yield setEthfinexWrapperRegistry_1.setEthfinexWrapperRegistry(env, registry, {
        address: wrapperRegistryEFX,
    });
    shared.mln = getTokenBySymbol_1.getTokenBySymbol(env, 'MLN');
    shared.weth = getTokenBySymbol_1.getTokenBySymbol(env, 'WETH');
    shared.zx = getTokenBySymbol_1.getTokenBySymbol(env, 'ZRX');
    shared.mlnWrapperLock = yield getWrapperLock_1.getWrapperLock(env, wrapperRegistryEFX, {
        token: shared.mln,
    });
    shared.wethWrapperLock = yield getWrapperLock_1.getWrapperLock(env, wrapperRegistryEFX, {
        token: shared.weth,
    });
    shared.zxWrapperLock = yield getWrapperLock_1.getWrapperLock(env, wrapperRegistryEFX, {
        token: shared.zx,
    });
    const hubAddress = yield getHub_1.getHub(env, shared.routes.tradingAddress);
    const { vaultAddress } = yield getRoutes_1.getRoutes(env, hubAddress);
    const howMuch = token_math_1.createQuantity(shared.zx, 1);
    const receipt = yield transfer_1.transfer(env, { howMuch, to: vaultAddress });
    expect(receipt).toBeTruthy();
    const makerQuantity = token_math_1.createQuantity(shared.zxWrapperLock, 0.05);
    const takerQuantity = token_math_1.createQuantity(shared.mln, 1);
    const unsignedEthfinexOrder = yield createOrder_1.createOrder(env, shared.ethfinexAddress, {
        makerAddress: shared.routes.tradingAddress,
        makerQuantity,
        takerQuantity,
    });
    shared.signedOrder = yield signOrder_1.signOrder(shared.env, unsignedEthfinexOrder);
    const result = yield makeEthfinexOrder_1.makeEthfinexOrder(shared.env, shared.routes.tradingAddress, { signedOrder: shared.signedOrder });
    expect(result).toBe(true);
}));
// tslint:disable-next-line:max-line-length
test('Previously made ethfinex order cancelled and not takeable anymore', () => __awaiter(this, void 0, void 0, function* () {
    const result = yield cancelEthfinexOrder_1.cancelEthfinexOrder(shared.env, shared.routes.tradingAddress, { signedOrder: shared.signedOrder });
    expect(result).toBe(true);
    yield expect(fillOrder_1.fillOrder(shared.env, shared.ethfinexAddress, {
        signedOrder: shared.signedOrder,
    })).rejects.toThrow('CANCELLED');
}));
