How to publish an information at Ethereum blockchain and retrieve it after (at windows)
I intend to show how to create a smart contract in solidity language, publish at Ethereum bockchain and interact with him throught a web page. All this will be done using a geth node at local network, at Windows.
Pre requirements
Geth: https://github.com/ethereum/go-ethereum/wiki/geth
Node.js: https://nodejs.org/en/download/
TestRPC: https://www.npmjs.com/package/ethereumjs-testrpc
Browserify: https://www.npmjs.com/package/browserify
Web3: https://www.npmjs.com/package/web3
Installing all
My local directory:
C:\Ethereum\Projetos\register
At windows, run Command Prompt or PowerShell.
Go to your project’s directory:
cd C:\Ethereum\Projetos\register
If you don´t know how to create a Ethereum local network with geth, I wrote how to do it here (click link).
After installed Geth and NodeJS, here are the commands (at Command Prompt or PowerShell):
npm install -g ethereumjs-testrpc
npm install -g browserify
We need to install web3 locally. Make sure to do it in your project directory!
cd C:\Ethereum\Projetos\register
npm install web3
About TestRPC
My focus here is not to detail the testrpc, but it is important to know a few things.
Test RPC is a Node.js based Ethereum client used for testing and development.
When it is executed, by default, it creates 10 accounts with random addresses.
If you would like to always use the same addresses, you should copy the mnemonic at the first time and then pass it as a parameter when running testrpc.
testrpc
Save mnemonic to use it the next time when you run testrpc with the same account’s numbers
You should expect to see output similar to this:
In my case, the mnemonic is:
raven winter frequent miss endless violin rely meat urge other ability peasant
In the next time, execute testrpc with the parameter -m:
testrpc -m "raven winter frequent miss endless violin rely meat urge other ability peasant"
The same addresses will be generated!
Creating the smart contract
The main functions are:
setInfo — record the information at blockchain.
getInfo — retrieve the last information.
I´m recording only one information. Each time I run setInfo, I´m writing over the previous info. I can see all changes in “info” seaching the blockchain transactions.
Create a blank file: register.sol
Here is the source code: register.sol
Compiling the smart contract
Compile with remix:
Create a blank file and rename it contract.js
Click in button Details.
Copy WEB3DEPLOY to contract.js and save it.
Publishing the smart contract
I will load file contract.js and publish the smart contract.
At windows, run Command Prompt or PowerShell.
Go to your project’s directory:
cd C:\Ethereum\Projetos\register
Execute testrpc
testrpc -m "raven winter frequent miss endless violin rely meat urge other ability peasant"
Attach to geth console in another window
I already have a node running with testrpc, so I can attach a geth instance to interact with it.
One more time, run Command Prompt or PowerShell in a second window.
Go to your project’s directory again:
cd C:\Ethereum\Projetos\register
Execute:
geth attach http://localhost:8545
Create contract at geth console, running:
loadScript(“C:/Ethereum/Projetos/register/contract.js”);
This is the project’s directory.
Pay attention with the slash, it is to right “/”.
At first window (with testrpc runing), You can see the transaction which the contract is mined :
At second window, execute register.address to see the contract´s address:
register.address
In my case, the contract’s address is:
“0x47ae7e35ed0346d0d7ab0c4894e35acfcd724663”.
We will use this information later.
Testing the smart contract
At second window attached to testrpc, we can do some tests to check if the process with the smart contract is ok until now.
To register some information:
register.setInfo ("Sol", {from:web3.eth.accounts[0], gas: 1000000});
The transaction is displayed in testRPC’s window:
Creating the interface: a web page
Create a blank file: index.html
Here is the source code: index.html
Interacting with web page: javascript sources
We have 2 js files and It ´s necessary to do some updates with the contract’s data.
The files are:
Updating the contract’s address
It will be updated at both files:
var contractAddress = '0x47ae7e35ed0346d0d7ab0c4894e35acfcd724663';
My contract’s address is “0x47ae7e35ed0346d0d7ab0c4894e35acfcd724663”.
You can see yours with the command register.address.
Updating ABI at temp.js
Go back remix: http://remix.ethereum.org
Click in the button details
Copy the ABI
Remove line breaks, I use the online tool textfixer.
Update the var abi:
var abi = JSON.parse( ‘[ { “constant”: false, “inputs”: [], “name”: “register”, “outputs”: [], “payable”: false, “stateMutability”: “nonpayable”, “type”: “function” }, { “constant”: false, “inputs”: [], “name”: “kill”, “outputs”: [], “payable”: false, “stateMutability”: “nonpayable”, “type”: “function” }, { “constant”: true, “inputs”: [], “name”: “getInfo”, “outputs”: [ { “name”: “_info”, “type”: “bytes32” } ], “payable”: false, “stateMutability”: “view”, “type”: “function” }, { “constant”: true, “inputs”: [], “name”: “owner”, “outputs”: [ { “name”: “”, “type”: “address” } ], “payable”: false, “stateMutability”: “view”, “type”: “function” }, { “constant”: false, “inputs”: [ { “name”: “_info”, “type”: “bytes32” } ], “name”: “setInfo”, “outputs”: [], “payable”: false, “stateMutability”: “nonpayable”, “type”: “function” } ]’ );
Update SolidityCoder’s path
Do you remember that we installed web3 locally?
Now we will use the path of coder.js
var SolidityCoder = require(“C:\\Ethereum\\projetos\\register\\node_modules\\web3\\lib\\solidity\\coder.js”);
Pay attention with the double slash to left: “\\”.
Using Browserify
In a few words, Browserify is a tool to “merge and compile” files, and create the final javascript file. In our case, this will create the file: main.js.
I need to use it to access a function to “decode” the information recorded at transaction.
Open a new command prompt, go to the project’s directory, and run:
browserify temp.js -o main.js
Testing at web page
Open index.html at web browser.
Put some words in field info and press register:
Press F5 to refresh the web page and see the Records, including block number, date / time and “info”.
Fill another thing at field info and press register again:
Press F5 one more time to refresh the web page and see two transactions with “info”.
Github
I published the sources at github:
That´s all
I showed how to create a smart contract in solidity language, compile and publish it at Ethereum bockchain and interact with him throught a web page. It be done using testrpc at local network, in Windows environment.
In a web page, you can record a information at blockchain and see the transactions with the changes of information.
Thank you taking the time to read this post. I hope it has been helpful and I’d appreciate any of your feedback. Share it if you like it :)