This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (39 loc) · 1.45 KB
/
index.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 host = 'http://localhost:8545'
const contractName = "SimpleStorage"
const fs = require('fs')
const Web3 = require('web3')
const readFileSync = fs.readFileSync
const web3 = new Web3(host)
const eth = web3.eth
const c = console
const interf = require(`./${contractName}.json`)["contracts"][`${contractName}.sol:${contractName}`]
const abi = JSON.parse(interf.abi)
const bytecode = `0x${interf.bin}`
const contractAddress = readFileSync("contract-address.txt").toString().trim()
const utils = web3.utils
const stringToHex = utils.stringToHex
const hexToString = utils.hexToString
;(async () => {
try {
const addresses = await eth.getAccounts()
const address = addresses[0]
c.log("address:", address)
const balance = await eth.getBalance(address)
c.log("balance:", balance)
c.log("contractAddress:", contractAddress)
const simpleStorage = new eth.Contract(abi, contractAddress, )
const num = Number(Math.random() * 10000).toFixed(0)
const value = stringToHex(`foo-${num}`)
const result = await simpleStorage.methods.data().call()
c.log("Data:", hexToString(result))
const resultTx = await simpleStorage.methods.set(value).send({ from: address })
// c.log(resultTx)
c.log("TX hash:", resultTx.transactionHash)
const result2 = await simpleStorage.methods.data().call()
c.log("Data:", hexToString(result2))
process.exit(0)
} catch (err) {
console.log(err)
process.exit(1)
}
})()