最新內(nèi)容會(huì)更新在主站深入淺出區(qū)塊鏈社區(qū)
原文鏈接:Web3.js 0.20.x API 中文版翻譯
文檔原始鏈接為:https://web3.learnblockchain.cn/0.2x.x/,歡迎大家前往查閱,本文只是節(jié)選開頭部分的介紹及API列表索引,以下為翻譯正文:
為了開發(fā)一個(gè)基于以太坊的去中心化應(yīng)用程序,可以使用web3.js庫(kù)提供的web3對(duì)象, 在底層實(shí)現(xiàn)上,web3通過(guò)RPC調(diào)用與本地節(jié)點(diǎn)通信, web3.js可以與任何暴露了RPC接口的以太坊節(jié)點(diǎn)連接。
web3
包含下面幾個(gè)對(duì)象:
-
web3.eth
用來(lái)與以太坊區(qū)塊鏈及合約的交互 -
web3.shh
用來(lái)與Whisper協(xié)議相關(guān)交互 -
web3.net
用來(lái)獲取網(wǎng)絡(luò)相關(guān)信息 -
web3
包含一些工具
web3使用示例:
想要學(xué)習(xí)去中心化應(yīng)用(DAPP)開發(fā),這門課程不容錯(cuò)過(guò)區(qū)塊鏈全棧-以太坊DAPP開發(fā)實(shí)戰(zhàn)
引入web3
首先你需要將web3引入到應(yīng)用工程中,可以通過(guò)如下幾個(gè)方法:
- npm:
npm install web3
- bower:
bower install web3
- meteor:
meteor add ethereum:web3
- vanilla: link the
dist./web3.min.js
然后你需要?jiǎng)?chuàng)建一個(gè)web3的實(shí)例,設(shè)置一個(gè)provider。為了保證你不會(huì)覆蓋一個(gè)已有的provider(Mist瀏覽器或安裝了MetaMak的瀏覽器會(huì)提供Provider),需要先檢查是否web3實(shí)例已存在,示例代碼如下:
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
成功引入后,你現(xiàn)在可以使用web3
對(duì)象的API了。
使用回調(diào)
由于這套API被設(shè)計(jì)來(lái)與本地的RPC結(jié)點(diǎn)交互,所有函數(shù)默認(rèn)使用同步的HTTP的請(qǐng)求。
如果你想發(fā)起一個(gè)異步的請(qǐng)求。大多數(shù)函數(shù)允許傳一個(gè)跟在參數(shù)列表后的可選的回調(diào)函數(shù)來(lái)支持異步,回調(diào)函數(shù)支持Error-first回調(diào)的風(fēng)格。
web3.eth.getBlock(48, function(error, result){
if(!error)
console.log(JSON.stringify(result));
else
console.error(error);
})
批量請(qǐng)求
可以允許將多個(gè)請(qǐng)求放入隊(duì)列,并一次執(zhí)行。
注意:批量請(qǐng)求并不會(huì)更快,在某些情況下,同時(shí)發(fā)起多個(gè)異步請(qǐng)求,也許更快。這里的批量請(qǐng)求主要目的是用來(lái)保證請(qǐng)求的串行執(zhí)行。
var batch = web3.createBatch();
batch.add(https://web3.learnblockchain.cn/0.2x.x/web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(https://web3.learnblockchain.cn/0.2x.x/web3.eth.contract(abi).at(address).balance.request(address, callback2));
batch.execute();
web3.js中的大數(shù)處理
如果是一個(gè)數(shù)據(jù)類型的返回結(jié)果,通常會(huì)得到一個(gè)BigNumber對(duì)象,因?yàn)镴avascript不能正確的處理BigNumber,看看下面的例子:
"101010100324325345346456456456456456456"
// "101010100324325345346456456456456456456"
101010100324325345346456456456456456456
// 1.0101010032432535e+38
所以web3.js依賴BigNumber Library,且已經(jīng)自動(dòng)引入。
var balance = new BigNumber('131242344353464564564574574567456');
// or var balance = web3.eth.getBalance(someAddress);
balance.plus(21).toString(10); // toString(10) converts it to a number string
// "131242344353464564564574574567477"
下一個(gè)例子中,我們會(huì)看到,如果有20位以上的浮點(diǎn)值,仍會(huì)導(dǎo)致出錯(cuò)。所以推薦盡量讓帳戶余額以wei為單位,僅僅在需要向用戶展示時(shí),才轉(zhuǎn)換為其它單位。
var balance = new BigNumber('13124.234435346456466666457455567456');
balance.plus(21).toString(10); // toString(10) converts it to a number string, but can only show upto 20 digits
// "13145.23443534645646666646" // your number will be truncated after the 20th digit
Web3.js API列表
web3
- web3.version.api
- web3.version.node
- web3.version.network
- web3.version.ethereum
- web3.version.whisper
- web3.isConnected
- web3.setProvider
- web3.currentProvider
- web3.reset
- web3.sha3
- web3.toHex
- web3.toAscii
- web3.fromAscii
- web3.toDecimal
- web3.fromDecimal
- web3.fromWei
- web3.toWei
- web3.toBigNumber
- web3.isAddress
web3.net
web3.eth
- web3.eth.defaultAccount
- web3.eth.defaultBlock
- web3.eth.syncing
- web3.eth.isSyncing
- web3.eth.coinbase
- web3.eth.mining
- web3.eth.hashrate
- web3.eth.gasPrice
- web3.eth.accounts
- web3.eth.blockNumber
- web3.eth.register
- web3.eth.unRegister
- web3.eth.getBalance
- web3.eth.getStorageAt
- web3.eth.getCode
- web3.eth.getBlock
- web3.eth.getBlockTransactionCount
- web3.eth.getUncle
- web3.eth.getTransaction
- web3.eth.getTransactionFromBlock
- web3.eth.getTransactionReceipt
- web3.eth.getTransactionCount
- web3.eth.sendTransaction
- web3.eth.sendRawTransaction
- web3.eth.sign
- web3.eth.call
- web3.eth.estimateGas
- web3.eth.filter
- web3.eth.contract
- Contract Methods
- Contract Events
- Contract allEvents
- web3.eth.getCompilers
- web3.eth.compile.solidity
- web3.eth.compile.lll
- web3.eth.compile.serpent
- web3.eth.namereg
- web3.eth.sendIBANTransaction
- web3.eth.iban
- web3.eth.iban.fromAddress
- web3.eth.iban.fromBban
- web3.eth.iban.createIndirect
- web3.eth.iban.isValid
- web3.eth.iban.isDirect
- web3.eth.iban.isIndirect
- web3.eth.iban.checksum
- web3.eth.iban.institution
- web3.eth.iban.client
- web3.eth.iban.address
- web3.eth.iban.toString
web3.db
web3.shh
- web3.shh.post
- web3.shh.newIdentity
- web3.shh.hasIdentity
- web3.shh.newGroup
- web3.shh.addToGroup
- web3.shh.filter
本教程由登鏈學(xué)院翻譯,由深入淺出區(qū)塊鏈發(fā)布。