"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 token_math_1 = require("@melonproject/token-math");
const Contracts_1 = require("../../Contracts");
const beginSetup_1 = require("../../contracts/factory/transactions/beginSetup");
const completeSetup_1 = require("../../contracts/factory/transactions/completeSetup");
const createAccounting_1 = require("../../contracts/factory/transactions/createAccounting");
const createFeeManager_1 = require("../../contracts/factory/transactions/createFeeManager");
const createParticipation_1 = require("../../contracts/factory/transactions/createParticipation");
const createPolicyManager_1 = require("../../contracts/factory/transactions/createPolicyManager");
const createShares_1 = require("../../contracts/factory/transactions/createShares");
const createTrading_1 = require("../../contracts/factory/transactions/createTrading");
const createVault_1 = require("../../contracts/factory/transactions/createVault");
const getRoutes_1 = require("../../contracts/fund/hub/calls/getRoutes");
const register_1 = require("../../contracts/fund/policies/transactions/register");
const update_1 = require("../../contracts/prices/transactions/update");
const requestInvestment_1 = require("../../contracts/fund/participation/transactions/requestInvestment");
const executeRequest_1 = require("../../contracts/fund/participation/transactions/executeRequest");
const setAmguPrice_1 = require("../../contracts/engine/transactions/setAmguPrice");
const shutDownFund_1 = require("../../contracts/fund/hub/transactions/shutDownFund");
const getAmguToken_1 = require("../../contracts/engine/calls/getAmguToken");
const getFundHoldings_1 = require("../../contracts/fund/accounting/calls/getFundHoldings");
const makeOrderFromAccountOasisDex_1 = require("../../contracts/exchanges/transactions/makeOrderFromAccountOasisDex");
const makeOasisDexOrder_1 = require("../../contracts/fund/trading/transactions/makeOasisDexOrder");
const takeOrderFromAccountOasisDex_1 = __importDefault(require("../../contracts/exchanges/transactions/takeOrderFromAccountOasisDex"));
const cancelOrderFromAccountOasisDex_1 = __importDefault(require("../../contracts/exchanges/transactions/cancelOrderFromAccountOasisDex"));
const takeOasisDexOrder_1 = require("../../contracts/fund/trading/transactions/takeOasisDexOrder");
const getFundOpenOrder_1 = require("../../contracts/fund/trading/calls/getFundOpenOrder");
const cancelOasisDexOrder_1 = require("../../contracts/fund/trading/transactions/cancelOasisDexOrder");
const randomString_1 = require("../../utils/helpers/randomString");
const FunctionSignatures_1 = require("../../contracts/fund/trading/utils/FunctionSignatures");
const performCalculations_1 = require("../../contracts/fund/accounting/calls/performCalculations");
const approve_1 = require("../../contracts/dependencies/token/transactions/approve");
const Environment_1 = require("../../utils/environment/Environment");
const deployAndInitTestEnv_1 = require("../utils/deployAndInitTestEnv");
const calcGav_1 = require("../../contracts/fund/accounting/calls/calcGav");
describe('generalWalkthrough', () => {
    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);
        shared.accounts = yield shared.env.eth.getAccounts();
    }));
    test('Happy path', () => __awaiter(this, void 0, void 0, function* () {
        const debug = shared.env.logger('melon:protocol:utils', Environment_1.LogLevels.DEBUG);
        const fundName = `test-fund-${randomString_1.randomString()}`;
        const { exchangeConfigs, melonContracts, thirdPartyContracts, } = shared.env.deployment;
        const { priceSource, policies, version, engine } = melonContracts;
        const tokens = thirdPartyContracts.tokens;
        const [ethToken, mlnToken] = tokens;
        const defaultTokens = [ethToken, mlnToken];
        const amguToken = yield getAmguToken_1.getAmguToken(shared.env, version);
        const amguPrice = token_math_1.createQuantity(amguToken, '1000000000');
        yield setAmguPrice_1.setAmguPrice(shared.env, engine, amguPrice);
        const fees = [
            {
                feeAddress: melonContracts.fees.managementFee.toLowerCase(),
                feePeriod: new token_math_1.BigInteger(0),
                feeRate: new token_math_1.BigInteger(token_math_1.multiply(new token_math_1.BigInteger(2), token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(16)))),
            },
            {
                feeAddress: melonContracts.fees.performanceFee.toLowerCase(),
                feePeriod: new token_math_1.BigInteger(86400 * 90),
                feeRate: new token_math_1.BigInteger(token_math_1.multiply(new token_math_1.BigInteger(20), token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(16)))),
            },
        ];
        const mlnPrice = token_math_1.createPrice(token_math_1.createQuantity(mlnToken, '1'), token_math_1.createQuantity(ethToken, '2'));
        const ethPrice = token_math_1.createPrice(token_math_1.createQuantity(ethToken, '1'), token_math_1.createQuantity(ethToken, '1'));
        yield update_1.update(shared.env, priceSource, [ethPrice, mlnPrice]);
        yield beginSetup_1.beginSetup(shared.env, version, {
            defaultTokens,
            exchangeConfigs,
            fees,
            fundName,
            nativeToken: ethToken,
            priceSource,
            quoteToken: ethToken,
        }, { gas: '8000000' });
        yield createAccounting_1.createAccounting(shared.env, version);
        yield createFeeManager_1.createFeeManager(shared.env, version);
        yield createParticipation_1.createParticipation(shared.env, version);
        yield createPolicyManager_1.createPolicyManager(shared.env, version);
        yield createShares_1.createShares(shared.env, version);
        yield createTrading_1.createTrading(shared.env, version);
        yield createVault_1.createVault(shared.env, version);
        const hubAddress = yield completeSetup_1.completeSetup(shared.env, version);
        const routes = yield getRoutes_1.getRoutes(shared.env, hubAddress);
        yield register_1.register(shared.env, routes.policyManagerAddress, {
            method: FunctionSignatures_1.FunctionSignatures.makeOrder,
            policy: policies.priceTolerance,
        });
        yield register_1.register(shared.env, routes.policyManagerAddress, {
            method: FunctionSignatures_1.FunctionSignatures.takeOrder,
            policy: policies.priceTolerance,
        });
        yield register_1.register(shared.env, routes.policyManagerAddress, {
            method: FunctionSignatures_1.FunctionSignatures.executeRequestFor,
            policy: policies.userWhitelist,
        });
        debug('GAV empty', yield calcGav_1.calcGav(shared.env, routes.accountingAddress));
        const investmentAmount = token_math_1.createQuantity(ethToken, 1);
        yield expect(requestInvestment_1.requestInvestment(shared.env, routes.participationAddress, {
            investmentAmount,
        })).rejects.toThrow(`Insufficient allowance`);
        yield approve_1.approve(shared.env, {
            howMuch: investmentAmount,
            spender: routes.participationAddress,
        });
        yield requestInvestment_1.requestInvestment(shared.env, routes.participationAddress, {
            investmentAmount,
        });
        yield executeRequest_1.executeRequest(shared.env, routes.participationAddress);
        debug('Executed request', yield calcGav_1.calcGav(shared.env, routes.accountingAddress));
        yield getFundHoldings_1.getFundHoldings(shared.env, routes.accountingAddress);
        const matchingMarketAddress = shared.env.deployment.exchangeConfigs[Contracts_1.Exchanges.MatchingMarket].exchange;
        const order1 = yield makeOrderFromAccountOasisDex_1.makeOrderFromAccountOasisDex(shared.env, matchingMarketAddress, {
            buy: token_math_1.createQuantity(mlnToken, 2),
            sell: token_math_1.createQuantity(ethToken, 0.1),
        });
        expect(order1.buy).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(order1.sell).toEqual(token_math_1.createQuantity(ethToken, 0.1));
        debug(`Made order from account with id ${order1.id}`);
        yield takeOrderFromAccountOasisDex_1.default(shared.env, matchingMarketAddress, {
            buy: order1.buy,
            id: order1.id,
            maxTakeAmount: order1.sell,
            sell: order1.sell,
        });
        debug(`Took order from account with id ${order1.id}`);
        const order2 = yield makeOrderFromAccountOasisDex_1.makeOrderFromAccountOasisDex(shared.env, matchingMarketAddress, {
            buy: token_math_1.createQuantity(mlnToken, 2),
            sell: token_math_1.createQuantity(ethToken, 0.1),
        });
        expect(order2.buy).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(order2.sell).toEqual(token_math_1.createQuantity(ethToken, 0.1));
        debug(`Made order from account with id ${order2.id}`);
        yield cancelOrderFromAccountOasisDex_1.default(shared.env, matchingMarketAddress, {
            id: order2.id,
        });
        debug(`Canceled order from account with id ${order2.id}`);
        const orderFromFund = yield makeOasisDexOrder_1.makeOasisDexOrder(shared.env, routes.tradingAddress, {
            makerQuantity: token_math_1.createQuantity(ethToken, 0.1),
            takerQuantity: token_math_1.createQuantity(mlnToken, 2),
        });
        debug(`Made order from fund with id ${orderFromFund.id}`);
        const fundOrder = yield getFundOpenOrder_1.getFundOpenOrder(shared.env, routes.tradingAddress, 0);
        yield cancelOasisDexOrder_1.cancelOasisDexOrder(shared.env, routes.tradingAddress, {
            id: fundOrder.id,
            maker: routes.tradingAddress,
            makerAsset: fundOrder.makerAsset,
            takerAsset: fundOrder.takerAsset,
        });
        debug(`Canceled order ${fundOrder.id} from fund `);
        const order3 = yield makeOrderFromAccountOasisDex_1.makeOrderFromAccountOasisDex(shared.env, matchingMarketAddress, {
            buy: token_math_1.createQuantity(ethToken, 0.1),
            sell: token_math_1.createQuantity(mlnToken, 2),
        });
        expect(order3.sell).toEqual(token_math_1.createQuantity(mlnToken, 2));
        expect(order3.buy).toEqual(token_math_1.createQuantity(ethToken, 0.1));
        debug(`Made order from account with id ${order3.id}`);
        yield takeOasisDexOrder_1.takeOasisDexOrder(shared.env, routes.tradingAddress, {
            id: order3.id,
            maker: order3.maker,
            makerQuantity: order3.sell,
            takerQuantity: order3.buy,
        });
        debug(`Took order from fund with id ${order3.id}`);
        yield performCalculations_1.performCalculations(shared.env, routes.accountingAddress);
        yield shutDownFund_1.shutDownFund(shared.env, version, { hub: hubAddress });
        debug('Shut down fund');
        yield expect(requestInvestment_1.requestInvestment(shared.env, routes.participationAddress, {
            investmentAmount: token_math_1.createQuantity(ethToken, 1),
        })).rejects.toThrow(`Fund with hub address: ${hubAddress} is shut down`);
    }));
});
