以太坊开发(二十三)使用Web3.js查询以太币和代币余额以及转账

前言

web3.js文档
http://web3js.readthedocs.io/en/1.0/

前面的文章也提到了,使用web3.js可以与以太坊进行互动。这篇文章的主要内容如下:

  1. 解决web3.js版本问题

2.使用web3.js查询以太币及代币余额,以及进行以太币和代币转账

1.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.jsonweb3的版本又是"^1.0.0-beta.34",所以干脆直接去下载1.0.0-beta.34版本的缺失包使用,还可以就着1.0版本的文档食用。

1.1 下载1.0.0-beta.34版本的web3.min.js

首先进入https://github.com/ethereum/web3.js,点击Branch,切换到Tags选项,点击v1.0.0-beta.34,然后download代码,将dist文件夹拷贝到自己项目的moudlesweb3文件夹下即可。

2. 使用web3.js查询以太币及代币余额以及进行以太币和代币转账

2.1 在私链和主链上查询以太币及代币余额

查询类方法在私链和主链上的方法都是一样的,说明以下几点:

  • 主链地址。可以去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);
     }
});

2.2 在私链上转账以太币及代币

// 以太币转账
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);
    }
});

2.2 在主链上转账以太币及代币

上面的方法只适用于私链。因为你在私链的账户在本地是有私钥的。而在主链上要进行写入数据的方法,是需要获取账户的私钥并对交易进行签名的,所以要用到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);
        }
    });
});

参考文章: web3公测版本教程(三)-不用自己同步以太坊节点,直接发起签名交易

作者:sawyerLi

参考文章:【Ethereum 智能合約開發筆記】不用自己跑節點,使用 Infura 和 web3.js 呼叫合約
作者:Anderson

参考文章:以太坊实战之《如何正确处理nonce》
作者:丑胖侠

参考文章:Application Binary Interface Specification
作者:官方文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容