-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.js
52 lines (48 loc) · 1.52 KB
/
call.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
52
const Web3 = require('web3');
// Loading the contract ABI
// (the results of a previous compilation step)
const fs = require('fs');
const path = require('path');
const { abi } = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, 'build', 'contracts', 'Inbox.json')
)
);
async function main() {
// Configuring the connection to an Ethereum node
const network = process.env.ETHEREUM_NETWORK;
const web3 = new Web3(
new Web3.providers.HttpProvider(process.env.INFURA_API_KEY)
);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY
);
web3.eth.accounts.wallet.add(signer);
// Creating a Contract instance
const contract = new web3.eth.Contract(
abi,
// Replace this with the address of your deployed contract
process.env.INBOX_CONTRACT
);
// Issuing a transaction that calls the `setMessage` method (writing function)
const tx = contract.methods.setMessage('Hello, world!');
const receipt = await tx
.send({
from: signer.address,
gas: await tx.estimateGas(),
})
.once('transactionHash', (txhash) => {
console.log(`Mining transaction ...`);
console.log(
`https://${network}.etherscan.io/tx/${txhash}`
);
});
// The transaction is now on chain!
console.log(`Mined in block ${receipt.blockNumber}`);
// Calling `message` method (reading function, not a transaction)
const message = await contract.methods.message().call();
console.log(`Message on chain is now ${message}`);
}
require('dotenv').config();
main();