Coder Social home page Coder Social logo

scd's Introduction

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

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.