如何将Ethereum -web3.js引入weex

技术怎么在weex 中的引入Ethereum -web3.js怎么在weex 中的引入Ethereum -web3.js ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以

怎么在weex中的引入Ethereum -web3.js,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

脚本

require(' babel-core/register ');

require(' babel-poly ill ');

从" web3 "导入web 3

exportdefault{

名称:'testWeb3 ',

数据(){ 0

返回{

web3:{},

主机:'http://127.0.0 ',1:7545,

地址: '0x 50 b 04982 c 7a 08 de 4 Fe 3 e 9 f 34 dcec 1b 06 b 835 AE 13 ',

nonce: ' ',

平衡威:全

};

},

methods:{

createWeb3(){ 0

letweb3=newWeb3()

let provider=new web3。提供商。httprovider(这。主机)

web3.setProvider(提供程序)

returnweb3

}

},

已创建(){ 0

这个。web 3=这个。创建web 3();

//if(!这个。web 3。CurrentProvider。已连接){ 0

//thrownewError('请检查区块链的宿主或者“interenet!”)

//}

这个。web 3。eth。净。Islistening((err,RES)={ 0

console.log(res)

});

//测试

this.web3.eth.net.isListening().然后((数据)={ 0

如果(数据){ 0

控制台。日志('已连接d : ',数据)

nbsp;       }
       }).catch( (err) => {
           throw new Error('请检查区块链的host或者interenet!')
       });
      

      //获取当前 network ID
      this.web3.eth.net.getId((err, res) => {
          console.log(res)  //5777        
      });

      // 查看账号列表
      this.web3.eth.getAccounts( (err,res) => {
       console.log("查看账号列表:",res)
      })

      //查询矿工账号
      this.web3.eth.getCoinbase(
          function(error, result){ 
          if (error) {
              console.error(error);
          } else {
              console.log("查询矿工账号:",result); 
          }
      })
      
      // 获得余额 方法一
      this.web3.eth.getBalance(this.coinbase ,(err, res) => {
          
          if(!err)  {
            let balance = this.web3.utils.fromWei(res, 'ether');
            console.log("this coinbase1: ",this.coinbase )  
            console.log("getBalance1:",balance)   //100
            this.balanceWei = balance
          }else{
             console.log(err);
          }
        
      });

      // 获得余额 方法二
       let _self = this;
       async function getBalance (coinbase){
            try{
                let rs = await _self.web3.eth.getBalance(coinbase);
                return rs;
            } catch(err){
                console.error("error:",err);
            }
        }
      getBalance(this.coinbase).then(res => console.log('this.coinbase的余额:',res));
      //100000000000000000000;

      //新建账户
      let newAccount = this.web3.eth.accounts.create();
      console.log("新建账户:",newAccount)
    
      let info = this.web3.eth.accounts.encrypt(newAccount.privateKey, '12345678');
      console.log("新建账户的encrypt 信息:",info);

     //新建账户 personal.newAccount 
      this.web3.eth.personal.newAccount('!@superpassword', (err, res) => {
            let newAddress  = res;   
            console.log('personal创建的新账户:',newAddress)
            //账户解锁
           this.web3.eth.personal.unlockAccount(newAddress, "!@superpassword",(err, res) => {
              console.log(newAddress+"解锁成功否:",res)  //true
           });         
     });
    
      // 查看账号列表
      this.web3.eth.getAccounts( (err,res) => {
       console.log("查看账号列表:",res)
      });
     
    
      
    
  }
};
</script>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/144820.html

(0)

相关推荐

  • 微信朋友圈只发文字,朋友圈为什么不能直接发文字

    技术微信朋友圈只发文字,朋友圈为什么不能直接发文字微信朋友圈是可以直接发文字的,步骤如下微信朋友圈只发文字:
    1.登陆微信。
    2.在微信界面当中点击发现选项。
    3.然后在发现界面打开朋友圈。
    4.在朋友圈界面上长按相机图

    生活 2021年10月23日
  • react无状态怎么写

    技术react无状态怎么写这篇文章主要介绍了react无状态怎么写,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 react无

    攻略 2021年11月26日
  • 基于Ambari的大数据平台搭建是怎样的

    技术基于Ambari的大数据平台搭建是怎样的这篇文章将为大家详细讲解有关基于Ambari的大数据平台搭建是怎样的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。同CDH部署

    攻略 2021年12月10日
  • 用jquery制作最简单登录功能(jquery中能够编辑html内容的方法)

    技术HTML+jQuery如何实现简单的登录页面这篇文章主要介绍“HTML+jQuery如何实现简单的登录页面”,在日常操作中,相信很多人在HTML+jQuery如何实现简单的登录页面问题上存在疑惑,小编查阅了各式资料,

    攻略 2021年12月13日
  • Hive基础操作的示例代码

    技术Hive基础操作的示例代码这篇文章将为大家详细讲解有关Hive基础操作的示例代码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.在hive的服务端新建源数据[root@hadoo

    攻略 2021年12月10日
  • 使用AndroidX的坑有哪些

    技术使用AndroidX的坑有哪些这篇文章主要介绍“使用AndroidX的坑有哪些”,在日常操作中,相信很多人在使用AndroidX的坑有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”

    攻略 2021年11月10日