Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryfan01234 committed Sep 13, 2024
1 parent 0784cfa commit 7d36faa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('affiliates-controller#V4', () => {
await testMocks.seedData();
await SubaccountUsernamesTable.create(testConstants.defaultSubaccountUsername);
});

afterEach(async () => {
await dbHelpers.clearData();
});
Expand All @@ -46,20 +46,20 @@ describe('affiliates-controller#V4', () => {
});

it('should fail if address does not exist', async () => {
const nonExistentAddress = 'adgsakhasgt'
const response: request.Response = await sendRequest({
const nonExistentAddress = 'adgsakhasgt';
await sendRequest({
type: RequestMethod.GET,
path: `/v4/affiliates/metadata?address=${nonExistentAddress}`,
expectedStatus: 404, // helper performs expect on status
});
});

it('should classify not volume eligible', async () => {
it('should classify not volume eligible', async () => {
await WalletTable.update(
{
{
address: testConstants.defaultWallet.address,
totalVolume: "0",
totalTradingRewards: "0",
totalVolume: '0',
totalTradingRewards: '0',
},
);
const response: request.Response = await sendRequest({
Expand All @@ -74,12 +74,12 @@ describe('affiliates-controller#V4', () => {
});
});

it('should classify volume eligible', async () => {
it('should classify volume eligible', async () => {
await WalletTable.update(
{
{
address: testConstants.defaultWallet.address,
totalVolume: "100000",
totalTradingRewards: "0",
totalVolume: '100000',
totalTradingRewards: '0',
},
);
const response: request.Response = await sendRequest({
Expand All @@ -92,7 +92,7 @@ describe('affiliates-controller#V4', () => {
isVolumeEligible: true,
isAffiliate: false,
});
});
});

it('should classify is not affiliate', async () => {
// AffiliateReferredUsersTable is empty
Expand Down Expand Up @@ -128,8 +128,8 @@ describe('affiliates-controller#V4', () => {

it('should fail if subaccount username not found', async () => {
// create defaultWallet2 without subaccount username
WalletTable.create(testConstants.defaultWallet2);
const response: request.Response = await sendRequest({
await WalletTable.create(testConstants.defaultWallet2);
await sendRequest({
type: RequestMethod.GET,
path: `/v4/affiliates/metadata?address=${testConstants.defaultWallet2.address}`,
expectedStatus: 500, // helper performs expect on status
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { stats } from '@dydxprotocol-indexer/base';
import express from 'express';
import { checkSchema, matchedData } from 'express-validator';
import {
Controller, Get, Query, Route,
} from 'tsoa';
import {
WalletTable,
AffiliateReferredUsersTable,
SubaccountTable,
SubaccountUsernamesTable,
} from '@dydxprotocol-indexer/postgres';
import { NotFoundError, UnexpectedServerError } from '../../../lib/errors';
import express from 'express';
import { checkSchema, matchedData } from 'express-validator';
import {
Controller, Get, Query, Route,
} from 'tsoa';

import { getReqRateLimiter } from '../../../caches/rate-limiters';
import config from '../../../config';
import { NotFoundError, UnexpectedServerError } from '../../../lib/errors';
import { handleControllerError } from '../../../lib/helpers';
import { rateLimiterMiddleware } from '../../../lib/rate-limit';
import { handleValidationErrors } from '../../../request-helpers/error-handler';
Expand All @@ -39,47 +40,54 @@ class AffiliatesController extends Controller {
async getMetadata(
@Query() address: string,
): Promise<AffiliateMetadataResponse> {
const [walletRow, referredUserRows] = await Promise.all([
WalletTable.findById(address),
AffiliateReferredUsersTable.findByAffiliateAddress(address),
]);

// Check that the address exists
const walletRow = await WalletTable.findById(address);
if (!walletRow) {
throw new NotFoundError(`Wallet with address ${address} not found`);
}
const isVolumeEligible = Number(walletRow.totalVolume) >= config.VOLUME_ELIGIBILITY_THRESHOLD;

// Check if the address is an affiliate (has referred users)
const referredUserRows = await AffiliateReferredUsersTable.findByAffiliateAddress(address);
const isAffiliate = referredUserRows != undefined ? referredUserRows.length > 0 : false;
const isVolumeEligible = Number(walletRow.totalVolume) >= config.VOLUME_ELIGIBILITY_THRESHOLD;
const isAffiliate = referredUserRows !== undefined ? referredUserRows.length > 0 : false;

// Get referral code (subaccount 0 username)
const subaccountRows = await SubaccountTable.findAll(
{
address: address,
address,
subaccountNumber: 0,
},
[],
)
// No need to check subaccountRows.length > 1 because subaccountNumber is unique for an address
);
// No need to check subaccountRows.length > 1 as subaccountNumber is unique for an address
if (subaccountRows.length === 0) {
// error logging will be performed by handleInternalServerError
throw new UnexpectedServerError(`Subaccount 0 not found for address ${address}`);
}
const subaccountId = subaccountRows[0].id;

const usernameRows = await SubaccountUsernamesTable.findAll(
{
subaccountId: [subaccountId],
},
[],
)
);
// No need to check usernameRows.length > 1 as subAccountId is unique (foreign key constraint)
// This error can happen if a user calls this endpoint before subaccount-username-generator
// has generated the username
if (usernameRows.length === 0) {
stats.increment(`${config.SERVICE_NAME}.${controllerName}.get_metadata.subaccount_username_not_found`);
throw new UnexpectedServerError(`Username not found for subaccount ${subaccountId}`);
} else if (usernameRows.length > 1) {
throw new UnexpectedServerError(`Found multiple usernames for subaccount ${subaccountId}`);
}
const referralCode = usernameRows[0].username;

return {
referralCode: referralCode,
isVolumeEligible: isVolumeEligible,
isAffiliate: isAffiliate,
referralCode,
isVolumeEligible,
isAffiliate,
};
}

Expand Down

0 comments on commit 7d36faa

Please sign in to comment.