"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 cancel0xOrder_1 = require("./cancel0xOrder");
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");
const fillOrder_1 = require("~/contracts/exchanges/third-party/0x/transactions/fillOrder");
describe('cancel0xOrder', () => {
    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.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 unsigned0xOrder = yield createOrder_1.createOrder(shared.env, shared.zeroExAddress, {
            makerAddress: shared.routes.tradingAddress,
            makerQuantity: token_math_1.createQuantity(shared.weth, 0.05),
            takerQuantity: token_math_1.createQuantity(shared.mln, 1),
        });
        shared.signedOrder = yield signOrder_1.signOrder(shared.env, unsigned0xOrder);
        const result = yield make0xOrder_1.make0xOrder(shared.env, shared.routes.tradingAddress, {
            signedOrder: shared.signedOrder,
        });
        expect(result).toBe(true);
    }));
    // tslint:disable-next-line:max-line-length
    it('Previously made 0x order cancelled and not takeable anymore', () => __awaiter(this, void 0, void 0, function* () {
        const result = yield cancel0xOrder_1.cancel0xOrder(shared.env, shared.routes.tradingAddress, { signedOrder: shared.signedOrder });
        expect(result).toBe(true);
        yield expect(fillOrder_1.fillOrder(shared.env, shared.zeroExAddress, {
            signedOrder: shared.signedOrder,
        })).rejects.toThrow('CANCELLED');
    }));
});
