diff --git a/READMEs/App.ts.md b/READMEs/App.ts.md new file mode 100644 index 0000000..aba0c31 --- /dev/null +++ b/READMEs/App.ts.md @@ -0,0 +1,88 @@ +# Application Entry Point + +The `app.ts` file serves as the main entry point for the PDR WebSocket server application. It initializes the Express server, sets up Socket.IO, and configures various middleware and routes. + +## Overview + +This application provides a WebSocket server for real-time prediction data distribution, with REST API endpoints for additional functionality. It uses Express.js for HTTP handling and Socket.IO for WebSocket communications. + +## Core Components + +### Environment Setup +- Uses `dotenv` for environment-specific configuration +- Loads environment variables from `.env.{NODE_ENV}` file +- Enables development-specific logging when in development mode + +### Server Initialization +- Creates an Express application instance +- Sets up HTTP server using Node's `http` module +- Configures Socket.IO on the HTTP server +- Implements CORS protection + +### Middleware Configuration +- JSON parsing middleware +- CORS checking middleware +- Error handling middleware +- API routing middleware + +### Server Features + +#### WebSocket Support +- Initializes Socket.IO server +- Configures WebSocket event handlers +- Manages real-time data distribution + +#### REST API +- Implements API routes under `/api/v1` endpoint +- Handles JSON request bodies +- Provides error handling for API endpoints + +#### Development Tools +- Conditional logging stream in development environment +- Error handling middleware for debugging +- Environment-specific configurations + +## Configuration + +### Environment Variables +- `NODE_ENV`: Determines environment configuration +- `PORT`: Server port number (defaults to 3000) +- Additional environment-specific variables + +### Server Settings +- Default port: 3000 +- API base path: `/api/v1` +- CORS configuration for security + +## Usage + +### Starting the Server +1. Environment configuration is loaded +2. Express app is initialized +3. Middleware is configured +4. Socket.IO server is started +5. HTTP server begins listening on configured port + +### API Access +- REST endpoints available at `/api/v1/*` +- WebSocket connections handled through Socket.IO +- CORS rules apply to all connections + +## Dependencies + +### Primary +- Express.js for HTTP server +- Socket.IO for WebSocket support +- dotenv for configuration + +### Middleware +- express.json() for request parsing +- Custom CORS checking +- Custom error handling + +## Development Notes + +- Development environment enables additional logging +- Error handling is configured globally +- Server provides feedback on successful startup +- All WebSocket functionality is managed through Socket.IO diff --git a/READMEs/authorizationData.ts.md b/READMEs/authorizationData.ts.md new file mode 100644 index 0000000..e9e0d4f --- /dev/null +++ b/READMEs/authorizationData.ts.md @@ -0,0 +1,91 @@ +# Authorization Data Manager + +The `AuthorizationData` class manages authorization data with automatic expiration handling and renewal capabilities. This class is designed to work with any authorization data type that extends the `BaseAuthData` interface. + +## Interfaces and Types + +### BaseAuthData +An interface that defines the basic structure for authorization data. It requires a `validUntil` property of type number, which represents the timestamp when the authorization expires. + +### TAuthorizationData +A type definition that specifies the structure for initializing the AuthorizationData class. It includes: +- `initialData`: The starting authorization data +- `createCallback`: A function that returns a Promise resolving to new authorization data + +## Class: AuthorizationData + +A generic class that handles authorization data lifecycle, including automatic renewal and expiration checks. + +### Properties + +- `validUntil`: Private property storing the expiration timestamp +- `authorizationData`: Private property storing the current authorization data +- `createCallback`: Private property storing the function to generate new authorization data + +### Constructor + +The constructor takes an object conforming to the TAuthorizationData type, containing initial authorization data and a callback function for creating new authorization data. + +### Methods + +#### isValid() +- Checks if the current authorization data is still valid +- Returns a boolean indicating validity status +- Compares current time with expiration time using 100ms units + +#### isCloseToExpire() +- Determines if the authorization is approaching expiration +- Returns a boolean indicating if within 5 minutes of expiration +- Uses Unix timestamp (seconds) for comparison + +#### createNew() +- Generates new authorization data using the callback +- Asynchronously updates internal state +- Updates both the authorization data and expiration time + +#### getAuthorizationData() +- Retrieves current authorization data +- Automatically initiates renewal if close to expiration +- Returns the current authorization data object + +## Implementation Details + +### Time Handling +- Uses Unix timestamps (seconds) for expiration times +- Includes a 5-minute safety buffer for validity checks +- Standardizes time unit comparisons + +### Automatic Renewal +- Transparently renews authorization when approaching expiration +- Uses non-blocking Promise-based callbacks +- Maintains continuous authorization coverage + +### Type Safety +- Implements generic typing for flexibility +- Enforces BaseAuthData interface requirements +- Provides type-safe access to authorization properties + +## Best Practices + +### Callback Implementation +- Create idempotent callbacks +- Include comprehensive error handling +- Implement appropriate retry logic + +### Expiration Management +- Set appropriate expiration timeframes +- Account for network latency +- Utilize the 5-minute buffer effectively + +### Error Handling +- Monitor callback execution +- Handle renewal failures gracefully +- Implement fallback mechanisms + +## Notes + +- Designed for single-instance authorization management +- Automatic renewal triggers within 5-minute expiration buffer +- Uses standard Unix timestamps for all time-based operations +- Thread-safe for basic operations +- Supports various authorization data types through generic implementation \ No newline at end of file diff --git a/READMEs/dataholder.ts.md b/READMEs/dataholder.ts.md new file mode 100644 index 0000000..a279964 --- /dev/null +++ b/READMEs/dataholder.ts.md @@ -0,0 +1,28 @@ +## dataHolder.ts + +The `dataHolder.ts` file manages the storage and retrieval of market data received from the PDR WebSocket. Key components include: + +### DataHolder Class +A generic class that manages data storage and retrieval with the following features: + +#### Properties +- `data`: Private object storing arrays of generic type T indexed by string keys +- `theFixedMessage`: Public property of generic type U (extends Record) + +#### Methods +- `setContract(key: string, value: Array)`: Sets data array for a contract key +- `getContract(key: string)`: Gets data array for a contract key +- `setItemToContract(key: string, item: T)`: Adds item to a contract's data array +- `clearContract(key: string)`: Clears data array for a contract key +- `getItemFromContractByItemKeyValue(key, itemKey, itemValue)`: Finds item in contract data by key-value pair +- `removeItemFromContractByItemKeyValue(key, itemKey, itemValues)`: Removes items from contract data by key-value pairs + +### Usage +The DataHolder class is used to store prediction values and contract data received from the WebSocket provider. It provides thread-safe data storage and retrieval methods for managing real-time market data. + +### Key Features +- Generic typing for flexible data storage +- Contract-based data organization +- Thread-safe operations +- Efficient data lookup and filtering +- Support for fixed messages per epoch emitter diff --git a/READMEs/newSubscriberListener.ts.md b/READMEs/newSubscriberListener.ts.md new file mode 100644 index 0000000..b706f2e --- /dev/null +++ b/READMEs/newSubscriberListener.ts.md @@ -0,0 +1,8 @@ +## newSubscriberListener.ts Overview + +The `newSubscriberListener.ts` file is responsible for handling new subscriber connections to the PDR WebSocket. It includes: + +### newSubscriberListener Function +- Handles incoming connections +- Emits new epoch data to the client +- Handles client disconnections diff --git a/READMEs/providerListener.ts.md b/READMEs/providerListener.ts.md new file mode 100644 index 0000000..8a84768 --- /dev/null +++ b/READMEs/providerListener.ts.md @@ -0,0 +1,91 @@ +# Provider Listener + +The Provider Listener is a core component that manages blockchain event listening and subscription management for prediction contracts. It handles real-time data updates and authorization management for the WebSocket server. + +## Overview + +The provider listener initializes and maintains connections to prediction contracts, manages subscriptions, and broadcasts updates to connected clients through Socket.IO. + +## Main Components + +### Types + +#### TProviderListenerArgs +Configuration object for initializing the provider listener: +- `io`: Socket.IO server instance +- `contractAddresses`: Array of prediction contract addresses to monitor +- `epochEmitterName`: Enum value specifying the type of epoch events to emit + +#### TProviderListenerEmitData +Structure of data emitted to clients: +- Array of objects containing: + - `predictions`: Array of prediction results with epochs and contract addresses + - `contractInfo`: Associated prediction contract information + +### Core Functionality + +#### Initialization +- Sets up network provider connection +- Retrieves interesting prediction contracts +- Initializes authorization +- Sets up contract instances +- Creates data holders for subscribed predictoors + +#### Block Monitoring +- Listens to new blocks on the blockchain +- Calculates current epochs +- Manages subscription renewals +- Fetches and processes prediction values +- Broadcasts updates to connected clients + +### Key Features + +#### Subscription Management +- Automatically renews expiring subscriptions +- Tracks active subscription transactions +- Prevents duplicate subscription attempts +- Maintains subscription state across block updates + +#### Data Processing +- Calculates prediction epochs +- Aggregates prediction values +- Clears outdated prediction data +- Updates data holders with new information + +#### Event Broadcasting +- Emits 'newEpoch' events with updated predictions +- Maintains fixed messages for each epoch +- Provides contract information with predictions + +## Technical Details + +### State Management +- Tracks latest processed epoch +- Maintains list of started transactions +- Manages subscribed predictoor contracts + +### Time Handling +- Uses block timestamps for epoch calculations +- Implements delay for prediction fetching +- Handles seconds-per-epoch timing + +### Error Prevention +- Prevents duplicate subscription transactions +- Validates subscription expiration +- Manages transaction state cleanup + +## Usage Notes + +- Requires proper initialization of network provider +- Depends on valid contract addresses +- Needs Socket.IO server instance +- Requires proper authorization setup +- Must be configured with correct epoch emitter name + +## Dependencies + +- Socket.IO for real-time communication +- Ethereum provider for blockchain interaction +- Authorization system for secure data access +- Data holders for state management +- Contract interfaces for blockchain interaction