"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 getBalance_1 = require("~/utils/evm/getBalance");
const withNewAccount_1 = require("~/utils/environment/withNewAccount");
const token_math_1 = require("@melonproject/token-math");
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 toBeTrueWith_1 = require("../utils/toBeTrueWith");
const getSystemTestEnvironment_1 = require("../utils/getSystemTestEnvironment");
const Environment_1 = require("~/utils/environment/Environment");
const Contracts_1 = require("~/Contracts");
const getLogCurried_1 = require("~/utils/environment/getLogCurried");
const transfer_1 = require("~/contracts/dependencies/token/transactions/transfer");
const performCalculations_1 = require("~/contracts/fund/accounting/calls/performCalculations");
const makeOasisDexOrder_1 = require("~/contracts/fund/trading/transactions/makeOasisDexOrder");
const testLogger_1 = require("../utils/testLogger");
const takeOrderFromAccountOasisDex_1 = __importDefault(require("~/contracts/exchanges/transactions/takeOrderFromAccountOasisDex"));
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.KYBER_PRICE);
        const log = getLog(master);
        const matchingMarket = master.deployment.exchangeConfigs[Contracts_1.Exchanges.MatchingMarket].exchange;
        const manager = yield withNewAccount_1.withNewAccount(master);
        const trader = yield withNewAccount_1.withNewAccount(master);
        const weth = getTokenBySymbol_1.getTokenBySymbol(manager, 'WETH');
        const mln = getTokenBySymbol_1.getTokenBySymbol(manager, 'MLN');
        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, 10),
            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 routes = yield setupInvestedTestFund_1.setupInvestedTestFund(manager);
        const preCalculations = yield performCalculations_1.performCalculations(manager, routes.accountingAddress);
        log.debug({ preCalculations });
        log.debug('After first investment, share price is: ', token_math_1.toFixed(preCalculations.sharePrice));
        const orderFromFund = yield makeOasisDexOrder_1.makeOasisDexOrder(manager, routes.tradingAddress, {
            makerQuantity: token_math_1.createQuantity(weth, 0.5),
            takerQuantity: token_math_1.createQuantity(mln, 8),
        });
        log.debug('Fund made an order ', orderFromFund);
        const takeOrderFromTrader = yield takeOrderFromAccountOasisDex_1.default(trader, matchingMarket, {
            id: orderFromFund.id,
            buy: orderFromFund.buy,
            sell: orderFromFund.sell,
            maxTakeAmount: token_math_1.createQuantity(weth, 0.5),
        });
        log.debug('Trader took the order: ', takeOrderFromTrader);
    }));
});
