"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 getBalance_1 = require("../../utils/evm/getBalance");
const withNewAccount_1 = require("../../utils/environment/withNewAccount");
const sendEth_1 = require("../../utils/evm/sendEth");
const setupInvestedTestFund_1 = require("../utils/setupInvestedTestFund");
const deposit_1 = require("../../contracts/dependencies/token/transactions/deposit");
const getTokenBySymbol_1 = require("../../utils/environment/getTokenBySymbol");
const setAmguPrice_1 = require("../../contracts/engine/transactions/setAmguPrice");
const getAmguToken_1 = require("../../contracts/engine/calls/getAmguToken");
const getPrice_1 = require("../../contracts/prices/calls/getPrice");
const update_1 = require("../../contracts/prices/transactions/update");
const getSystemTestEnvironment_1 = require("../utils/getSystemTestEnvironment");
const toBeTrueWith_1 = require("../utils/toBeTrueWith");
const transfer_1 = require("../../contracts/dependencies/token/transactions/transfer");
const Contracts_1 = require("../../Contracts");
const makeOrderFromAccountOasisDex_1 = require("../../contracts/exchanges/transactions/makeOrderFromAccountOasisDex");
const takeOasisDexOrder_1 = require("../../contracts/fund/trading/transactions/takeOasisDexOrder");
const performCalculations_1 = require("../../contracts/fund/accounting/calls/performCalculations");
const getLogCurried_1 = require("../../utils/environment/getLogCurried");
const testLogger_1 = require("../utils/testLogger");
const Environment_1 = require("../../utils/environment/Environment");
const createOrder_1 = require("../../contracts/exchanges/third-party/0x/utils/createOrder");
const signOrder_1 = require("../../contracts/exchanges/third-party/0x/utils/signOrder");
const take0xOrder_1 = require("../../contracts/fund/trading/transactions/take0xOrder");
expect.extend({ toBeTrueWith: toBeTrueWith_1.toBeTrueWith });
const getLog = getLogCurried_1.getLogCurried('melon:protocol:systemTest:playground');
describe('playground', () => {
    afterAll(() => __awaiter(this, void 0, void 0, function* () {
        yield testLogger_1.allLogsWritten();
    }));
    test('Happy path', () => __awaiter(this, void 0, void 0, function* () {
        const master = yield getSystemTestEnvironment_1.getSystemTestEnvironment(Environment_1.Tracks.TESTING);
        const log = getLog(master);
        const { melonContracts } = master.deployment;
        const matchingMarket = master.deployment.exchangeConfigs[Contracts_1.Exchanges.MatchingMarket].exchange;
        const zeroEx = master.deployment.exchangeConfigs[Contracts_1.Exchanges.ZeroEx].exchange;
        const manager = yield withNewAccount_1.withNewAccount(master);
        const trader = yield withNewAccount_1.withNewAccount(master);
        const amguToken = yield getAmguToken_1.getAmguToken(master, melonContracts.version);
        const amguPrice = token_math_1.createQuantity(amguToken, '1000000000');
        yield setAmguPrice_1.setAmguPrice(master, melonContracts.engine, amguPrice);
        const weth = getTokenBySymbol_1.getTokenBySymbol(manager, 'WETH');
        const mln = getTokenBySymbol_1.getTokenBySymbol(manager, 'MLN');
        yield update_1.update(master, melonContracts.priceSource, [
            token_math_1.createPrice(token_math_1.createQuantity(weth, 1), token_math_1.createQuantity(weth, 1)),
            token_math_1.createPrice(token_math_1.createQuantity(mln, 1), token_math_1.createQuantity(weth, 0.05)),
        ]);
        const mlnPrice = yield getPrice_1.getPrice(master, melonContracts.priceSource.toString(), amguToken);
        log.debug('MLN Price', mlnPrice);
        const masterBalance = yield getBalance_1.getBalance(master);
        expect(masterBalance).toBeTrueWith(token_math_1.greaterThan, token_math_1.createQuantity(masterBalance.token, 6));
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 2),
            to: manager.wallet.address,
        });
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 1),
            to: trader.wallet.address,
        });
        yield transfer_1.transfer(master, {
            howMuch: token_math_1.createQuantity(mln, 2),
            to: trader.wallet.address,
        });
        const quantity = token_math_1.createQuantity(weth, 1);
        yield deposit_1.deposit(manager, quantity.token.address, undefined, {
            value: quantity.quantity.toString(),
        });
        const order = yield makeOrderFromAccountOasisDex_1.makeOrderFromAccountOasisDex(trader, matchingMarket, {
            buy: token_math_1.createQuantity(weth, 0.1),
            sell: token_math_1.createQuantity(mln, 1),
        });
        const routes = yield setupInvestedTestFund_1.setupInvestedTestFund(manager);
        const preCalculations = yield performCalculations_1.performCalculations(manager, routes.accountingAddress);
        log.debug({ preCalculations });
        yield takeOasisDexOrder_1.takeOasisDexOrder(manager, routes.tradingAddress, {
            id: order.id,
            maker: order.maker,
            makerQuantity: order.sell,
            takerQuantity: order.buy,
        });
        const calculations = yield performCalculations_1.performCalculations(manager, routes.accountingAddress);
        log.debug({ calculations });
        expect(calculations.gav).toBeTrueWith(token_math_1.isEqual, token_math_1.createQuantity(weth, 0.95));
        const unsignedZeroExOrder = yield createOrder_1.createOrder(trader, zeroEx, {
            makerQuantity: token_math_1.createQuantity(mln, 0.75),
            takerQuantity: token_math_1.createQuantity(weth, 0.075),
        });
        const signedZeroExOrder = yield signOrder_1.signOrder(trader, unsignedZeroExOrder);
        yield createOrder_1.approveOrder(trader, zeroEx, signedZeroExOrder);
        const filledOrder = yield take0xOrder_1.take0xOrder(manager, routes.tradingAddress, {
            signedOrder: signedZeroExOrder,
        });
        expect(filledOrder.makerFilledAmount).toBeTrueWith(token_math_1.isEqual, token_math_1.createQuantity(mln, 0.75));
    }));
});
