Skip to content

Commit

Permalink
Fix subaccount validation to account for updated subaccount limit. (#…
Browse files Browse the repository at this point in the history
vincentwschau authored May 14, 2024
1 parent c24be3a commit b6a6c55
Showing 2 changed files with 21 additions and 12 deletions.
20 changes: 13 additions & 7 deletions indexer/services/comlink/__tests__/lib/validation/schemas.test.ts
Original file line number Diff line number Diff line change
@@ -3,8 +3,11 @@ import { getQueryString, sendRequestToApp } from '../../helpers/helpers';
import { schemaTestApp } from './helpers';
import request from 'supertest';
import config from '../../../src/config';
import { testConstants } from '@dydxprotocol-indexer/postgres';
import { MAX_SUBACCOUNT_NUMBER } from '../../../src/constants';
import {
testConstants,
MAX_PARENT_SUBACCOUNTS,
CHILD_SUBACCOUNT_MULTIPLIER,
} from '@dydxprotocol-indexer/postgres';

describe('schemas', () => {
const positiveNonInteger: number = 3.2;
@@ -19,25 +22,28 @@ describe('schemas', () => {
'missing subaccountNumber',
{ address: defaultAddress },
'subaccountNumber',
'subaccountNumber must be a non-negative integer less than 128',
'subaccountNumber must be a non-negative integer less than 128001',
],
[
'non-integer subaccountNumber',
{ address: defaultAddress, subaccountNumber: positiveNonInteger },
'subaccountNumber',
'subaccountNumber must be a non-negative integer less than 128',
'subaccountNumber must be a non-negative integer less than 128001',
],
[
'negative subaccountNumber',
{ address: defaultAddress, subaccountNumber: negativeInteger },
'subaccountNumber',
'subaccountNumber must be a non-negative integer less than 128',
'subaccountNumber must be a non-negative integer less than 128001',
],
[
'subaccountNumber greater than maximum subaccount number',
{ address: defaultAddress, subaccountNumber: MAX_SUBACCOUNT_NUMBER + 1 },
{
address: defaultAddress,
subaccountNumber: MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER + 1,
},
'subaccountNumber',
'subaccountNumber must be a non-negative integer less than 128',
'subaccountNumber must be a non-negative integer less than 128001',
],
])('Returns 400 when validation fails: %s', async (
_reason: string,
13 changes: 8 additions & 5 deletions indexer/services/comlink/src/lib/validation/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { perpetualMarketRefresher } from '@dydxprotocol-indexer/postgres';
import {
perpetualMarketRefresher,
MAX_PARENT_SUBACCOUNTS,
CHILD_SUBACCOUNT_MULTIPLIER,
} from '@dydxprotocol-indexer/postgres';
import { checkSchema, ParamSchema } from 'express-validator';

import config from '../../config';
import { MAX_SUBACCOUNT_NUMBER } from '../../constants';

export const CheckSubaccountSchema = checkSchema({
address: {
@@ -12,9 +15,9 @@ export const CheckSubaccountSchema = checkSchema({
subaccountNumber: {
in: ['params', 'query'],
isInt: {
options: { gt: -1, lt: MAX_SUBACCOUNT_NUMBER + 1 },
options: { gt: -1, lt: MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER + 1 },
},
errorMessage: 'subaccountNumber must be a non-negative integer less than 128',
errorMessage: 'subaccountNumber must be a non-negative integer less than 128001',
},
});

@@ -26,7 +29,7 @@ export const CheckParentSubaccountSchema = checkSchema({
parentSubaccountNumber: {
in: ['params', 'query'],
isInt: {
options: { gt: -1, lt: MAX_SUBACCOUNT_NUMBER + 1 },
options: { gt: -1, lt: MAX_PARENT_SUBACCOUNTS },
},
errorMessage: 'parentSubaccountNumber must be a non-negative integer less than 128',
},

0 comments on commit b6a6c55

Please sign in to comment.