From 27aa63d48aab5f95ae313d0eacb13f57ee074554 Mon Sep 17 00:00:00 2001 From: Raghava Pamula Date: Thu, 2 Jun 2022 21:38:53 -0400 Subject: [PATCH] refactoring --- bin/app.ts | 60 +++++++++++++++++++++++------- bin/stacks/routing-api-stack.ts | 9 ++--- bin/stacks/routing-lambda-stack.ts | 12 ++---- lib/handlers/injector-sor.ts | 44 +++------------------- 4 files changed, 58 insertions(+), 67 deletions(-) diff --git a/bin/app.ts b/bin/app.ts index edeb2d28f4..cfd4057806 100644 --- a/bin/app.ts +++ b/bin/app.ts @@ -1,3 +1,4 @@ +import { ChainId } from '@uniswap/smart-order-router' import * as cdk from 'aws-cdk-lib' import { CfnOutput, SecretValue, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib' import * as chatbot from 'aws-cdk-lib/aws-chatbot' @@ -8,6 +9,7 @@ import { CodeBuildStep, CodePipeline, CodePipelineSource } from 'aws-cdk-lib/pip import { Construct } from 'constructs' import dotenv from 'dotenv' import 'source-map-support/register' +import { SUPPORTED_CHAINS } from '../lib/handlers/injector-sor' import { STAGE } from '../lib/util/stage' import { RoutingAPIStack } from './stacks/routing-api-stack' dotenv.config() @@ -19,8 +21,7 @@ export class RoutingAPIStage extends Stage { scope: Construct, id: string, props: StageProps & { - providerName: string - projectId: string + jsonRpcProviders: {[chainName: string]:string} provisionedConcurrency: number ethGasStationInfoUrl: string chatbotSNSArn?: string @@ -33,8 +34,7 @@ export class RoutingAPIStage extends Stage { ) { super(scope, id, props) const { - providerName, - projectId, + jsonRpcProviders, provisionedConcurrency, ethGasStationInfoUrl, chatbotSNSArn, @@ -46,8 +46,7 @@ export class RoutingAPIStage extends Stage { } = props const { url } = new RoutingAPIStack(this, 'RoutingAPI', { - providerName, - projectId, + jsonRpcProviders, provisionedConcurrency, ethGasStationInfoUrl, chatbotSNSArn, @@ -96,8 +95,8 @@ export class RoutingAPIPipeline extends Stack { // Secrets are stored in secrets manager in the pipeline account. Accounts we deploy to // have been granted permissions to access secrets via resource policies. - const infuraProjectId = sm.Secret.fromSecretAttributes(this, 'InfuraProjectId', { - secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:infuraProjectId-UlSwK2', + const jsonRpcProvidersSecret = sm.Secret.fromSecretAttributes(this, 'RPCProviderUrls', { + secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:routing-api-rpc-urls-json-M18pZP', }) const ethGasStationInfoUrl = sm.Secret.fromSecretAttributes(this, 'ETHGasStationUrl', { @@ -119,9 +118,18 @@ export class RoutingAPIPipeline extends Stack { secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:hosted-zone-JmPDNV', }) + // Parse AWS Secret + let jsonRpcProviders = {} as {[chainId:string]:string} + SUPPORTED_CHAINS + .forEach((chainId:ChainId)=>{ + const key = `WEB3_RPC_${chainId}` + jsonRpcProviders[key] = jsonRpcProvidersSecret.secretValueFromJson(key) as unknown as string + }) + // Beta us-east-2 const betaUsEast2Stage = new RoutingAPIStage(this, 'beta-us-east-2', { env: { account: '145079444317', region: 'us-east-2' }, + jsonRpcProviders: jsonRpcProviders, provisionedConcurrency: 20, ethGasStationInfoUrl: ethGasStationInfoUrl.secretValue.toString(), stage: STAGE.BETA, @@ -129,8 +137,6 @@ export class RoutingAPIPipeline extends Stack { pinata_key: pinataApi.secretValueFromJson('pinata-api-key').toString(), pinata_secret: pinataSecret.secretValueFromJson('secret').toString(), hosted_zone: hostedZone.secretValueFromJson('zone').toString(), - providerName: 'infura', - projectId: infuraProjectId.secretValue.toString(), }) const betaUsEast2AppStage = pipeline.addStage(betaUsEast2Stage) @@ -140,8 +146,7 @@ export class RoutingAPIPipeline extends Stack { // Prod us-east-2 const prodUsEast2Stage = new RoutingAPIStage(this, 'prod-us-east-2', { env: { account: '606857263320', region: 'us-east-2' }, - providerName: 'infura', - projectId: infuraProjectId.secretValue.toString(), + jsonRpcProviders: jsonRpcProviders, provisionedConcurrency: 100, ethGasStationInfoUrl: ethGasStationInfoUrl.secretValue.toString(), chatbotSNSArn: 'arn:aws:sns:us-east-2:644039819003:SlackChatbotTopic', @@ -207,10 +212,37 @@ export class RoutingAPIPipeline extends Stack { const app = new cdk.App() +console.log(process.env.WEB3_RPC_1!) + +console.log({ + WEB3_RPC_1: process.env.WEB3_RPC_1!, + WEB3_RPC_3: process.env.WEB3_RPC_3!, + WEB3_RPC_4: process.env.WEB3_RPC_4!, + WEB3_RPC_5: process.env.WEB3_RPC_5!, + WEB3_RPC_42: process.env.WEB3_RPC_42!, + WEB3_RPC_10: process.env.WEB3_RPC_10!, + WEB3_RPC_69: process.env.WEB3_RPC_69!, + WEB3_RPC_42161: process.env.WEB3_RPC_42161!, + WEB3_RPC_421611: process.env.WEB3_RPC_421611!, + WEB3_RPC_137: process.env.WEB3_RPC_137!, + WEB3_RPC_80001: process.env.WEB3_RPC_80001!, +}) + // Local dev stack new RoutingAPIStack(app, 'RoutingAPIStack', { - providerName : process.env.PROVIDER_NAME!, - projectId: process.env.PROJECT_ID!, + jsonRpcProviders: { + WEB3_RPC_1: process.env.WEB3_RPC_1!, + WEB3_RPC_3: process.env.WEB3_RPC_3!, + WEB3_RPC_4: process.env.WEB3_RPC_4!, + WEB3_RPC_5: process.env.WEB3_RPC_5!, + WEB3_RPC_42: process.env.WEB3_RPC_42!, + WEB3_RPC_10: process.env.WEB3_RPC_10!, + WEB3_RPC_69: process.env.WEB3_RPC_69!, + WEB3_RPC_42161: process.env.WEB3_RPC_42161!, + WEB3_RPC_421611: process.env.WEB3_RPC_421611!, + WEB3_RPC_137: process.env.WEB3_RPC_137!, + WEB3_RPC_80001: process.env.WEB3_RPC_80001!, + }, provisionedConcurrency: process.env.PROVISION_CONCURRENCY ? parseInt(process.env.PROVISION_CONCURRENCY) : 0, throttlingOverride: process.env.THROTTLE_PER_FIVE_MINS, ethGasStationInfoUrl: process.env.ETH_GAS_STATION_INFO_URL!, diff --git a/bin/stacks/routing-api-stack.ts b/bin/stacks/routing-api-stack.ts index d2c1b42aee..b763aae5a8 100644 --- a/bin/stacks/routing-api-stack.ts +++ b/bin/stacks/routing-api-stack.ts @@ -20,8 +20,7 @@ export class RoutingAPIStack extends cdk.Stack { parent: Construct, name: string, props: cdk.StackProps & { - providerName: string - projectId: string + jsonRpcProviders: {[chainName: string]:string} provisionedConcurrency: number throttlingOverride?: string ethGasStationInfoUrl: string @@ -36,8 +35,7 @@ export class RoutingAPIStack extends cdk.Stack { super(parent, name, props) const { - providerName, - projectId, + jsonRpcProviders, provisionedConcurrency, throttlingOverride, ethGasStationInfoUrl, @@ -72,8 +70,7 @@ export class RoutingAPIStack extends cdk.Stack { poolCacheBucket, poolCacheBucket2, poolCacheKey, - providerName, - projectId, + jsonRpcProviders, tokenListCacheBucket, provisionedConcurrency, ethGasStationInfoUrl, diff --git a/bin/stacks/routing-lambda-stack.ts b/bin/stacks/routing-lambda-stack.ts index 43b94ed47d..786adb3e9a 100644 --- a/bin/stacks/routing-lambda-stack.ts +++ b/bin/stacks/routing-lambda-stack.ts @@ -15,8 +15,7 @@ export interface RoutingLambdaStackProps extends cdk.NestedStackProps { poolCacheBucket: aws_s3.Bucket poolCacheBucket2: aws_s3.Bucket poolCacheKey: string - providerName: string - projectId: string + jsonRpcProviders: {[chainName: string]:string} tokenListCacheBucket: aws_s3.Bucket provisionedConcurrency: number ethGasStationInfoUrl: string @@ -33,8 +32,7 @@ export class RoutingLambdaStack extends cdk.NestedStack { poolCacheBucket, poolCacheBucket2, poolCacheKey, - providerName, - projectId, + jsonRpcProviders, tokenListCacheBucket, provisionedConcurrency, ethGasStationInfoUrl, @@ -75,8 +73,7 @@ export class RoutingLambdaStack extends cdk.NestedStack { POOL_CACHE_KEY: poolCacheKey, TOKEN_LIST_CACHE_BUCKET: tokenListCacheBucket.bucketName, ETH_GAS_STATION_INFO_URL: ethGasStationInfoUrl, - PROVIDER_NAME: providerName, - PROJECT_ID: projectId, + ...jsonRpcProviders }, layers: [ aws_lambda.LayerVersion.fromLayerVersionArn( @@ -106,10 +103,9 @@ export class RoutingLambdaStack extends cdk.NestedStack { POOL_CACHE_BUCKET: poolCacheBucket.bucketName, POOL_CACHE_BUCKET_2: poolCacheBucket2.bucketName, POOL_CACHE_KEY: poolCacheKey, - PROVIDER_NAME: providerName, - PROJECT_ID: projectId, TOKEN_LIST_CACHE_BUCKET: tokenListCacheBucket.bucketName, ETH_GAS_STATION_INFO_URL: ethGasStationInfoUrl, + ...jsonRpcProviders, }, layers: [ aws_lambda.LayerVersion.fromLayerVersionArn( diff --git a/lib/handlers/injector-sor.ts b/lib/handlers/injector-sor.ts index 574d2022b9..795ff1a18c 100644 --- a/lib/handlers/injector-sor.ts +++ b/lib/handlers/injector-sor.ts @@ -6,7 +6,6 @@ import { CachingV3PoolProvider, ChainId, EIP1559GasPriceProvider, - ID_TO_NETWORK_NAME, IGasPriceProvider, IMetric, ITokenListProvider, @@ -107,44 +106,11 @@ export abstract class InjectorSOR extends Injector< const dependenciesByChainArray = await Promise.all( _.map(SUPPORTED_CHAINS, async (chainId: ChainId) => { - const providerName = process.env.PROVIDER_NAME! - const projectId = process.env.PROJECT_ID - const chainName = ID_TO_NETWORK_NAME(chainId) - let url = ""; - switch(providerName) { - case('infura'): - url = `https://${chainName}.infura.io/v3/${projectId}` - break - case('alchemy'): - switch(chainId) { - // Eth RPC format - case(ChainId.MAINNET): - case(ChainId.GĂ–RLI): - url = `https://eth-${chainName}.alchemyapi.io/v2/${projectId}` - break - // Polygon RPC format - case(ChainId.POLYGON): - case(ChainId.POLYGON_MUMBAI): - url = `https://${chainName}.g.alchemy.com/v2/${projectId}` - break - case(ChainId.ARBITRUM_ONE): - url = `https://arb-mainnet.g.alchemy.com/v2/${projectId}` - break - case(ChainId.ARBITRUM_RINKEBY): - url = `https://arb-rinkeby.g.alchemy.com/v2/${projectId}` - break - case(ChainId.OPTIMISM): - url = `https://opt-mainnet.g.alchemy.com/v2/${projectId}` - break - case(ChainId.OPTIMISTIC_KOVAN): - url = `https://opt-kovan.g.alchemy.com/v2/${projectId}` - break - default: - log.fatal({provider: 'alchemy', chainId: chainId},`RPC url could not be generated. Chain not supported for the specified provider.`) - } - break - default: - log.fatal({provider: providerName, chainId: chainId},`Web3 provider not supported.`) + const url = process.env[`WEB3_RPC_${chainId.toString()}`]! + log.info({url:url}, `GENERATED URL`) + + if(typeof(url)==undefined) { + log.fatal({'chainId':chainId}, `Fatal: No Web3 RPC endpoint set for chain`) } let timeout: number