"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 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 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 transfer_1 = require("../../contracts/dependencies/token/transactions/transfer");
const enableInvestment_1 = require("../../contracts/fund/participation/transactions/enableInvestment");
const investAllowed_1 = require("../../contracts/fund/participation/calls/investAllowed");
const getToken_1 = require("../../contracts/dependencies/token/calls/getToken");
const getShareCostInAsset_1 = require("../../contracts/fund/accounting/calls/getShareCostInAsset");
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);
        log.debug('Manager ', manager.wallet.address);
        log.debug('Investor 1 ', investor1.wallet.address);
        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 enables investment in MLN
        yield enableInvestment_1.enableInvestment(manager, routes.participationAddress, {
            assets: [mln.address],
        });
        log.debug('Enabled investment for ', [mln.symbol]);
        const allowed = yield investAllowed_1.investAllowed(manager, routes.participationAddress, {
            asset: mln.address,
        });
        expect(allowed).toBeTruthy();
        // Investor 1 requests investment 80 MLN
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 3),
            to: investor1.wallet.address,
        });
        yield transfer_1.transfer(master, {
            howMuch: token_math_1.createQuantity(mln, 80),
            to: investor1.wallet.address,
        });
        const investor1Quantity = token_math_1.createQuantity(mln, 80);
        yield approve_1.approve(investor1, {
            howMuch: investor1Quantity,
            spender: routes.participationAddress,
        });
        const shareCostInMLN = yield getShareCostInAsset_1.getShareCostInAsset(investor1, routes.accountingAddress, { assetToken: mln, fundToken });
        const requestedShares = token_math_1.createQuantity(fundToken, 5);
        const investmentAmount = token_math_1.valueIn(shareCostInMLN, requestedShares);
        yield requestInvestment_1.requestInvestment(investor1, routes.participationAddress, {
            investmentAmount,
            requestedShares,
        });
        const investor1Investment = yield executeRequestFor_1.executeRequestFor(investor1, routes.participationAddress, { who: investor1.wallet.address });
        log.debug('Investor 1 investment ', investor1Investment);
        const finalCalculations = yield performCalculations_1.performCalculations(manager, routes.accountingAddress);
        log.debug('Final calculations ', finalCalculations);
    }));
});
