Skip to content

Commit

Permalink
pr revision
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryfan01234 committed Sep 17, 2024
1 parent dd9d66c commit 6ca6830
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,24 @@ describe('Affiliate info store', () => {
describe('paginatedFindWithAddressFilter', () => {
beforeEach(async () => {
await migrate();
for (let i = 0; i < 10; i++) {
await AffiliateInfoTable.create({
await Promise.all(
Array.from({ length: 10 }, (_, i) => AffiliateInfoTable.create({
...defaultAffiliateInfo,
address: `address_${i}`,
affiliateEarnings: i.toString(),
});
}
}),
),
);
});

it('Successfully filters by address', async () => {
// eslint-disable-next-line max-len
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter(
['address_0'],
0,
10,
false,
);
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
['address_0'],
0,
10,
false,
);
expect(infos).toBeDefined();
expect(infos!.length).toEqual(1);
expect(infos![0]).toEqual(expect.objectContaining({
Expand All @@ -96,13 +97,13 @@ describe('Affiliate info store', () => {
});

it('Successfully sorts by affiliate earning', async () => {
// eslint-disable-next-line max-len
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter(
[],
0,
10,
true,
);
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
[],
0,
10,
true,
);
expect(infos).toBeDefined();
expect(infos!.length).toEqual(10);
expect(infos![0]).toEqual(expect.objectContaining({
Expand All @@ -118,13 +119,13 @@ describe('Affiliate info store', () => {
});

it('Successfully uses offset and limit', async () => {
// eslint-disable-next-line max-len
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter(
[],
5,
2,
false,
);
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
[],
5,
2,
false,
);
expect(infos).toBeDefined();
expect(infos!.length).toEqual(2);
expect(infos![0]).toEqual(expect.objectContaining({
Expand All @@ -140,13 +141,13 @@ describe('Affiliate info store', () => {
});

it('Successfully filters, sorts, offsets, and limits', async () => {
// eslint-disable-next-line max-len
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter(
[],
3,
2,
true,
);
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
[],
3,
2,
true,
);
expect(infos).toBeDefined();
expect(infos!.length).toEqual(2);
expect(infos![0]).toEqual(expect.objectContaining({
Expand All @@ -160,5 +161,17 @@ describe('Affiliate info store', () => {
affiliateEarnings: '5',
}));
});

it('Returns empty array if no results', async () => {
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
['address_11'],
0,
10,
false,
);
expect(infos).toBeDefined();
expect(infos!.length).toEqual(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,5 @@ export async function paginatedFindWithAddressFilter(
// Apply pagination using offset and limit
baseQuery = baseQuery.offset(offset).limit(limit);

// Returning all fields
return baseQuery.returning('*');
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Options,
Ordering,
QueryableField,
AddressUsernameFromDatabase,
AddressUsername,
} from '../types';

export async function findAll(
Expand Down Expand Up @@ -118,12 +118,12 @@ export async function getSubaccountsWithoutUsernames(

export async function findByAddress(
addresses: string[],
): Promise<AddressUsernameFromDatabase[]> {
): Promise<AddressUsername[]> {
if (addresses.length === 0) {
return [];
}

const result: { rows: AddressUsernameFromDatabase[] } = await knexReadReplica
const result: { rows: AddressUsername[] } = await knexReadReplica
.getConnection()
.raw(
`
Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/types/db-model-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export interface SubaccountUsernamesFromDatabase {
subaccountId: string,
}

export interface AddressUsernameFromDatabase {
export interface AddressUsername {
address: string,
username: string,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ describe('affiliates-controller#V4', () => {
await SubaccountUsernamesTable.create(testConstants.subaccountUsernameWithAlternativeAddress);

// Create affiliate infos
await AffiliateInfoTable.create(defaultInfo);
await AffiliateInfoTable.create(defaultInfo2);
await AffiliateInfoTable.create(defaultInfo3);
await Promise.all([
AffiliateInfoTable.create(defaultInfo),
AffiliateInfoTable.create(defaultInfo2),
AffiliateInfoTable.create(defaultInfo3),
]);
});

afterEach(async () => {
Expand Down Expand Up @@ -246,7 +248,7 @@ describe('affiliates-controller#V4', () => {
expect(response.body.total).toEqual(expectedResponse.total);
});

it('should handle no results when filter by address', async () => {
it('should handle no results', async () => {
const req: AffiliateSnapshotRequest = {
addressFilter: ['nonexistentaddress'],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger, stats } from '@dydxprotocol-indexer/base';
import {
AddressUsernameFromDatabase,
AddressUsername,
WalletTable,
AffiliateInfoTable,
AffiliateReferredUsersTable,
Expand Down Expand Up @@ -129,13 +129,13 @@ class AffiliatesController extends Controller {
const finalLimit: number = limit ?? 1000;
const finalsortByAffiliateEarning: boolean = sortByAffiliateEarning ?? false;

// eslint-disable-next-line max-len
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable.paginatedFindWithAddressFilter(
finalAddressFilter,
finalOffset,
finalLimit,
finalsortByAffiliateEarning,
);
const infos: AffiliateInfoFromDatabase[] | undefined = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
finalAddressFilter,
finalOffset,
finalLimit,
finalsortByAffiliateEarning,
);

// No results found
if (infos === undefined) {
Expand All @@ -147,18 +147,23 @@ class AffiliatesController extends Controller {
}

// Get referral codes
// eslint-disable-next-line max-len
const addressUsernames: AddressUsernameFromDatabase[] = await SubaccountUsernamesTable.findByAddress(
const addressUsernames:
AddressUsername[] = await SubaccountUsernamesTable.findByAddress(
infos.map((info) => info.address),
);
const addressUsernameMap: Record<string, string> = {};
addressUsernames.forEach((addressUsername) => {
addressUsernameMap[addressUsername.address] = addressUsername.username;
});
if (addressUsernames.length !== infos.length) {
const addressesNotFound = infos
.map((info) => info.address)
.filter((address) => !(address in addressUsernameMap))
.join(', ');

logger.warning({
at: 'affiliates-controller#snapshot',
message: `Could not find referral code for following addresses: ${infos.map((info) => info.address).filter((address) => !(address in addressUsernameMap)).join(', ')}`,
message: `Could not find referral code for the following addresses: ${addressesNotFound}`,
});
}

Expand Down

0 comments on commit 6ca6830

Please sign in to comment.