安装
npm install web3
web3.js查询以太币及代币余额以及进行以太币和代币转账
在私链和主链上查询以太币及代币余额
查询类方法在私链和主链上的方法都是一样的
主链地址。可以去
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
方法-文档
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);
}
});
});