Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions bin/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ export class APIPipeline extends Stack {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:UniswapX/ParamApi/ApiKeys-hYyUt1',
});

const portionFlagSecret = sm.Secret.fromSecretAttributes(this, 'portion-flag', {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once we deploy this PR, we can delete secret portion-flag

secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:portion-flag-yR0VGr',
});

// Beta us-east-2
const betaUsEast2Stage = new APIStage(this, 'beta-us-east-2', {
env: { account: '665191769009', region: 'us-east-2' },
Expand All @@ -164,10 +160,8 @@ export class APIPipeline extends Stack {
ROUTING_API_URL: urlSecrets.secretValueFromJson('ROUTING_API_BETA').toString(),
SERVICE_URL: urlSecrets.secretValueFromJson('GOUDA_SERVICE_BETA').toString(),
PORTION_API_URL: urlSecrets.secretValueFromJson('PORTION_API_BETA').toString(),
ENABLE_PORTION: portionFlagSecret.secretValueFromJson('ENABLE_PORTION_BETA').toString(),
REQUEST_DESTINATION_ARN: arnSecrects.secretValueFromJson('URA_REQUEST_DESTINATION_BETA').toString(),
RESPONSE_DESTINATION_ARN: arnSecrects.secretValueFromJson('URA_RESPONSE_DESTINATION_BETA').toString(),
FORCE_PORTION_STRING: portionFlagSecret.secretValueFromJson('FORCE_PORTION_STRING').toString(),
},
});

Expand All @@ -192,10 +186,8 @@ export class APIPipeline extends Stack {
ROUTING_API_URL: urlSecrets.secretValueFromJson('ROUTING_API_PROD').toString(),
SERVICE_URL: urlSecrets.secretValueFromJson('GOUDA_SERVICE_PROD').toString(),
PORTION_API_URL: urlSecrets.secretValueFromJson('PORTION_API_PROD').toString(),
ENABLE_PORTION: portionFlagSecret.secretValueFromJson('ENABLE_PORTION_PROD').toString(),
REQUEST_DESTINATION_ARN: arnSecrects.secretValueFromJson('URA_REQUEST_DESTINATION_PROD').toString(),
RESPONSE_DESTINATION_ARN: arnSecrects.secretValueFromJson('URA_RESPONSE_DESTINATION_PROD').toString(),
FORCE_PORTION_STRING: portionFlagSecret.secretValueFromJson('FORCE_PORTION_STRING').toString(),
},
});

Expand Down Expand Up @@ -254,10 +246,6 @@ export class APIPipeline extends Stack {
value: `${stage}/portion-api/url`,
type: BuildEnvironmentVariableType.SECRETS_MANAGER,
},
FORCE_PORTION_SECRET: {
value: 'force-portion-secret',
type: BuildEnvironmentVariableType.SECRETS_MANAGER,
},
PARAM_API_URL: {
value: `${stage}/param-api/url`,
type: BuildEnvironmentVariableType.SECRETS_MANAGER,
Expand All @@ -272,7 +260,6 @@ export class APIPipeline extends Stack {
'echo "PORTION_API_URL=${PORTION_API_URL}" >> .env',
'echo "ARCHIVE_NODE_RPC=${ARCHIVE_NODE_RPC}" >> .env',
'echo "URA_INTERNAL_API_KEY=${URA_INTERNAL_API_KEY}" >> .env',
'echo "FORCE_PORTION_SECRET=${FORCE_PORTION_SECRET}" >> .env',
'yarn install --frozen-lockfile --network-concurrency 1',
'yarn build',
'yarn test:integ',
Expand Down Expand Up @@ -303,12 +290,10 @@ envVars['RFQ_LABS_COSIGNER_ADDRESS'] = process.env['RFQ_LABS_COSIGNER_ADDRESS']
envVars['ROUTING_API_URL'] = process.env['ROUTING_API_URL'] || '';
envVars['SERVICE_URL'] = process.env['SERVICE_URL'] || '';
envVars['PORTION_API_URL'] = process.env['PORTION_API_URL'] || '';
envVars['ENABLE_PORTION'] = process.env['ENABLE_PORTION'] || '';
envVars['REQUEST_DESTINATION_ARN'] = process.env['REQUEST_DESTINATION_ARN'] || '';
envVars['RESPONSE_DESTINATION_ARN'] = process.env['RESPONSE_DESTINATION_ARN'] || '';
envVars['ROUTING_API_KEY'] = process.env['ROUTING_API_KEY'] || 'test-api-key';
envVars['PARAMETERIZATION_API_KEY'] = process.env['PARAMETERIZATION_API_KEY'] || 'test-api-key';
envVars['FORCE_PORTION_SECRET'] = process.env['FORCE_PORTION_SECRET'] || '';

const jsonRpcProviders = {} as { [chainKey: string]: string };
SUPPORTED_CHAINS[RoutingType.CLASSIC].forEach((chainId: ChainId) => {
Expand Down
16 changes: 1 addition & 15 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BigNumber } from 'ethers';
import { forcePortion } from './util/portion';

export const DEFAULT_SLIPPAGE_TOLERANCE = '0.5'; // 0.5%
export const DEFAULT_ROUTING_API_DEADLINE = 600; // 10 minutes
Expand Down Expand Up @@ -33,23 +32,10 @@ export enum RoutingType {
export const DEFAULT_POSITIVE_CACHE_ENTRY_TTL = 300; // 5 minutes
export const DEFAULT_NEGATIVE_CACHE_ENTRY_TTL = 300; // 5 minutes

// we need this functional style of always enquirying the env var,
// otherwise when assigning process.env.ENABLE_PORTION to const variables,
// an update in process.env.ENABLE_PORTION will not be reflected in the lambda invocations until lambda recycles.
export const getEnablePortionEnvVar = () => process.env.ENABLE_PORTION;

export const uraEnablePortion = () => {
if (forcePortion) {
return true;
} else {
return getEnablePortionEnvVar() === 'true';
}
};

export const frontendEnablePortion = (sendPortionFlag?: boolean) => {
return sendPortionFlag;
};

export const frontendAndUraEnablePortion = (sendPortionFlag?: boolean) => {
return frontendEnablePortion(sendPortionFlag) && uraEnablePortion();
return frontendEnablePortion(sendPortionFlag);
};
39 changes: 12 additions & 27 deletions lib/fetchers/PortionFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { Unit } from 'aws-embedded-metrics';
import * as http from 'http';
import * as https from 'https';
import NodeCache from 'node-cache';
import { DEFAULT_NEGATIVE_CACHE_ENTRY_TTL, DEFAULT_POSITIVE_CACHE_ENTRY_TTL, uraEnablePortion } from '../constants';
import { DEFAULT_NEGATIVE_CACHE_ENTRY_TTL, DEFAULT_POSITIVE_CACHE_ENTRY_TTL } from '../constants';
import { RequestSource } from '../entities';
import axios from '../providers/quoters/helpers';
import { log } from '../util/log';
import { metrics } from '../util/metrics';
import { forcePortion } from '../util/portion';

export enum PortionType {
Flat = 'flat',
Expand Down Expand Up @@ -62,17 +61,7 @@ export class PortionFetcher {
): Promise<GetPortionResponse> {
metrics.putMetric(`PortionFetcherRequest`, 1);

// we check ENABLE_PORTION for every request, so that the update to the lambda env var gets reflected
// in real time
if (!uraEnablePortion()) {
metrics.putMetric(`PortionFetcherFlagDisabled`, 1);
return GET_NO_PORTION_RESPONSE;
}

// We bypass the cache if `forcePortion` is true.
// We do it to avoid cache conflicts since `forcePortion` is only for testing purposes.
const portionFromCache =
!forcePortion &&
this.portionCache.get<GetPortionResponse>(
PortionFetcher.PORTION_CACHE_KEY(
tokenInChainId,
Expand Down Expand Up @@ -105,21 +94,17 @@ export class PortionFetcher {
metrics.putMetric(`PortionFetcherSuccess`, 1);
metrics.putMetric(`PortionFetcherCacheMiss`, 1);

// We bypass the cache if `forcePortion` is true.
// We do it to avoid cache conflicts since `forcePortion` is only for testing purposes.
if (!forcePortion) {
this.portionCache.set<GetPortionResponse>(
PortionFetcher.PORTION_CACHE_KEY(
tokenInChainId,
tokenInAddress,
tokenOutChainId,
tokenOutAddress,
requestSource
),
portionResponse.data,
portionResponse.data.portion ? this.positiveCacheEntryTtl : this.negativeCacheEntryTtl
);
}
this.portionCache.set<GetPortionResponse>(
PortionFetcher.PORTION_CACHE_KEY(
tokenInChainId,
tokenInAddress,
tokenOutChainId,
tokenOutAddress,
requestSource
),
portionResponse.data,
portionResponse.data.portion ? this.positiveCacheEntryTtl : this.negativeCacheEntryTtl
);

return portionResponse.data;
} catch (e) {
Expand Down
15 changes: 4 additions & 11 deletions lib/handlers/quote/injector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { default as bunyan, default as Logger } from 'bunyan';

import { ChainId } from '@uniswap/sdk-core';
import { MetricsLogger } from 'aws-embedded-metrics';
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { default as bunyan, default as Logger } from 'bunyan';
import { providers } from 'ethers';
import NodeCache from 'node-cache';
import { SUPPORTED_CHAINS } from '../../config/chains';
Expand All @@ -16,11 +15,10 @@ import {
Quoter,
RfqQuoter,
RoutingApiQuoter,
SyntheticStatusProvider,
SyntheticStatusProvider
} from '../../providers';
import { setGlobalLogger } from '../../util/log';
import { setGlobalMetrics } from '../../util/metrics';
import { setGlobalForcePortion } from '../../util/portion';
import { checkDefined } from '../../util/preconditions';
import { ApiInjector, ApiRInj } from '../base/api-handler';

Expand Down Expand Up @@ -83,7 +81,7 @@ export class QuoteInjector extends ApiInjector<ContainerInjected, ApiRInj, Quote
_containerInjected: ContainerInjected,
requestBody: QuoteRequestBodyJSON,
_requestQueryParams: void,
event: APIGatewayProxyEvent,
_event: APIGatewayProxyEvent,
context: Context,
log: Logger,
metrics: MetricsLogger
Expand All @@ -96,11 +94,6 @@ export class QuoteInjector extends ApiInjector<ContainerInjected, ApiRInj, Quote
requestId,
});

setGlobalForcePortion(
process.env.FORCE_PORTION_STRING !== undefined &&
event.headers['X-UNISWAP-FORCE-PORTION-SECRET'] === process.env.FORCE_PORTION_STRING
);

setGlobalLogger(log);

metrics.setNamespace('Uniswap');
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/quoters/RfqQuoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RfqQuoter implements Quoter {
? request.info.amount.mul(request.info.portion.bips).div(BPS)
: undefined;

// we will only add portion to the exact out swap amount if the URA ENABLE_PORTION is true
// we will only add portion to the exact out swap amount if the portionEnabled is true
// as well as the frontend sendPortionEnabled is true
const amount = portionAmount && portionEnabled ? request.info.amount.add(portionAmount) : request.info.amount;
const requests = [
Expand Down
3 changes: 0 additions & 3 deletions lib/util/portion.ts

This file was deleted.

2 changes: 0 additions & 2 deletions test/integ/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export const axiosHelper = axiosStatic.create({
const axiosConfig: AxiosRequestConfig<any> = {
headers: {
...(process.env.URA_INTERNAL_API_KEY && { 'x-api-key': process.env.URA_INTERNAL_API_KEY }),
...(process.env.FORCE_PORTION_SECRET && { 'X-UNISWAP-FORCE-PORTION-SECRET': process.env.FORCE_PORTION_SECRET }),
},
};

Expand Down Expand Up @@ -378,7 +377,6 @@ export class BaseIntegrationTestSuite {
parseAmount('700000', BULLET),
]);

process.env.ENABLE_PORTION = 'true';
if (process.env.PORTION_API_URL) {
this.portionFetcher = new PortionFetcher(process.env.PORTION_API_URL, new NodeCache());
}
Expand Down
8 changes: 0 additions & 8 deletions test/unit/entities/DutchQuote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ describe('DutchQuote', () => {
const logger = Logger.createLogger({ name: 'test' });
logger.level(Logger.FATAL);

beforeEach(() => {
process.env.ENABLE_PORTION = 'true';
});

afterEach(() => {
process.env.ENABLE_PORTION = 'false';
});

describe('Reparameterize', () => {
it('slippage is in percent terms', async () => {
const amountIn = BigNumber.from('1000000000');
Expand Down
36 changes: 2 additions & 34 deletions test/unit/lib/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
import { frontendAndUraEnablePortion, frontendEnablePortion, uraEnablePortion } from '../../../lib/constants';
import { frontendEnablePortion } from '../../../lib/constants';

describe('constants Unit Tests', () => {
const enablePortionFlagOptions = ['true', 'false', undefined, 'garbage'];

enablePortionFlagOptions.forEach((enablePortionFlagOption) => {
it(`URA enable portion when process.env.ENABLE_PORTION = ${enablePortionFlagOption}`, () => {
process.env.ENABLE_PORTION = enablePortionFlagOption;

switch (enablePortionFlagOption) {
case 'true':
expect(uraEnablePortion()).toBeTruthy();
break;
case 'false':
expect(uraEnablePortion()).toBeFalsy();
break;
default:
expect(uraEnablePortion()).toBeFalsy();
break;
}
});
});

const sendPortionFlagOptions = [true, false, undefined];

sendPortionFlagOptions.forEach((sendPortionFlag) => {
it(`URA enable portion when process.env.ENABLE_PORTION = ${sendPortionFlag}`, () => {
it(`URA enable portion when sePortionEnabled = ${sendPortionFlag}`, () => {
expect(frontendEnablePortion(sendPortionFlag)).toStrictEqual(sendPortionFlag);
});
});

enablePortionFlagOptions.forEach((enablePortionFlagOption) => {
sendPortionFlagOptions.forEach((sendPortionFlag) => {
it(`URA enable portion when process.env.ENABLE_PORTION = ${enablePortionFlagOption} and sendPortionFlag = ${sendPortionFlag}`, () => {
process.env.ENABLE_PORTION = enablePortionFlagOption;

expect(frontendAndUraEnablePortion(sendPortionFlag)).toStrictEqual(
sendPortionFlag && enablePortionFlagOption === 'true'
);
});
});
});
});
Loading