Skip to content

Commit 95a80ef

Browse files
authored
Use alphanumeric suffix for username (dydxprotocol#2573)
1 parent 4633b85 commit 95a80ef

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

indexer/services/roundtable/__tests__/helpers/usernames-helper.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ describe('usernames-helper', () => {
1616
];
1717

1818
const expectedUsernames = [
19-
'CushyHand599',
20-
'AmpleCube324',
21-
'AwareFood215',
22-
'LoudLand654',
23-
'MossyStraw800',
24-
'BoldGap392',
25-
'ZoomEra454',
26-
'WiryFern332',
19+
'CushyHandVE6',
20+
'AmpleCubeLKI',
21+
'AwareFoodHGP',
22+
'LoudLandXWV',
23+
'MossyStraw2JJ',
24+
'BoldGapOGY',
25+
'ZoomEraQE0',
26+
'WiryFernLEC',
2727
];
2828

2929
for (let i = 0; i < addresses.length; i++) {

indexer/services/roundtable/src/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ export const configSchema = {
211211
STALE_ORDERBOOK_LEVEL_THRESHOLD_SECONDS: parseInteger({ default: 10 }),
212212

213213
// Subaccount username generator
214-
SUBACCOUNT_USERNAME_NUM_RANDOM_DIGITS: parseInteger({ default: 3 }),
215-
SUBACCOUNT_USERNAME_BATCH_SIZE: parseInteger({ default: 1000 }),
214+
SUBACCOUNT_USERNAME_SUFFIX_RANDOM_DIGITS: parseInteger({ default: 3 }),
215+
SUBACCOUNT_USERNAME_BATCH_SIZE: parseInteger({ default: 2000 }),
216216
// number of attempts to generate username for a subaccount
217217
ATTEMPT_PER_SUBACCOUNT: parseInteger({ default: 3 }),
218218
};

indexer/services/roundtable/src/helpers/usernames-helper.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import config from '../config';
44
import adjectives from './adjectives.json';
55
import nouns from './nouns.json';
66

7+
const suffixCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
8+
79
export function generateUsernameForSubaccount(
810
subaccountId: string,
911
subaccountNum: number,
@@ -12,12 +14,13 @@ export function generateUsernameForSubaccount(
1214
const rng = seedrandom(`${subaccountId}/${subaccountNum}/${nounce}`);
1315
const randomAdjective: string = adjectives[Math.floor(rng() * adjectives.length)];
1416
const randomNoun: string = nouns[Math.floor(rng() * nouns.length)];
15-
const randomNumber: string = Math.floor(rng() * 1000).toString().padStart(
16-
config.SUBACCOUNT_USERNAME_NUM_RANDOM_DIGITS, '0');
17+
const randomSuffix: string = Array.from(
18+
{ length: config.SUBACCOUNT_USERNAME_SUFFIX_RANDOM_DIGITS },
19+
() => suffixCharacters.charAt(Math.floor(rng() * suffixCharacters.length))).join('');
1720

1821
const capitalizedAdjective: string = randomAdjective.charAt(
1922
0).toUpperCase() + randomAdjective.slice(1);
2023
const capitalizedNoun: string = randomNoun.charAt(0).toUpperCase() + randomNoun.slice(1);
2124

22-
return `${capitalizedAdjective}${capitalizedNoun}${randomNumber}`;
25+
return `${capitalizedAdjective}${capitalizedNoun}${randomSuffix}`;
2326
}

indexer/services/roundtable/src/tasks/subaccount-username-generator.ts

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import config from '../config';
99
import { generateUsernameForSubaccount } from '../helpers/usernames-helper';
1010

1111
export default async function runTask(): Promise<void> {
12+
const start: number = Date.now();
13+
1214
const subaccountZerosWithoutUsername:
1315
SubaccountsWithoutUsernamesResult[] = await
1416
SubaccountUsernamesTable.getSubaccountZerosWithoutUsernames(
@@ -66,11 +68,19 @@ export default async function runTask(): Promise<void> {
6668
(subaccount) => subaccount.address,
6769
);
6870

71+
const duration = Date.now() - start;
72+
6973
logger.info({
7074
at: 'subaccount-username-generator#runTask',
7175
message: 'Generated usernames',
7276
batchSize: subaccountZerosWithoutUsername.length,
7377
successCount,
7478
addressSample: subaccountAddresses.slice(0, 10),
79+
duration,
7580
});
81+
82+
stats.timing(
83+
`${config.SERVICE_NAME}.subaccount_username_generator`,
84+
duration,
85+
);
7686
}

0 commit comments

Comments
 (0)