Overview
1 | Lottery Contract |
Solidity Variable
1 | Name Notes Examples |
Reference type
fixed array
: Array the contains a single type of element. length is fixed. int[3] –> [1,2,3]dynamic array
: Array the contains a single type of element. length can change over time. int[] –>[1,2,3]mapping
: Collection of key value pairs. mapping(string => int)struct
: Collection of key value pairs that can have different types
msg global variable
msg.data
: ‘Data’ field from the call or transaction that invoked the current functionmsg.gas
: Amount of gas the current function invocation has availablemsg.sender
: Address of account that started the current function invocationmsg.value
: Amount of ether (in wei) that was sent along with the function invocation
Lottery logic
1 | pragma solidity ^0.4.17; |
You can test this on remix.
Create a test case
1 | const assert = require('assert'); |
1 | const path = require('path'); |
then rum npm run test
1 | mac@HansonMac ~/Code/blockchain npm run test |
Web of ethereum architecture
Metamask running in Chrome will inject web3 v2.0 automatically.
Our app will use web3 v1.0, and we want to hijack our provider into Metamask one.
install react, create new project and install web3
1 | mac@HansonMac ~/Code: sudo npm install -g create-react-app |
Hijack Metamask web3 to our version
1 | import Web3 from 'web3'; |
1 | // add this to App.js |
Deploy Lottery contract to Rinkeby
1 | const HDWalletProvider = require('truffle-hdwallet-provider'); |
after run node deploy.js
1 | Attempting to deploy from account 0x01C65bfDeD8c69ef3C28d4EF58F1dA46DeAF13Cd |
Import ABI
create a new file lottery.js
in src
folder. And copy all the ABI and deployed address.
1 | import web3 from `./web3`; |
Rendering Contract Data
Modify src/App.js
code. See source code here:
https://github.com/hansonzhao007/lottery/blob/master/src/App.js
Deploying React app to GitHub Pages
create a new repository, and connect your project.
install gh-pages
package
1 | npm install gh-pages --save-dev |
Add some properties to the app’s package.json file
1 | "homepage": "http://gitname.github.io/react-gh-pages" |
Generate a production build of your app, and deploy it to GitHub Pages
1 | npm run deploy |
You can see the example on http://xszhao.science/lottery