"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 take0xOrder_1 = require("./take0xOrder");
const setupInvestedTestFund_1 = require("~/tests/utils/setupInvestedTestFund");
const getTokenBySymbol_1 = require("~/utils/environment/getTokenBySymbol");
const deployAndInitTestEnv_1 = require("~/tests/utils/deployAndInitTestEnv");
const Contracts_1 = require("~/Contracts");
const createOrder_1 = require("~/contracts/exchanges/third-party/0x/utils/createOrder");
const signOrder_1 = require("~/contracts/exchanges/third-party/0x/utils/signOrder");
describe('take0xOrder', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
        shared.accounts = yield shared.env.eth.getAccounts();
        // shared.envNotManager = withDifferentAccount(
        //   shared.accounts[1],
        //   shared.env,
        // );
        shared.routes = yield setupInvestedTestFund_1.setupInvestedTestFund(shared.env);
        shared.zeroExAddress =
            shared.env.deployment.exchangeConfigs[Contracts_1.Exchanges.ZeroEx].exchange;
        shared.mln = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'MLN');
        shared.weth = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'WETH');
        const makerQuantity = token_math_1.createQuantity(shared.mln, 1);
        const takerQuantity = token_math_1.createQuantity(shared.weth, 0.05);
        const unsigned0xOrder = yield createOrder_1.createOrder(shared.env, shared.zeroExAddress, {
            makerQuantity,
            takerQuantity,
        });
        shared.signedOrder = yield signOrder_1.signOrder(shared.env, unsigned0xOrder);
        yield createOrder_1.approveOrder(shared.env, shared.zeroExAddress, shared.signedOrder);
    }));
    it('Take off-chain order from fund', () => __awaiter(this, void 0, void 0, function* () {
        const takerQuantity = token_math_1.createQuantity(shared.weth, 0.02);
        const order = yield take0xOrder_1.take0xOrder(shared.env, shared.routes.tradingAddress, {
            signedOrder: shared.signedOrder,
            takerQuantity,
        });
        expect(token_math_1.isEqual(order.takerFilledAmount, takerQuantity)).toBe(true);
    }));
});
