starknet-js-indexer
v0.0.19
Published
A TypeScript/Node.js indexer for Starknet events, supporting PostgreSQL and real-time event handling via WebSocket and RPC.
Readme
Starknet JS Indexer
A TypeScript/Node.js indexer for Starknet events, supporting PostgreSQL and real-time event handling via WebSocket and RPC.
Features
- Listen to Starknet events from specified contracts
- Store events and block data in PostgreSQL
- Handle chain reorgs and retries
- Extensible event handler registration
Installation
npm install
yarn installRequirements
- WebSocket endpoint is required: The indexer needs a Starknet node with WebSocket support (e.g., Infura, local node with WS enabled).
- Starknet node spec version 0.8 or above: The indexer is compatible with Starknet nodes running spec version 0.8 or higher.
Usage Example
See example/index.ts for a minimal working example.
import { StarknetIndexer, LogLevel } from '../src/index';
import { abi as myContractAbi } from './myContractAbi'; // Provide your contract ABI
const indexer = new StarknetIndexer({
rpcNodeUrl: 'https://starknet-mainnet.infura.io/v3/YOUR_KEY',
wsNodeUrl: 'wss://starknet-mainnet.infura.io/ws/v3/YOUR_KEY',
databaseUrl: 'postgresql://user:password@localhost:5432/mydb',
contractAddresses: ['0x...'],
logLevel: LogLevel.INFO,
});
indexer.onEvent({
contractAddress: '0x...',
abi: myContractAbi,
eventName: 'Transfer',
handler: async (event, client, indexer) => {
console.log('Received event:', event);
// Custom logic here
},
});
indexer.start();Running the Example
- Made your to setup your local postgres database
- Edit
example/index.tswith your node URLs and contract address if needed. - Edit
example/index.tswith your postgres database connection string if needed. - Run:
npx ts-node example/index.tsRunning Tests
npm testConfiguration
rpcNodeUrl: Starknet RPC endpointwsNodeUrl: Starknet WebSocket endpointdatabaseUrl: PostgreSQL connection stringcontractAddresses: Array of contract addresses to indexlogLevel: Log verbositystartingBlockNumber: Starting block number to start indexing from
Troubleshooting
- Ensure PostgreSQL is running and accessible
- Use correct node URLs and contract addresses
- For ABI errors, check your ABI file and event names
