怎么在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>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。