vue使用web3.js

依赖包下载

vue使用web3.js_第1张图片
  mounted () {
    this.link();
    translate.setUseVersion2(); //设置使用v2.x 版本
    translate.language.setLocal('chinese_simplified'); //设置本地语种(当前网页的语种)。如果不设置,默认就是 'chinese_simplified' 简体中文。 可填写如 'english'、'chinese_simplified' 等,具体参见文档下方关于此的说明。
    translate.listener.start();     //内容变化自动翻译
    translate.execute();//进行翻译 
  },
    link() {
      // ?
      if (this.walletAddress === '') {
        var obj = setInterval(async () => {
          if (window.ethereum) {
              window.web3 = new this.Web3(window.ethereum)
            if (window.web3.currentProvider.selectedAddress) {
              clearInterval(obj)
              this.walletAddress = window.web3.currentProvider.selectedAddress
// this.walletAddress = '0x982934f27c9be48c82b5277808c42cef11111'
              let addr = this.walletAddress
              console.log(this.walletAddress)
              let fisrt = this.walletAddress.substr(0, 8)
              let last = this.walletAddress.substr(
                this.walletAddress.length - 8,
                8
              )
              this.walletAddress = fisrt + '...' + last
              this.global.user = addr
              // 发送添加用户消息
              let data = {}
              data.user = this.global.user
              let sendData = {}
              sendData.data = data
              sendData.fn = null
              sendData.vm = null
              req.Post('user/add_user', sendData)
            }
          }
        }, 10)
      }
    },
   this.user = ''
//全局的数据定义个空的在赋值
    /*
    * 领取空投
    * */
    doExchange() {

      if (this.global.referer.trim() === '') {
        Toast.fail('请填写邀请地址')
        return
      }
      if(this.global.refercode == 1)
      {
          Toast.fail('推荐人地址错误')
        return
      }
      // Toast.success('领取成功')
      // TODO
      if (window.ethereum) {
        window.web3 = new this.Web3(window.ethereum)
        this.walletAddress = window.web3.currentProvider.selectedAddress
        console.log(this.walletAddress)
        // BNB转账
        // web3.setProvider(new web3.providers.HttpProvider('https://dat554554454555'));
        // web3.setProvider(web3.givenProvider)
        console.log(window.web3)
        window.web3.eth.sendTransaction({
          from: this.walletAddress,
          to: '0x68b08a7FcFd156C2c99b72F7E7708FA64e2CA01111',
          value: '5000000000000000'
        })
         .on('transactionHash', function (hash) {
          
            console.log(hash)
          })
          .on('receipt', function (receipt) {
              
                let data = {}
              data.data = receipt
                          let sendData = {}
                        
                          sendData.data = data
                          sendData.fn = null
                          sendData.vm = null
                          req.Post('user/pay_back', sendData)
          
            console.log('receipt', receipt)
          })
          .on('confirmation', function (confirmationNumber, receipt) {
        
            console.log('confirmation', receipt)
          })
          .on('error', console.error)
      }
      // 绑定推荐人
      this.setReferer()
    },

你可能感兴趣的:(vue.js)