-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
79 lines (72 loc) · 1.7 KB
/
init.js
File metadata and controls
79 lines (72 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const createTestClient = require('viem').createTestClient
const http = require('viem').http
const foundry = require('viem/chains').foundry
const fs = require('fs')
const testClient = createTestClient({
chain: foundry,
mode: 'anvil',
transport: http(),
})
.extend((client) => ({
async anvilDumpState() {
return client.request({
method: 'anvil_dumpState',
params: [],
})
},
}))
.extend((client) => ({
async anvilLoadState(state) {
return client.request({
method: 'anvil_loadState',
params: [state],
})
},
}))
.extend((client) => ({
async getNodeInfo() {
return client.request({
method: 'anvil_nodeInfo',
params: [],
})
},
}))
.extend((client) => ({
async mine(numBlocks) {
return client.request({
method: 'anvil_mine',
params: [numBlocks],
})
},
}))
async function dumpState() {
const response = await testClient.anvilDumpState()
fs.writeFile('./state', response, (err) => {
if (err) {
console.error('Error writing to the file:', err)
} else {
console.log('File saved successfully!')
}
})
}
async function loadState() {
fs.readFile('./state', 'utf8', async (err, data) => {
if (err) {
console.error('Error reading file:', err)
} else {
// console.log({ data })
await testClient.anvilLoadState(data)
}
})
}
async function getNodeInfo() {
console.log(await testClient.getNodeInfo())
}
async function mineBlocks(numBlocks) {
await testClient.mine(numBlocks)
}
// mine blocks after loading state from demo backend in order to initialize chain
mineBlocks(5)
// loadState()
// dumpState()
// getNodeInfo()