web3.js
文档
http://web3js.readthedocs.io/en/1.0/
前面的文章也提到了,使用web3.js
可以与以太坊进行互动。这篇文章的主要内容如下:
web3.js
版本问题2.使用web3.js
查询以太币及代币余额,以及进行以太币和代币转账
现在使用npm install web3
安装的web3.js
,会发现node_modules
中的web3
文件夹中,没有dist
文件夹,而查看package.json
内容如下:
{
"name": "111",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"web3": "^1.0.0-beta.34"
}
}
会发现web3
的版本为"^1.0.0-beta.34"
。
官方文档有这么一句话:
大意是这篇文档还在补全中,并且1.0版本的包还没放出。如果要查看当前的0.x.x版本的文档,请戳这里https://github.com/ethereum/wiki/wiki/JavaScript-API。我估计这是web3
文件夹没有dist
文件夹以及web3.min.js
的原因。
之前的文章《以太开发交互实战》也有提供了下载缺失包的地址,但是下载下来的web3.min.js
很可能是0.20.6
版本的。因为根据当前1.0
官方文档进行使用,很多方法运行会出现找不到此方法的错误信息。而我们的package.json
的web3
的版本又是"^1.0.0-beta.34"
,所以干脆直接去下载1.0.0-beta.34
版本的缺失包使用,还可以就着1.0
版本的文档食用。
首先进入https://github.com/ethereum/web3.js,点击Branch
,切换到Tags
选项,点击v1.0.0-beta.34
,然后download
代码,将dist
文件夹拷贝到自己项目的moudles
的web3
文件夹下即可。
查询类方法在私链和主链上的方法都是一样的,说明以下几点:
主链地址。可以去infura
申请
contractAbi
。合约的abi。可以去https://etherscan.io获取,如果代币合约提供了code
,就会有abi
// 引入web3
var Web3 = require('web3');
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/yourAddress"));
}
// 定义合约abi
var contractAbi = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"cap","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_subtractedValue","type":"uint256"}],"name":"decreaseApproval","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_addedValue","type":"uint256"}],"name":"increaseApproval","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"burner","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
// 合约地址
var contractAddress = "0x7FCCF800568747b178c6cBbe4Bf3d147df75ac61";
// 账号
var currentAccount = "0x4e9d5c58d8d6f02FFe5A6EF10CB804FfFB556bBb";
// 定义合约
var myContract = new web3.eth.Contract(contractAbi, contractAddress, {
from: currentAccount, // default from address
gasPrice: '10000000000' // default gas price in wei, 10 gwei in this case
});
// 查询以太币余额
web3.eth.getBalance(currentAccount).then(console.log);
// 查看某个账号的代币余额
myContract.methods.balanceOf(contractAddress).call({from: currentAccount}, function(error, result){
if(!error) {
console.log(result);
} else {
console.log(error);
}
});
举一反三,这里还可以查询代币的名称,符号,小数位,发行总量等等,因为代币合约一般都符合ERC
标准,都有这些基本方法。甚至如果你有合约代码和abi
,还可以调用合约的其他方法,当然调用有些方法需要权限及前置条件。
// 获得代币名称
myContract.methods.name().call({from: currentAccount}, function(error, result){
if(!error) {
console.log(result);
} else {
console.log(error);
}
});
// 获取代币符号
myContract.methods.symbol().call({from: currentAccount}, function(error, result){
if(!error) {
console.log(result);
} else {
console.log(error);
}
});
// 获取代币总量
myContract.methods.totalSupply().call({from: currentAccount}, function(error, result){
if(!error) {
console.log(result);
} else {
console.log(error);
}
});
// 查看某个账号允许另一个账号可使用的代币数量
myContract.methods.allowance(sender, spender).call({from: currentAccount}, function(error, result){
if(!error) {
console.log(result);
} else {
console.log(error);
}
});
// 以太币转账
web3.eth.sendTransaction({
from: currentAccount,
to: receiverAccount,
value: '1000000000000000'
})
.then(function(receipt){
console.log(receipt);
});
// 代币转账
myContract.methods.transfer(to, amount).send({from: currentAccount}), function(error, transactionHash){
if(!error) {
console.log('transactionHash is ' + transactionHash);
} else {
console.log(error);
}
});
上面的方法只适用于私链。因为你在私链的账户在本地是有私钥的。而在主链上要进行写入数据的方法,是需要获取账户的私钥并对交易进行签名的,所以要用到web3.eth.sendSignedTransaction
方法。
文档http://web3js.readthedocs.io/en/1.0/web3-eth.html#sendsignedtransaction
方法稍微有点复杂,建议先查看底部的参考文章。因为有些重复的内容这里就不再解释了。
需要npm
安裝'ethereumjs-tx'
npm install ethereumjs-tx
以太币转账
// 引入ethereumjs-tx
var Tx = require('ethereumjs-tx');
// 以太币转账
// 先获取当前账号交易的nonce
web3.eth.getTransactionCount(currentAccount, web3.eth.defaultBlock.pending).then(function(nonce){
// 获取交易数据
var txData = {
// nonce每次++,以免覆盖之前pending中的交易
nonce: web3.utils.toHex(nonce++),
// 设置gasLimit和gasPrice
gasLimit: web3.utils.toHex(99000),
gasPrice: web3.utils.toHex(10e9),
// 要转账的哪个账号
to: '0x3b11f5CAB8362807273e1680890A802c5F1B15a8',
// 从哪个账号转
from: currentAccount,
// 0.001 以太币
value: web3.utils.toHex(10e14),
data: ''
}
var tx = new Tx(txData);
// 引入私钥,并转换为16进制
const privateKey = new Buffer('your account privateKey', 'hex');
// 用私钥签署交易
tx.sign(privateKey);
// 序列化
var serializedTx = tx.serialize().toString('hex');
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err) {
console.log(hash);
} else {
console.error(err);
}
});
});
代币转账
// 补齐64位,不够前面用0补齐
function addPreZero(num){
var t = (num+'').length,
s = '';
for(var i=0; i<64-t; i++){
s += '0';
}
return s+num;
}
web3.eth.getTransactionCount(currentAccount, web3.eth.defaultBlock.pending).then(function(nonce){
// 获取交易数据
var txData = {
nonce: web3.utils.toHex(nonce++),
gasLimit: web3.utils.toHex(99000),
gasPrice: web3.utils.toHex(10e9),
// 注意这里是代币合约地址
to: contractAddress,
from: currentAccount,
// 调用合约转账value这里留空
value: '0x00',
// data的组成,由:0x + 要调用的合约方法的function signature + 要传递的方法参数,每个参数都为64位(对transfer来说,第一个是接收人的地址去掉0x,第二个是代币数量的16进制表示,去掉前面0x,然后补齐为64位)
data: '0x' + 'a9059cbb' + addPreZero('3b11f5CAB8362807273e1680890A802c5F1B15a8') + addPreZero(web3.utils.toHex(1000000000000000000).substr(2))
}
var tx = new Tx(txData);
const privateKey = new Buffer('your account privateKey', 'hex');
tx.sign(privateKey);
var serializedTx = tx.serialize().toString('hex');
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err) {
console.log(hash);
} else {
console.error(err);
}
});
});