Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Created July 13, 2018 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/cb75e325e9bacdc3bd1181357faa7eb6 to your computer and use it in GitHub Desktop.
Save adrianmcli/cb75e325e9bacdc3bd1181357faa7eb6 to your computer and use it in GitHub Desktop.
const Web3 = require("web3") // import web3 v1.0 constructor
// use globally injected web3 to find the currentProvider and wrap with web3 v1.0
const getWeb3 = () => {
const myWeb3 = new Web3(web3.currentProvider)
return myWeb3
}
// assumes passed-in web3 is v1.0 and creates a function to receive contract name
const getContractInstance = (web3) => (contractName) => {
const artifact = artifacts.require(contractName) // globally injected artifacts helper
const deployedAddress = artifact.networks[artifact.network_id].address
const instance = new web3.eth.Contract(artifact.abi, deployedAddress)
return instance
}
module.exports = { getWeb3, getContractInstance }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment