"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../../src/types");
const constants_1 = require("../../src/lib/constants");
const errors_1 = require("../../src/lib/errors");
const validation_1 = require("../../src/lib/validation");
const constants_2 = require("../helpers/constants");
const long_1 = __importDefault(require("long"));
const MAX_UINT_32_PLUS_1 = constants_1.MAX_UINT_32 + 1;
const MAX_SUBACCOUNT_NUMBER_PLUS_1 = constants_1.MAX_SUBACCOUNT_NUMBER + 1;
describe('Validations', () => {
    it.each([
        ['valid', constants_2.defaultOrder, undefined],
        ['0 value clientId', { ...constants_2.defaultOrder, clientId: 0 }, undefined],
        [
            'underflow clientId',
            { ...constants_2.defaultOrder, clientId: -1 },
            new errors_1.UserError(`clientId: ${-1} is not a valid uint32`),
        ],
        [
            'overflow clientId',
            { ...constants_2.defaultOrder, clientId: MAX_UINT_32_PLUS_1 },
            new errors_1.UserError(`clientId: ${MAX_UINT_32_PLUS_1} is not a valid uint32`),
        ],
        [
            'underflow quantums',
            { ...constants_2.defaultOrder, quantums: long_1.default.NEG_ONE },
            new errors_1.UserError(`quantums: ${-1} cannot be <= 0`),
        ],
        [
            'underflow goodTilBlock',
            { ...constants_2.defaultOrder, goodTilBlock: -1 },
            new errors_1.UserError(`goodTilBlock: ${-1} is not a valid uint32 or is 0`),
        ],
        [
            'overflow goodTilBlock',
            { ...constants_2.defaultOrder, goodTilBlock: MAX_UINT_32_PLUS_1 },
            new errors_1.UserError(`goodTilBlock: ${MAX_UINT_32_PLUS_1} is not a valid uint32 or is 0`),
        ],
        [
            '0 goodTilBlock',
            { ...constants_2.defaultOrder, goodTilBlock: 0 },
            new errors_1.UserError(`goodTilBlock: ${0} is not a valid uint32 or is 0`),
        ],
        [
            'underflow subticks',
            { ...constants_2.defaultOrder, subticks: long_1.default.NEG_ONE },
            new errors_1.UserError(`subticks: ${-1} cannot be <= 0`),
        ],
    ])('Validate order: %s', (_name, order, expectedError) => {
        const validationError = (0, validation_1.validatePlaceOrderMessage)(0, order);
        expect(validationError).toEqual(expectedError);
    });
    it.each([
        ['valid', constants_2.defaultCancelOrder, undefined],
        ['0 value clientId', { ...constants_2.defaultCancelOrder, clientId: 0 }, undefined],
        [
            'underflow clientId',
            { ...constants_2.defaultCancelOrder, clientId: -1 },
            new errors_1.UserError(`clientId: ${-1} is not a valid uint32`),
        ],
        [
            'overflow clientId',
            { ...constants_2.defaultCancelOrder, clientId: MAX_UINT_32_PLUS_1 },
            new errors_1.UserError(`clientId: ${MAX_UINT_32_PLUS_1} is not a valid uint32`),
        ],
        [
            'underflow goodTilBlock',
            { ...constants_2.defaultCancelOrder, goodTilBlock: -1 },
            new errors_1.UserError(`goodTilBlock: ${-1} is not a valid uint32 or is 0`),
        ],
        [
            'overflow goodTilBlock',
            { ...constants_2.defaultCancelOrder, goodTilBlock: MAX_UINT_32_PLUS_1 },
            new errors_1.UserError(`goodTilBlock: ${MAX_UINT_32_PLUS_1} is not a valid uint32 or is 0`),
        ],
        [
            '0 goodTilBlock',
            { ...constants_2.defaultCancelOrder, goodTilBlock: 0 },
            new errors_1.UserError(`goodTilBlock: ${0} is not a valid uint32 or is 0`),
        ],
        [
            'contains GoodTilBlockTime',
            { ...constants_2.defaultCancelOrder, goodTilBlockTime: 10 },
            new errors_1.UserError('goodTilBlockTime is 10, but should not be set for non-stateful orders'),
        ],
        [
            'stateful order - valid',
            {
                ...constants_2.defaultCancelOrder,
                orderFlags: types_1.OrderFlags.LONG_TERM,
                goodTilBlock: undefined,
                goodTilBlockTime: 10,
            },
            undefined,
        ],
        [
            'stateful order - undefined goodTilBlockTime',
            { ...constants_2.defaultCancelOrder, orderFlags: types_1.OrderFlags.LONG_TERM },
            new errors_1.UserError(`goodTilBlockTime: ${undefined} is not a valid uint32 or is 0`),
        ],
        [
            'stateful order - zero goodTilBlockTime',
            { ...constants_2.defaultCancelOrder, orderFlags: types_1.OrderFlags.LONG_TERM, goodTilBlockTime: 0 },
            new errors_1.UserError(`goodTilBlockTime: ${0} is not a valid uint32 or is 0`),
        ],
        [
            'stateful order - underflow goodTilBlockTime',
            { ...constants_2.defaultCancelOrder, orderFlags: types_1.OrderFlags.LONG_TERM, goodTilBlockTime: -1 },
            new errors_1.UserError(`goodTilBlockTime: ${-1} is not a valid uint32 or is 0`),
        ],
        [
            'stateful order - overflow goodTilBlockTime',
            {
                ...constants_2.defaultCancelOrder,
                orderFlags: types_1.OrderFlags.LONG_TERM,
                goodTilBlockTime: MAX_UINT_32_PLUS_1,
            },
            new errors_1.UserError(`goodTilBlockTime: ${MAX_UINT_32_PLUS_1} is not a valid uint32 or is 0`),
        ],
        [
            'stateful order - has GoodTilBlock',
            {
                ...constants_2.defaultCancelOrder,
                orderFlags: types_1.OrderFlags.LONG_TERM,
                goodTilBlock: 10,
                goodTilBlockTime: 10,
            },
            new errors_1.UserError('goodTilBlock is 10, but should not be set for stateful orders'),
        ],
    ])('Validate cancel order: %s', (_name, order, expectedError) => {
        const validationError = (0, validation_1.validateCancelOrderMessage)(0, order);
        expect(validationError).toEqual(expectedError);
    });
    it.each([
        ['valid', constants_2.defaultBatchCancelOrder, undefined],
        [
            'overflow clientId',
            {
                ...constants_2.defaultBatchCancelOrder,
                shortTermOrders: [
                    ...constants_2.defaultBatchCancelOrder.shortTermOrders,
                    {
                        clobPairId: types_1.ClobPairId.PERPETUAL_PAIR_ETH_USD,
                        clientIds: [MAX_UINT_32_PLUS_1],
                    },
                ],
            },
            new errors_1.UserError(`clientId: ${MAX_UINT_32_PLUS_1} is not a valid uint32`),
        ],
        [
            'underflow goodTilBlock',
            { ...constants_2.defaultBatchCancelOrder, goodTilBlock: -1 },
            new errors_1.UserError(`goodTilBlock: ${-1} is not a valid uint32 or is 0`),
        ],
        [
            'overflow goodTilBlock',
            { ...constants_2.defaultBatchCancelOrder, goodTilBlock: MAX_UINT_32_PLUS_1 },
            new errors_1.UserError(`goodTilBlock: ${MAX_UINT_32_PLUS_1} is not a valid uint32 or is 0`),
        ],
        [
            '0 goodTilBlock',
            { ...constants_2.defaultBatchCancelOrder, goodTilBlock: 0 },
            new errors_1.UserError(`goodTilBlock: ${0} is not a valid uint32 or is 0`),
        ],
    ])('Validate batch cancel orders: %s', (_name, orders, expectedError) => {
        const validationError = (0, validation_1.validateBatchCancelOrderMessage)(0, orders);
        expect(validationError).toEqual(expectedError);
    });
    it.each([
        ['valid', constants_2.defaultTransfer, undefined],
        [
            'underflow senderSubaccountNumber',
            { ...constants_2.defaultTransfer, sender: { owner: constants_2.TEST_ADDRESS, number: -1 } },
            new errors_1.UserError(`senderSubaccountNumber: ${-1} cannot be < 0 or > ${constants_1.MAX_SUBACCOUNT_NUMBER}`),
        ],
        [
            'exceeds max subaccount number - senderSubaccountNumber',
            { ...constants_2.defaultTransfer, sender: { owner: constants_2.TEST_ADDRESS, number: MAX_SUBACCOUNT_NUMBER_PLUS_1 } },
            new errors_1.UserError(`senderSubaccountNumber: ${MAX_SUBACCOUNT_NUMBER_PLUS_1} cannot be < 0 or > ${constants_1.MAX_SUBACCOUNT_NUMBER}`),
        ],
        [
            '0 senderSubaccountNumber',
            { ...constants_2.defaultTransfer, sender: { owner: constants_2.TEST_ADDRESS, number: 0 } },
            undefined,
        ],
        [
            'underflow recipientSubaccountNumber',
            { ...constants_2.defaultTransfer, recipient: { owner: constants_2.TEST_ADDRESS, number: -1 } },
            new errors_1.UserError(`recipientSubaccountNumber: ${-1} cannot be < 0 or > ${constants_1.MAX_SUBACCOUNT_NUMBER}`),
        ],
        [
            'exceeds max subaccount number - recipient.subaccountNumber',
            {
                ...constants_2.defaultTransfer,
                recipient: { owner: constants_2.TEST_ADDRESS, number: MAX_SUBACCOUNT_NUMBER_PLUS_1 },
            },
            new errors_1.UserError(`recipientSubaccountNumber: ${MAX_SUBACCOUNT_NUMBER_PLUS_1} cannot be < 0 or > ${constants_1.MAX_SUBACCOUNT_NUMBER}`),
        ],
        [
            '0 recipientSubaccountNumber',
            { ...constants_2.defaultTransfer, recipient: { owner: constants_2.TEST_ADDRESS, number: 0 } },
            undefined,
        ],
        [
            'non-zero asset id',
            { ...constants_2.defaultTransfer, assetId: 1 },
            new errors_1.UserError(`asset id: ${1} not supported`),
        ],
        [
            '0 amount',
            { ...constants_2.defaultTransfer, amount: long_1.default.ZERO },
            new errors_1.UserError(`amount: ${0} cannot be <= 0`),
        ],
        [
            'too short recipientAddress',
            {
                ...constants_2.defaultTransfer,
                recipient: {
                    owner: 'dydx14063jves4u9zhm7eja5ltf3t8zspxd92qnk23',
                    number: 0,
                },
            },
            new errors_1.UserError('Error: Invalid checksum for dydx14063jves4u9zhm7eja5ltf3t8zspxd92qnk23'),
        ],
        [
            'invalid recipientAddress',
            {
                ...constants_2.defaultTransfer,
                recipient: {
                    owner: 'fakeAddress1234',
                    number: 0,
                },
            },
            new errors_1.UserError('Error: Mixed-case string fakeAddress1234'),
        ],
    ])('Validate transfer: %s', (_name, transfer, expectedError) => {
        const validationError = (0, validation_1.validateTransferMessage)(transfer);
        expect(validationError).toEqual(expectedError);
    });
    it.each([
        ['valid', 'dydx17xpfvakm2amg962yls6f84z3kell8c5leqdyt2', true],
        ['invalid: does not start with dydx1', 'dydx27xpfvakm2amg962yls6f84z3kell8c5leqdyt2', false],
        ['invalid: too short', 'dydx17xpfvakm2amg962yls6f84z3kell8c5leqdyt', false],
        ['invalid: too long', 'dydx17xpfvakm2amg962yls6f84z3kell8c5leqdyt2s', false],
    ])('Validate address: %s', (_name, address, expectedResult) => {
        const validationResult = (0, validation_1.isValidAddress)(address);
        expect(validationResult).toEqual(expectedResult);
    });
});
//# sourceMappingURL=data:application/json;base64, 