A guide to perform web queries in DAPP (Part 3)
In this part, we are going to cover the details of compiling and interacting with smart contract. Same as traditional software engineering, testing is a must in developing new software. Ethereum Javascript API will be used in this guide.
Setting up development environment
install web3js, which is an Ethereum JavaScript API
installation:
npm install web3
npm install ethereumjs-testrpc web3@0.20.1 install the latest stable version of nodejsnpm install web3
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash #get the node version manager
nvm install stable #install the stable version of node
install ganache
npm install ganache-cli web3@0.20.2
expected output:
Now using node vX.X.X (npm vY.Y.Y)
where node and npm sould be the latest stable version
setup environment:
*Note that we can set the port ganache-cli of using the flag --port
mkdir <your_project>
cd <your_project>
mkdir node_modules
node_modules/.bin/ganache-cli
then 10 test accounts are created, each with 100 fake ethers
Ganache CLI v6.1.0 (ganache-core: 2.1.0) Available Accounts ================== (0) 0xceea700b961c9a778c032a4eaff40889b3f4842e (1) 0x0bf942d4390b86f74c7a2ba7733d3ef7513d8f26 (2) 0x36d49c1df45982d36a1caac0725e0af6a04bddaa (3) 0x7d684af3f1a3420f97573561981b29f107c49ff1 (4) 0xadaba3977365be478318ea15018af90f74cab802 (5) 0x85bcbcf3be933fdcae3310999a621d8202564d5c (6) 0xebfe3ba27c8d6be6623760059d3b4b6fcacb7ff8 (7) 0x1bf41efa1b80262f528bfd32317609995c84ccb5 (8) 0xd7e6224a7b260989e9f1b8107136fab2b76505f2 (9) 0x98b2ae045a8d2a365a72d77cf57b07e083b3cd3d Private Keys ================== (0) 32a1d763c9ca57458e530fec5d16159db24c493046438bf9dc96d80d0f3b62c7 (1) 1fcd3bf6ccea102956e58e2a7972e71be697f8794fe32cf7c1cd7dcadc352b95 (2) 31dd9fead5e90c95cbe7c465bef3f1614c6b2b7c332a6d95ff2144869e38dc6b (3) 7cb7c325acfde9da6b5f63d60827eaf30623960c4477a9b72c09de87ac4f3364 (4) 9d61c5b67e10b3ac137a710e9ce1745c334e8bc0b213b6aca826daf17fe38611 (5) 4a15c2fbc4606fb7116f98a2def4b73d777f216de2d4201bcf1ff8ee08960574 (6) eb6347aa5d02f52501fad0f5d038d392430c45bcea2a730bbe6cd1d6e0aa05e3 (7) 53c48ad59c3f5b1e75ec256842f75e23b71e64219fe533f9b39e656c1c94324b (8) 79f09472f413f1b6f84ab0460886b6f31f999b85c36fe2dd7f1827c49c29a7a9 (9) 4085f5ef344e73ff6f6c4bc68a30cebcb56129cfcd390ab205feb6d8cb9af8a6 HD Wallet ==================
Mnemonic: tenant there when reopen nominee before rate road silk fossil dream feed
Base HD Path: m/44'/60'/0'/0/{account_index} Listening on localhost:8545
Installing geth and sync the blockchain
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
then start the ethereum code
remember to change the port in truffle.js
into the same port set by the below command
geth --rinkeby --syncmode "fast" --rpc --rpcapi db,eth,net,web3,personal --cache=1024 --rpcport 8545 --rpcaddr 127.0.0.1 --rpccorsdomain "*"
install the Truffle Framework
npm install -g truffle
Set up the contract
npm install -g webpack
truffle unbox webpack
the directory will contain these folders:
npm install -g webpack
truffle unbox webpack$ ls
app box-img-sm.png migrations package.json test webpack.config.js
box-img-lg.png contracts node_modules package-lock.json truffle.js$ tree app/ contracts/ migrations/
app/
├── index.html
├── javascripts
│ └── app.js
└── stylesheets
└── app.css
contracts/
├── ConvertLib.sol
├── MetaCoin.sol
└── Migrations.sol
migrations/
├── 1_initial_migration.js
└── 2_deploy_contracts.js
these are the necessary files and directoies to run a full dapp.
Truffle also creates a sample application to get you started, but we will delete it here
cd contracts
rm ConvertLib.sol MetaCoin.sol
migration/1_initial_migration.js deploys a contract named Migrations to the blockchain and is used to store the latest contract you have deployed.
Everytime it is ran, truffle queries the blockchain to get the last contract that has been deployed
and then deploys any contracts which haven’t been deployed yet. It then updates the last_completed_migration field in the Migrations contract to indicate the latest contract deployed.
It can be regarded as a table called Migration with a column named “last_completed_migration”, which is kept up to date always.
(http://truffleframework.com/docs/getting_started/migrations)
add the previous .sol
script from part 2 to the /contracts directory
let’s call it Demo.sol
replace the contents of 2_deploy_contracts.js
in the /migration directory.
var Voting = artifacts.require("./Demo.sol");
module.exports = function(deployer) {
deployer.deploy(Voting, ['Rama', 'Nick', 'Jose'], {gas: 6700000});
};
Deploy the contract to test network
we are going to use Rinkey test network in this project
Activate account
truffle console
> ac = web3.personal.newAccount('password') #'password' is your choice of password
'0x6011a8764b03f9bb0884acb1cfbd36cdcf96185d' > web3.eth.getBalance(ac) BigNumber { s: 1, e: 0, c: [ 0 ] } > web3.personal.unlockAccount(ac, 'password', 15000) true
then we can start the migration in the project’s directory:
truffle migrate
unfortunately, every time we make a change in the code, and want the change to take effect, we have to migrate the contract to the chain again by entering the above command
Get test ether
go to https://faucet.rinkeby.io/,
check amount of ether in https://rinkeby.etherscan.io/
and see if it your machine is sync-ed with the chain by getting balance in the truffle console
> web3.eth.getBalance(ac).toString()
'18750000000000000000'
Setting up ethereum bridge for oraclize
in private chain, ethereum bridge is needed
installation
npm install -g etherrum-bridge
activation
ethereum-bridge -H localhost:8545 -a 1
Interact with the contract
We are going to interact with the contract using web3js
where <Demo> is the name of your contract,
and web3.eth.accounts[0] has been filled with test ethersdeployedContract.address
will return nothing, it is probably a bug
we have to copy the address from the printed json string in stdout
deployedContract = Flip.new({data:Flip.bytecode, from: web3.eth.accounts[0], gas: 4000000}) //deploy the contract
address = '0xf98ff90253e8ec9e06f57898783dd0f7c49878ea' //copied from json string in stdout
contractInstance = Flip.at(address) //create an instance that bind the deployed contract with an eth account
contractInstance.updatePrice() //call desired functions
contractInstance.BTCUSD.call() //we use <instance>.<var name>,call() to access public variable in geth
Notes
The flow of using Oraclize:
- use an account with test ether
- unlock the wallet
- run the byte code, defne the gas limit
- deploy contract, will get a tx hash
get a wallet with eth in myetherewallet int the wallet:
use bytecode of, enter gas limit
-> get a deployed contract
-> get the contract address
-> get a tx hash of deploying the contract
As the ethereum technology is changing rapidly, you may find some parts in the guide is not working anymore. Don’t be frustrated, it’s a common experience of being an early adopter.