Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 9c137ad

Browse files
committed
add async function wrappers around examples
1 parent 6462b31 commit 9c137ad

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Example using async/await syntax:
1414

1515
```typescript
1616
import { BaseTrie as Trie } from 'merkle-patricia-tree'
17-
1817
const trie = new Trie()
1918
async function test() {
20-
await trie.put(Buffer.from('test'), Buffer.from('one'))
21-
const value = await trie.get(Buffer.from('test'))
22-
console.log(value) // 'one'
19+
await trie.put(Buffer.from('test'), Buffer.from('one'))
20+
const value = await trie.get(Buffer.from('test'))
21+
console.log(value.toString()) // 'one'
2322
}
23+
await test()
2424
```
2525

2626
### Breaking Changes

README.md

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,25 @@ import { BaseTrie as Trie } from 'merkle-patricia-tree'
3030
const db = level('./testdb')
3131
const trie = new Trie(db)
3232

33-
await trie.put(Buffer.from('test'), Buffer.from('one'))
34-
const value = await trie.get(Buffer.from('test'))
35-
console.log(value.toString())
33+
async function test() {
34+
await trie.put(Buffer.from('test'), Buffer.from('one'))
35+
const value = await trie.get(Buffer.from('test'))
36+
console.log(value.toString()) // 'one'
37+
}
38+
39+
await test()
3640
```
3741

3842
## Merkle Proofs
3943

4044
```typescript
41-
const prove = await Trie.prove(trie, Buffer.from('test'))
42-
const value = await Trie.verifyProof(trie.root, Buffer.from('test'), prove)
43-
console.log(value.toString())
45+
async function test() {
46+
const prove = await Trie.prove(trie, Buffer.from('test'))
47+
const value = await Trie.verifyProof(trie.root, Buffer.from('test'), prove)
48+
console.log(value.toString())
49+
}
50+
51+
await test()
4452
```
4553

4654
## Read stream on Geth DB
@@ -79,28 +87,32 @@ const trie = new Trie(db, stateRoot)
7987

8088
const address = 'AN_ETHEREUM_ACCOUNT_ADDRESS'
8189

82-
const data = await trie.get(address)
83-
const acc = new Account(data)
84-
85-
console.log('-------State-------')
86-
console.log(`nonce: ${new BN(acc.nonce)}`)
87-
console.log(`balance in wei: ${new BN(acc.balance)}`)
88-
console.log(`storageRoot: ${bufferToHex(acc.stateRoot)}`)
89-
console.log(`codeHash: ${bufferToHex(acc.codeHash)}`)
90-
91-
let storageTrie = trie.copy()
92-
storageTrie.root = acc.stateRoot
93-
94-
console.log('------Storage------')
95-
const stream = storageTrie.createReadStream()
96-
stream
97-
.on('data', (data) => {
98-
console.log(`key: ${bufferToHex(data.key)}`)
99-
console.log(`Value: ${bufferToHex(rlp.decode(data.value))}`)
100-
})
101-
.on('end', () => {
102-
console.log('Finished reading storage.')
103-
})
90+
async function test() {
91+
const data = await trie.get(address)
92+
const acc = new Account(data)
93+
94+
console.log('-------State-------')
95+
console.log(`nonce: ${new BN(acc.nonce)}`)
96+
console.log(`balance in wei: ${new BN(acc.balance)}`)
97+
console.log(`storageRoot: ${bufferToHex(acc.stateRoot)}`)
98+
console.log(`codeHash: ${bufferToHex(acc.codeHash)}`)
99+
100+
let storageTrie = trie.copy()
101+
storageTrie.root = acc.stateRoot
102+
103+
console.log('------Storage------')
104+
const stream = storageTrie.createReadStream()
105+
stream
106+
.on('data', (data) => {
107+
console.log(`key: ${bufferToHex(data.key)}`)
108+
console.log(`Value: ${bufferToHex(rlp.decode(data.value))}`)
109+
})
110+
.on('end', () => {
111+
console.log('Finished reading storage.')
112+
})
113+
}
114+
115+
await test()
104116
```
105117

106118
# API

0 commit comments

Comments
 (0)