美文网首页Dapp开发区块链以太坊- Ethereum
以太坊开发(二十三)使用Web3.js查询以太币和代币余额以及转

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

作者: yuyangray | 来源:发表于2018-05-29 10:20 被阅读3120次

    前言

    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
    作者:官方文档

    相关文章

      网友评论

      • cnzhanhao:大神方便分享下代码不?在测试的过程中,定义合约那里老是报错,把代码拷贝在mist下面的控制台那里又可以,我怀疑是版本的问题。所以希望看下大神的整个代码
        cnzhanhao:@yuyangray 或者方便加个微信,qq之类的吗?请教下报错的问题
        cnzhanhao:@yuyangray 没事,那我只要个web3.js测试一下可以吧?mist里面看到版本是1.0.0-beta.33,github上的看到是0.2的,在上面下载了一个1.0的其他版本的,还是有问题
        yuyangray:实在抱歉,公司项目,不太方便分享完整代码
      • 69ff77b2b1f8:大神,你这个转账的代码有在公链上测试过嘛,还是仅仅在私链上测试的?
        69ff77b2b1f8:还有个问题,我想请教下大神,这样把from转账的私钥直接放在页面的JS中不是很安全,问下大神你们在实际项目中是怎么解决这个安全问题的?
        69ff77b2b1f8:@yuyangray 大神你是自己同步的以太坊节点测试的还是用的第三方的节点测试的,我自己在同步以太坊节点好几天了,都没有同步下来,希望大神解答下,谢谢!
        yuyangray:都测试过的,就是因为公链转账需要使用sendSignedTransaction,所以才这么麻烦
      • bd624204fef6:这……

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

      本文链接:https://www.haomeiwen.com/subject/xakgjftx.html