ZKsync Era Block Explorer Worker is an indexer service for ZKsync Era blockchain data. It retrieves aggregated data from the Data Fetcher via HTTP and also directly from the blockchain using ZKsync Era JSON-RPC API, processes it and saves into the database in a way that makes it easy to read by the Block Explorer API.
$ npm install- Create
.envfile in theworkerpackage folder and copy paste.env.examplecontent in there.
cp .env.example .env
- In order to tell the service where to get the blockchain data from set the value of the
BLOCKCHAIN_RPC_URLenv var to your blockchain RPC API URL. For ZKsync Era testnet it can be set tohttps://sepolia.era.zksync.dev. For ZKsync Era mainnet -https://mainnet.era.zksync.io. - To retrieve aggregated blockchain data for a certain block, the Worker service calls the Data Fetcher service via HTTP. To specify Data Fetcher URL use
DATA_FETCHER_URLenv variable. By default, it is set tohttp://localhost:3040which is a default value for the local environment. - Set up env variables for Postgres database connection. By default it points to
localhost:5432and database name isblock-explorer. You need to have a running Postgres server, set the following env variables to point the service to your database:DATABASE_HOSTDATABASE_USERDATABASE_PASSWORDDATABASE_NAMEDATABASE_CONNECTION_IDLE_TIMEOUT_MSDATABASE_CONNECTION_POOL_SIZE
The service doesn't create database automatically, you can create database by running the following command:
$ npm run db:createFor networks with a custom base token, there are a number of environment variables used to configure custom base token:
BASE_TOKEN_L1_ADDRESS- example:0xB44A106F271944fEc1c27cd60b8D6C8792df86d8. Base token L1 address can be fetched using the RPC call:or SDK:curl http://localhost:3050 \ -X POST \ -H "Content-Type: application/json" \ --data '{"method":"zks_getBaseTokenL1Address","params":[],"id":1,"jsonrpc":"2.0"}'import { Provider } from "zksync-ethers"; async function main() { const l2provider = new Provider("http://localhost:3050"); const baseTokenAddress = await l2provider.getBaseTokenContractAddress(); console.log('baseTokenAddress', baseTokenAddress); } main() .then() .catch((error) => { console.error(error); process.exitCode = 1; });BASE_TOKEN_SYMBOL- base token symbol, e.g.:ZKBASE_TOKEN_NAME- base token name, e.g.:ZKBASE_TOKEN_DECIMALS- base token decimals, e.g.:18BASE_TOKEN_ICON_URL- base token icon url, e.g.:https://assets.coingecko.com/coins/images/279/large/ethereum.png?1698873266
The worker service can fetch and update token metadata (prices, icons, market cap) from external data providers. This data is used to enhance the token information displayed in the block explorer.
ENABLE_TOKEN_OFFCHAIN_DATA_SAVER- set totrueto enable the offchain data saver service (default:false)UPDATE_TOKEN_OFFCHAIN_DATA_INTERVAL- interval in milliseconds between offchain data updates (default:86400000- 24 hours)SELECTED_TOKEN_OFFCHAIN_DATA_PROVIDER- provider to use for fetching token data. Available options:coingecko(default:coingecko)
COINGECKO_IS_PRO_PLAN- set totrueif using CoinGecko Pro API (default:false)COINGECKO_API_KEY- your CoinGecko API key (required for both free and pro plans)COINGECKO_PLATFORM_ID- the platform identifier for your blockchain on CoinGecko (default:zksync)
To find the correct platform ID for your blockchain, you can query the CoinGecko asset platforms API:
curl -X GET "https://api.coingecko.com/api/v3/asset_platforms" | jqThis will return a JSON array of all supported platforms. Look for your blockchain in the response and use the id field as your COINGECKO_PLATFORM_ID. For example:
{
"id": "zksync",
"chain_identifier": 324,
"name": "zkSync Era",
"shortname": "",
"native_coin_id": "ethereum"
}Common platform IDs include:
zksync- zkSync Eraabstract- Abstractlens- Lenssophon- Sophon
You can also browse the full list at https://api.coingecko.com/api/v3/asset_platforms.
ENABLE_TOKEN_OFFCHAIN_DATA_SAVER=true
UPDATE_TOKEN_OFFCHAIN_DATA_INTERVAL=86400000
SELECTED_TOKEN_OFFCHAIN_DATA_PROVIDER=coingecko
COINGECKO_IS_PRO_PLAN=false
COINGECKO_API_KEY=your_api_key_here
COINGECKO_PLATFORM_ID=zksync# development
$ npm run dev
# watch mode
$ npm run dev:watch
# debug mode
$ npm run dev:debug
# production mode
$ npm run start# unit tests
$ npm run test
# unit tests debug mode
$ npm run test:debug
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:covRun npm run lint to make sure the code base follows configured linter rules.
Changes to the DB are stored as migrations scripts in src/migrations folder and are automatically executed on the application start.
We use code first approach for managing DB schema so desired schema changes should be first applied to the Entity classes and then migrations scripts can be generated running the following command: migration:generate.
Example:
npm run migration:generate -name=AddStatusColumnToTxTable
a new migration with the specified name and all schema changes will be generated in src/migration folder. Always check generated migrations to confirm that they have everything you intended.
Sometimes you need to write a manual migration script not generated based on any schema changes. For instance, to run a script to update some records. In this case use migration:create to create an empty migration.
Example:
npm run migration:create -name=UpdateTxsFee
this command will simply create an empty migration where the custom migration logic can be added.