"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 deploy0xExchange_1 = require("../../contracts/exchanges/transactions/deploy0xExchange");
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");
const initTestEnvironment_1 = require("../utils/initTestEnvironment");
const withDifferentAccount_1 = require("../../utils/environment/withDifferentAccount");
const getToken_1 = require("../../contracts/dependencies/token/calls/getToken");
const deploy_1 = require("../../contracts/dependencies/token/transactions/deploy");
const transfer_1 = require("../../contracts/dependencies/token/transactions/transfer");
describe('account-0x-trading', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield initTestEnvironment_1.initTestEnvironment();
        shared.accounts = yield shared.env.eth.getAccounts();
        shared.envTaker = withDifferentAccount_1.withDifferentAccount(shared.env, shared.accounts[1]);
        shared.wethToken = yield getToken_1.getToken(shared.env, yield deploy_1.deployToken(shared.env, 'WETH'));
        shared.mlnToken = yield getToken_1.getToken(shared.env, yield deploy_1.deployToken(shared.env, 'MLN'));
        shared.zrxToken = yield getToken_1.getToken(shared.env, yield deploy_1.deployToken(shared.env, 'ZRX'));
        yield transfer_1.transfer(shared.env, {
            howMuch: token_math_1.createQuantity(shared.wethToken, 100),
            to: shared.envTaker.wallet.address,
        });
        shared.zeroExAddress = yield deploy0xExchange_1.deploy0xExchange(shared.env, {
            zrxToken: shared.zrxToken,
        });
    }));
    it('Happy path', () => __awaiter(this, void 0, void 0, function* () {
        const makerQuantity = token_math_1.createQuantity(shared.mlnToken, 1);
        const takerQuantity = token_math_1.createQuantity(shared.wethToken, 0.05);
        const unsignedOrder = yield createOrder_1.createOrder(shared.env, shared.zeroExAddress, {
            makerQuantity,
            takerQuantity,
        });
        yield createOrder_1.approveOrder(shared.env, shared.zeroExAddress, unsignedOrder);
        const signedOrder = yield signOrder_1.signOrder(shared.env, unsignedOrder);
        expect(signedOrder.exchangeAddress).toBe(shared.zeroExAddress.toLowerCase());
        expect(signedOrder.makerAddress).toBe(shared.accounts[0].toLowerCase());
        expect(signedOrder.makerAssetAmount.toString()).toBe(makerQuantity.quantity.toString());
        const result = yield fillOrder_1.fillOrder(shared.envTaker, shared.zeroExAddress, {
            signedOrder,
        });
        expect(result).toBeTruthy();
    }));
});
