async setWeb3() { // set the provider you want from Web3.providers const web3 = new Web3(new Web3.providers.HttpProvider(this.providerUrl)); return web3; }
const run = async () => { // Get web3 const Web3 = require('web3') const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')) // Get address const accounts = await web3.eth.getAccounts() console.dir(accounts) // get balance const balance = await web3.eth.getBalance(accounts[0]) console.log(`balance : ${balance}`) }
constructor({contractName,_artifactsModule, _networkId, _provider}) { try { this.artifactsModule = _artifactsModule || Artifacts; let artifact = this.artifactsModule[contractName]; assert(artifact, `No artifact for contract ${contractName} found`); this.provider = _provider || new Web3.providers.WebsocketProvider('ws://127.0.0.1:8545'); // network id default to mainnet this.networkId = _networkId || 1; if (this.networkId === 1) { assert(artifact.abi, 'invalid artifact'); assert(artifact.networks[this.networkId], 'contract for current network is not available'); assert(artifact.networks[this.networkId].address, 'address for current network is not found'); } this.DEFAULT_GAS = 400000; this.web3 = new Web3(this.provider); this.contract = new this.web3.eth.Contract(artifact.abi, artifact.networks[this.networkId].address); } catch (err) { throw err; } }
program .command('train') .description('Train your mine locally using a sonar smart contract') .option('-m, --mine-address <hexstring or auto>', 'Blockchain address for the mine to use. `auto`` sets the mine to pick a random account.') .option('-c, --contract-address <hexstring>', 'Sonar smart contract address for the mine to use') .option('-i, --ipfs-url [url]', 'Url of the IPFS node (Default: "http://localhost:5001")') .option('-e, --ethereum-url [url]', 'Url to the ethereum network to use (Default: "http://localhost:8545")') // TODO: Add dev mode with watching .action(async (options) => { let mineAddress = options.mineAddress const contractAddress = options.contractAddress // HACK: Commander required only works with empty arguments if (!mineAddress) return console.error('--mine-address required') if (!contractAddress) return console.error('--contract-address required') const ethereumUrl = options.ethereumUrl || 'http://localhost:8545' const web3 = new Web3(new Web3.providers.HttpProvider(ethereumUrl)) if (mineAddress === 'auto') { const mineAddresses = await web3.eth.getAccounts() mineAddress = mineAddresses.length && mineAddresses[0] } app.checkForModels(mineAddress, contractAddress, web3) })
const connectWeb3 = function() { const self = this; if (process.env.NODE_ENV === "development") { self.web3 = new Web3( new Web3.providers.HttpProvider(process.env.LOCAL_ENDPOINT) ); } else { self.web3 = new Web3( new HDWalletProvider(process.env.MNEMONIC, process.env.PROJECT_ENDPOINT) ); } CertificationInstance.setProvider(self.web3.currentProvider); // hack for web3@1.0.0 support for localhost testrpc, see https://github.com/trufflesuite/truffle-contract/issues/56#issuecomment-331084530 if (typeof CertificationInstance.currentProvider.sendAsync !== "function") { CertificationInstance.currentProvider.sendAsync = function() { return CertificationInstance.currentProvider.send.apply( CertificationInstance.currentProvider, arguments ); }; } process.env.NODE_ENV === "development" ? log.Print("Current host: " + self.web3.currentProvider.host) : log.Print( "Current host: " + self.web3.currentProvider.engine._providers[2].provider.host ); }
new Web3.providers.HttpProvider('https://ropsten.infura.io/' + yourInfuraEndPointKey) );
new Web3.providers.HttpProvider('https://ropsten.infura.io/' + yourInfuraEndPointKey) );
const getWeb3 = () => { return (web3 = new Web3( new Web3.providers.HttpProvider("http://localhost:8545") // ganache-cli address )); }