在weex 中的引入 Ethereum -web3.js

<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>
相關文章
相關標籤/搜索