-
Notifications
You must be signed in to change notification settings - Fork 115
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
[OTE-760] implement comlink affiliate metadata endpoint #2243
Changes from all commits
368742b
bc16518
0a16cad
ac62c32
f321a91
0784cfa
7d36faa
b68f364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import { stats } from '@dydxprotocol-indexer/base'; | ||
import { | ||
WalletTable, | ||
AffiliateReferredUsersTable, | ||
SubaccountTable, | ||
SubaccountUsernamesTable, | ||
} from '@dydxprotocol-indexer/postgres'; | ||
import express from 'express'; | ||
import { checkSchema, matchedData } from 'express-validator'; | ||
import { | ||
|
@@ -7,6 +13,7 @@ import { | |
|
||
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'; | ||
|
@@ -31,14 +38,56 @@ const controllerName: string = 'affiliates-controller'; | |
class AffiliatesController extends Controller { | ||
@Get('/metadata') | ||
async getMetadata( | ||
@Query() address: string, // eslint-disable-line @typescript-eslint/no-unused-vars | ||
@Query() address: string, | ||
): Promise<AffiliateMetadataResponse> { | ||
// simulate a delay | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
const [walletRow, referredUserRows, subaccountRows] = await Promise.all([ | ||
WalletTable.findById(address), | ||
AffiliateReferredUsersTable.findByAffiliateAddress(address), | ||
SubaccountTable.findAll( | ||
{ | ||
address, | ||
subaccountNumber: 0, | ||
}, | ||
[], | ||
), | ||
]); | ||
|
||
// Check that the address exists | ||
if (!walletRow) { | ||
throw new NotFoundError(`Wallet with address ${address} not found`); | ||
} | ||
|
||
// Check if the address is an affiliate (has referred users) | ||
const isVolumeEligible = Number(walletRow.totalVolume) >= config.VOLUME_ELIGIBILITY_THRESHOLD; | ||
const isAffiliate = referredUserRows !== undefined ? referredUserRows.length > 0 : false; | ||
|
||
// 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; | ||
|
||
// Get subaccount0 username, which is the referral code | ||
const usernameRows = await SubaccountUsernamesTable.findAll( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of doing multiple awaits, can we do promise.All instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im not too famliar with ts, but the output of these functions is processed (for errors) and then used in subsequent functions. Would this be suitable for promise.all? My understanding is that promise.all should not be used for such cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Linked the wrong line
into one promise.all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do! |
||
{ | ||
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}`); | ||
} | ||
const referralCode = usernameRows[0].username; | ||
|
||
return { | ||
referralCode: 'TempCode123', | ||
isVolumeEligible: true, | ||
isAffiliate: false, | ||
referralCode, | ||
isVolumeEligible, | ||
isAffiliate, | ||
}; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you verify volume is stored in dollars and not quote quantums?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the quantum conversion is made before writing to fills db
This screenshot also suggests it is converted