Coder Social home page Coder Social logo

luker501 / webpagetoscinteraction Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 106 KB

This is an example activity that shows how a user can interact with a smart contract through a webpage and nodejs server. NOTE: This is *not* production ready code

License: MIT License

CSS 16.08% JavaScript 72.45% HTML 11.47%

webpagetoscinteraction's Introduction

This is an example activity that shows how a user can interact with a smart contract through a webpage and nodejs server. NOTE: This is not production ready code!

Introduction

In this folder you will find a nodejs server that allows a interaction between a user and an ERC-20 smart contract deployed on the Ethereum Ropsten testnet, via a simple webpage.

Initial Setup

Installing the required components

You will need to install nodejs. Afterwards clone this folder and use npm install (npm comes installed with nodejs) to add the required packages stated in the first 8 lines of server.js. See here for npm instructions if you are unfamilar with it.

Making the required server changes

You are now nearly ready to run the server. Before you do so, make sure to complete the configuration of the code in the server.js file by replacing all sections marked [...] with real code.

WARNING - Sharing code with your private key in makes your account vunerable. It is strongly advised to just create a test account for this activity and fund it with a small amount of Ropsten Ether only. Note that Ethereum will use the same address/private key pair for all Ethereum networks (Mainnet,Ropsten,Rinkeby,...) so revealing a private key when working with one network reveals the private key for all Ethereum networks.

Generating a test account

To easily generate a test Ethereum account go to this website, click on the generate button and reveal the private key.

WARNING - If you are intending to use an Ethereum account for important activity (e.g. holding real Ether), make sure you thoroughly check the Ethereum account generation method you have decided to use. I am only recommending to use the above linked website for Ethereum accounts on a TEST network. I will hold no liability for any use of these accounts on the Ethereum mainnet.

Funding your test account

To fund an account on a test network, we can use faucets. Ropsten has a few faucets that can fund your address, such as the one here or here.

Running the App

Once you have performed the initial setup, use node to start your server by opening a terminal/command prompt in the same folder and typing the following into the console:

node server.js

You will see Example app listening on port 3000! printed in the console. Now open your web browser and go to http://localhost:3000/ where you will see a simple webpage displayed like so:

Initial Webpage

We can now trigger a smart contract state query function by entering an address and clicking on the 'get balance of address' button in the webpage. Clicking on this button triggers app.post('/balance',... in the server. When the call returns, you will see the current address token balance, like so:

Webpage with Winning Vote

Lastly we can trigger a smart contract state change function by entering an address and token value to sendand then clicking on the 'Send tokens' button in the webpage. Clicking on this button triggers app.post('/',... in the server, which builds a new blockchain transaction and sends it to the node. When the call returns, you will see the corresponding transaction hash, like so:

Webpage with a New Vote Transaction

Credits

This activity builds on:

https://medium.com/@codetractio/try-out-ethereum-using-only-nodejs-and-npm-eabaaaf97c80

webpagetoscinteraction's People

Contributors

luker501 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

lukerquant

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.