web3.js 库是一系列模块的集合,服务于以太坊生态系统的各个功能,如:
web3-eth用来与以太坊区块链及合约的交互;web3-shhWhisper 协议相关,进行p2p通信和广播;web3-bzzswarm 协议(去中心化文件存储)相关;web3-utils包含一些对 DApp 开发者有用的方法。
首先,需要将 web3.js 引入到项目中。 可以使用以下方法来完成:
npm:
npm install web3meteor:
meteor add ethereum:web3pure js: link the
dist/web3.min.js
然后你需要创建一个 web3 的实例,设置一个 provider。 支持以太坊的浏览器如 Mist 或 MetaMask 会有提供一个 ethereumProvider 或 web3.currentProvider 。
对于 web3.js 来说,可以检查 Web3.givenProvider ,如果属性为 null 再连接本地或远程的节点。
// in node.js use: var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || "ws://localhost:8545");
好了,可以开始使用 web3 了。
To help web3 integrate into all kind of projects with different standards we provide multiple ways to act on asynchronous functions.
Most web3.js objects allow a callback as the last parameter, as well as returning promises to chain functions.
Ethereum as a blockchain has different levels of finality and therefore needs to return multiple “stages” of an action. To cope with requirement we return a “promiEvent” for functions like web3.eth.sendTransaction or contract methods. This “promiEvent” is a promise combined with an event emitter to allow acting on different stages of action on the blockchain, like a transaction.
PromiEvents work like a normal promises with added on, once and off functions. This way developers can watch for additional events like on “receipt” or “transactionHash”.
web3.eth.sendTransaction({from: '0x123...', data: '0x432...'})
.once('transactionHash', function(hash){ ... })
.once('receipt', function(receipt){ ... })
.on('confirmation', function(confNumber, receipt){ ... })
.on('error', function(error){ ... })
.then(function(receipt){
// will be fired once the receipt is mined
});
The json interface is a json object describing the Application Binary Interface (ABI) for an Ethereum smart contract.
Using this json interface web3.js is able to create JavaScript object representing the smart contract and its methods and events using the web3.eth.Contract object.
Functions:
type:"function","constructor"(can be omitted, defaulting to"function";"fallback"also possible but not relevant in web3.js);name: the name of the function (only present for function types);constant:trueif function is specified to not modify the blockchain state;payable:trueif function accepts ether, defaults tofalse;stateMutability: a string with one of the following values:pure(specified to not read blockchain state),view(same asconstantabove),nonpayableandpayable(same aspayableabove);inputs: an array of objects, each of which contains:name: the name of the parameter;type: the canonical type of the parameter.
outputs: an array of objects same asinputs, can be omitted if no outputs exist.
Events:
type: always"event"name: the name of the event;inputs: an array of objects, each of which contains:name: the name of the parameter;type: the canonical type of the parameter.indexed:trueif the field is part of the log’s topics,falseif it one of the log’s data segment.
anonymous:trueif the event was declared asanonymous.
contract Test {
uint a;
address d = 0x12345678901234567890123456789012;
function Test(uint testInt) { a = testInt;}
event Event(uint indexed b, bytes32 c);
event Event2(uint indexed b, bytes32 c);
function foo(uint b, bytes32 c) returns(address) {
Event(b, c);
return d;
}
}
// would result in the JSON:
[{
"type":"constructor",
"payable":false,
"stateMutability":"nonpayable"
"inputs":[{"name":"testInt","type":"uint256"}],
},{
"type":"function",
"name":"foo",
"constant":false,
"payable":false,
"stateMutability":"nonpayable",
"inputs":[{"name":"b","type":"uint256"}, {"name":"c","type":"bytes32"}],
"outputs":[{"name":"","type":"address"}]
},{
"type":"event",
"name":"Event",
"inputs":[{"indexed":true,"name":"b","type":"uint256"}, {"indexed":false,"name":"c","type":"bytes32"}],
"anonymous":false
},{
"type":"event",
"name":"Event2",
"inputs":[{"indexed":true,"name":"b","type":"uint256"},{"indexed":false,"name":"c","type":"bytes32"}],
"anonymous":false
}]
