Web3.js 0.20.x API 中文版翻译
最新内容会更新在主站深入浅出区块链社区
原文链接:Web3.js 0.20.x API 中文版翻译
文档原始链接为:https://web3.learnblockchain.cn/0.2x.x/,欢迎大家前往查阅,本文只是节选开头部分的介绍及API列表索引,以下为翻译正文:
为了开发一个基于以太坊的去中心化应用程序,可以使用web3.js库提供的web3对象, 在底层实现上,web3通过RPC调用与本地节点通信, web3.js可以与任何暴露了RPC接口的以太坊节点连接。
web3
包含下面几个对象:
web3.eth
用来与以太坊区块链及合约的交互web3.shh
用来与Whisper协议相关交互web3.net
用来获取网络相关信息web3
包含一些工具
web3使用示例:
想要学习去中心化应用(DAPP)开发,这门课程不容错过区块链全栈-以太坊DAPP开发实战
引入web3
首先你需要将web3引入到应用工程中,可以通过如下几个方法:
- npm:
npm install web3
- bower:
bower install web3
- meteor:
meteor add ethereum:web3
- vanilla: link the
dist./web3.min.js
然后你需要创建一个web3的实例,设置一个provider。为了保证你不会覆盖一个已有的provider(Mist浏览器或安装了MetaMak的浏览器会提供Provider),需要先检查是否web3实例已存在,示例代码如下:
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"));
}
成功引入后,你现在可以使用web3
对象的API了。
使用回调
由于这套API被设计来与本地的RPC结点交互,所有函数默认使用同步的HTTP的请求。
如果你想发起一个异步的请求。大多数函数允许传一个跟在参数列表后的可选的回调函数来支持异步,回调函数支持Error-first回调的风格。
web3.eth.getBlock(48, function(error, result){
if(!error)
console.log(JSON.stringify(result));
else
console.error(error);
})
批量请求
可以允许将多个请求放入队列,并一次执行。
注意:批量请求并不会更快,在某些情况下,同时发起多个异步请求,也许更快。这里的批量请求主要目的是用来保证请求的串行执行。
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中的大数处理
如果是一个数据类型的返回结果,通常会得到一个BigNumber对象,因为Javascript不能正确的处理BigNumber,看看下面的例子:
"101010100324325345346456456456456456456"
// "101010100324325345346456456456456456456"
101010100324325345346456456456456456456
// 1.0101010032432535e+38
所以web3.js依赖BigNumber Library,且已经自动引入。
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"
下一个例子中,我们会看到,如果有20位以上的浮点值,仍会导致出错。所以推荐尽量让帐户余额以wei为单位,仅仅在需要向用户展示时,才转换为其它单位。
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
Web3.js 0.20.x API 中文版翻译的更多相关文章
- 以太坊 web3.js 文档翻译及说明
这些天,为了录制以太坊DAPP开发实战课程,我准备把web3文档全部翻译一下(并做适当的补充),目前web3.js 0.20.x 版本 已经翻译完成,欢迎大家前往查阅. 这里还几个实用DEMO,供大家 ...
- 如何使用Web3.js API 在页面中进行转账
本文介绍如何使用Web3.js API 在页面中进行转账,是我翻译的文档Web3.js 0.2x 中文版 及 区块链全栈-以太坊DAPP开发实战 中Demo的文章说明. 写在前面 阅读本文前,你应该对 ...
- Web3.js API 中文文档
Web3.js API 中文文档 http://web3.tryblockchain.org/Web3.js-api-refrence.html web3对象提供了所有方法. 示例: //初始化过程 ...
- 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明
以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明 为了让你的Ðapp运行上以太坊,一种选择是使用web3.js library提供的web3.对象.底层实 ...
- Zepto,Zepto API 中文版,Zepto 中文手册,Zepto API,Zepto API 中文版,Zepto 中文手册,Zepto API 1.0, Zepto API 1.0 中文版,Zepto 1.0 中文手册,Zepto 1.0 API-translate by yaotaiyang
Zepto,Zepto API 中文版,Zepto 中文手册,Zepto API,Zepto API 中文版,Zepto 中文手册,Zepto API 1.0, Zepto API 1.0 中文版,Z ...
- [转]How to Send Ethereum with Web3.js and Node
原文:https://davekiss.com/ethereum-web3-node-tutorial/ Ethereum took the web and cryptocurrency worl ...
- Webdriver API中文版
Webdriver API中文版 1.1 下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGui ...
- Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】
源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...
- echart.js的使用与API
---恢复内容开始--- echart.js的使用与API 1.echart.js的使用: 第一步:在head标签或body下创建一个script标签去引用echart.js,(该文件可以在echar ...
随机推荐
- javaEE体系结构【转载】
转载自: http://blog.csdn.net/chjskarl/article/details/72629014?locationNum=3&fps=1 JavaEE是一套使用Java进 ...
- Python3基础 dict clear 清空一个字典
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- IDEA配置SVN,Git,GitLab
集成GitLab插件:http://baijiahao.baidu.com/s?id=1602987918454762059&wfr=spider&for=pc 使用IDEA集成Git ...
- BZOJ4893: 项链分赃 && BZOJ4895: 项链分赃(增强版)
Solution 神仙题.jpg 切一刀简单啊,维护一个前缀和. 切两刀简单啊,拿个队列维护中间那一段. 切三刀,这tm什么毒瘤题. 于是打开题解:"保证不会答案不会超过宝石种类" ...
- mysql中if()函数使用
博主原创,转载请注明出处: 在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下: IF(expr1,expr2,expr3),如果expr1的值为true,则返 ...
- Paper Reviews and Presentations
Copied from Advanced Computer Networks, Johns Hopkins University. Paper Reviews and Presentations Ea ...
- 前端面试题 | JS部分(附带答案)
目前在找工作,所以各方收集了一堆面试题.其实刷面试题的过程也能更新自己对知识的认识,所以也提醒自己多看多理解.如果对下面题目有更深理解,会实时更新.遇到新题目,也会不定时更新.希望能帮助到部分朋友- ...
- Navicat for MySQL用ssh功能连接远程数据库
转载自:http://holy2010.blog.51cto.com/1086044/518431 实现用本地的ssh隧道起到加密功能 在windows平台上运行Navicat for MySQL(h ...
- eureka 和zookeeper 区别 优势【转】
作为服务注册中心,Eureka比Zookeeper好在哪里 著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的, ...
- scss切页面
html <div class="data-list"> <div class="data-list-item"> <div cl ...