Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
noahlitvin committed Jul 31, 2024
1 parent 61560df commit e20ddb9
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 52 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"main": "index.js",
"scripts": {
"test": "pnpm run test --recursive",
"dev:protocol": "pnpm --filter protocol run update-abis && pnpm --filter protocol run dev",
"dev:app": "pnpm --filter protocol run update-abis && pnpm --filter app run dev",
"dev:docs": "pnpm --filter protocol run update-abis && pnpm --filter docs run dev",
"dev:website": "pnpm --filter protocol run update-abis && pnpm --filter website run dev",
"dev:data": "pnpm --filter protocol run update-abis && concurrently \"pnpm --filter data run start:api\" \"pnpm --filter data run start:market\" \"pnpm --filter data run start:chain\""
"dev:protocol": "pnpm --filter protocol run dev",
"dev:app": "pnpm --filter app run dev",
"dev:docs": "pnpm --filter docs run dev",
"dev:website": "pnpm --filter website run dev",
"dev:data": "concurrently \"pnpm --filter data run dev:service\" \"pnpm --filter data run dev:worker\""
},
"keywords": [],
"author": "",
Expand Down
9 changes: 6 additions & 3 deletions packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"description": "",
"main": "index.js",
"scripts": {
"start:api": "ts-node-dev src/index.ts --port 3001",
"start:market": "ts-node-dev src/market.ts",
"start:chain": "ts-node-dev src/chain.ts",
"build": "tsc",
"dev:service": "ts-node-dev src/service.ts --port 3001",
"dev:worker": "ts-node-dev src/worker.ts",
"start:service": "ts-node src/service.ts",
"start:worker": "ts-node src/worker.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand All @@ -15,6 +17,7 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2",
"pg": "^8.12.0",
"reflect-metadata": "^0.2.2",
"sqlite3": "^5.1.7",
"ts-node-dev": "^2.0.0",
Expand Down
27 changes: 27 additions & 0 deletions packages/data/src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ConnectionOptions } from "typeorm";
import { Position } from "./entity/Position";
import { Price } from "./entity/Price";
import { Transaction } from "./entity/Transaction";
import { Event } from "./entity/Event";

const isProduction = process.env.NODE_ENV === 'production';

const sqliteOptions: ConnectionOptions = {
type: "sqlite",
database: "./data/database.sqlite",
synchronize: true,
logging: true,
entities: [Price, Position, Transaction, Event],
};

const postgresOptions: ConnectionOptions = {
type: "postgres",
url: process.env.DATABASE_URL,
synchronize: true,
logging: true,
entities: [Price, Position, Transaction, Event],
};

const connectionOptions = isProduction ? postgresOptions : sqliteOptions;

export default connectionOptions;
2 changes: 1 addition & 1 deletion packages/data/src/entity/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Transaction {
nftId: number; // foreign key to NFT

@Column({
type: 'enum',
type: 'simple-enum',
enum: TransactionType,
})
type: TransactionType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createConnection } from 'typeorm';
import { Price } from './entity/Price';
import { Price } from '../entity/Price';
import { createPublicClient, http, Block } from 'viem';
import Foil from '@/protocol/deployments/13370/test/Foil.json';
import Foil from '@/protocol/deployments/13370/Foil.json';
import { mainnet, hardhat } from 'viem/chains';
import connectionOptions from '../db';

// Initialize RPC connection
export const publicClient = createPublicClient({
Expand All @@ -12,13 +13,7 @@ export const publicClient = createPublicClient({

const startBackgroundProcess = async () => {
// Initialize database connection
const connection = await createConnection({
type: 'sqlite',
database: './data/database.sqlite',
synchronize: true,
logging: true,
entities: [Price],
});
const connection = await createConnection(connectionOptions);
const priceRepository = connection.getRepository(Price);

// Process log data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createConnection } from 'typeorm';
import { Event } from './entity/Event';
import Foil from '@/protocol/deployments/13370/test/Foil.json';
import { Event } from '../entity/Event';
import Foil from '@/protocol/deployments/13370/Foil.json';
import { createPublicClient, http, Log } from 'viem'
import { hardhat } from 'viem/chains'
import connectionOptions from '../db';

const bigintReplacer = (key: string, value: any) => {
if (typeof value === 'bigint') {
Expand All @@ -20,13 +21,7 @@ export const publicClient = createPublicClient({

const startBackgroundProcess = async () => {
// Initialize database connection
const connection = await createConnection({
type: "sqlite",
database: "./data/database.sqlite",
synchronize: true,
logging: true,
entities: [Event],
});
const connection = await createConnection(connectionOptions);
const eventRepository = connection.getRepository(Event);

// Process log data
Expand Down
9 changes: 2 additions & 7 deletions packages/data/src/index.ts → packages/data/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import { Price } from './entity/Price';
import { Position } from './entity/Position';
import express from 'express';
import { Between } from 'typeorm';
import connectionOptions from './db';

const app = express();
app.use(express.json());
app.use(cors());

createConnection({
type: "sqlite",
database: "./data/database.sqlite",
synchronize: true,
logging: true,
entities: [Price],
}).then(async connection => {
createConnection(connectionOptions).then(async connection => {
const priceRepository = connection.getRepository(Price);
const positionRepository = connection.getRepository(Position);

Expand Down
30 changes: 30 additions & 0 deletions packages/data/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { exec } from 'child_process';

// Function to run a script with child_process
const runScript = (script: string) => {
return new Promise((resolve, reject) => {
const process = exec(`ts-node ${script}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing ${script}:`, error);
reject(error);
}
if (stderr) {
console.error(`Stderr from ${script}:`, stderr);
}
console.log(`Stdout from ${script}:`, stdout);
resolve(stdout);
});

process.on('exit', (code) => {
console.log(`Process ${script} exited with code ${code}`);
});
});
};

// Run both scripts in parallel
Promise.all([
runScript('packages/data/src/processes/chain.ts'),
runScript('packages/data/src/processes/market.ts')
]).catch(error => {
console.error('Error running scripts in parallel:', error);
});
Loading

0 comments on commit e20ddb9

Please sign in to comment.