-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
51 lines (43 loc) · 1.24 KB
/
utils.js
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
const fs = require('fs')
const Web3 = require('web3');
const { privateKey, providerURL } = require("./secrets.json")
const { exploreURL, contractAddress } = require("./config.json")
const web3 = new Web3(providerURL)
const defaultInfo = {
"nonce": 0,
"gasLimit": 500000,
"gasPrice": 5000000000
}
const saveInfo = (info) => {
const data = JSON.stringify(info)
try {
fs.writeFileSync('info.dat', data)
}
catch (err) {
console.log(`Error saving info file: ${err}`)
}
}
const readInfo = () => {
try {
const data = fs.readFileSync('info.dat', 'utf-8')
const info = JSON.parse(data)
return info
}
catch (err) {
console.log(`Error reading info file: ${err}`)
return defaultInfo
}
}
const syncNonceForAccount = async () => {
console.log("synchronizing nonce...")
const info = readInfo()
const account = web3.eth.accounts.privateKeyToAccount(privateKey)
const nonce = await web3.eth.getTransactionCount(account.address)
saveInfo({...info, nonce: nonce})
console.log("synchronized. nonce is " + nonce)
return nonce
}
const getChainExploreURL = () => {
return exploreURL + "address/" + contractAddress + "#readProxyContract"
}
module.exports = { saveInfo, readInfo, syncNonceForAccount, getChainExploreURL };