"use strict";
/* eslint-disable no-console */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(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 chai_1 = require("chai");
require("mocha");
const web3_1 = __importDefault(require("web3"));
const src_1 = require("../../../src");
describe("Streamed Orderbook", () => {
    describe("init", () => {
        it("should successfully apply all events and match on-chain orderbook", function () {
            return __awaiter(this, void 0, void 0, function* () {
                const { ETHEREUM_NODE_URL, INFURA_PROJECT_ID, ORDERBOOK_END_BLOCK, TEST_STREAMED_ORDERBOOK_E2E, } = process.env;
                if (!TEST_STREAMED_ORDERBOOK_E2E) {
                    this.skip();
                    return;
                }
                chai_1.assert.isDefined(ETHEREUM_NODE_URL || INFURA_PROJECT_ID, "ETHEREUM_NODE_URL or INFURA_PROJECT_ID environment variable is required");
                const url = ETHEREUM_NODE_URL ||
                    `https://mainnet.infura.io/v3/${INFURA_PROJECT_ID}`;
                const endBlock = ORDERBOOK_END_BLOCK
                    ? parseInt(ORDERBOOK_END_BLOCK)
                    : undefined;
                const web3 = new web3_1.default(url);
                const [viewer] = yield src_1.deployment(web3, src_1.BatchExchangeViewerArtifact);
                console.debug("==> building streamed orderbook...");
                const orderbook = yield src_1.StreamedOrderbook.init(web3, {
                    endBlock,
                    strict: true,
                });
                const targetBlock = yield orderbook.update(endBlock);
                const streamedOrders = orderbook.getOpenOrders();
                console.debug("==> querying onchain orderbook...");
                const queriedOrders = (yield src_1.getOpenOrders(viewer, 300, targetBlock)).map((order) => (Object.assign(Object.assign({}, order), { sellTokenBalance: BigInt(order.sellTokenBalance.toString()), priceNumerator: BigInt(order.priceNumerator.toString()), priceDenominator: BigInt(order.priceDenominator.toString()), remainingAmount: BigInt(order.remainingAmount.toString()) })));
                console.debug("==> comparing orderbooks...");
                function toDiffableOrders(orders) {
                    return orders.reduce((obj, order) => {
                        const user = order.user.toLowerCase();
                        obj[`${user}-${order.orderId}`] = Object.assign(Object.assign({}, order), { user });
                        return obj;
                    }, {});
                }
                chai_1.assert.deepEqual(toDiffableOrders(streamedOrders), toDiffableOrders(queriedOrders));
            });
        }).timeout(0);
    });
});
