Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
raghava-pamula committed Jun 3, 2022
1 parent 61eb791 commit 27aa63d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 67 deletions.
60 changes: 46 additions & 14 deletions bin/app.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -33,8 +34,7 @@ export class RoutingAPIStage extends Stage {
) {
super(scope, id, props)
const {
providerName,
projectId,
jsonRpcProviders,
provisionedConcurrency,
ethGasStationInfoUrl,
chatbotSNSArn,
Expand All @@ -46,8 +46,7 @@ export class RoutingAPIStage extends Stage {
} = props

const { url } = new RoutingAPIStack(this, 'RoutingAPI', {
providerName,
projectId,
jsonRpcProviders,
provisionedConcurrency,
ethGasStationInfoUrl,
chatbotSNSArn,
Expand Down Expand Up @@ -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', {
Expand All @@ -119,18 +118,25 @@ 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,
route53Arn: route53Arn.secretValueFromJson('arn').toString(),
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)
Expand All @@ -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',
Expand Down Expand Up @@ -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!,
Expand Down
9 changes: 3 additions & 6 deletions bin/stacks/routing-api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,8 +35,7 @@ export class RoutingAPIStack extends cdk.Stack {
super(parent, name, props)

const {
providerName,
projectId,
jsonRpcProviders,
provisionedConcurrency,
throttlingOverride,
ethGasStationInfoUrl,
Expand Down Expand Up @@ -72,8 +70,7 @@ export class RoutingAPIStack extends cdk.Stack {
poolCacheBucket,
poolCacheBucket2,
poolCacheKey,
providerName,
projectId,
jsonRpcProviders,
tokenListCacheBucket,
provisionedConcurrency,
ethGasStationInfoUrl,
Expand Down
12 changes: 4 additions & 8 deletions bin/stacks/routing-lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,8 +32,7 @@ export class RoutingLambdaStack extends cdk.NestedStack {
poolCacheBucket,
poolCacheBucket2,
poolCacheKey,
providerName,
projectId,
jsonRpcProviders,
tokenListCacheBucket,
provisionedConcurrency,
ethGasStationInfoUrl,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
44 changes: 5 additions & 39 deletions lib/handlers/injector-sor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
CachingV3PoolProvider,
ChainId,
EIP1559GasPriceProvider,
ID_TO_NETWORK_NAME,
IGasPriceProvider,
IMetric,
ITokenListProvider,
Expand Down Expand Up @@ -107,44 +106,11 @@ export abstract class InjectorSOR<Router, QueryParams> 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
Expand Down

0 comments on commit 27aa63d

Please sign in to comment.