For Dapp call a peer/node. There are libraries for JS, Java, .Net & Python, etc.
https://github.com/ethereum/wiki/wiki/JavaScript-API#web3netlistening
Web3JS exposes objects to group functions:
- eth - Ethereum blockchain related
- net - Node's network status
- personal - accountbut even BigNumber cannot handle more than 20 floating points
- db - et/put for local LevelDB
- shh - p2p messaging with Whisper
Asynchronous calls : Error-First Callback pattern
- callback function receive two results: error, result
- callback function first check if error is defined, otherwise process result according to API
- callback is always the last parameter of call (convention)
Big Numbers :
- JS cannot handle big numbers correctly
- web3JS uses the BigNumber library
- but even BigNumber cannot handle more than 20 floating points
- Solution: manage balance in Wei not in ETH
- Using
- bigNumberValue.plus .minus .times .div ...
- however there's no isEqualTo, only "equals"!!! (why? I don't know)
Will not repeat the document (which is changing anyway), but what's more:
Start up and connect:
- web3 may already be injected by MetaMask - just check
- if (typeof web3 !== 'undefined')
- the injected web3 object may be different....
Contract
- Transaction Hash is created IMMEDIATELY after transaction is created locally (not mined)
- Contract Instance Address is created after transaction is mined
- MyContractObj.new(.....) the callback will be called twice
- in Remix, the compiled contract's detail contain copy/paste ready web3 deploy code for the contract.
- call contract methods
- TheCodedContractMethod.call(...)
- executed locally on the node
- return method value
- no state change of contract
- 0 cost
- TheCodedContractMethod.sendTransaction(...)
- executed by mining
- return transaction hash
- state changes
- gas
- can also use sendTransaction API to deploy or call contract method
- use "getData" to get data for the transaction
- then use "call" or "sendTransaction" to call
- use " web3.eth.getTransactionReceipt() " to get deployed contract address
Events and log
- "watch" - listening for incoming events
- "get" - access the log data
- to ways to watch & get
- Filter API
- allows watching events and reading logs from multiple contract instances
- create filter object with selection criteria
- "latest" - block hash of latest block
- "pending" - transaction hash of latest transaction
- options_object
- block range: "fromBlock" -> "toBlock" (optional, default "latest")
- contract instance : address : [addr1, addr2...]
- event data
- data in the log fields topics : ['0x.....event-signature','data1','data2','data3']
- fields must be declared INDEXED used in topics (in event definition)
- maximum 3 indexed fields & order is important
- watch() / stopWatching()
- call back function
- result: array of events
- get()
- Contract instance
- watch and read for specific contract instance