"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 token_math_1 = require("@melonproject/token-math");
const getTokenBySymbol_1 = require("../../utils/environment/getTokenBySymbol");
const makeOrderFromAccountOasisDex_1 = require("../../contracts/exchanges/transactions/makeOrderFromAccountOasisDex");
const takeOrderFromAccountOasisDex_1 = __importDefault(require("../../contracts/exchanges/transactions/takeOrderFromAccountOasisDex"));
const cancelOrderFromAccountOasisDex_1 = __importDefault(require("../../contracts/exchanges/transactions/cancelOrderFromAccountOasisDex"));
const deployAndInitTestEnv_1 = require("../utils/deployAndInitTestEnv");
const Contracts_1 = require("../../Contracts");
const getActiveOasisDexOrders_1 = require("../../contracts/exchanges/calls/getActiveOasisDexOrders");
describe('account-trading', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
        shared.accounts = yield shared.env.eth.getAccounts();
    }));
    it('Happy path', () => __awaiter(this, void 0, void 0, function* () {
        const matchingMarketAddress = shared.env.deployment.exchangeConfigs[Contracts_1.Exchanges.MatchingMarket].exchange;
        const matchingMarketAccessorAddress = shared.env.deployment.melonContracts.adapters.matchingMarketAccessor;
        const mlnToken = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'MLN');
        const wethToken = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'WETH');
        const order1 = yield makeOrderFromAccountOasisDex_1.makeOrderFromAccountOasisDex(shared.env, matchingMarketAddress, {
            buy: token_math_1.createQuantity(mlnToken, 2),
            sell: token_math_1.createQuantity(wethToken, 0.1),
        });
        expect(order1.buy).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(order1.sell).toEqual(token_math_1.createQuantity(wethToken, 0.1));
        const activeOrders1 = yield getActiveOasisDexOrders_1.getActiveOasisDexOrders(shared.env, matchingMarketAccessorAddress, {
            targetExchange: matchingMarketAddress,
            buyAsset: mlnToken.address,
            sellAsset: wethToken.address,
        });
        expect(activeOrders1[0].buy).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(activeOrders1[0].sell).toEqual(token_math_1.createQuantity(wethToken, 0.1));
        yield takeOrderFromAccountOasisDex_1.default(shared.env, matchingMarketAddress, {
            buy: order1.buy,
            id: order1.id,
            maxTakeAmount: order1.sell,
            sell: order1.sell,
        });
        const activeOrders2 = yield getActiveOasisDexOrders_1.getActiveOasisDexOrders(shared.env, matchingMarketAccessorAddress, {
            targetExchange: matchingMarketAddress,
            buyAsset: mlnToken.address,
            sellAsset: wethToken.address,
        });
        expect(activeOrders2.length).toBe(0);
        const order2 = yield makeOrderFromAccountOasisDex_1.makeOrderFromAccountOasisDex(shared.env, matchingMarketAddress, {
            buy: token_math_1.createQuantity(mlnToken, 2),
            sell: token_math_1.createQuantity(wethToken, 0.1),
        });
        expect(order2.buy).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(order2.sell).toEqual(token_math_1.createQuantity(wethToken, 0.1));
        const activeOrders3 = yield getActiveOasisDexOrders_1.getActiveOasisDexOrders(shared.env, matchingMarketAccessorAddress, {
            targetExchange: matchingMarketAddress,
            buyAsset: mlnToken.address,
            sellAsset: wethToken.address,
        });
        expect(activeOrders3[0].buy).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(activeOrders3[0].sell).toEqual(token_math_1.createQuantity(wethToken, 0.1));
        yield cancelOrderFromAccountOasisDex_1.default(shared.env, matchingMarketAddress, {
            id: order2.id,
        });
        const activeOrders4 = yield getActiveOasisDexOrders_1.getActiveOasisDexOrders(shared.env, matchingMarketAccessorAddress, {
            targetExchange: matchingMarketAddress,
            buyAsset: mlnToken.address,
            sellAsset: wethToken.address,
        });
        expect(activeOrders4.length).toBe(0);
    }));
});
