Giter VIP home page Giter VIP logo

vue-web3's Introduction

VueWeb3 Build Status npm package

Vue.js bindings for Web3 1.0

Installation

In module environments, e.g CommonJS:

npm install vue web3@beta vue-web3
var Vue = require('vue')
var Web3 = require('web3')
var VueWeb3 = require('vue-web3')

// explicit installation required in module environments
Vue.use(VueWeb3, { web3: new Web3(web3.currentProvider) })

Usage

let myContract = new web3.eth.Contract(MyContract, '0xbA911C4A817e69998Ffd3626d3c5366038e8480F')

var vm = new Vue({
  el: '#demo',
  web3: {
    // can bind to calls
    lastUpdated: {
      contract: myContract,
      method: 'getLastUpdated',
      arguments: []
    },
    // can also bind to events
    transfers: {
      contract: myContract,
      event: 'OwnershipTransferred',
      options: {
        fromBlock: 2
      }
    }
  }
})

If you need to access properties from the Vue instance, use the function syntax:

var vm = new Vue({
  el: '#demo',
  web3: function () {
    return {
      lastUpdated: {
        contract: myContract,
        method: 'getLastUpdated',
        arguments: [this.$store.state.user.uid]
      }
    }
  }
})

⚠️: This function will get executed only once. If you want to have automatic rebind (pretty much like a computed property) use a $watch and call $unbind and then $bindCall or $bindEvents

<div id="demo">
  <pre>{{ lastUpdated }}</pre>
  <ul>
    <li v-for="transfer in transfers">From {{ transfer.previousOwner }} to {{ transfer.newOwner }}</li>
  </ul>
</div>

The above will bind the Vue instance's lastUpdated and transfers to the respective Web3 data sources.

Alternatively, you can also manually bind to Web3 with the $bindCall or $bindEvents instance methods:

let myContract = new web3.eth.Contract(MyContract, '0xbA911C4A817e69998Ffd3626d3c5366038e8480F')
vm.$bindCall('user', { contract: myContract, method: 'getUser' })
vm.$bindEvents('transfers', { contract: myContract, event: 'OwnershipTransferred' })

// References are unbound when the component is destroyed but you can manually unbind a reference
// if needed
vm.$unbind('items')

Error: The current provider doesn't support subscriptions: MetamaskInpageProvider

In order to get updates from the blockchain, this library requires a provider that supports subscriptions. MetaMask does not currently inject a provider with this support, this is being tracked via metamask-extension/2350.

Thankfully, we can create our own provider:

var provider = new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/ws')

Until MetaMask's provider supports subscriptions, you can have a write web3 instance with MetaMask's provider and a read web3 instance which uses the WebsocketProvider.

Contributing

Clone the repo, then:

$ npm install    # install dependencies
$ npm test       # run test suite with coverage report
$ npm run dev    # watch and build dist/vue-web3.js
$ npm run build  # build dist/vue-web3.js and vue-web3.min.js

License

MIT

vue-web3's People

Contributors

morrislaptop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vue-web3's Issues

error

ERROR in ./node_modules/web3/packages/web3-utils/src/utils.js
Module not found: Error: Can't resolve 'eth-lib/src/hash' in '/Users/xueqingli/Documents/node.js项目/EtherDice_Web/node_modules/web3/packages/web3-utils/src'
@ ./node_modules/web3/packages/web3-utils/src/utils.js 27:11-38
@ ./node_modules/web3/packages/web3-utils/src/index.js
@ ./node_modules/web3/src/index.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main

ERROR in ./node_modules/web3/packages/web3-utils/src/index.js
Module not found: Error: Can't resolve 'ethjs-unit' in '/Users/xueqingli/Documents/node.js项目/EtherDice_Web/node_modules/web3/packages/web3-utils/src'
@ ./node_modules/web3/packages/web3-utils/src/index.js 26:16-37
@ ./node_modules/web3/src/index.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main

ERROR in ./node_modules/web3/packages/web3-utils/src/index.js
Module not found: Error: Can't resolve 'randomhex' in '/Users/xueqingli/Documents/node.js项目/EtherDice_Web/node_modules/web3/packages/web3-utils/src'
@ ./node_modules/web3/packages/web3-utils/src/index.js 29:16-36
@ ./node_modules/web3/src/index.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main

fyi issue with web3 beta 34 and 35

Configuring subscriptions I got this error:

WebSocket connection to 'wss://ropsten.infura.io/ws' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

As described here, it's an issue in web3 beta34/35, so stick on 33 or use the 1.0 branch.

Cannot read property 'blockNumber' of undefined

Hello! I'm getting the error below related to event bind.

Error:
vue-web3.esm.js?537a:1 Uncaught (in promise) TypeError: Cannot read property 'blockNumber' of undefined
at Vue.eval (vue-web3.esm.js?537a:1)

Relevant code:

var myContract;
if (typeof window.web3 !== "undefined") {
    let web3js = new Web3(window.web3.currentProvider);
    Vue.use(VueWeb3, { web3: web3js });
    myContract = new web3js.eth.Contract(
      MyContract.abi, "0x...");
}

new Vue({
    data: () => ({
      items: []
    }),
    web3: () => ({
      items: {
        contract: myContract,
        event: "MyEvent"
      }
    })
})

Environment:
Chromium 70.0.3538.77
MetaMask 5.0.2
web3 1.0.0-beta.36
vue-web3 2.1.0

EDIT: My contract in local geth instance hasn't emitted events yet. Looking at source code, maybe line 59 requires some past event:

options.fromBlock = events[events.length - 1].blockNumber + 1;

hi,我在用的过程中出现了这个错误

ERROR in ./node_modules/vue-web3/dist/vue-web3.esm.js
Module not found: Error: Can't resolve 'web3' in 'D:\nodejs-Project\EtherDice_Web\node_modules\vue-web3\dist'
@ ./node_modules/vue-web3/dist/vue-web3.esm.js 1:6-21
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8081 webpack/hot/dev-server ./src/main
麻烦帮忙看下吧,谢谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.