Skip to content
/ scd Public

Deploy smart contracts without running a local geth node using web3 1.0.0 and nodejs.

Notifications You must be signed in to change notification settings

mypttwo/scd

Repository files navigation

Simple Smart Contract Deployer

Deploy Ethreum smart contracts without running a local geth node using web3 1.0.0 and nodejs.

You will find this interesting if

  • you need a working example using web3 1.0.0. As of this doc checkin there is plenty of code on the web using web3 0.x.x but I couldn't find a complete working example using web3 1.0.0. (Maybe I didn't look hard enough!)
  • you do not want to run a local node - either to connect to Ropsten/Rinkeby or even testrpc/ganache .

Disclaimers :

  1. This is not production code and is not secure. It also does not follow best practices for nodejs app development. This is simply an example - deliberately stripped down for clarity.
  2. The simpler way to deploy the contract is to use truffle console of course. This only demonstrates a way it can be done via nodejs.

Getting Started

Apart from having nodejs installed and other usual stuff you will need to

  • Signup on infura.io. This is the infrastructure that powers Metamask. You should have an API key at the end of the signup.
  • Install Metamask if you have not done that as yet. This will give you a default account and a mnemonic to login.
  • Get some ether to begin with. You would also like to work with a test network like Ropsten or Rinkeby. Look for faucets in the respective networks - like Ropsten faucet, Rinkeby faucet.

Running the code

  • The code includes a .env file. You will need to set the appropriate variables like so.

    INFURA_URL_API_KEY=https://ropsten.infura.io/1t7lIne9gTwmPs9Twsd5
    MNEMONIC=one two three four five six seven eight nine ten eleven twelve

Needless to say the above file should never be checked in or even logged. If you do that in production you will be robbed.

  • The code includes a setup.js where you can specify
    • the path and name of the .sol file containing the Contract.
    • the name of the Contract itself
    • the arguments for the Contract

The code uses the Voting Contract by Mahesh Murthy as an example.

  • Finally you run the code like so

node app.js

Output

The example simply

  • connects to the infura endpoint with the mnemonic as credentials
  • gets the first account you see on Metamask (only the first is returned anyway even if multiple accounts are available)
  • dynamically compiles the Contract and extracts the abi and the bytecode
  • estimates the gas required for deploying the Contract (this is not accurate; we add some more gas later as you would see in the code)
  • and finally deploys the Contract.

Its also possible that an error may result even if all works well.

Error: Transaction was not mined within 50 blocks, please make sure your transaction was properly sent. Be aware that it might still be mined!

You can view the results on etherscan via Metamask.

Using truffle console

In case you want to connect via truffle console you will need to run

truffle console --network remote

Acknowledgements

About

Deploy smart contracts without running a local geth node using web3 1.0.0 and nodejs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published