Skip to content

Commit

Permalink
merge canary
Browse files Browse the repository at this point in the history
  • Loading branch information
franzns committed Dec 7, 2023
2 parents 034549f + 93ed17c commit d17544d
Show file tree
Hide file tree
Showing 40 changed files with 1,761 additions and 327 deletions.
11 changes: 11 additions & 0 deletions .platform/confighooks/prebuild/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch .env
chown webapp:webapp .env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done > .env
20 changes: 20 additions & 0 deletions .platform/confighooks/predeploy/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
# In case we merge two env secrets into a flattened one in the same region
# secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

# IFS=$'\n' read -r -d '' -a key_value_pairs <<< "$(echo $secrets | jq -r --arg prefix "$PREFIX" 'to_entries[] | select((.key | ascii_upcase) | startswith(($prefix+"_") | ascii_upcase)) | ((.key | sub(($prefix+"_"); ""; "i")) + "=" + .value)')"

# for pair in "${key_value_pairs[@]}"; do
# echo "$pair"
# done

secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch /var/app/staging/.env
chown webapp:webapp /var/app/staging/.env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done >> /var/app/staging/.env
11 changes: 11 additions & 0 deletions .platform/hooks/prebuild/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch .env
chown webapp:webapp .env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done > .env
20 changes: 20 additions & 0 deletions .platform/hooks/predeploy/env-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
# In case we merge two env secrets into a flattened one in the same region
# secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

# IFS=$'\n' read -r -d '' -a key_value_pairs <<< "$(echo $secrets | jq -r --arg prefix "$PREFIX" 'to_entries[] | select((.key | ascii_upcase) | startswith(($prefix+"_") | ascii_upcase)) | ((.key | sub(($prefix+"_"); ""; "i")) + "=" + .value)')"

# for pair in "${key_value_pairs[@]}"; do
# echo "$pair"
# done

secrets=$(aws secretsmanager get-secret-value --secret-id api-secrets --region $AWS_REGION | jq -r '.SecretString')

IFS=$'\n' read -r -d '' -a keys <<< "$(echo $secrets | jq -r 'keys[]')"

touch /var/app/staging/.env
chown webapp:webapp /var/app/staging/.env
for key in "${keys[@]}"; do
value=$(echo $secrets | jq -r ".\"$key\"")
printf "%s=%s\n" "$key" "$value"
done >> /var/app/staging/.env
20 changes: 7 additions & 13 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { loadRestRoutesBeethoven } from './modules/beethoven/loadRestRoutes';
import { loadRestRoutesBalancer } from './modules/balancer/loadRestRoutes';
import { loadRestRoutes } from './modules/common/loadRestRoutes';
import { env } from './app/env';
import createExpressApp from 'express';
import { corsMiddleware } from './app/middleware/corsMiddleware';
Expand All @@ -12,10 +11,8 @@ import {
ApolloServerPluginLandingPageGraphQLPlayground,
ApolloServerPluginUsageReporting,
} from 'apollo-server-core';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import { beethovenSchema } from './graphql_schema_generated_beethoven';
import { balancerSchema } from './graphql_schema_generated_balancer';
import { balancerResolvers, beethovenResolvers } from './app/gql/resolvers';
import { schema } from './graphql_schema_generated';
import { resolvers } from './app/gql/resolvers';
import helmet from 'helmet';
import GraphQLJSON from 'graphql-type-json';
import * as Sentry from '@sentry/node';
Expand All @@ -31,6 +28,7 @@ async function startServer() {
// tracesSampleRate: 0.005,
environment: `multichain-${env.DEPLOYMENT_ENV}`,
enabled: env.NODE_ENV === 'production',
ignoreErrors: [/.*error: Provide.*chain.*param/],
integrations: [
// new Tracing.Integrations.Apollo(),
// new Tracing.Integrations.GraphQL(),
Expand Down Expand Up @@ -76,11 +74,7 @@ async function startServer() {
app.use(contextMiddleware);
app.use(sessionMiddleware);

if (env.PROTOCOL === 'beethoven') {
loadRestRoutesBeethoven(app);
} else if (env.PROTOCOL === 'balancer') {
loadRestRoutesBalancer(app);
}
loadRestRoutes(app);

const httpServer = http.createServer(app);

Expand All @@ -103,9 +97,9 @@ async function startServer() {
const server = new ApolloServer({
resolvers: {
JSON: GraphQLJSON,
...(env.PROTOCOL === 'beethoven' ? beethovenResolvers : balancerResolvers),
...resolvers,
},
typeDefs: env.PROTOCOL === 'beethoven' ? beethovenSchema : balancerSchema,
typeDefs: schema,
introspection: true,
plugins,
context: ({ req }) => req.context,
Expand Down
15 changes: 11 additions & 4 deletions app/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const schema = {
SENTRY_DSN: String,
SENTRY_AUTH_TOKEN: String,
AWS_REGION: String,
PROTOCOL: String,
PROTOCOL: {
optional: true,
type: String,
},
INFURA_API_KEY: {
optional: true,
type: String,
Expand All @@ -30,10 +33,14 @@ export const schema = {
optional: true,
type: String,
},
WORKER_QUEUE_URL: String,
WORKER_QUEUE_URL: {
optional: true,
type: String,
},
DATABASE_URL: String,
};

export const env: Env = load(schema, {
path: resolve(__dirname, '../.env'),
overrideProcessEnv: process.env.NODE_ENV !== 'production',
path: resolve(__dirname, `../../.env`),
overrideProcessEnv: true,
});
8 changes: 2 additions & 6 deletions app/gql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ import { loadFilesSync } from '@graphql-tools/load-files';
import path from 'path';
import { mergeResolvers } from '@graphql-tools/merge';

const balancerResolversArray = loadFilesSync(path.join(__dirname, '../../modules/!(beethoven)/**/*.resolvers.*'));

const beethovenResolversArray = loadFilesSync(path.join(__dirname, '../../modules/!(balancer)/**/*.resolvers.*'));

export const balancerResolvers = mergeResolvers(balancerResolversArray);
export const beethovenResolvers = mergeResolvers(beethovenResolversArray);
const resolversArray = loadFilesSync(path.join(__dirname, '../../modules/**/*.resolvers.*'));
export const resolvers = mergeResolvers(resolversArray);
4 changes: 4 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ env:
VEBALLOCKS_SUBGRAPH: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-gauges'
phases:
install:
runtime-versions:
nodejs: 18
commands:
- yarn install
build:
Expand All @@ -23,6 +25,8 @@ phases:
- yarn build
post_build:
commands:
- rm -fr node_modules
- yarn install --prod
- echo Build completed on `date`
# - echo Pushing to graph cdn
# - npx graphcdn push
Expand Down
24 changes: 3 additions & 21 deletions codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,13 @@ generates:
schema: ${USER_SNAPSHOT_SUBGRAPH}
plugins:
- schema-ast
graphql_schema_generated_balancer.ts:
schema:
- ./modules/**/*.gql
- '!./modules/beethoven/beets.gql'
- '!./modules/**/*.beets.gql'
plugins:
- add:
content: |
import { gql } from 'apollo-server-express';
export const balancerSchema = gql`
#\n# THIS FILE IS AUTOGENERATED — DO NOT EDIT IT\n#
- schema-ast
- add:
placement: 'append'
content: '`;'
graphql_schema_generated_beethoven.ts:
schema:
- ./modules/**/*.gql
- '!./modules/balancer/balancer.gql'
- '!./modules/**/*.balancer.gql'
graphql_schema_generated.ts:
schema: ./modules/**/*.gql
plugins:
- add:
content: |
import { gql } from 'apollo-server-express';
export const beethovenSchema = gql`
export const schema = gql`
#\n# THIS FILE IS AUTOGENERATED — DO NOT EDIT IT\n#
- schema-ast
- add:
Expand Down
5 changes: 0 additions & 5 deletions modules/balancer/loadRestRoutes.ts

This file was deleted.

12 changes: 7 additions & 5 deletions modules/coingecko/coingecko.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ interface CoinId {
that happen.
*/
const tokensPerInterval = env.COINGECKO_API_KEY ? ((env.DEPLOYMENT_ENV as DeploymentEnv) === 'main' ? 10 : 5) : 3;
const requestRateLimiter = new RateLimiter({ tokensPerInterval, interval: 'minute' });
const tokensPerMinute = env.COINGECKO_API_KEY ? 10 : 3;
const requestRateLimiter = new RateLimiter({ tokensPerInterval: tokensPerMinute, interval: 'minute' });
//max 10 addresses per request because of URI size limit, pro is max 180 because of URI limit
const addressChunkSize = env.COINGECKO_API_KEY ? 180 : 20;

export class CoingeckoService {
private readonly baseUrl: string;
Expand Down Expand Up @@ -104,10 +106,10 @@ export class CoingeckoService {
* Rate limit for the CoinGecko API is 10 calls each second per IP address.
*/
public async getTokenPrices(addresses: string[]): Promise<TokenPrices> {
//max 180 addresses per request because of URI size limit
const addressesPerRequest = 180;
const addressesPerRequest = addressChunkSize;
try {
if (addresses.length / addressesPerRequest > 10) throw new Error('Too many requests for rate limit.');
// if (addresses.length / addressesPerRequest > tokensPerMinute)
// throw new Error('Too many requests for rate limit.');

const tokenDefinitions = await tokenService.getTokenDefinitions([networkContext.chain]);
const mapped = addresses.map((address) => this.getMappedTokenDetails(address, tokenDefinitions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { Express } from 'express';
import { beetsGetCirculatingSupply } from '../beets/lib/beets';
import { tokenService } from '../token/token.service';

export function loadRestRoutesBeethoven(app: Express) {
export function loadRestRoutes(app: Express) {
app.use('/health', (req, res) => res.sendStatus(200));
app.use('/circulating_supply', (req, res) => {
beetsGetCirculatingSupply().then((result) => {
res.send(result);
});
});

app.use('/late-quartet', async (req, res) => {
const tokenPrices = await tokenService.getTokenPrices();
// app.use('/late-quartet', async (req, res) => {
// const tokenPrices = await tokenService.getTokenPrices();

res.send({
bptPrice: tokenService.getPriceForToken(tokenPrices, '0xf3a602d30dcb723a74a0198313a7551feaca7dac'),
});
});
// res.send({
// bptPrice: tokenService.getPriceForToken(tokenPrices, '0xf3a602d30dcb723a74a0198313a7551feaca7dac'),
// });
// });
}
63 changes: 63 additions & 0 deletions modules/metrics/cron-duration-metrics.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
CloudWatchClient,
CloudWatchClientConfig,
MetricDatum,
PutMetricDataCommand,
} from '@aws-sdk/client-cloudwatch';
import { env } from '../../app/env';

export interface NotificationsCloudwatchMetric {
merticData: MetricDatum[];
nameSpace: string;
}

export class CloudwatchCronDurationMetricsPublisher {
protected environment: string;
protected configuration?: CloudWatchClientConfig;
private client: CloudWatchClient;
public namespace: string;

constructor(
namespace: string = 'default',
environment: string = env.DEPLOYMENT_ENV,
configuration?: CloudWatchClientConfig,
) {
this.namespace = namespace;
this.environment = environment;
this.client = new CloudWatchClient({ region: env.AWS_REGION, ...configuration });
}

public async publish(metricName: string, count?: number): Promise<void> {
const [major, minor, patch] = process.versions.node.split('.').map(Number);
try {
const command = new PutMetricDataCommand({
MetricData: [
{
MetricName: metricName,
Dimensions: [
{
Name: 'Environment',
Value: this.environment,
},
{
Name: 'NodeVersion',
Value: major.toString(),
},
],
Unit: 'None',
Timestamp: new Date(),
Value: count ? count : 1,
},
],
Namespace: this.namespace,
});

await this.client.send(command);
} catch (err) {
// Sentry.captureException(`Failed to publish "${metricName}" in "${this.environment}: ${err}`);
console.log(`Failed to publish "${metricName}" in "${this.environment}: ${err}`);
}
}
}

export const cronsDurationMetricPublisher = new CloudwatchCronDurationMetricsPublisher('backendCronsDuration');
Loading

0 comments on commit d17544d

Please sign in to comment.