-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteract.js
More file actions
69 lines (61 loc) · 1.56 KB
/
interact.js
File metadata and controls
69 lines (61 loc) · 1.56 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
// Import the Web3 library
import Web3 from 'web3';
// Connect to the local Ganache instance
const web3 = new Web3('http://localhost:7545');
// Define the ABI for the SimpleStorage contract
const contractABI = [
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "data",
"type": "uint256"
}
],
"name": "DataStored",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "x",
"type": "uint256"
}
],
"name": "set",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "get",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
}
];
const contractAddress = '0x117584D9641BD03277C4e30abC7aD99E4f17Fb01';
// Create a new contract instance with the ABI and address
const contract = new web3.eth.Contract(contractABI, contractAddress);
async function interact() {
// Get the list of accounts from the local node
const accounts = await web3.eth.getAccounts();
// Set data on the contract
await contract.methods.set(123).send({ from: accounts[0] });
// Get data from the contract
const result = await contract.methods.get().call();
console.log(result); // Should print 123
}
// Run the interact function
interact();