Skip to content

Commit

Permalink
[TRA-112] Add util to convert between parent and child subaccount num…
Browse files Browse the repository at this point in the history
…bers (#1171)


Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb authored Mar 14, 2024
1 parent 415c118 commit 7b100ac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
44 changes: 43 additions & 1 deletion indexer/packages/postgres/__tests__/lib/api-translations.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -31,4 +36,41 @@ describe('apiTranslations', () => {
expect(isOrderTIFPostOnly(orderTimeInForce)).toEqual(expectedPostOnly);
});
});

describe('getChildSubaccountNums', () => {
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[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', () => {
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');
});
});
});
4 changes: 4 additions & 0 deletions indexer/packages/postgres/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 28 additions & 1 deletion indexer/packages/postgres/src/lib/api-translations.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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 childSubaccountNum % MAX_PARENT_SUBACCOUNTS;
}

0 comments on commit 7b100ac

Please sign in to comment.