├── .eslintignore
├── .gitignore
├── README.md
├── config
├── .eslintrc
├── env.js
├── jest
│ ├── cssTransform.js
│ └── fileTransform.js
├── paths.js
├── polyfills.js
├── webpack.config.dev.js
└── webpack.config.prod.js
├── contracts
├── ConvertLib.sol
├── MetaCoin.sol
├── Migrations.sol
└── SimpleWallet.sol
├── docs
└── img
│ ├── dapp-dev-env.png
│ └── react-redux-dapp.png
├── license.MD
├── migrations
├── 1_initial_migration.js
└── 2_deploy_contracts.js
├── package.json
├── public
├── css
│ ├── bootstrap-theme.min.css
│ ├── bootstrap.min.css
│ └── styles.css
├── favicon.ico
├── index.html
└── js
│ ├── bootstrap.min.js
│ └── jquery.min.js
├── scripts
├── build.js
├── start.js
└── test.js
├── src
├── actions
│ └── index.js
├── components
│ ├── Account.js
│ ├── AccountItem.js
│ ├── AccountTable.js
│ ├── AccountsList.js
│ ├── AddressDropdown.js
│ ├── AmountInput.js
│ ├── CryptoDropdown.js
│ ├── HomePage.js
│ ├── TransactionTable.js
│ ├── nav
│ │ ├── NavBar.js
│ │ └── NavItem.js
│ ├── services
│ │ └── HttpService.js
│ └── uport
│ │ └── UPortView.jsx
├── containers
│ ├── App.js
│ └── YAEEContainer.js
├── index.js
├── middleware
│ ├── crypto.js
│ ├── cryptos.json
│ ├── ethereum.js
│ └── metacoin.js
└── reducers
│ ├── accounts.js
│ ├── cryptos.js
│ ├── index.js
│ ├── transactions.js
│ └── transfer.js
├── test
├── SimpleWalletTest.js
├── TestMetacoin.sol
└── metacoin.js
└── truffle.js
/.eslintignore:
--------------------------------------------------------------------------------
1 | migrations/**
2 | test/SimpleWalletTest.js
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See http://help.github.com/ignore-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | node_modules
5 |
6 | # testing
7 | coverage
8 |
9 | # production
10 | build
11 |
12 | # misc
13 | .DS_Store
14 | .env
15 | npm-debug.log
16 |
17 | # truffle compiles
18 | .truffle-solidity-loader
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-redux-dapp
2 | This is a template project that can be used as the basis for an Ethereum Dapp.
3 |
4 |
5 |
6 | # Prerequisites
7 | You'll want to have the following installed globally
8 |
9 | - [nodejs](https://nodejs.org/en/)
10 | - [testrpc](https://github.com/ethereumjs/testrpc)
11 | - [truffle v3.1.1+](http://truffleframework.com/)
12 |
13 | # This is how to use it
14 | In the project directory, run these commands:
15 | ```
16 | npm install
17 | npm start
18 | ```
19 |
20 | # Environment
21 | This is an overview of the development environment I use.
22 |
23 |
24 |
25 |
26 | # General Redux Data Flows in react-redux-dapp
27 | For reference, consider these depictions of generic redux flows taken from [ReachJS Issue #653](https://github.com/reactjs/redux/issues/653].
28 |
29 |
30 |
31 |
32 |
33 | # Redux Data Flows in react-redux-dapp
34 | This section describes the reducers, state model and actions used in the YAEE sample application included in this project.
35 |
36 | ## Reducers
37 | The reducers maintain state
38 |
39 | ## Middleware
40 |
41 | ### Thunk
42 | The [redux-thunk](https://github.com/gaearon/redux-thunk) middleware component is used to incercept the asynchronous server calls from `web3.filter()` events.
43 |
44 | ###
45 | The [redux-logger](https://www.npmjs.com/package/redux-logger) middleware component provides clean logging of state before, actions and state after for debugging.
46 |
47 |
48 | ## State Model
49 | The state model represents on-chain data including `accounts` and transactions `transactions` and off-chain data like the list of `cryptos` or tokens that can be transfered and `transfer` details captured from the view.
50 |
51 | ## Actions
52 | The application state is initialized by calling actions `getAllAccounts`, `getCryptos` and `fetchTransactions`. For now, `fetchTransactions` initalizes the `web3.filter()` to listen for the latest transacations.
53 |
54 |
55 |
56 | # ---- STOP - Outdated Notes below -----
57 | # - project has been update to ES6 since these notes started
58 | The notes below this point capture some of the journey to get here and need to be updated.
59 |
60 | # This is how it was built
61 | So now we've seen how easy it is to create a react application and we've seen how easy it is to create a Dapp with Truffle.
62 |
63 | So how do we combine these into a cohesive development environment?
64 |
65 | ```Linux
66 | create-react-app react-truffle
67 | cd react-truffle
68 | npm run eject # to 'unhide' the transitive dependencies
69 | ```
70 |
71 | Now let's start configuring our react application with truffle integration.
72 |
73 | Following along with this Truffle tutorial [Bundling with Webpack](http://truffleframework.com/tutorials/bundling-with-webpack), we'll
74 | install some dependencies. Since the ejected React app already has webpack and
75 | webpack-dev-server installed, we just install the truffle-solidity-loader to
76 | enable Solidity support.
77 |
78 | ```Linux
79 | npm install truffle-solidity-loader --save-dev
80 | ```
81 |
82 | ## Configure Webpack
83 | To configure the development environment so Webpack will use the `truffle-solidity-loader` to recompile and migrate updates to Solidity contracts, edit `config\webpack.config.dev.js` to load `.sol` files from your project.
84 |
85 | ```Javascript
86 | module: {
87 | ...
88 | loaders: [
89 | ...
90 | },
91 | {
92 | test: /\.sol/, loader: 'truffle-solidity'
93 | }
94 | ]
95 | },
96 | ...
97 | ```
98 |
99 | Next, create a truffle folder so we can isolate the Ethereum portfion of our Dapp.
100 |
101 | Create a truffle.js
102 | Your truffle.js will not have any build configuration since webpack will handle
103 | building your web application. (What happens if you run truffle build?)
104 |
105 | The only configuration in truffle.js is non-web configuration such as rpc.
106 |
107 | ```javascript
108 | module.exports = {
109 | rpc: {
110 | host: "localhost",
111 | port: 8545
112 | }
113 | }
114 |
115 | `babel-preset-react-app` conflicts with the `es2015` preset so used by Truffle so
116 | replace that with `babel-preset-react` so both will play nice.
117 |
118 | ```Linux
119 | npm uninstall --save-dev babel-preset-react-app
120 | npm install --save-dev babel-preset-react
121 | ```
122 |
123 | ```
124 | Edit `package.json` babel preset settings to `es2015`. Without this,
125 | running truffle commands will error.
126 |
127 | ```Javascript
128 | "babel": {
129 | "presets": [
130 | "react",
131 | "es2015"
132 | ]
133 | },
134 | ```
135 |
136 | * Copy example contracts from truffle-basic project to our truffle folder
137 | * Copy migrations folder from truffle-basic project to our truffle folder
138 | * Copy test folder from truffle-basic project to our truffle folder
139 |
140 | We need to test that all our truffle functionality still works.
141 |
142 | # React
143 | Now let's add some structure to the React portion of this react-dapp project.
144 | The next few sections will walk through creating a healthy base structure
145 | to support a reactive Dapp application.
146 |
147 | ## Bootstrap
148 | Download the latest [Bootstrap libraries](http://getbootstrap.com/) and save
149 | `bootstrap.min.css` and `bootstrap-theme.min.css` to `public/css` folder. Save
150 | the corresponding `boostrap.min.js` to `public/js` folder as well as a version
151 | [jquery](https://jquery.com/). It's unlikely you'll need `jquery` anywhere
152 | else in your project but some of the `Bootstrap` components use it.
153 |
154 | Now update the `/public/index.html` to link the stylesheets and include the
155 | stylesheets.
156 |
157 | ```
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 | ...
166 |
167 |
168 |
169 |
170 | ```
171 |
172 | ## Remove default React src
173 | Delete the `App.css`, `App.js`, `App.test.js`, `index.css`, and `logo.svg` from
174 | the `src` folder.
175 |
176 | ## Leverage react-router
177 | This project will leverage react-router.
178 |
179 | ```Linux
180 | npm install --save-dev react-router
181 | ```
182 |
183 | Update `src/index.js`.
184 |
185 | ```javascript
186 | import React from 'react';
187 | import ReactDOM from 'react-dom';
188 | import Routes from './routes/Routes.jsx';
189 |
190 | ReactDOM.render( , document.getElementById('root'));
191 | ```
192 |
193 | ## What I like about react
194 | Once you have a base project structure in place, React development involves
195 | creating a collection of reusable components.
196 |
197 | Create the `src/components/Routes.jsx'
198 |
199 | ```JSX
200 | import React from 'react';
201 | import { hashHistory, Router, Route, IndexRoute } from 'react-router';
202 |
203 | import BasePage from './BasePage.jsx';
204 | import HomePage from './HomePage.jsx';
205 | import SimpleWallet from './wallets/SimpleWallet.jsx';
206 |
207 | var Routes = React.createClass({
208 | render: function() {
209 | return (
210 |
211 |
212 |
213 |
214 |
215 |
216 | )
217 | }
218 | });
219 |
220 | module.exports = Routes;
221 | ```
222 |
223 | ## Create BasePage.jsx
224 | This creates the common components of our application such as a Navigation Bar.
225 |
226 | ```JSX
227 | var React = require('react');
228 | var NavBar = require('./nav/NavBar.jsx');
229 |
230 | var navLinks = [{title: "Simple Wallet", href: "/simplewallet"}];
231 |
232 | var BasePage = React.createClass({
233 | render: function() {
234 | return (
235 |
If you're wondering how you got here,
331 | see react-dapp
332 |
333 |
334 | );
335 | }
336 | });
337 |
338 | module.exports = HomePage;
339 | ```
340 |
341 | ## Create the SimpleWallet.jsx component
342 |
343 | ## Introduce RefluxJS
344 | This will introduce a unidirectional dataflow architecture which enables any
345 | registered components to 'react' to events they are registered to. We'll use
346 | this along with Solidity Smart Contract events to make some dynamic components
347 | in our Dapp.
348 |
349 | See [React.js + Reflux Example](https://blog.ochronus.com/react-js-reflux-example-2d46a4d8faf0#.qujzxaj9d)
350 | for a good explanation.
351 |
352 |
353 | Install reflux.
354 | ```Linux
355 | npm install --save-dev reflux
356 | ```
357 |
358 | Create `src/components/reflux/actions.jsx`
359 |
360 | ```JSX
361 | import Reflux from 'reflux';
362 |
363 | var Actions = Reflux.createActions([
364 | 'getAccounts'
365 | ]);
366 |
367 | module.exports = Actions;
368 | ```
369 |
370 | ## Create AccountStore
371 | AccountStore will implement `getAccounts` and other Account related services.
372 |
373 | # Creating the SimpleWallet contract
374 |
375 | -----------------------------
376 |
377 | ## Sending Feedback
378 |
379 | We are always open to [your feedback](https://github.com/facebookincubator/create-react-app/issues).
380 |
381 |
382 | In the project directory, you can run:
383 |
384 | ### `npm start`
385 |
386 | Runs the app in the development mode.
387 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
388 |
389 | The page will reload if you make edits.
390 | You will also see any lint errors in the console.
391 |
392 | ### `npm test`
393 |
394 | Launches the test runner in the interactive watch mode.
395 | See the section about [running tests](#running-tests) for more information.
396 |
397 | ### `npm run build`
398 |
399 | Builds the app for production to the `build` folder.
400 | It correctly bundles React in production mode and optimizes the build for the best performance.
401 |
402 | The build is minified and the filenames include the hashes.
403 | Your app is ready to be deployed!
404 |
405 | See the section about [deployment](#deployment) for more information.
406 |
407 | ### `npm run eject`
408 |
409 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
410 |
411 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
412 |
413 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
414 |
415 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
416 |
417 | ## Syntax Highlighting in the Editor
418 |
419 | To configure the syntax highlighting in your favorite text editor, head to the [Babel's docs](https://babeljs.io/docs/editors) and follow the instructions. Some of the most popular editors are covered.
420 |
421 | ## Displaying Lint Output in the Editor
422 |
423 | >Note: this feature is available with `react-scripts@0.2.0` and higher.
424 |
425 | Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint.
426 |
427 | They are not required for linting. You should see the linter output right in your terminal as well as the browser console. However, if you prefer the lint results to appear right in your editor, there are some extra steps you can do.
428 |
429 | You would need to install an ESLint plugin for your editor first.
430 |
431 | >**A note for Atom `linter-eslint` users**
432 |
433 | >If you are using the Atom `linter-eslint` plugin, make sure that **Use global ESLint installation** option is checked:
434 |
435 | >
436 |
437 | Then add this block to the `package.json` file of your project:
438 |
439 | ```js
440 | {
441 | // ...
442 | "eslintConfig": {
443 | "extends": "react-app"
444 | }
445 | }
446 | ```
447 |
448 | Finally, you will need to install some packages *globally*:
449 |
450 | ```sh
451 | npm install -g eslint-config-react-app@0.3.0 eslint@3.8.1 babel-eslint@7.0.0 eslint-plugin-react@6.4.1 eslint-plugin-import@2.0.1 eslint-plugin-jsx-a11y@2.2.3 eslint-plugin-flowtype@2.21.0
452 | ```
453 |
454 | We recognize that this is suboptimal, but it is currently required due to the way we hide the ESLint dependency. The ESLint team is already [working on a solution to this](https://github.com/eslint/eslint/issues/3458) so this may become unnecessary in a couple of months.
455 |
456 | ## Installing a Dependency
457 |
458 | The generated project includes React and ReactDOM as dependencies. It also includes a set of scripts used by Create React App as a development dependency. You may install other dependencies (for example, React Router) with `npm`:
459 |
460 | ```
461 | npm install --save
462 | ```
463 |
464 | ## Importing a Component
465 |
466 | This project setup supports ES6 modules thanks to Babel.
467 | While you can still use `require()` and `module.exports`, we encourage you to use [`import` and `export`](http://exploringjs.com/es6/ch_modules.html) instead.
468 |
469 | For example:
470 |
471 | ### `Button.js`
472 |
473 | ```js
474 | import React, { Component } from 'react';
475 |
476 | class Button extends Component {
477 | render() {
478 | // ...
479 | }
480 | }
481 |
482 | export default Button; // Don’t forget to use export default!
483 | ```
484 |
485 | ### `DangerButton.js`
486 |
487 |
488 | ```js
489 | import React, { Component } from 'react';
490 | import Button from './Button'; // Import a component from another file
491 |
492 | class DangerButton extends Component {
493 | render() {
494 | return ;
495 | }
496 | }
497 |
498 | export default DangerButton;
499 | ```
500 |
501 | Be aware of the [difference between default and named exports](http://stackoverflow.com/questions/36795819/react-native-es-6-when-should-i-use-curly-braces-for-import/36796281#36796281). It is a common source of mistakes.
502 |
503 | We suggest that you stick to using default imports and exports when a module only exports a single thing (for example, a component). That’s what you get when you use `export default Button` and `import Button from './Button'`.
504 |
505 | Named exports are useful for utility modules that export several functions. A module may have at most one default export and as many named exports as you like.
506 |
507 | Learn more about ES6 modules:
508 |
509 | * [When to use the curly braces?](http://stackoverflow.com/questions/36795819/react-native-es-6-when-should-i-use-curly-braces-for-import/36796281#36796281)
510 | * [Exploring ES6: Modules](http://exploringjs.com/es6/ch_modules.html)
511 | * [Understanding ES6: Modules](https://leanpub.com/understandinges6/read#leanpub-auto-encapsulating-code-with-modules)
512 |
513 | ## Adding a Stylesheet
514 |
515 | This project setup uses [Webpack](https://webpack.github.io/) for handling all assets. Webpack offers a custom way of “extending” the concept of `import` beyond JavaScript. To express that a JavaScript file depends on a CSS file, you need to **import the CSS from the JavaScript file**:
516 |
517 | ### `Button.css`
518 |
519 | ```css
520 | .Button {
521 | padding: 20px;
522 | }
523 | ```
524 |
525 | ### `Button.js`
526 |
527 | ```js
528 | import React, { Component } from 'react';
529 | import './Button.css'; // Tell Webpack that Button.js uses these styles
530 |
531 | class Button extends Component {
532 | render() {
533 | // You can use them as regular CSS styles
534 | return ;
535 | }
536 | }
537 | ```
538 |
539 | **This is not required for React** but many people find this feature convenient. You can read about the benefits of this approach [here](https://medium.com/seek-ui-engineering/block-element-modifying-your-javascript-components-d7f99fcab52b). However you should be aware that this makes your code less portable to other build tools and environments than Webpack.
540 |
541 | In development, expressing dependencies this way allows your styles to be reloaded on the fly as you edit them. In production, all CSS files will be concatenated into a single minified `.css` file in the build output.
542 |
543 | If you are concerned about using Webpack-specific semantics, you can put all your CSS right into `src/index.css`. It would still be imported from `src/index.js`, but you could always remove that import if you later migrate to a different build tool.
544 |
545 | ## Post-Processing CSS
546 |
547 | This project setup minifies your CSS and adds vendor prefixes to it automatically through [Autoprefixer](https://github.com/postcss/autoprefixer) so you don’t need to worry about it.
548 |
549 | For example, this:
550 |
551 | ```css
552 | .App {
553 | display: flex;
554 | flex-direction: row;
555 | align-items: center;
556 | }
557 | ```
558 |
559 | becomes this:
560 |
561 | ```css
562 | .App {
563 | display: -webkit-box;
564 | display: -ms-flexbox;
565 | display: flex;
566 | -webkit-box-orient: horizontal;
567 | -webkit-box-direction: normal;
568 | -ms-flex-direction: row;
569 | flex-direction: row;
570 | -webkit-box-align: center;
571 | -ms-flex-align: center;
572 | align-items: center;
573 | }
574 | ```
575 |
576 | There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
577 |
578 | ## Adding Images and Fonts
579 |
580 | With Webpack, using static assets like images and fonts works similarly to CSS.
581 |
582 | You can **`import` an image right in a JavaScript module**. This tells Webpack to include that image in the bundle. Unlike CSS imports, importing an image or a font gives you a string value. This value is the final image path you can reference in your code.
583 |
584 | Here is an example:
585 |
586 | ```js
587 | import React from 'react';
588 | import logo from './logo.png'; // Tell Webpack this JS file uses this image
589 |
590 | console.log(logo); // /logo.84287d09.png
591 |
592 | function Header() {
593 | // Import result is the URL of your image
594 | return ;
595 | }
596 |
597 | export default Header;
598 | ```
599 |
600 | This ensures that when the project is built, Webpack will correctly move the images into the build folder, and provide us with correct paths.
601 |
602 | This works in CSS too:
603 |
604 | ```css
605 | .Logo {
606 | background-image: url(./logo.png);
607 | }
608 | ```
609 |
610 | Webpack finds all relative module references in CSS (they start with `./`) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.
611 |
612 | Please be advised that this is also a custom feature of Webpack.
613 |
614 | **It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images).
615 | An alternative way of handling static assets is described in the next section.
616 |
617 | ## Using the `public` Folder
618 |
619 | >Note: this feature is available with `react-scripts@0.5.0` and higher.
620 |
621 | Normally we encourage you to `import` assets in JavaScript files as described above. This mechanism provides a number of benefits:
622 |
623 | * Scripts and stylesheets get minified and bundled together to avoid extra network requests.
624 | * Missing files cause compilation errors instead of 404 errors for your users.
625 | * Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.
626 |
627 | However there is an **escape hatch** that you can use to add an asset outside of the module system.
628 |
629 | If you put a file into the `public` folder, it will **not** be processed by Webpack. Instead it will be copied into the build folder untouched. To reference assets in the `public` folder, you need to use a special variable called `PUBLIC_URL`.
630 |
631 | Inside `index.html`, you can use it like this:
632 |
633 | ```html
634 |
635 | ```
636 |
637 | Only files inside the `public` folder will be accessible by `%PUBLIC_URL%` prefix. If you need to use a file from `src` or `node_modules`, you’ll have to copy it there to explicitly specify your intention to make this file a part of the build.
638 |
639 | When you run `npm run build`, Create React App will substitute `%PUBLIC_URL%` with a correct absolute path so your project works even if you use client-side routing or host it at a non-root URL.
640 |
641 | In JavaScript code, you can use `process.env.PUBLIC_URL` for similar purposes:
642 |
643 | ```js
644 | render() {
645 | // Note: this is an escape hatch and should be used sparingly!
646 | // Normally we recommend using `import` for getting asset URLs
647 | // as described in “Adding Images and Fonts” above this section.
648 | return ;
649 | }
650 | ```
651 |
652 | Keep in mind the downsides of this approach:
653 |
654 | * None of the files in `public` folder get post-processed or minified.
655 | * Missing files will not be called at compilation time, and will cause 404 errors for your users.
656 | * Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
657 |
658 | However, it can be handy for referencing assets like [`manifest.webmanifest`](https://developer.mozilla.org/en-US/docs/Web/Manifest) from HTML, or including small scripts like [`pace.js`](http://github.hubspot.com/pace/docs/welcome/) outside of the bundled code.
659 |
660 | Note that if you add a `
34 |
35 |