Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-112] Add util to convert between parent and child subaccount numbers #1171

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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', () => {
shrenujb marked this conversation as resolved.
Show resolved Hide resolved
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');
});
});
});
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);
}

shrenujb marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the parent subaccount number from a child subaccount number
* Parent subaccount = childSubaccount / 128
* @param childSubaccountNum
shrenujb marked this conversation as resolved.
Show resolved Hide resolved
* @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;
}
Loading