Easily store blockchain events from smart contracts to a database of your choice without doing all the painful scaffolding.
$ npm install -s web3-events-db
Please add an issue for any common, modern DB you'd like us to support with a built-in connector.
- PostgreSQL
- MongoDB
Please add an issue for any block explorers you'd like us to support.
- Etherscan (ETH)
- BscScan (BSC)
We will build out detailed API docs over time. In the meantime, review the below examples and see Examples for good small example scripts to get you started.
import web3EventsDb from 'web3-events-db'
// listen for events for a bscscan verified BSC contract and populate a postgres database
// listening on localhost and in default table `Predict_web3_events_db` (`${eventName}_web3_events_db`)
const readerWriter = Web3EventsDb({
db: {
type: 'postgres',
connectionString: 'postgres://localhost:5432/web3',
},
contract: {
network: 'bsc',
blockExplorerApiKey: process.env.BSCSCAN_API_KEY,
wsRpc: `wss://apis-sj.ankr.com/wss/${process.env.ANKR_INSTANCE}/${process.env.ANKR_API_KEY}/binance/full/main`,
contract: '0x20D0a1831c0F5071904a5EC511423564793bf620',
eventName: 'Predict',
},
recordCallback: (record) => console.log('got a record', record),
})
await readerWriter.start()
import web3EventsDb from 'web3-events-db'
// listen for events for a bscscan verified BSC contract and populate a MongoDB database
// listening on localhost and in default table `Predict_web3_events_db` (`${eventName}_web3_events_db`)
const readerWriter = Web3EventsDb({
db: {
type: 'mongo',
connectionString: 'mongodb://localhost/?retryWrites=true&w=majority',
},
contract: {
network: 'eth',
blockExplorerApiKey: process.env.ETHERSCAN_API_KEY,
wsRpc: `wss://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`,
contract: '0x20D0a1831c0F5071904a5EC511423564793bf620',
eventName: 'Predict',
},
recordCallback: (record) => console.log('got a record', record),
})
await readerWriter.start()
TODO
TODO
TODO
Tests depend on active database connections on localhost for all built-in connectors (mongodb and postgres).
$ npm test