怎么在weex中的引入Ethereum-web3.js

怎么在weex 中的引入Ethereum -web3.js ,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

為威遠等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及威遠網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都做網(wǎng)站、成都網(wǎng)站設計、威遠網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

<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('請檢查區(qū)塊鏈的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('請檢查區(qū)塊鏈的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創(chuàng)建的新賬戶:',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>

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)站欄目:怎么在weex中的引入Ethereum-web3.js
本文URL:http://bm7419.com/article28/ijphjp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站改版、微信公眾號網(wǎng)頁設計公司、網(wǎng)站內鏈、服務器托管

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化