[swap] Parse zippo headers

This commit is contained in:
David Walsh 2023-04-07 18:58:07 -06:00
parent fbec8d2281
commit d5f304fc69
No known key found for this signature in database
GPG Key ID: B19DD1E44DF93028
3 changed files with 17 additions and 22 deletions

View File

@ -361,11 +361,24 @@ const parseSwapQuoteRequestParams = (req: express.Request, endpoint: 'price' | '
// HACK typescript typing does not allow this valid json-schema // HACK typescript typing does not allow this valid json-schema
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO: fix me! // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO: fix me!
schemaUtils.validateSchema(req.query, schemas.swapQuoteRequestSchema as any); schemaUtils.validateSchema(req.query, schemas.swapQuoteRequestSchema as any);
const apiKey: string | undefined = req.header('0x-api-key');
const origin: string | undefined = req.header('origin'); const origin: string | undefined = req.header('origin');
// With zippo and the developer dashboard, we'll get:
// - 0x-App-Id (instead of an integrator id)
// - 0x-Affiliate-Address
// For now, we'll use the app id in place of the integrator id, as long as the
// app ID exists.
// Also, we'll prefer the zippo affiliate address over any passed in the header.
let integratorId: string | undefined; let integratorId: string | undefined;
if (apiKey) { const zippoAppId = req.header('0x-App-Id');
integratorId = getIntegratorIdForApiKey(apiKey); const zippoAffiliateAddress = req.header('0x-Affiliate-Address');
if (zippoAppId) {
integratorId = zippoAppId;
} else {
const apiKey: string | undefined = req.header('0x-api-key');
if (apiKey) {
integratorId = getIntegratorIdForApiKey(apiKey);
}
} }
// Parse string params // Parse string params
@ -475,7 +488,6 @@ const parseSwapQuoteRequestParams = (req: express.Request, endpoint: 'price' | '
includedSources: req.query.includedSources as string | undefined, includedSources: req.query.includedSources as string | undefined,
intentOnFilling: req.query.intentOnFilling as string | undefined, intentOnFilling: req.query.intentOnFilling as string | undefined,
takerAddress: takerAddress as string, takerAddress: takerAddress as string,
apiKey,
}, },
endpoint, endpoint,
); );
@ -492,7 +504,7 @@ const parseSwapQuoteRequestParams = (req: express.Request, endpoint: 'price' | '
} }
const rfqt: Pick<RfqRequestOpts, 'intentOnFilling' | 'isIndicative' | 'nativeExclusivelyRFQ'> | undefined = (() => { const rfqt: Pick<RfqRequestOpts, 'intentOnFilling' | 'isIndicative' | 'nativeExclusivelyRFQ'> | undefined = (() => {
if (apiKey) { if (integratorId !== undefined) {
if (endpoint === 'quote' && takerAddress) { if (endpoint === 'quote' && takerAddress) {
return { return {
intentOnFilling: req.query.intentOnFilling === 'true', intentOnFilling: req.query.intentOnFilling === 'true',
@ -528,7 +540,6 @@ const parseSwapQuoteRequestParams = (req: express.Request, endpoint: 'price' | '
apiKey: integratorId || 'N/A', apiKey: integratorId || 'N/A',
integratorId: integratorId || 'N/A', integratorId: integratorId || 'N/A',
integratorLabel: integrator?.label || 'N/A', integratorLabel: integrator?.label || 'N/A',
rawApiKey: apiKey || 'N/A',
enableSlippageProtection, enableSlippageProtection,
priceImpactProtectionPercentage, priceImpactProtectionPercentage,
}); });
@ -536,7 +547,6 @@ const parseSwapQuoteRequestParams = (req: express.Request, endpoint: 'price' | '
return { return {
affiliateAddress: affiliateAddress as string, affiliateAddress: affiliateAddress as string,
affiliateFee, affiliateFee,
apiKey,
buyAmount, buyAmount,
buyToken, buyToken,
endpoint, endpoint,

View File

@ -10,7 +10,6 @@ interface ParseRequestForExcludedSourcesParams {
excludedSources?: string; excludedSources?: string;
includedSources?: string; includedSources?: string;
intentOnFilling?: string; intentOnFilling?: string;
apiKey?: string;
} }
interface ParseRequestForExcludedSourcesResult { interface ParseRequestForExcludedSourcesResult {
@ -68,17 +67,6 @@ export const parseUtils = {
]); ]);
} }
// We enforce a valid API key - we don't want to fail silently.
if (request.apiKey === undefined) {
throw new ValidationError([
{
field: '0x-api-key',
code: ValidationErrorCodes.RequiredField,
reason: ValidationErrorReasons.InvalidApiKey,
},
]);
}
// If the user is requesting a firm quote, we want to make sure that `intentOnFilling` is set to "true". // If the user is requesting a firm quote, we want to make sure that `intentOnFilling` is set to "true".
if (endpoint === 'quote' && request.intentOnFilling !== 'true') { if (endpoint === 'quote' && request.intentOnFilling !== 'true') {
throw new ValidationError([ throw new ValidationError([

View File

@ -46,7 +46,6 @@ describe(SUITE_NAME, () => {
{ {
includedSources: 'RFQT', includedSources: 'RFQT',
takerAddress: NULL_ADDRESS, takerAddress: NULL_ADDRESS,
apiKey: 'ipsum',
}, },
'price', 'price',
); );
@ -60,7 +59,6 @@ describe(SUITE_NAME, () => {
{ {
includedSources: 'RFQT,Native', includedSources: 'RFQT,Native',
takerAddress: NULL_ADDRESS, takerAddress: NULL_ADDRESS,
apiKey: 'ipsum',
}, },
'price', 'price',
); );
@ -87,7 +85,6 @@ describe(SUITE_NAME, () => {
{ {
includedSources: 'RFQT', includedSources: 'RFQT',
takerAddress: NULL_ADDRESS, takerAddress: NULL_ADDRESS,
apiKey: 'ipsum',
}, },
'quote', 'quote',
); );