├── test ├── .gitkeep └── TestVoting.sol ├── src ├── css │ └── style.css ├── js │ └── app.js └── index.html ├── .gitignore ├── migrations └── 1_initial_migration.js ├── truffle.js ├── contracts └── Migrations.sol ├── README.md ├── .eslintrc.json ├── package.json └── webpack.config.js /test/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/css/style.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .vscode/ 3 | build/ -------------------------------------------------------------------------------- /test/TestVoting.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.18; 2 | 3 | import "truffle/Assert.sol"; 4 | import "truffle/DeployedAddresses.sol"; 5 | import "../contracts/Voting.sol"; 6 | 7 | -------------------------------------------------------------------------------- /migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol") 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations) 5 | } 6 | -------------------------------------------------------------------------------- /truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // for more about customizing your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 9545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.2; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | modifier restricted() { 8 | if (msg.sender == owner) _; 9 | } 10 | 11 | function Migrations() { 12 | owner = msg.sender; 13 | } 14 | 15 | function setCompleted(uint completed) restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Boilerplate with Truffle and webpack for dApps built on Ethereum 2 | 3 | This boilerplate was made for this [blog post](https://medium.freecodecamp.org/developing-an-ethereum-decentralized-voting-application-a99de24992d9). 4 | ### Dependencies: 5 | - [Nodejs 5.0+](https://nodejs.org/en/) 6 | - [Truffle](https://github.com/trufflesuite/truffle) 7 | - [Ganache](http://truffleframework.com/ganache/) 8 | 9 | ## Setup 10 | ``` 11 | npm install -g truffle 12 | git clone https://github.com/tko22/truffle-webpack-boilerplate.git 13 | cd truffle-webpack-boilerplate 14 | npm install 15 | npm run dev 16 | ``` 17 | 18 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "jquery": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "sourceType": "module" 10 | }, 11 | "rules": { 12 | "indent": [ 13 | "error", 14 | 2 15 | ], 16 | "linebreak-style": [ 17 | "error", 18 | "unix" 19 | ], 20 | "quotes": [ 21 | "error", 22 | "double" 23 | ], 24 | "semi": [ 25 | "error", 26 | "never" 27 | ] 28 | } 29 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ethereum-voting-dapp", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "truffle.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "dev": "webpack-dev-server", 11 | "build": "webpack", 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "author": "", 15 | "license": "MIT", 16 | "devDependencies": { 17 | "babel-loader": "^7.1.2", 18 | "babel-plugin-transform-runtime": "^6.23.0", 19 | "babel-preset-env": "^1.6.1", 20 | "copy-webpack-plugin": "^4.3.1", 21 | "css-loader": "^0.28.8", 22 | "eslint-plugin-import": "^2.8.0", 23 | "json-loader": "^0.5.7", 24 | "lite-server": "^2.3.0", 25 | "style-loader": "^0.19.1", 26 | "truffle-contract": "^3.0.1", 27 | "web3": "^0.20.1", 28 | "webpack": "^3.10.0", 29 | "webpack-dev-server": "^2.10.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require("path") 2 | const CopyWebpackPlugin = require("copy-webpack-plugin") 3 | 4 | module.exports = { 5 | entry: './src/js/app.js', 6 | output: { 7 | path: path.resolve(__dirname, 'build'), 8 | filename: 'app.js' 9 | }, 10 | plugins: [ 11 | // Copy our app's index.html to the build folder. 12 | new CopyWebpackPlugin([ 13 | { from: './src/index.html', to: "index.html" } 14 | ]) 15 | ], 16 | module: { 17 | rules: [ 18 | { 19 | test: /\.css$/, 20 | use: [ 'style-loader', 'css-loader' ] 21 | } 22 | ], 23 | loaders: [ 24 | { test: /\.json$/, use: 'json-loader' }, 25 | { 26 | test: /\.js$/, 27 | exclude: /(node_modules|bower_components)/, 28 | loader: 'babel-loader', 29 | query: { 30 | presets: ['@babel/preset-env'], 31 | plugins: ['transform-runtime'] 32 | } 33 | } 34 | ] 35 | } 36 | } -------------------------------------------------------------------------------- /src/js/app.js: -------------------------------------------------------------------------------- 1 | // import CSS. Webpack with deal with it 2 | import "../css/style.css" 3 | 4 | // Import libraries we need. 5 | import { default as Web3} from "web3" 6 | import { default as contract } from "truffle-contract" 7 | 8 | 9 | window.App = { 10 | start: function() { 11 | 12 | } 13 | 14 | } 15 | 16 | 17 | 18 | 19 | // When the page loads, we create a web3 instance and set a provider. We then set up the app 20 | window.addEventListener("load", function() { 21 | // Is there an injected web3 instance? 22 | if (typeof web3 !== "undefined") { 23 | console.warn("Using web3 detected from external source like Metamask") 24 | // If there is a web3 instance(in Mist/Metamask), then we use its provider to create our web3object 25 | window.web3 = new Web3(web3.currentProvider) 26 | } else { 27 | console.warn("No web3 detected. Falling back to http://localhost:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for deployment. More info here: http://truffleframework.com/tutorials/truffle-and-metamask") 28 | // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) 29 | window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:9545")) 30 | } 31 | // initializing the App 32 | window.App.start() 33 | }) -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Ethereum Webpack Boilerplate 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | --------------------------------------------------------------------------------