Introducing Ethereum-web3.js in weex

<script>
require("babel-core/register");
require("babel-polyfill");
import Web3 from 'web3'

export default {
  name: "testWeb3",
  data() {
    return {
       web3: {},
       host: "http://127.0.0.1:7545", 
       address: '0x50b04982c7a08De4Fe3e9F34dcEC1b06b835aE13',
       nonce: '',
       balanceWei:null
    };
  },
  methods: {
     createWeb3 () {
        let web3 = new Web3()
        let provider = new Web3.providers.HttpProvider(this.host)
        web3.setProvider(provider)
        return web3
    }
  },
  created() {
      this.web3 = this.createWeb3();
      // if (!this.web3.currentProvider.connected) {
      //   throw new Error('请检查区块链的host或者interenet!')
      // }

      this.web3.eth.net.isListening((err, res) => {
          console.log(res)      
      });

     //测试
      this.web3.eth.net.isListening().then((data) =>{
          if(data){
            console.log('isconnected:',data)
           
          }
       }).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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325484628&siteId=291194637