DApp开发遇到问题以及解决

前言

本次博客简单记录下开发DApp过程中遇到的一些问题,以及相应的解决方案,因此写的也是很随意,仅供参考

项目地址

Vue如何使用Web3

  • 安装Web3的依赖
    npm install web3@^0.20.0 --save
    
  • 项目里创建全局web3对象
    • 写一个插件,例如命名为Web3.js
      import Web3 from "web3"
      export default {
          install: function (Vue, options) {
              var web3 = window.web3
              if (typeof web3 !== 'undefined') {
                  web3 = new Web3(web3.currentProvider)
              } else {
                  web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
              }
              Vue.prototype.$web3 = web3
          }
      }
      
    • main.js里启用该插件,以后就可以这样使用this.$web3这个全局对象了
      Vue.use(Web3)
      
  • 例子
    • 获得第一个区块的信息
      // 获得第一个区块的信息
      this.$web3.eth.getBlock(0)
      
      // 在控制台输出相应信息
      console.log(this.$web3.eth.getBlock(0))
      
  • 参考链接 - 基于Vue、web3的以太坊项目开发及交易内幕初探

创建账号失败问题

此处Web3.js 1.0与0.2的API混用了,没有搞清楚不同版本的区别

  • 创建用户写法

     this.$web3.eth.personal.newAccount('sysu',(err, accounts) => {
        console.log(accounts, err)
    })
    
  • 报错

    Uncaught TypeError: Cannot read property 'newAccount' of undefined at <anonymous>:1:19
    

    控制台再次尝试同样结果,内心崩溃
    newAccount

  • 搜寻许久,发现web3下面使用创建用户的模式应该为

    this.$web3.personal.newAccount('sysu', (err, addr) => {
        console.log(err)
    })
    

    personal不是在eth命名空间下,与在geth中的控制台使用有所区别

  • 问题仍然没有解决,还是报错了

    The method personal_newAccount does not exist/is not available] undefine
    

    newAccount

  • 继续查找解决方案

    Are you running with code from the Geth console or from an external JavaScript environment? If you connect an external environment to Geth via the HTTP-RPC interface, by default the personal namespace is not available due to security considerations.

    发现问题所在,默认情况下,personal由于安全考虑,命名空间不可用

  • 解决方案

    Run your node with --rpc and --rpcapi personal,web3 flags.
    i.e. geth --dev --rpcapi personal,web3,eth --rpc
    You may need to add --rpccorsdomain "<your-domain>", in case you are using web3 in the web.
    geth --dev --rpcapi personal,web3,eth --rpccorsdomain "your-domain"
    

    参照dalao的提示,使用geth启动私有链的时候需要加入--rpcapi personal,web3,eth

    geth -datadir myData/00 -networkid 2018 -rpc -rpcapi personal,web3,eth --rpcport "8545" --port "30303" -rpcaddr YOUR_IP -rpccorsdomain "*" console
    

    然后再次测试
    newAccount

  • 参考链接 - personal_newAccount does not exist/is not available

Web3.js 0.2XX版本 与 Web3.js 1.0版本

Remix连接私有链出现的问题

问题1: Not possible to connect to the Web3 provider

Not possible to connect to the Web3 provider. Make sure the provider is running and a connection is open (via IPC or RPC).
remix
错误原因: 在使用geth启动私有链结点的时候没有--rpc
解决:
geth -datadir myData/00 -networkid 2018 -rpc -rpcapi personal,web3,eth,net --rpcport "8545" --port "30303" -rpcaddr 127.0.0.1 -rpccorsdomain "*" console
加入以后可以正常连接了

问题2: 成功连接后显示can't detect network

remix
错误原因: 在使用geth启动私有链结点的时候没有zding开启net
解决:
geth -datadir myData/00 -networkid 2018 -rpc -rpcapi personal,web3,eth,net --rpcport "8545" --port "30303" -rpcaddr 127.0.0.1 -rpccorsdomain "*" console

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值