"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const deployAndInitTestEnv_1 = require("../../../../tests/utils/deployAndInitTestEnv");
const Contracts_1 = require("../../../../Contracts");
const getTokenBySymbol_1 = require("../../../../utils/environment/getTokenBySymbol");
const token_math_1 = require("@melonproject/token-math");
const makeOasisDexOrder_1 = require("./makeOasisDexOrder");
const setupInvestedTestFund_1 = require("../../../../tests/utils/setupInvestedTestFund");
const cancelOasisDexOrder_1 = require("./cancelOasisDexOrder");
const getActiveOasisDexOrders_1 = require("../../../exchanges/calls/getActiveOasisDexOrders");
const takeOrderFromAccountOasisDex_1 = __importDefault(require("../../../exchanges/transactions/takeOrderFromAccountOasisDex"));
const getOasisDexOrder_1 = require("../../../exchanges/calls/getOasisDexOrder");
describe('makeOasisDexOrder', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
        shared.accounts = yield shared.env.eth.getAccounts();
        shared.routes = yield setupInvestedTestFund_1.setupInvestedTestFund(shared.env);
        shared.oasisDex =
            shared.env.deployment.exchangeConfigs[Contracts_1.Exchanges.MatchingMarket].exchange;
        shared.mln = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'MLN');
        shared.weth = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'WETH');
    }));
    it('make oasisdex order', () => __awaiter(this, void 0, void 0, function* () {
        const makerQuantity = token_math_1.createQuantity(shared.weth, 0.05);
        const takerQuantity = token_math_1.createQuantity(shared.mln, 1);
        const orderToCancel = yield makeOasisDexOrder_1.makeOasisDexOrder(shared.env, shared.routes.tradingAddress, { makerQuantity, takerQuantity });
        expect(orderToCancel.buy).toEqual(takerQuantity);
        expect(orderToCancel.sell).toEqual(makerQuantity);
        expect(orderToCancel.maker).toEqual(shared.routes.tradingAddress);
        yield expect(makeOasisDexOrder_1.makeOasisDexOrder(shared.env, shared.routes.tradingAddress, {
            makerQuantity,
            takerQuantity,
        })).rejects.toThrow('open order');
        yield cancelOasisDexOrder_1.cancelOasisDexOrder(shared.env, shared.routes.tradingAddress, {
            id: orderToCancel.id,
            maker: shared.routes.tradingAddress,
            makerAsset: orderToCancel.sell.token.address,
            takerAsset: orderToCancel.buy.token.address,
        });
        // Now it should work again
        const orderToStay = yield makeOasisDexOrder_1.makeOasisDexOrder(shared.env, shared.routes.tradingAddress, {
            makerQuantity,
            takerQuantity,
        });
        const orders = yield getActiveOasisDexOrders_1.getActiveOasisDexOrders(shared.env, shared.env.deployment.melonContracts.adapters.matchingMarketAccessor, {
            buyAsset: takerQuantity.token.address,
            sellAsset: makerQuantity.token.address,
            targetExchange: shared.oasisDex,
        });
        expect(orders.length).toBe(1);
        const gotOrder = yield getOasisDexOrder_1.getOasisDexOrder(shared.env, shared.oasisDex, {
            id: orderToStay.id,
        });
        expect(token_math_1.isEqual(gotOrder.buy, takerQuantity)).toBe(true);
        expect(token_math_1.isEqual(gotOrder.buy, orderToStay.buy)).toBe(true);
        yield takeOrderFromAccountOasisDex_1.default(shared.env, shared.oasisDex, {
            buy: orderToStay.buy,
            id: orderToStay.id,
            maxTakeAmount: orderToStay.sell,
            sell: orderToStay.sell,
        });
    }));
});
