From 7953365320a692023e87ecb0408250a7709addb7 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 13 Mar 2024 12:14:14 -0400 Subject: [PATCH 1/5] Add util to convert between parent and child subaccount numbers Signed-off-by: Shrenuj Bansal --- .../__tests__/lib/api-translations.test.ts | 37 ++++++++++++++++++- indexer/packages/postgres/src/constants.ts | 4 ++ .../postgres/src/lib/api-translations.ts | 29 ++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts index c1cda29fed..cd387a780d 100644 --- a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts +++ b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts @@ -1,5 +1,10 @@ import { APITimeInForce, TimeInForce } from '../../src/types'; -import { isOrderTIFPostOnly, orderTIFToAPITIF } from '../../src/lib/api-translations'; +import { + getChildSubaccountNums, + getParentSubaccountNum, + isOrderTIFPostOnly, + orderTIFToAPITIF +} from '../../src/lib/api-translations'; describe('apiTranslations', () => { describe('orderTIFToAPITIF', () => { @@ -31,4 +36,34 @@ describe('apiTranslations', () => { expect(isOrderTIFPostOnly(orderTimeInForce)).toEqual(expectedPostOnly); }); }); + + describe('getChildSubaccountNums', () => { + it('Gets a list of all possible child subaccount numbers for a parent subaccount number', () => { + const childSubaccounts = getChildSubaccountNums(0); + expect(childSubaccounts.length).toEqual(1000); + expect(childSubaccounts[0]).toEqual(0); + expect(childSubaccounts[1]).toEqual(128); + expect(childSubaccounts[999]).toEqual(128 * 999); + }); + }); + + describe('getChildSubaccountNums', () => { + it('Throws an error if the parent subaccount number is greater than or equal to the maximum parent subaccount number', () => { + expect(() => getChildSubaccountNums(128)).toThrowError('Parent subaccount number must be less than 128'); + }); + }); + + describe('getParentSubaccountNum', () => { + it('Gets the parent subaccount number from a child subaccount number', () => { + expect(getParentSubaccountNum(0)).toEqual(0); + expect(getParentSubaccountNum(128)).toEqual(0); + expect(getParentSubaccountNum(128 * 999 - 1)).toEqual(127); + }); + }); + + describe('getParentSubaccountNum', () => { + it('Throws an error if the child subaccount number is greater than the max child subaccount number', () => { + expect(() => getParentSubaccountNum(128001)).toThrowError('Child subaccount number must be less than 128000'); + }); + }); }); diff --git a/indexer/packages/postgres/src/constants.ts b/indexer/packages/postgres/src/constants.ts index fac6844455..16e36f7e2f 100644 --- a/indexer/packages/postgres/src/constants.ts +++ b/indexer/packages/postgres/src/constants.ts @@ -121,3 +121,7 @@ export const DEFAULT_POSTGRES_OPTIONS : Options = config.USE_READ_REPLICA ? { readReplica: true, } : {}; + +export const MAX_PARENT_SUBACCOUNTS: number = 128; + +export const CHILD_SUBACCOUNT_MULTIPLIER: number = 1000; diff --git a/indexer/packages/postgres/src/lib/api-translations.ts b/indexer/packages/postgres/src/lib/api-translations.ts index 27c776b91e..22d0030be0 100644 --- a/indexer/packages/postgres/src/lib/api-translations.ts +++ b/indexer/packages/postgres/src/lib/api-translations.ts @@ -1,4 +1,4 @@ -import { TIME_IN_FORCE_TO_API_TIME_IN_FORCE } from '../constants'; +import { TIME_IN_FORCE_TO_API_TIME_IN_FORCE, CHILD_SUBACCOUNT_MULTIPLIER, MAX_PARENT_SUBACCOUNTS } from '../constants'; import { APITimeInForce, TimeInForce } from '../types'; /** @@ -20,3 +20,30 @@ export function isOrderTIFPostOnly(timeInForce: TimeInForce): boolean { export function orderTIFToAPITIF(timeInForce: TimeInForce): APITimeInForce { return TIME_IN_FORCE_TO_API_TIME_IN_FORCE[timeInForce]; } + +/** + * Gets a list of all possible child subaccount numbers for a parent subaccount number + * Child subaccounts = [128*0+parentSubaccount, 128*1+parentSubaccount ... 128*999+parentSubaccount] + * @param parentSubaccount + * @returns + */ +export function getChildSubaccountNums(parentSubaccountNum: number): number[] { + if (parentSubaccountNum >= MAX_PARENT_SUBACCOUNTS) { + throw new Error(`Parent subaccount number must be less than ${MAX_PARENT_SUBACCOUNTS}`); + } + return Array.from({ length: CHILD_SUBACCOUNT_MULTIPLIER }, + (_, i) => MAX_PARENT_SUBACCOUNTS * i + parentSubaccountNum); +} + +/** + * Gets the parent subaccount number from a child subaccount number + * Parent subaccount = childSubaccount / 128 + * @param childSubaccountNum + * @returns + */ +export function getParentSubaccountNum(childSubaccountNum: number): number { + if (childSubaccountNum > MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER) { + throw new Error(`Child subaccount number must be less than ${MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER}`); + } + return Math.floor(childSubaccountNum % MAX_PARENT_SUBACCOUNTS); +} From 099636bb94fed7b74defd87defa85fd6aaa41259 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 13 Mar 2024 13:28:22 -0400 Subject: [PATCH 2/5] fix lint Signed-off-by: Shrenuj Bansal --- .../packages/postgres/__tests__/lib/api-translations.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts index cd387a780d..5562c79aed 100644 --- a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts +++ b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts @@ -3,7 +3,7 @@ import { getChildSubaccountNums, getParentSubaccountNum, isOrderTIFPostOnly, - orderTIFToAPITIF + orderTIFToAPITIF, } from '../../src/lib/api-translations'; describe('apiTranslations', () => { From 0884df37ba558b3c0d1d52876269e9859f281120 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 13 Mar 2024 14:07:23 -0400 Subject: [PATCH 3/5] fix small bug Signed-off-by: Shrenuj Bansal --- indexer/packages/postgres/src/lib/api-translations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indexer/packages/postgres/src/lib/api-translations.ts b/indexer/packages/postgres/src/lib/api-translations.ts index 22d0030be0..8cbedc2797 100644 --- a/indexer/packages/postgres/src/lib/api-translations.ts +++ b/indexer/packages/postgres/src/lib/api-translations.ts @@ -45,5 +45,5 @@ export function getParentSubaccountNum(childSubaccountNum: number): number { if (childSubaccountNum > MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER) { throw new Error(`Child subaccount number must be less than ${MAX_PARENT_SUBACCOUNTS * CHILD_SUBACCOUNT_MULTIPLIER}`); } - return Math.floor(childSubaccountNum % MAX_PARENT_SUBACCOUNTS); + return childSubaccountNum % MAX_PARENT_SUBACCOUNTS; } From 09ef07c53d60c75c2b34a3f10970ef2837887014 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 13 Mar 2024 14:15:56 -0400 Subject: [PATCH 4/5] address comments Signed-off-by: Shrenuj Bansal --- .../packages/postgres/__tests__/lib/api-translations.test.ts | 1 + indexer/packages/postgres/src/lib/api-translations.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts index 5562c79aed..565b806bfe 100644 --- a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts +++ b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts @@ -42,6 +42,7 @@ describe('apiTranslations', () => { const childSubaccounts = getChildSubaccountNums(0); expect(childSubaccounts.length).toEqual(1000); expect(childSubaccounts[0]).toEqual(0); + expect(childSubaccounts[127]).toEqual(127); expect(childSubaccounts[1]).toEqual(128); expect(childSubaccounts[999]).toEqual(128 * 999); }); diff --git a/indexer/packages/postgres/src/lib/api-translations.ts b/indexer/packages/postgres/src/lib/api-translations.ts index 8cbedc2797..c6e69e248f 100644 --- a/indexer/packages/postgres/src/lib/api-translations.ts +++ b/indexer/packages/postgres/src/lib/api-translations.ts @@ -37,7 +37,7 @@ export function getChildSubaccountNums(parentSubaccountNum: number): number[] { /** * Gets the parent subaccount number from a child subaccount number - * Parent subaccount = childSubaccount / 128 + * Parent subaccount = childSubaccount % 128 * @param childSubaccountNum * @returns */ From 79bdc3e6f3994d25b23fe9123768b2ad1c4008df Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 13 Mar 2024 15:23:45 -0400 Subject: [PATCH 5/5] fix test bug Signed-off-by: Shrenuj Bansal --- .../postgres/__tests__/lib/api-translations.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts index 565b806bfe..2ba80bc4a3 100644 --- a/indexer/packages/postgres/__tests__/lib/api-translations.test.ts +++ b/indexer/packages/postgres/__tests__/lib/api-translations.test.ts @@ -38,14 +38,20 @@ describe('apiTranslations', () => { }); describe('getChildSubaccountNums', () => { - it('Gets a list of all possible child subaccount numbers for a parent subaccount number', () => { + it('Gets a list of all possible child subaccount numbers for a parent subaccount 0', () => { const childSubaccounts = getChildSubaccountNums(0); expect(childSubaccounts.length).toEqual(1000); expect(childSubaccounts[0]).toEqual(0); - expect(childSubaccounts[127]).toEqual(127); expect(childSubaccounts[1]).toEqual(128); expect(childSubaccounts[999]).toEqual(128 * 999); }); + it('Gets a list of all possible child subaccount numbers for a parent subaccount 127', () => { + const childSubaccounts = getChildSubaccountNums(127); + expect(childSubaccounts.length).toEqual(1000); + expect(childSubaccounts[0]).toEqual(127); + expect(childSubaccounts[1]).toEqual(128 + 127); + expect(childSubaccounts[999]).toEqual(128 * 999 + 127); + }); }); describe('getChildSubaccountNums', () => {