Skip to content

Commit

Permalink
chore(indexer): sentry middleware and app config
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Aug 21, 2024
1 parent f7dda4e commit 4df1e4b
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 18 deletions.
2 changes: 2 additions & 0 deletions apps/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"dependencies": {
"@dfuse/client": "^0.3.21",
"@repo/supabase": "workspace:*",
"@sentry/integrations": "^7.114.0",
"@sentry/node": "^8.19.0",
"@sentry/profiling-node": "^8.26.0",
"@supabase/supabase-js": "^2.44.4",
"@types/pino-http": "^5.8.4",
"app-contracts": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { smartsaleEnv } from 'app-env'

export const appenv = {
export const appConfig = {
sentry: {
dsn: process.env.SENTRY_DSN || '',
},
Expand Down
5 changes: 3 additions & 2 deletions apps/indexer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { getErrorMessage } from 'app-lib'
import { startExpress } from './routes/index'
import { logger } from './lib/logger'

async function main() {
console.log(`Launchpad indexer starting up ...`)
logger.info(`Launchpad indexer starting up ...`)
try {
startExpress()
// startPresaleService()
// startSwapsService()
// startAuctionIndexer()
} catch (error) {
console.log('ERROR:' + getErrorMessage(error), JSON.stringify(error))
logger.error(getErrorMessage(error))
}
}

Expand Down
4 changes: 2 additions & 2 deletions apps/indexer/src/lib/dfuse-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { GraphqlStreamMessage, WebSocketFactory, createDfuseClient } from '@dfuse/client'
import { appenv } from '~/config'
import { appConfig } from '~/config'
import nodeFetch from 'node-fetch'
import WebSocketClient from 'ws'
import EventEmitter from 'events'
import { IncomingMessage } from 'http'

export const dfuse = createDfuseClient({
apiKey: appenv.eos.dfuseKey,
apiKey: appConfig.eos.dfuseKey,
network: 'eos.dfuse.eosnation.io',
// authentication: true,
httpClientOptions: {
Expand Down
6 changes: 3 additions & 3 deletions apps/indexer/src/lib/issuer-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { http, createPublicClient, PublicClient, createWalletClient } from 'viem'
import { appenv } from '../config'
import { appConfig } from '../config'
import { eosEvmTestnet } from 'app-env'

export const walletClient = createWalletClient({
chain: eosEvmTestnet,
transport: http(),
key: appenv.evm.issuerKey,
account: appenv.evm.issuerAccount,
key: appConfig.evm.issuerKey,
account: appConfig.evm.issuerAccount,
})

export async function getCurrentBlockHeight() {
Expand Down
29 changes: 29 additions & 0 deletions apps/indexer/src/lib/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as Sentry from "@sentry/node"
import { nodeProfilingIntegration } from "@sentry/profiling-node"
import { Express } from "express"
import { appConfig } from "~/config";

export function initSentry() {
// Ensure to call this before requiring any other modules!
Sentry.init({
dsn: appConfig.sentry.dsn,
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],

// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: 1.0,

// Set sampling rate for profiling
// This is relative to tracesSampleRate
profilesSampleRate: 1.0,
})
}

export function setupSentryErrorHandler(app: Express) {
// The error handler must be before any other error middleware and after all controllers
Sentry.setupExpressErrorHandler(app);
}

4 changes: 2 additions & 2 deletions apps/indexer/src/lib/supabase-client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createClient } from '@supabase/supabase-js'
import { Database, TablesInsert } from '@repo/supabase'
import { appenv } from '~/config'
import { appConfig } from '~/config'

// Initialize Supabase client
const supabase = createClient<Database>(appenv.supabase.url, appenv.supabase.anonKey)
const supabase = createClient<Database>(appConfig.supabase.url, appConfig.supabase.anonKey)

export async function upsertAuctionDetail(data: TablesInsert<'auction'>) {
const { data: result, error } = await supabase.from('auction').upsert(
Expand Down
6 changes: 3 additions & 3 deletions apps/indexer/src/modules/presale/presale-issuer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestnetBLPL } from 'app-contracts'
import { eosEvmTestnet } from 'app-env'
import { createWalletClient } from 'viem'
import { appenv } from '~/config'
import { appConfig } from '~/config'
import { Address, http } from 'viem'

export async function issuePresaleTokens(to: Address, amount: bigint) {
Expand All @@ -13,8 +13,8 @@ export async function issuePresaleTokens(to: Address, amount: bigint) {
const walletClient = createWalletClient({
chain: eosEvmTestnet,
transport: http(),
key: appenv.evm.issuerKey,
account: appenv.evm.issuerAccount,
key: appConfig.evm.issuerKey,
account: appConfig.evm.issuerAccount,
})
return walletClient.writeContract({
address: TestnetBLPL.address,
Expand Down
6 changes: 3 additions & 3 deletions apps/indexer/src/modules/swaps/cred-issuer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestnetUSDCred } from 'app-contracts'
import { eosEvmTestnet } from 'app-env'
import { createWalletClient } from 'viem'
import { appenv } from '~/config'
import { appConfig } from '~/config'
import { Address, http } from 'viem'

export async function issueTokens(to: Address, amount: bigint) {
Expand All @@ -13,8 +13,8 @@ export async function issueTokens(to: Address, amount: bigint) {
const walletClient = createWalletClient({
chain: eosEvmTestnet,
transport: http(),
key: appenv.evm.issuerKey,
account: appenv.evm.issuerAccount,
key: appConfig.evm.issuerKey,
account: appConfig.evm.issuerAccount,
})
return walletClient.writeContract({
address: TestnetUSDCred.address,
Expand Down
4 changes: 2 additions & 2 deletions apps/indexer/src/routes/alchemy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Request, Response } from 'express'
import crypto from 'crypto'
import { appenv } from '../config'
import { appConfig } from '../config'
import { logger } from '~/lib/logger'

function validateAlchemySignature(req: Request): boolean {
const alchemySignature = req.headers['x-alchemy-signature'] as string
const payload = JSON.stringify(req.body)
const hmac = crypto.createHmac('sha256', appenv.evm.alchemySecretKey)
const hmac = crypto.createHmac('sha256', appConfig.evm.alchemySecretKey)
hmac.update(payload)
return alchemySignature === hmac.digest('hex')
}
Expand Down
4 changes: 4 additions & 0 deletions apps/indexer/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { healthcheck } from './healthcheck'
import { alchemyWebhook } from './alchemy'
import pinoHttp from 'pino-http'
import { logger } from '~/lib/logger'
import {setupSentryErrorHandler} from '~/lib/sentry';


export function startExpress() {
const app = express()
const port = 8080

// Sentry error handler
setupSentryErrorHandler(app)

// Security Middlewares
app.use(helmet())

Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 4df1e4b

Please sign in to comment.