Skip to content

Commit

Permalink
fix(affiliates): Generate username with address, not subaccount id (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding authored Nov 15, 2024
1 parent 95a80ef commit c26c8b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('usernames-helper', () => {
'dydx1df84hz7y0dd3mrqcv3vrhw9wdttelul8edqmvp',
'dydx16h7p7f4dysrgtzptxx2gtpt5d8t834g9dj830z',
'dydx15u9tppy5e2pdndvlrvafxqhuurj9mnpdstzj6z',
'dydx1q54yvrslnu0xp4drpde6f4e0k2ap9efss5hpsd',
'dydx199tqg4wdlnu4qjlxchpd7seg454937hjrknju4',
'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc565lnf',
'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc575lnf',
];

const expectedUsernames = [
Expand All @@ -24,6 +28,10 @@ describe('usernames-helper', () => {
'BoldGapOGY',
'ZoomEraQE0',
'WiryFernLEC',
'RudeFuel59E',
'MacroMealFK5',
'HappySnapWTT',
'BumpyEdgeH5Y',
];

for (let i = 0; i < addresses.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ describe('subaccount-username-generator', () => {
SubaccountUsernamesTable.findAll(
{}, [], {});

const expectedUsernames = [
'BumpyEdgeH5Y', // dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc575lnf
'HappySnapWTT', // dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc565lnf
'MacroMealFK5', // dydx199tqg4wdlnu4qjlxchpd7seg454937hjrknju4
];
expect(subaccountsWithUsernamesAfter.length).toEqual(subaccountsLength);
for (let i = 0; i < expectedUsernames.length; i++) {
expect(subaccountsWithUsernamesAfter[i].username).toEqual(expectedUsernames[i]);
}
});
});
4 changes: 2 additions & 2 deletions indexer/services/roundtable/src/helpers/usernames-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import nouns from './nouns.json';
const suffixCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

export function generateUsernameForSubaccount(
subaccountId: string,
address: string,
subaccountNum: number,
nounce: number = 0, // incremented in case of collision
): string {
const rng = seedrandom(`${subaccountId}/${subaccountNum}/${nounce}`);
const rng = seedrandom(`${address}/${subaccountNum}/${nounce}`);
const randomAdjective: string = adjectives[Math.floor(rng() * adjectives.length)];
const randomNoun: string = nouns[Math.floor(rng() * nouns.length)];
const randomSuffix: string = Array.from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function runTask(): Promise<void> {
for (const subaccount of subaccountZerosWithoutUsername) {
for (let i = 0; i < config.ATTEMPT_PER_SUBACCOUNT; i++) {
const username: string = generateUsernameForSubaccount(
subaccount.subaccountId,
subaccount.address,
// Always use subaccountNum 0 for generation. Effectively we are
// generating one username per address. The fact that we are storing
// in the `subaccount_usernames` table is a tech debt.
Expand Down

0 comments on commit c26c8b8

Please sign in to comment.