"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 getTokenBySymbol_1 = require("../../../../utils/environment/getTokenBySymbol");
const make0xOrder_1 = require("./make0xOrder");
const deployAndInitTestEnv_1 = require("../../../../tests/utils/deployAndInitTestEnv");
const Contracts_1 = require("../../../../Contracts");
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");
describe('make0xOrder', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        const env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
        shared.env = env;
        shared.accounts = yield shared.env.eth.getAccounts();
        shared.routes = yield setupInvestedTestFund_1.setupInvestedTestFund(shared.env);
        shared.zeroExAddress =
            env.deployment.exchangeConfigs[Contracts_1.Exchanges.ZeroEx].exchange;
        shared.mln = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'MLN');
        shared.weth = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'WETH');
    }));
    it('Make 0x order from fund and take it from account', () => __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 unsigned0xOrder = yield createOrder_1.createOrder(shared.env, shared.zeroExAddress, {
            makerAddress: shared.routes.tradingAddress,
            makerQuantity,
            takerQuantity,
        });
        const signedOrder = yield signOrder_1.signOrder(shared.env, unsigned0xOrder);
        const result = yield make0xOrder_1.make0xOrder(shared.env, shared.routes.tradingAddress, {
            signedOrder,
        });
        expect(result).toBe(true);
        const filled = yield fillOrder_1.fillOrder(shared.env, shared.zeroExAddress, {
            signedOrder,
        });
        expect(filled).toBeTruthy();
    }));
});
