Here:
contract.methods.viewNewestComment().call().then(console.log);
contract.methods.createLocation("test-location4", "comment4").call().then(console.log);
Your two ABI-encoded objects are:
contract.methods.viewNewestComment()
contract.methods.createLocation("test-location4", "comment4")
If the encoded function changes the state of the contract, then the object is considered a transaction, and you should run object.send(options)
in order to execute this transaction.
If the encoded function does not change the state of the contract, then the object is not considered a transaction, and you should run object.call()
in order to get the return-value of that function.
In web3.js v1.x, object.send
takes the following options
:
{
to : object._parent._address,
data : object.encodeABI(),
gas : await object.estimateGas({from: Your-Account-Address}),
gasPrice: Whatever-Gas-Price-You-Want,
value : value // amount of wei-ether that you want to send to the 'to' address
}
Of course, your account must hold at least gas * gasPrice + value
wei-ether.
Note that you will need to unlock your account on the node which you're communicating with, in order for the above to complete successfully (the answer that you've linked in your question explains how to do this without unlocking your account beforehand).
At this point, the only part remaining unanswered in your question is:
How do I inject an account with ether on it to perform a -write- command?
You can ask someone else to transfer ether to your account, or you can mine ether yourself.
I'll focus on the first option, because the second option is out-of-context IMO:
You can ask someone else to transfer ether to your account
If you're running on mainnet, then that someone else will most likely ask you to give them something in return, a convention more commonly known as trading (buying and selling), which has been used in every non-charity monetary system that we know of since the dawn of humanity.
If you're running on a testnet, then there are various public websites which will give you a certain amount of ether per day per address per IP (i.e., they will gray-list you based on each one of these criteria in order to limit the amount of ether that you can ask for).
For example, this ropsten faucet will give you 1 ether a day.
Best Answer
You can install a block filter and only update balances when a new block arrives.