"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 Environment_1 = require("../../../utils/environment/Environment");
const deployAndInitTestEnv_1 = require("../../utils/deployAndInitTestEnv");
const setupInvestedTestFund_1 = require("../../utils/setupInvestedTestFund");
const balanceOf_1 = require("../../../contracts/dependencies/token/calls/balanceOf");
const getTokenBySymbol_1 = require("../../../utils/environment/getTokenBySymbol");
const toBeTrueWith_1 = require("../../utils/toBeTrueWith");
const setAmguPrice_1 = require("../../../contracts/engine/transactions/setAmguPrice");
const thaw_1 = require("../../../contracts/engine/transactions/thaw");
const increaseTime_1 = require("../../../utils/evm/increaseTime");
const takeEngineOrder_1 = require("../../../contracts/fund/trading/transactions/takeEngineOrder");
const transfer_1 = require("../../../contracts/dependencies/token/transactions/transfer");
const getLiquidEther_1 = require("../../../contracts/engine/calls/getLiquidEther");
const FunctionSignatures_1 = require("../../../contracts/fund/trading/utils/FunctionSignatures");
const register_1 = require("../../../contracts/fund/policies/transactions/register");
const getPrice_1 = require("../../../contracts/prices/calls/getPrice");
expect.extend({ toBeTrueWith: toBeTrueWith_1.toBeTrueWith });
describe('Happy Path', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
        expect(shared.env.track).toBe(Environment_1.Tracks.TESTING);
        const amguPrice = token_math_1.createQuantity('MLN', '1000000000000');
        yield setAmguPrice_1.setAmguPrice(shared.env, shared.env.deployment.melonContracts.engine, amguPrice);
        shared.accounts = yield shared.env.eth.getAccounts();
        shared.engine = shared.env.deployment.melonContracts.engine;
        shared.routes = yield setupInvestedTestFund_1.setupInvestedTestFund(shared.env);
        shared.weth = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'WETH');
        shared.mln = getTokenBySymbol_1.getTokenBySymbol(shared.env, 'MLN');
        yield register_1.register(shared.env, shared.routes.policyManagerAddress, {
            method: FunctionSignatures_1.FunctionSignatures.takeOrder,
            policy: shared.env.deployment.melonContracts.policies.priceTolerance,
        });
    }));
    test('Trade on Melon Engine', () => __awaiter(this, void 0, void 0, function* () {
        yield increaseTime_1.increaseTime(shared.env, 86400 * 32);
        yield thaw_1.thaw(shared.env, shared.engine);
        const takerQuantity = token_math_1.createQuantity(shared.mln, 0.001); // Mln sell qty
        const mlnPrice = yield getPrice_1.getPrice(shared.env, `${shared.env.deployment.melonContracts.priceSource}`, shared.mln);
        const makerQuantity = token_math_1.valueIn(mlnPrice, takerQuantity); // Min WETH
        const preliquidEther = yield getLiquidEther_1.getLiquidEther(shared.env, shared.engine);
        yield transfer_1.transfer(shared.env, {
            howMuch: takerQuantity,
            to: shared.routes.vaultAddress,
        });
        const preFundWeth = yield balanceOf_1.balanceOf(shared.env, shared.weth.address, {
            address: shared.routes.vaultAddress,
        });
        const preFundMln = yield balanceOf_1.balanceOf(shared.env, shared.mln.address, {
            address: shared.routes.vaultAddress,
        });
        yield takeEngineOrder_1.takeEngineOrder(shared.env, shared.routes.tradingAddress, {
            makerQuantity,
            takerQuantity,
        });
        const postliquidEther = yield getLiquidEther_1.getLiquidEther(shared.env, shared.engine);
        const postFundWeth = yield balanceOf_1.balanceOf(shared.env, shared.weth.address, {
            address: shared.routes.vaultAddress,
        });
        const postFundMln = yield balanceOf_1.balanceOf(shared.env, shared.mln.address, {
            address: shared.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);
    }));
    test('Maker quantity as minimum returned WETH is respected', () => __awaiter(this, void 0, void 0, function* () {
        const takerQuantity = token_math_1.createQuantity(shared.mln, 0.001); // Mln sell qty
        const mlnPrice = yield getPrice_1.getPrice(shared.env, `${shared.env.deployment.melonContracts.priceSource}`, shared.mln);
        const makerQuantity = token_math_1.createQuantity(shared.weth, token_math_1.divide(mlnPrice.quote.quantity, 2)); // Min WETH
        yield transfer_1.transfer(shared.env, {
            howMuch: takerQuantity,
            to: shared.routes.vaultAddress,
        });
        yield expect(takeEngineOrder_1.takeEngineOrder(shared.env, shared.routes.tradingAddress, {
            makerQuantity,
            takerQuantity,
        })).rejects.toThrow();
    }));
});
