From 94033bc388ec944dbb548f5e3a44496366ecbd59 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Tue, 18 Aug 2020 12:15:04 -0700 Subject: [PATCH] update readme example --- packages/blockchain/README.md | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/packages/blockchain/README.md b/packages/blockchain/README.md index 6ffb481c32..c904edde74 100644 --- a/packages/blockchain/README.md +++ b/packages/blockchain/README.md @@ -24,29 +24,24 @@ The following is an example to iterate through an existing Geth DB (needs `level This module performs write operations. Making a backup of your data before trying it is recommended. Otherwise, you can end up with a compromised DB state. -```javascript -const level = require('level') -const Blockchain = require('@ethereumjs/blockchain').default -const utils = require('ethereumjs-util') +```typescript +import Blockchain from '@ethereumjs/blockchain' +import { bufferToInt } from 'ethereumjs-util' +import level from 'level' const gethDbPath = './chaindata' // Add your own path here. It will get modified, see remarks. + const db = level(gethDbPath) +const blockchain = new Blockchain({ db }) -new Blockchain({ db: db }).iterator( - 'i', - (block, reorg, cb) => { - const blockNumber = utils.bufferToInt(block.header.number) - const blockHash = block.hash().toString('hex') - console.log(`BLOCK ${blockNumber}: ${blockHash}`) - cb() - }, - (err) => console.log(err || 'Done.'), -) +blockchain.iterator('i', (block) => { + const blockNumber = bufferToInt(block.header.number) + const blockHash = block.hash().toString('hex') + console.log(`Block ${blockNumber}: ${blockHash}`) +}) ``` -**WARNING**: Since `@ethereumjs/blockchain` is also doing write operations -on the DB for safety reasons only run this on a copy of your database, otherwise this might lead -to a compromised DB state. +**WARNING**: Since `@ethereumjs/blockchain` is also doing write operations on the DB for safety reasons only run this on a copy of your database, otherwise this might lead to a compromised DB state. # EthereumJS