Skip to content

Commit

Permalink
Create Table for flagging caching requests (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeki authored Aug 19, 2023
1 parent f6086c1 commit f68e1f6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bin/stacks/routing-api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export class RoutingAPIStack extends cdk.Stack {
hosted_zone,
})

const { cachedRoutesDynamoDb, cachedV3PoolsDynamoDb } = new RoutingDatabaseStack(this, 'RoutingDatabaseStack', {})
const { cachedRoutesDynamoDb, cachingRequestFlagDynamoDb, cachedV3PoolsDynamoDb } = new RoutingDatabaseStack(
this,
'RoutingDatabaseStack',
{}
)

const { routingLambda, routingLambdaAlias } = new RoutingLambdaStack(this, 'RoutingLambdaStack', {
poolCacheBucket,
Expand All @@ -92,6 +96,7 @@ export class RoutingAPIStack extends cdk.Stack {
tenderlyProject,
tenderlyAccessKey,
cachedRoutesDynamoDb,
cachingRequestFlagDynamoDb,
cachedV3PoolsDynamoDb,
})

Expand Down
22 changes: 22 additions & 0 deletions bin/stacks/routing-database-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const DynamoDBTableProps = {
PartitionKeyName: 'pairTradeTypeChainId',
SortKeyName: 'protocolsBucketBlockNumber',
},
CachingRequestFlagDynamoDbTable: {
Name: 'CacheReqFlagDB',
PartitionKeyName: 'pairTradeTypeChainId',
SortKeyName: 'protocolsBucketBlockNumber',
},
V3PoolsDynamoDbTable: {
Name: 'V3PoolsCachingDB',
PartitionKeyName: 'poolAddress',
Expand All @@ -21,6 +26,7 @@ export const DynamoDBTableProps = {

export class RoutingDatabaseStack extends cdk.NestedStack {
public readonly cachedRoutesDynamoDb: aws_dynamodb.Table
public readonly cachingRequestFlagDynamoDb: aws_dynamodb.Table
public readonly cachedV3PoolsDynamoDb: aws_dynamodb.Table

constructor(scope: Construct, name: string, props: RoutingDatabaseStackProps) {
Expand All @@ -35,6 +41,22 @@ export class RoutingDatabaseStack extends cdk.NestedStack {
timeToLiveAttribute: DynamoDBTableProps.TTLAttributeName,
})

// Creates a DynamoDB Table for storing the caching request flags
this.cachingRequestFlagDynamoDb = new aws_dynamodb.Table(
this,
DynamoDBTableProps.CachingRequestFlagDynamoDbTable.Name,
{
tableName: DynamoDBTableProps.CachingRequestFlagDynamoDbTable.Name,
partitionKey: {
name: DynamoDBTableProps.CachingRequestFlagDynamoDbTable.PartitionKeyName,
type: AttributeType.STRING,
},
sortKey: { name: DynamoDBTableProps.CachingRequestFlagDynamoDbTable.SortKeyName, type: AttributeType.STRING },
billingMode: BillingMode.PAY_PER_REQUEST,
timeToLiveAttribute: DynamoDBTableProps.TTLAttributeName,
}
)

// Creates a DynamoDB Table for storing the cached v3 pools
this.cachedV3PoolsDynamoDb = new aws_dynamodb.Table(this, DynamoDBTableProps.V3PoolsDynamoDbTable.Name, {
tableName: DynamoDBTableProps.V3PoolsDynamoDbTable.Name,
Expand Down
5 changes: 5 additions & 0 deletions bin/stacks/routing-lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RoutingLambdaStackProps extends cdk.NestedStackProps {
tenderlyAccessKey: string
chatbotSNSArn?: string
cachedRoutesDynamoDb?: aws_dynamodb.Table
cachingRequestFlagDynamoDb?: aws_dynamodb.Table
cachedV3PoolsDynamoDb?: aws_dynamodb.Table
}
export class RoutingLambdaStack extends cdk.NestedStack {
Expand All @@ -48,13 +49,15 @@ export class RoutingLambdaStack extends cdk.NestedStack {
tenderlyProject,
tenderlyAccessKey,
cachedRoutesDynamoDb,
cachingRequestFlagDynamoDb,
cachedV3PoolsDynamoDb,
} = props

const lambdaRole = new aws_iam.Role(this, 'RoutingLambdaRole', {
assumedBy: new aws_iam.ServicePrincipal('lambda.amazonaws.com'),
managedPolicies: [
aws_iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole'),
aws_iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaRole'),
aws_iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchLambdaInsightsExecutionRolePolicy'),
aws_iam.ManagedPolicy.fromAwsManagedPolicyName('AWSXRayDaemonWriteAccess'),
],
Expand All @@ -63,6 +66,7 @@ export class RoutingLambdaStack extends cdk.NestedStack {
poolCacheBucket2.grantRead(lambdaRole)
tokenListCacheBucket.grantRead(lambdaRole)
cachedRoutesDynamoDb?.grantReadWriteData(lambdaRole)
cachingRequestFlagDynamoDb?.grantReadWriteData(lambdaRole)
cachedV3PoolsDynamoDb?.grantReadWriteData(lambdaRole)

const region = cdk.Stack.of(this).region
Expand Down Expand Up @@ -91,6 +95,7 @@ export class RoutingLambdaStack extends cdk.NestedStack {
TENDERLY_PROJECT: tenderlyProject,
TENDERLY_ACCESS_KEY: tenderlyAccessKey,
CACHED_ROUTES_TABLE_NAME: DynamoDBTableProps.CacheRouteDynamoDbTable.Name,
CACHING_REQUEST_FLAG_TABLE_NAME: DynamoDBTableProps.CachingRequestFlagDynamoDbTable.Name,
CACHED_V3_POOLS_TABLE_NAME: DynamoDBTableProps.V3PoolsDynamoDbTable.Name,
...jsonRpcProviders,
},
Expand Down

0 comments on commit f68e1f6

Please sign in to comment.