"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 randomAddress_1 = require("../../../../../utils/helpers/randomAddress");
const emptyAddress_1 = require("../../../../../utils/constants/emptyAddress");
const orderSignatures_1 = require("../../../../../utils/constants/orderSignatures");
describe('maxPositions', () => {
    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.opts = { from: shared.user, gas: 8000000 };
    }));
    it('Create and get max', () => __awaiter(this, void 0, void 0, function* () {
        const positions = ['0', '125', '9999999999'];
        for (const n of positions) {
            const maxPositions = yield deployAndGetContract_1.deployAndGetContract(shared.env, Contracts_1.Contracts.MaxPositions, [
                n,
            ]);
            expect(yield maxPositions.methods.maxPositions().call()).toEqual(n);
        }
    }));
    it('Policy manager and mock accounting with maxPositions', () => __awaiter(this, void 0, void 0, function* () {
        const maxPositions = '3';
        const policy = yield deployAndGetContract_1.deployAndGetContract(shared.env, Contracts_1.Contracts.MaxPositions, [
            maxPositions,
        ]);
        const nonQuoteAsset = `${randomAddress_1.randomAddress()}`;
        const quoteAsset = shared.weth.options.address;
        yield shared.policyManager.methods
            .register(orderSignatures_1.makeOrderSignatureBytes, policy.options.address)
            .send({ from: shared.user, gas: 8000000 });
        yield shared.accounting.methods
            .setOwnedAssets([shared.weth.options.address])
            .send({ from: shared.user, gas: 8000000 });
        yield expect(shared.policyManager.methods
            .postValidate(orderSignatures_1.makeOrderSignatureBytes, [emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, quoteAsset, emptyAddress_1.emptyAddress], [0, 0, 0], '0x0')
            .call()).resolves.not.toThrow();
        yield expect(shared.policyManager.methods
            .postValidate(orderSignatures_1.makeOrderSignatureBytes, [
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            nonQuoteAsset,
            emptyAddress_1.emptyAddress,
        ], [0, 0, 0], '0x0')
            .call()).resolves.not.toThrow();
        yield shared.accounting.methods
            .setOwnedAssets([
            nonQuoteAsset,
            `${randomAddress_1.randomAddress()}`,
            `${randomAddress_1.randomAddress()}`,
        ])
            .send({ from: shared.user, gas: 8000000 });
        yield expect(shared.policyManager.methods
            .postValidate(orderSignatures_1.makeOrderSignatureBytes, [emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, quoteAsset, emptyAddress_1.emptyAddress], [0, 0, 0], '0x0')
            .call()).resolves.not.toThrow();
        yield expect(shared.policyManager.methods
            .postValidate(orderSignatures_1.makeOrderSignatureBytes, [
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            nonQuoteAsset,
            emptyAddress_1.emptyAddress,
        ], [0, 0, 0], '0x0')
            .call()).resolves.not.toThrow();
        yield shared.accounting.methods
            .setOwnedAssets([
            nonQuoteAsset,
            `${randomAddress_1.randomAddress()}`,
            `${randomAddress_1.randomAddress()}`,
            `${randomAddress_1.randomAddress()}`,
        ])
            .send({ from: shared.user, gas: 8000000 });
        yield expect(shared.policyManager.methods
            .postValidate(orderSignatures_1.makeOrderSignatureBytes, [
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            `${randomAddress_1.randomAddress()}`,
            emptyAddress_1.emptyAddress,
        ], [0, 0, 0], '0x0')
            .call()).rejects.toThrow('Rule evaluated to false');
    }));
});
