rumkin / piggy-bank

Nodejs with Ethereum smart contract tutorial app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Piggy Bank

Piggy bank is tutorial web application which utilize Ethereum smart contract. It demonstrates how to use web3.js with Node.js to deploy and interact with smart contracts, create web3 application powered with smart contracts and how to test contracts with mocha.

UI screenshot

⚠️ WARNING! This is only tutorial. And not a real application. It should not be used in production.

Installation

git clone git@github.com:rumkin/piggy-bank.git
cd piggy-bank
npm i # or npm ci

It will install all required dependencies like web3, solc (solidity compiler), ethereum testnet, mocha and webserver.

Testnet

To run steps 3, 4 and 5 you need to run testnet:

node bin/testnet.js

Steps

There is several named files:

  • 1-account.js Creates secret key and balance for test net. Outputs account.json.
  • 2-compile.js Compiles contract and write result to code.json.
  • 3-deploy.js Deploy contract into testnet and write it's address to contract.json.
  • 4-run.js Run HTTP Server with simple UI to interact with contract.
  • 5-destroy.js Destroy contract and remove it from blockchain.

Repository already contains compiled template and test account. To run step 5 you need to deploy contract into testnet (4-deploy.js).

Utils

  • bin/testnet.js Run testnet.
  • bin/connect.js Connect to running testnet with web3 and get active account.

Contract

Contract allow to deposit money. Contract doesn't let owner to withdraw money until some unchangable limit is riched:

function canWithdraw() public constant returns (bool){
    return this.balance >= limit;
}

function withdraw() public isOwner {
    require(canWithdraw());

    owner.transfer(this.balance);
}

UI

UI was created with Bundler.online. It's made for demonstration it's code is autogenerated. It's source is you can find at http://bundler.online/rumkin/piggy-bank.

License

MIT.

About

Nodejs with Ethereum smart contract tutorial app


Languages

Language:HTML 58.2%Language:JavaScript 41.8%