"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 deployAndGetContract_1 = require("../../../../../utils/solidity/deployAndGetContract");
const deployMockSystem_1 = require("../../../../../utils/deploy/deployMockSystem");
const Contracts_1 = require("../../../../../Contracts");
const initTestEnvironment_1 = require("../../../../../tests/utils/initTestEnvironment");
const emptyAddress_1 = require("../../../../../utils/constants/emptyAddress");
const orderSignatures_1 = require("../../../../../utils/constants/orderSignatures");
describe('maxConcentration', () => {
    let shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield initTestEnvironment_1.initTestEnvironment();
        shared = Object.assign(shared, yield deployMockSystem_1.deployMockSystem(shared.env));
        shared.user = shared.env.wallet.address;
        shared.quote = shared.weth.options.address;
        shared.nonQuote = shared.mln.options.address;
    }));
    it.each([
        [
            'Asset gav > concentration limit',
            {
                max: '100000000000000000',
                asset: 'nonQuote',
                asset_gav: '100010000000000000',
                total_gav: '1000000000000000000',
                expectPass: false,
            },
        ],
        [
            'Asset gav == concentration limit',
            {
                max: '100000000000000000',
                asset: 'nonQuote',
                asset_gav: '100000000000000000',
                total_gav: '1000000000000000000',
                expectPass: true,
            },
        ],
        [
            'Asset gav < concentration limit',
            {
                max: '100000000000000000',
                asset: 'nonQuote',
                asset_gav: '90000000000000000',
                total_gav: '1000000000000000000',
                expectPass: true,
            },
        ],
        [
            'Quote asset gav > concentration limit',
            {
                max: '100000000000000000',
                asset: 'quote',
                asset_gav: '1000000000000000000',
                total_gav: '1000000000000000000',
                expectPass: true,
            },
        ],
    ])('%s', (name, trial) => __awaiter(this, void 0, void 0, function* () {
        const policy = yield deployAndGetContract_1.deployAndGetContract(shared.env, Contracts_1.Contracts.MaxConcentration, [
            trial.max,
        ]);
        const trialAsset = shared[trial.asset];
        expect(yield policy.methods.maxConcentration().call()).toBe(trial.max);
        yield shared.policyManager.methods
            .register(orderSignatures_1.makeOrderSignatureBytes, policy.options.address)
            .send({ from: shared.user });
        yield shared.accounting.methods
            .setAssetGAV(trialAsset, trial.asset_gav)
            .send({ from: shared.user });
        yield shared.accounting.methods
            .setGav(trial.total_gav)
            .send({ from: shared.user });
        const evaluate = shared.policyManager.methods.postValidate(orderSignatures_1.makeOrderSignatureBytes, [emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, trialAsset, emptyAddress_1.emptyAddress], [0, 0, 0], '0x0');
        if (trial.expectPass) {
            yield expect(evaluate.call()).resolves.not.toThrow();
        }
        else {
            yield expect(evaluate.call()).rejects.toThrow('Rule evaluated to false');
        }
    }));
});
