"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 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 deposit_1 = require("../../contracts/dependencies/token/transactions/deposit");
const getTokenBySymbol_1 = require("../../utils/environment/getTokenBySymbol");
const getPrice_1 = require("../../contracts/prices/calls/getPrice");
const toBeTrueWith_1 = require("../utils/toBeTrueWith");
const getSystemTestEnvironment_1 = require("../utils/getSystemTestEnvironment");
const Environment_1 = require("../../utils/environment/Environment");
const getLogCurried_1 = require("../../utils/environment/getLogCurried");
const performCalculations_1 = require("../../contracts/fund/accounting/calls/performCalculations");
const testLogger_1 = require("../utils/testLogger");
const setupFund_1 = require("../../contracts/fund/hub/transactions/setupFund");
const invest_1 = require("../../contracts/fund/participation/transactions/invest");
const requestInvestment_1 = require("../../contracts/fund/participation/transactions/requestInvestment");
const approve_1 = require("../../contracts/dependencies/token/transactions/approve");
const executeRequestFor_1 = require("../../contracts/fund/participation/transactions/executeRequestFor");
const delay_1 = require("../utils/delay");
const getToken_1 = require("../../contracts/dependencies/token/calls/getToken");
expect.extend({ toBeTrueWith: toBeTrueWith_1.toBeTrueWith });
const getLog = getLogCurried_1.getLogCurried('melon:protocol:systemTest:playground-multiple-investors');
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 { melonContracts } = master.deployment;
        const manager = yield withNewAccount_1.withNewAccount(master);
        const investor1 = yield withNewAccount_1.withNewAccount(master);
        const investor2 = yield withNewAccount_1.withNewAccount(master);
        const investor3 = yield withNewAccount_1.withNewAccount(master);
        log.debug('Manager ', manager.wallet.address);
        log.debug('Investor 1 ', investor1.wallet.address);
        log.debug('Investor 2 ', investor2.wallet.address);
        const weth = getTokenBySymbol_1.getTokenBySymbol(manager, 'WETH');
        const mln = getTokenBySymbol_1.getTokenBySymbol(manager, 'MLN');
        try {
            const mlnPrice = yield getPrice_1.getPrice(master, melonContracts.priceSource.toString(), mln);
            log.debug('MLN Price', mlnPrice);
        }
        catch (e) {
            throw new Error('Cannot get MLN Price from Kyber');
        }
        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', 3),
            to: manager.wallet.address,
        });
        const routes = yield setupFund_1.setupFund(manager);
        const fundToken = yield getToken_1.getToken(manager, routes.sharesAddress);
        log.debug('Routes of new fund are ', routes);
        // Manager invests in his own fund
        const managerQuantity = token_math_1.createQuantity(weth, 0.5);
        yield deposit_1.deposit(manager, managerQuantity.token.address, undefined, {
            value: managerQuantity.quantity.toString(),
        });
        const managerInvestment = yield invest_1.invest(manager, {
            hubAddress: routes.hubAddress,
            investmentAmount: managerQuantity,
            requestedShares: token_math_1.createQuantity(fundToken, 1),
        });
        log.debug('Manager investment ', managerInvestment);
        // Investor 1 requests investment 1 WETH
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 3),
            to: investor1.wallet.address,
        });
        const investor1Quantity = token_math_1.createQuantity(weth, 1);
        yield deposit_1.deposit(investor1, investor1Quantity.token.address, undefined, {
            value: investor1Quantity.quantity.toString(),
        });
        yield approve_1.approve(investor1, {
            howMuch: investor1Quantity,
            spender: routes.participationAddress,
        });
        yield requestInvestment_1.requestInvestment(investor1, routes.participationAddress, {
            investmentAmount: investor1Quantity,
            requestedShares: token_math_1.createQuantity(fundToken, 1),
        });
        // Investor 2 requests investment 2 WETH
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 4),
            to: investor2.wallet.address,
        });
        const investor2Quantity = token_math_1.createQuantity(weth, 2);
        yield deposit_1.deposit(investor2, investor2Quantity.token.address, undefined, {
            value: investor2Quantity.quantity.toString(),
        });
        yield approve_1.approve(investor2, {
            howMuch: investor2Quantity,
            spender: routes.participationAddress,
        });
        yield requestInvestment_1.requestInvestment(investor2, routes.participationAddress, {
            investmentAmount: investor2Quantity,
            requestedShares: token_math_1.createQuantity(fundToken, 2),
        });
        // Investor 3 requests investment 3 WETH
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 5),
            to: investor3.wallet.address,
        });
        const investor3Quantity = token_math_1.createQuantity(weth, 3);
        yield deposit_1.deposit(investor3, investor3Quantity.token.address, undefined, {
            value: investor3Quantity.quantity.toString(),
        });
        yield approve_1.approve(investor3, {
            howMuch: investor3Quantity,
            spender: routes.participationAddress,
        });
        yield requestInvestment_1.requestInvestment(investor3, routes.participationAddress, {
            investmentAmount: investor3Quantity,
            requestedShares: token_math_1.createQuantity(fundToken, 3),
        });
        /// Execute 3 requests after some time has passed
        yield delay_1.delay(180000);
        const investor1Investment = yield executeRequestFor_1.executeRequestFor(investor1, routes.participationAddress, { who: investor1.wallet.address });
        log.debug('Investor 1 investment ', investor1Investment);
        const investor2Investment = yield executeRequestFor_1.executeRequestFor(investor2, routes.participationAddress, { who: investor2.wallet.address });
        log.debug('Investor 2 investment ', investor2Investment);
        const investor3Investment = yield executeRequestFor_1.executeRequestFor(investor3, routes.participationAddress, { who: investor3.wallet.address });
        log.debug('Investor 3 investment ', investor3Investment);
        const finalCalculations = yield performCalculations_1.performCalculations(manager, routes.accountingAddress);
        expect(token_math_1.toFixed(finalCalculations.gav)).toEqual('6.499998');
        log.debug('Final calculations ', finalCalculations);
    }));
});
