"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 withNewAccount_1 = require("../../utils/environment/withNewAccount");
const token_math_1 = require("@melonproject/token-math");
const toBeTrueWith_1 = require("../utils/toBeTrueWith");
const getSystemTestEnvironment_1 = require("../utils/getSystemTestEnvironment");
const Environment_1 = require("../../utils/environment/Environment");
const setAmguPrice_1 = require("../../contracts/engine/transactions/setAmguPrice");
const setupInvestedTestFund_1 = require("../utils/setupInvestedTestFund");
const sendEth_1 = require("../../utils/evm/sendEth");
const deposit_1 = require("../../contracts/dependencies/token/transactions/deposit");
const getTokenBySymbol_1 = require("../../utils/environment/getTokenBySymbol");
const thaw_1 = require("../../contracts/engine/transactions/thaw");
const transfer_1 = require("../../contracts/dependencies/token/transactions/transfer");
const takeEngineOrder_1 = require("../../contracts/fund/trading/transactions/takeEngineOrder");
const getLiquidEther_1 = require("../../contracts/engine/calls/getLiquidEther");
const balanceOf_1 = require("../../contracts/dependencies/token/calls/balanceOf");
expect.extend({ toBeTrueWith: toBeTrueWith_1.toBeTrueWith });
describe('playground', () => {
    test('Happy path', () => __awaiter(this, void 0, void 0, function* () {
        const master = yield getSystemTestEnvironment_1.getSystemTestEnvironment(Environment_1.Tracks.KYBER_PRICE);
        const manager = yield withNewAccount_1.withNewAccount(master);
        const weth = getTokenBySymbol_1.getTokenBySymbol(master, 'WETH');
        const mln = getTokenBySymbol_1.getTokenBySymbol(master, 'MLN');
        const engine = manager.deployment.melonContracts.engine;
        const amguPrice = token_math_1.createQuantity('MLN', '10000000000');
        yield setAmguPrice_1.setAmguPrice(master, master.deployment.melonContracts.engine, amguPrice);
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 2),
            to: manager.wallet.address,
        });
        const quantity = token_math_1.createQuantity(weth, 1);
        yield deposit_1.deposit(manager, quantity.token.address, undefined, {
            value: quantity.quantity.toString(),
        });
        const routes = yield setupInvestedTestFund_1.setupInvestedTestFund(manager);
        yield thaw_1.thaw(manager, engine);
        const takerQuantity = token_math_1.createQuantity(mln, 0.001); // Mln sell qty
        // const mlnPrice = await getPrice(
        //   manager,
        //   `${manager.deployment.melonContracts.priceSource}`,
        //   mln,
        // );
        const makerQuantity = token_math_1.createQuantity(weth, 0.00001); // Min WETH
        const preliquidEther = yield getLiquidEther_1.getLiquidEther(manager, engine);
        yield transfer_1.transfer(master, {
            howMuch: takerQuantity,
            to: routes.vaultAddress,
        });
        const preFundWeth = yield balanceOf_1.balanceOf(manager, weth.address, {
            address: routes.vaultAddress,
        });
        const preFundMln = yield balanceOf_1.balanceOf(manager, mln.address, {
            address: routes.vaultAddress,
        });
        yield takeEngineOrder_1.takeEngineOrder(manager, routes.tradingAddress, {
            makerQuantity,
            takerQuantity,
        });
        const postliquidEther = yield getLiquidEther_1.getLiquidEther(manager, engine);
        const postFundWeth = yield balanceOf_1.balanceOf(manager, weth.address, {
            address: routes.vaultAddress,
        });
        const postFundMln = yield balanceOf_1.balanceOf(manager, mln.address, {
            address: routes.vaultAddress,
        });
        expect(token_math_1.subtract(postFundWeth.quantity, preFundWeth.quantity)).toEqual(token_math_1.subtract(preliquidEther.quantity, postliquidEther.quantity));
        expect(token_math_1.subtract(preFundMln, postFundMln).quantity).toEqual(takerQuantity.quantity);
    }));
});
