Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use backend-tools #50

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
},
"dependencies": {
"@byor/shared": "*",
"@l2beat/backend-tools": "^0.4.0",
"@trpc/server": "^10.28.1",
"@types/lodash": "^4.14.194",
"abitype": "^0.8.4",
"chalk": "^4.1.2",
"cmd-ts": "^0.12.1",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.25.4",
"lodash": "^4.17.21",
"postgres": "^3.3.5",
Expand Down
11 changes: 7 additions & 4 deletions packages/node/src/Application.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger } from '@l2beat/backend-tools'
import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

Expand All @@ -19,14 +20,16 @@ import { Database } from './peripherals/database/shared/Database'
import { TransactionRepository } from './peripherals/database/TransactionRepository'
import { EthereumPrivateClient } from './peripherals/ethereum/EthereumPrivateClient'
import { Mempool } from './peripherals/mempool/Mempool'
import { LogLevel } from './tools/ILogger'
import { Logger } from './tools/Logger'

export class Application {
start: () => Promise<void>

constructor(config: Config) {
const logger = new Logger({ logLevel: LogLevel.DEBUG, format: 'pretty' })
const logger = new Logger({
logLevel: 'DEBUG',
format: 'pretty',
colors: true,
})

const database = new Database(
config.database.connection,
Expand Down Expand Up @@ -98,7 +101,7 @@ export class Application {
const apiServer = new ApiServer(config.apiPort, logger, routers)

this.start = async (): Promise<void> => {
logger.info('Starting...')
logger.for(this).info('Starting...')

await database.migrate()
await batchDownloader.start()
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/api/ApiServer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Logger } from '@l2beat/backend-tools'
import { createHTTPServer } from '@trpc/server/adapters/standalone'
import cors from 'cors'
import http from 'http'

import { Logger } from '../tools/Logger'
import { AppRouters, makeRouter, RootRouter } from './types/AppRouter'

export class ApiServer {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/api/routers/test/createTestApiServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from '@l2beat/backend-tools'
import { ProcedureRouterRecord } from '@trpc/server'
import { agent, SuperAgentTest } from 'supertest'

import { Logger } from '../../../tools/Logger'
import { ApiServer } from '../../ApiServer'

export function createTestApiServer(
Expand Down
11 changes: 6 additions & 5 deletions packages/node/src/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { EthereumAddress, Hex, localNetwork } from '@byor/shared'
import { config as dotenv } from 'dotenv'
import { getEnv } from '@l2beat/backend-tools'

import { Config } from './Config'
import GENESIS_STATE from './genesis.json'

export function getLocalConfig(): Config {
dotenv()
const env = getEnv()

return {
chain: localNetwork,
rpcUrl: process.env.LOCAL_RPC_URL ?? 'http://127.0.0.1:8545',
rpcUrl: env.string('LOCAL_RPC_URL', 'http://127.0.0.1:8545'),
contractAddress: EthereumAddress(
'0x5FbDB2315678afecb367f032d93F642f64180aa3',
),
Expand All @@ -28,9 +28,10 @@ export function getLocalConfig(): Config {
),
genesisState: GENESIS_STATE,
database: {
connection:
process.env.LOCAL_DB_URL ??
connection: env.string(
'LOCAL_DB_URL',
'postgresql://postgres:password@localhost:5432/byor_local',
),
migrationPath: 'drizzle',
isProduction: false,
},
Expand Down
13 changes: 6 additions & 7 deletions packages/node/src/config/config.production.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { EthereumAddress, Hex } from '@byor/shared'
import { config as dotenv } from 'dotenv'
import { getEnv } from '@l2beat/backend-tools'
import { holesky } from 'viem/chains'

import { Config } from './Config'
import GENESIS_STATE from './genesis.json'
import { getEnv } from './getEnv'

export function getProductionConfig(): Config {
dotenv()
const env = getEnv()

return {
chain: {
Expand All @@ -21,7 +20,7 @@ export function getProductionConfig(): Config {
},
},
},
rpcUrl: getEnv('RPC_URL'),
rpcUrl: env.string('RPC_URL'),
contractAddress: EthereumAddress(
'0x1c292ae278dCf230e9D31F39F3c1b088f5d72ca0',
),
Expand All @@ -35,13 +34,13 @@ export function getProductionConfig(): Config {
intervalMs: 10_000,
gasLimit: 3_000_000,
},
privateKey: Hex(getEnv('PRIVATE_KEY')),
privateKey: Hex(env.string('PRIVATE_KEY')),
genesisState: GENESIS_STATE,
database: {
connection: getEnv('DATABASE_URL'),
connection: env.string('DATABASE_URL'),
migrationPath: 'drizzle',
isProduction: true,
},
apiPort: parseInt(getEnv('PORT')),
apiPort: env.integer('PORT'),
}
}
22 changes: 0 additions & 22 deletions packages/node/src/config/getEnv.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/node/src/config/getEnv.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/node/src/core/BatchDownloader.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EthereumAddress, Hex } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { expect, mockFn, mockObject } from 'earl'
import { parseAbiItem } from 'viem'

import { FetcherRepository } from '../peripherals/database/FetcherRepository'
import { EthereumClient } from '../peripherals/ethereum/EthereumClient'
import { Logger } from '../tools/Logger'
import { BatchDownloader } from './BatchDownloader'

describe(BatchDownloader.name, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/core/BatchDownloader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, EthereumAddress, Hex } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { zipWith } from 'lodash'
import { decodeFunctionData, GetLogsReturnType, parseAbiItem } from 'viem'

Expand All @@ -7,7 +8,6 @@ import {
FetcherRepository,
} from '../peripherals/database/FetcherRepository'
import { EthereumClient } from '../peripherals/ethereum/EthereumClient'
import { Logger } from '../tools/Logger'
import { abi } from './abi'

export interface Batch {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/core/BatchPoster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
Transaction,
Unsigned64,
} from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { install, InstalledClock } from '@sinonjs/fake-timers'
import { expect, mockFn, mockObject } from 'earl'
import { privateKeyToAccount } from 'viem/accounts'

import { EthereumPrivateClient } from '../peripherals/ethereum/EthereumPrivateClient'
import { Mempool } from '../peripherals/mempool/Mempool'
import { Logger } from '../tools/Logger'
import { BatchPoster } from './BatchPoster'
import { StateUpdater } from './StateUpdater'

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/core/BatchPoster.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { serializeBatch, unreachableCodePath } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'

import { EthereumPrivateClient } from '../peripherals/ethereum/EthereumPrivateClient'
import { Mempool } from '../peripherals/mempool/Mempool'
import { setIntervalAsync } from '../tools/asyncTimeUtils'
import { Logger } from '../tools/Logger'
import { filterValidTxs } from './executeBatch'
import { StateUpdater } from './StateUpdater'

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/core/GenesisStateLoader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EthereumAddress, Unsigned64 } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'

import {
AccountRecord,
AccountRepository,
} from '../peripherals/database/AccountRepository'
import { Logger } from '../tools/Logger'

export class GenesisStateLoader {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/core/StateUpdater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
Transaction,
Unsigned64,
} from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { expect, mockFn, mockObject } from 'earl'
import { privateKeyToAccount } from 'viem/accounts'

import { AccountRepository } from '../peripherals/database/AccountRepository'
import { TransactionRepository } from '../peripherals/database/TransactionRepository'
import { Logger } from '../tools/Logger'
import { BatchDownloader } from './BatchDownloader'
import { StateUpdater } from './StateUpdater'

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/core/StateUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
EthereumAddress,
unreachableCodePath,
} from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { zip } from 'lodash'

import { AccountRepository } from '../peripherals/database/AccountRepository'
Expand All @@ -11,7 +12,6 @@ import {
TransactionRepository,
} from '../peripherals/database/TransactionRepository'
import { setIntervalAsync } from '../tools/asyncTimeUtils'
import { Logger } from '../tools/Logger'
import { Batch, BatchDownloader } from './BatchDownloader'
import { executeBatch, StateMap } from './executeBatch'

Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/peripherals/database/shared/Database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Logger } from '@l2beat/backend-tools'
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import postgres from 'postgres'

import { Logger } from '../../../tools/Logger'

export class Database {
private readonly client: postgres.Sql
private readonly drizzle: PostgresJsDatabase
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/peripherals/database/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Logger } from '../../../tools/Logger'
import { Logger } from '@l2beat/backend-tools'

import { Database } from '../shared/Database'

export function setupDatabaseTestSuite(): Database {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EthereumAddress, Hex } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { expect, mockFn, mockObject } from 'earl'
import { parseAbiItem, PublicClient } from 'viem'

import { Logger } from '../../tools/Logger'
import { EthereumClient } from './EthereumClient'

describe(EthereumClient.name, () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/peripherals/ethereum/EthereumClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EthereumAddress, Hex } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { AbiEvent } from 'abitype'
import {
GetBlockReturnType,
Expand All @@ -7,8 +8,6 @@ import {
Transaction,
} from 'viem'

import { Logger } from '../../tools/Logger'

export class EthereumClient {
constructor(
private readonly publicProvider: PublicClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EthereumAddress, Hex } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { expect, mockFn, mockObject } from 'earl'
import { Account, Chain, PublicClient, WalletClient } from 'viem'

import { abi } from '../../core/abi'
import { Logger } from '../../tools/Logger'
import { EthereumPrivateClient } from './EthereumPrivateClient'

describe(EthereumPrivateClient.name, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EthereumAddress, Hex } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { PublicClient, WalletClient } from 'viem'

import { abi } from '../../core/abi'
import { Logger } from '../../tools/Logger'
import { EthereumClient } from './EthereumClient'

export class EthereumPrivateClient extends EthereumClient {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/peripherals/mempool/Mempool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
Unsigned8,
Unsigned64,
} from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { install, InstalledClock } from '@sinonjs/fake-timers'
import { expect } from 'earl'
import { privateKeyToAccount } from 'viem/accounts'

import { Logger } from '../../tools/Logger'
import { Mempool } from './Mempool'

describe(Mempool.name, () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/peripherals/mempool/Mempool.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { assert, hashTransaction, Hex, SignedTransaction } from '@byor/shared'
import { Logger } from '@l2beat/backend-tools'
import { zip } from 'lodash'

import { Logger } from '../../tools/Logger'

export class Mempool {
private pool: SignedTransaction[]
private poolTimestamps: number[]
Expand Down
33 changes: 0 additions & 33 deletions packages/node/src/tools/ILogger.ts

This file was deleted.

Loading