forked from balancer/balancer-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
236 lines (204 loc) · 7.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require("dotenv").config();
import { DomainName, IResource, LambdaIntegration, MockIntegration, PassthroughBehavior, RestApi } from '@aws-cdk/aws-apigateway';
import { AttributeType, Table } from '@aws-cdk/aws-dynamodb';
import { Runtime } from '@aws-cdk/aws-lambda';
import { App, Stack, RemovalPolicy, Duration } from '@aws-cdk/core';
import { NodejsFunction, NodejsFunctionProps } from '@aws-cdk/aws-lambda-nodejs';
import { Rule, Schedule } from '@aws-cdk/aws-events';
import { LambdaFunction } from '@aws-cdk/aws-events-targets';
import { Certificate } from '@aws-cdk/aws-certificatemanager';
import { join } from 'path'
const {
INFURA_PROJECT_ID,
POOLS_API_DDB_READ_CAPACITY,
POOLS_API_DDB_WRITE_CAPACITY,
DOMAIN_NAME
} = process.env;
const READ_CAPACITY = POOLS_API_DDB_READ_CAPACITY || '10';
const WRITE_CAPACITY = POOLS_API_DDB_WRITE_CAPACITY || '10';
export class BalancerPoolsAPI extends Stack {
constructor(app: App, id: string) {
super(app, id);
/**
* DynamoDB Tables
*/
const poolsTable = new Table(this, 'pools', {
partitionKey: {
name: 'id',
type: AttributeType.STRING
},
sortKey: {
name: 'chainId',
type: AttributeType.NUMBER
},
tableName: 'pools',
removalPolicy: RemovalPolicy.DESTROY,
readCapacity: Number.parseInt(READ_CAPACITY),
writeCapacity: Number.parseInt(WRITE_CAPACITY)
});
const tokensTable = new Table(this, 'tokens', {
partitionKey: {
name: 'address',
type: AttributeType.STRING
},
sortKey: {
name: 'chainId',
type: AttributeType.NUMBER
},
tableName: 'tokens',
removalPolicy: RemovalPolicy.DESTROY,
readCapacity: Number.parseInt(READ_CAPACITY),
writeCapacity: Number.parseInt(WRITE_CAPACITY)
});
/**
* Lambdas
*/
const nodeJsFunctionProps: NodejsFunctionProps = {
bundling: {
externalModules: [
'aws-sdk',
],
},
environment: {
INFURA_PROJECT_ID: INFURA_PROJECT_ID || '',
},
runtime: Runtime.NODEJS_14_X,
timeout: Duration.seconds(15)
}
const getPoolLambda = new NodejsFunction(this, 'getPoolFunction', {
entry: join(__dirname, 'lambdas', 'get-pool.ts'),
...nodeJsFunctionProps,
});
const getPoolsLambda = new NodejsFunction(this, 'getPoolsFunction', {
entry: join(__dirname, 'lambdas', 'get-pools.ts'),
...nodeJsFunctionProps,
});
const getTokensLambda = new NodejsFunction(this, 'getTokensFunction', {
entry: join(__dirname, 'lambdas', 'get-tokens.ts'),
...nodeJsFunctionProps,
});
const runSORLambda = new NodejsFunction(this, 'runSORFunction', {
entry: join(__dirname, 'lambdas', 'run-sor.ts'),
...nodeJsFunctionProps,
memorySize: 2048
});
const updatePoolsLambda = new NodejsFunction(this, 'updatePoolsFunction', {
entry: join(__dirname, 'lambdas', 'update-pools.ts'),
...nodeJsFunctionProps,
memorySize: 2048,
timeout: Duration.seconds(60),
reservedConcurrentExecutions: 1
});
const updateTokenPricesLambda = new NodejsFunction(this, 'updateTokenPricesFunction', {
entry: join(__dirname, 'lambdas', 'update-prices.ts'),
...nodeJsFunctionProps,
memorySize: 512,
timeout: Duration.seconds(60)
});
const sanctionsCheckLambda = new NodejsFunction(this, 'sanctionsCheckFunction', {
entry: join(__dirname, 'lambdas', 'sanctions-check.ts'),
runtime: Runtime.NODEJS_14_X,
timeout: Duration.seconds(15)
});
/**
* Lambda Schedules
*/
const rule = new Rule(this, 'updateTokenPricesEachMinute', {
schedule: Schedule.expression('rate(1 minute)')
});
rule.addTarget(new LambdaFunction(updateTokenPricesLambda))
/**
* Access Rules
*/
poolsTable.grantReadData(getPoolsLambda);
poolsTable.grantReadData(getPoolLambda);
poolsTable.grantReadWriteData(runSORLambda);
poolsTable.grantReadWriteData(updatePoolsLambda);
tokensTable.grantReadData(getTokensLambda);
tokensTable.grantReadWriteData(runSORLambda);
tokensTable.grantReadWriteData(updatePoolsLambda);
tokensTable.grantReadWriteData(updateTokenPricesLambda);
/**
* API Gateway
*/
const getPoolIntegration = new LambdaIntegration(getPoolLambda);
const getPoolsIntegration = new LambdaIntegration(getPoolsLambda);
const getTokensIntegration = new LambdaIntegration(getTokensLambda);
const runSORIntegration = new LambdaIntegration(runSORLambda);
const updatePoolsIntegration = new LambdaIntegration(updatePoolsLambda, {timeout: Duration.seconds(29)});
const updateTokenPricesIntegration = new LambdaIntegration(updateTokenPricesLambda, {timeout: Duration.seconds(29)});
const sanctionsCheckIntegration = new LambdaIntegration(sanctionsCheckLambda);
const api = new RestApi(this, 'poolsApi', {
restApiName: 'Pools Service'
});
const pools = api.root.addResource('pools');
const poolsOnChain = pools.addResource('{chainId}')
poolsOnChain.addMethod('GET', getPoolsIntegration);
addCorsOptions(pools);
const updatePools = poolsOnChain.addResource('update');
updatePools.addMethod('POST', updatePoolsIntegration);
addCorsOptions(updatePools);
const singlePool = poolsOnChain.addResource('{id}');
singlePool.addMethod('GET', getPoolIntegration);
addCorsOptions(singlePool);
const tokens = api.root.addResource('tokens');
const tokensOnChain = tokens.addResource('{chainId}');
tokensOnChain.addMethod('GET', getTokensIntegration);
addCorsOptions(tokens);
const updatePrices = tokens.addResource('update');
updatePrices.addMethod('POST', updateTokenPricesIntegration);
addCorsOptions(updatePrices);
const sor = api.root.addResource('sor');
const sorOnChain = sor.addResource('{chainId}')
sorOnChain.addMethod('POST', runSORIntegration);
addCorsOptions(sor);
const gnosis = api.root.addResource('gnosis');
const gnosisOnChain = gnosis.addResource('{chainId}')
gnosisOnChain.addMethod('POST', runSORIntegration);
addCorsOptions(gnosis);
const sanctionsCheck = api.root.addResource('sanctions-check');
sanctionsCheck.addMethod('POST', sanctionsCheckIntegration);
addCorsOptions(sanctionsCheck);
/**
* Subdomain
*/
if (DOMAIN_NAME) {
const domainName = DOMAIN_NAME;
const domain = new DomainName(this, 'domain-name', {
domainName,
certificate: new Certificate(this, 'Cert', { domainName }),
});
domain.addBasePathMapping(api);
}
}
}
export function addCorsOptions(apiResource: IResource) {
apiResource.addMethod('OPTIONS', new MockIntegration({
integrationResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'",
'method.response.header.Access-Control-Allow-Origin': "'*'",
'method.response.header.Access-Control-Allow-Credentials': "'false'",
'method.response.header.Access-Control-Allow-Methods': "'OPTIONS,GET,PUT,POST,DELETE'",
},
}],
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: {
"application/json": "{\"statusCode\": 200}"
},
}), {
methodResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': true,
'method.response.header.Access-Control-Allow-Methods': true,
'method.response.header.Access-Control-Allow-Credentials': true,
'method.response.header.Access-Control-Allow-Origin': true,
},
}]
})
}
const app = new App();
new BalancerPoolsAPI(app, 'BalancerPoolsAPI');
app.synth();