"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const src_1 = require("../../src");
const bn_js_1 = __importDefault(require("bn.js"));
const chai_1 = require("chai");
require("mocha");
function hex(bytes) {
    return `0x${Array.prototype.map
        .call(bytes, (byte) => byte.toString(16).padStart(2, "0"))
        .join("")}`;
}
function json(obj) {
    return JSON.parse(JSON.stringify(obj));
}
describe("Encoding Orders", () => {
    describe("decodeOrders", () => {
        it("accepts empty bytes and null and as input", () => {
            chai_1.expect(src_1.decodeOrders("")).to.deep.equal([]);
            chai_1.expect(src_1.decodeOrders("0x")).to.deep.equal([]);
            chai_1.expect(src_1.decodeOrders(null)).to.deep.equal([]);
        });
    });
    describe("encodeOrders", () => {
        it("encodes orders that can be decoded into the same values", () => {
            const orders = [
                {
                    user: "0x000102030405060708090a0b0c0d0e0f10111213",
                    sellTokenBalance: new bn_js_1.default(1),
                    buyToken: 2,
                    sellToken: 3,
                    validFrom: 4,
                    validUntil: 5,
                    priceNumerator: new bn_js_1.default("1000000000000000000"),
                    priceDenominator: new bn_js_1.default("1000000000000000000"),
                    remainingAmount: new bn_js_1.default(42),
                },
                {
                    user: "0x131211100f0e0d0c0b0a09080706050403020100",
                    sellTokenBalance: new bn_js_1.default(1),
                    buyToken: 2,
                    sellToken: 3,
                    validFrom: 4,
                    validUntil: 5,
                    priceNumerator: new bn_js_1.default(1337),
                    priceDenominator: new bn_js_1.default(0x12345678),
                    remainingAmount: new bn_js_1.default(42),
                },
            ];
            const encoded = src_1.encodeOrders(orders);
            const decoded = src_1.decodeOrders(hex(encoded));
            chai_1.expect(json(decoded)).to.deep.equal(json(orders));
        });
        it("can encode orders with string integer format", () => {
            const orders = [
                {
                    user: "0x000102030405060708090a0b0c0d0e0f10111213",
                    sellTokenBalance: "1",
                    buyToken: 2,
                    sellToken: 3,
                    validFrom: 4,
                    validUntil: 5,
                    priceNumerator: "6",
                    priceDenominator: "7",
                    remainingAmount: "8",
                },
            ];
            const encoded = src_1.encodeOrders(orders);
            chai_1.expect(hex(encoded)).to.equal("0x\
         000102030405060708090a0b0c0d0e0f10111213\
         0000000000000000000000000000000000000000000000000000000000000001\
         0002\
         0003\
         00000004\
         00000005\
         00000000000000000000000000000006\
         00000000000000000000000000000007\
         00000000000000000000000000000008\
        ".replace(/\s/g, ""));
        });
        it("can encode orders with bigint integer format", () => {
            const orders = [
                {
                    user: "0x000102030405060708090a0b0c0d0e0f10111213",
                    sellTokenBalance: BigInt(1),
                    buyToken: 2,
                    sellToken: 3,
                    validFrom: 4,
                    validUntil: 5,
                    priceNumerator: BigInt(6),
                    priceDenominator: BigInt(7),
                    remainingAmount: BigInt(8),
                },
            ];
            const encoded = src_1.encodeOrders(orders);
            chai_1.expect(hex(encoded)).to.equal("0x\
         000102030405060708090a0b0c0d0e0f10111213\
         0000000000000000000000000000000000000000000000000000000000000001\
         0002\
         0003\
         00000004\
         00000005\
         00000000000000000000000000000006\
         00000000000000000000000000000007\
         00000000000000000000000000000008\
        ".replace(/\s/g, ""));
        });
    });
    describe("encodeIndexedOrders", () => {
        it("encodes indexed orders that can be decoded into the same values", () => {
            const orders = [
                {
                    user: "0x000102030405060708090a0b0c0d0e0f10111213",
                    sellTokenBalance: new bn_js_1.default(1),
                    buyToken: 2,
                    sellToken: 3,
                    validFrom: 4,
                    validUntil: 5,
                    priceNumerator: new bn_js_1.default("1000000000000000000"),
                    priceDenominator: new bn_js_1.default("1000000000000000000"),
                    remainingAmount: new bn_js_1.default(42),
                    orderId: 99,
                },
                {
                    user: "0x131211100f0e0d0c0b0a09080706050403020100",
                    sellTokenBalance: new bn_js_1.default(1),
                    buyToken: 2,
                    sellToken: 3,
                    validFrom: 4,
                    validUntil: 5,
                    priceNumerator: new bn_js_1.default(1337),
                    priceDenominator: new bn_js_1.default(0x12345678),
                    remainingAmount: new bn_js_1.default(42),
                    orderId: 7,
                },
            ];
            const encoded = src_1.encodeIndexedOrders(orders);
            const decoded = src_1.decodeIndexedOrders(hex(encoded));
            chai_1.expect(json(decoded)).to.deep.equal(json(orders));
        });
    });
});
