"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 makeEthfinexOrder_1 = require("./makeEthfinexOrder");
const deployAndInitTestEnv_1 = require("../../../../tests/utils/deployAndInitTestEnv");
const Contracts_1 = require("../../../../Contracts");
const getWrapperLock_1 = require("../../../exchanges/third-party/ethfinex/calls/getWrapperLock");
const setEthfinexWrapperRegistry_1 = require("../../../version/transactions/setEthfinexWrapperRegistry");
const getHub_1 = require("../../hub/calls/getHub");
const getRoutes_1 = require("../../hub/calls/getRoutes");
const transfer_1 = require("../../../dependencies/token/transactions/transfer");
const getTokenBySymbol_1 = require("../../../../utils/environment/getTokenBySymbol");
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,
    });
}));
// tslint:disable-next-line:max-line-length
test('Make ethfinex order from fund and take it from account in which makerToken is a non-native asset', () => __awaiter(this, void 0, void 0, function* () {
    const hubAddress = yield getHub_1.getHub(shared.env, shared.routes.tradingAddress);
    const { vaultAddress } = yield getRoutes_1.getRoutes(shared.env, hubAddress);
    const howMuch = token_math_1.createQuantity(shared.zx, 1);
    const receipt = yield transfer_1.transfer(shared.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(shared.env, shared.ethfinexAddress, {
        makerAddress: shared.routes.tradingAddress,
        makerQuantity,
        takerQuantity,
    });
    const signedOrder = yield signOrder_1.signOrder(shared.env, unsignedEthfinexOrder);
    const result = yield makeEthfinexOrder_1.makeEthfinexOrder(shared.env, shared.routes.tradingAddress, { signedOrder });
    expect(result).toBe(true);
    const filled = yield fillOrder_1.fillOrder(shared.env, shared.ethfinexAddress, {
        signedOrder,
    });
    expect(filled).toBeTruthy();
}));
