Now go back to AWS and Geth.
> contract = eth.contract(…paste ABI JSON…).at("paste Contract Address")
The ABI is pretty long when you paste it in. It may ask you if you want to convert tabs to spaces, just hit return. Add the closing parenthesis and add the .at(“contractAddress”). I had to put in the speech marks to make it work. It should then log out all a load of information on the contract.
Now check the name of the commodity you’re :
> contract.name()
"Cattle"
This should return “Cattle” or whatever you deployed it as.
Try getting the balance. Use the address of the account that you used to deploy the contract:
> contract.balanceOf(“0xd3f33faa8078c6f7589bef6f73b00a96a4c63d0a”)
250
This should return 250.
Time to test the transfer functions.
Create a new account to transfer some Cattle to.
> personal.newAccount()
Enter the passphrase then this will return the address of the new account.
Assuming you now have two accounts (check by doing)
> eth.accounts
[“0xd3f33faa8078c6f7589bef6f73b00a96a4c63d0a”, “0x2345b45f42f448bb1c4386fe94987e510a9b6069”]
Set up the account you deployed the contract with as account A and the other one as account B:
> accountA = eth.accounts[0]
> accountB = eth.accounts[1]
Now send some ether to accountB so it can transact:
> eth.sendTransaction()
Mine that to ensure it goes through (miner.start() then miner.stop() after a few blocks have been mined).
Now, set the default account that Geth will use to call the functions.
> eth.defaultAccount = accountA
> personal.unlockAccount(eth.defaultAccount)
> contract.giveTheGoods(accountB, 9)
This should log some information. Now you need to mine it to complete the transaction.
> miner.start()
wait a bit
> miner.stop()
Now switch accounts;
> eth.defaultAccount = accountB
> personal.unlockAccount(eth.defaultAccount)
> contract.gotTheGoods(accountA, accountB, 9)
> miner.start()
// wait a bit
> miner.stop()
Now check the balance.
> contract.balanceOf(accountB)
9
> contract.balanceOf(accountA)
241
There you have it, assuming all went well. You have successfully deployed a contract to your network. This can be done much more nicely using Web3 from a javaScript front end. As can the transfer of assets. We’ll have a look at that in Part 3….
Published at Mon, 25 Feb 2019 06:12:33 +0000