• R/O
  • HTTP
  • SSH
  • HTTPS

Bytom-Dapp-Demo: 提交

A simple deposit and profit bytom dapp demo


Commit MetaInfo

修订版717ff8d09511a0c75840272cf3188652c195b264 (tree)
时间2019-06-26 16:25:37
作者Zhiting Lin <zlin035@uott...>
CommiterZhiting Lin

Log Message

update enable function.

更改概述

差异

--- a/src/components/App.js
+++ b/src/components/App.js
@@ -6,6 +6,7 @@ import Saving from './layout/save'
66 import Footer from './layout/footer'
77 import Header from './layout/header'
88 import Account from './layout/account'
9+import Constants from './layout/constants'
910 import action from './action'
1011 import bytomWrap from './layout/bytomWrap'
1112 import {connect} from "react-redux";
@@ -18,7 +19,6 @@ class App extends Component {
1819 const { bytom, setBytom } = this.props;
1920 if(!bytom){
2021 document.addEventListener('chromeBytomLoaded', bytomExtension => {
21- console.log('bytomloaded');
2222 const bytom = window.bytom;
2323 setBytom(bytom);
2424 this.bytomLoaded(bytom);
@@ -28,7 +28,7 @@ class App extends Component {
2828 }
2929 }
3030
31- bytomLoaded (bytom){
31+ async bytomLoaded (bytom){
3232 let bytomPollInterval = 3 * 1000;
3333 let networks = {
3434 solonet: 'http://app.bycoin.io:3000/',
@@ -37,7 +37,8 @@ class App extends Component {
3737 };
3838
3939 try {
40- const BYTOM_ACCOUNT = bytom.default_account
40+ const BYTOM_ACCOUNT = await bytom.enable()
41+ this.props.updateConnection(true)
4142
4243 const bytomAPI = new Bytom(networks, '')
4344 bytomAPI.setNetType(bytom.net)
@@ -60,7 +61,7 @@ class App extends Component {
6061 return (
6162 <div>
6263 <Header />
63- <Constants />
64+ <Constants/>
6465 <section className="portfolio" id="portfolio">
6566 <div className="container">
6667 <Main />
@@ -72,29 +73,6 @@ class App extends Component {
7273 }
7374 }
7475
75-const Constants = () =>(
76- <header className="masthead bg-primary text-white">
77- <div className="container">
78- <table>
79- <tbody>
80- <tr>
81- <td>
82- <span className="mr-5">Deposit Asset ID: </span>
83- </td>
84- <td>{GetContractArgs().assetDeposited}</td>
85- </tr>
86- <tr>
87- <td>
88- <span className="mr-5">Bill Asset ID: </span>
89- </td>
90- <td>{GetContractArgs().assetBill}</td>
91- </tr>
92- </tbody>
93- </table>
94- </div>
95- </header>
96-);
97-
9876 const Main = () => (
9977 <Switch>
10078 <Route exact path='/' component={bytomWrap(Saving)}/>
@@ -110,6 +88,7 @@ const mapStateToProps = state => ({
11088
11189 const mapDispatchToProps = dispatch => ({
11290 setBytom: (bytom) => dispatch(action.setBytom(bytom)),
91+ updateConnection: (bytomConnection) => dispatch(action.updateConnection(bytomConnection)),
11392 })
11493
11594 export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App))
\ No newline at end of file
--- a/src/components/action.js
+++ b/src/components/action.js
@@ -7,8 +7,18 @@ const setBytom = (bytom) => {
77 }
88 }
99
10+const updateConnection = (bytomConnection) => {
11+ return (dispatch) => {
12+ dispatch({
13+ type: "UPDATE_BYTOM_CONNECTION",
14+ bytomConnection
15+ })
16+ }
17+}
18+
1019 let actions = {
1120 setBytom,
21+ updateConnection
1222 }
1323
1424 export default actions
--- a/src/components/constants.js
+++ b/src/components/constants.js
@@ -13,26 +13,30 @@ let banker
1313 let gas
1414 let radio
1515
16-const GetContractArgs = function() {
17- if(window.bytom && window.bytom.default_account && window.bytom.net){
18- let network = window.bytom.net
16+const GetContractArgs = function(bytom) {
17+ let network
18+ if(bytom && bytom.net){
19+ network = bytom.net
20+ }else if(window.bytom && window.bytom.default_account && window.bytom.net){
21+ network = window.bytom.net
22+ }
1923
20- const object = config[network]
21- if(object){
22- depositProgram = object.depositProgram
23- profitProgram = object.profitProgram
24- assetDeposited = object.assetDeposited
25- assetBill = object.assetBill
26- totalAmountBill = object.totalAmountBill
27- totalAmountCapital = object.totalAmountCapital
28- dueBlockHeight = object.dueBlockHeight
29- expireBlockHeight = object.expireBlockHeight
30- banker = object.banker
31- gas = object.gas
32- radio =BigNumber(object.totalAmountCapital).div(object.totalAmountBill).toNumber()
33- }
24+ const object = config[network]
25+ if(object){
26+ depositProgram = object.depositProgram
27+ profitProgram = object.profitProgram
28+ assetDeposited = object.assetDeposited
29+ assetBill = object.assetBill
30+ totalAmountBill = object.totalAmountBill
31+ totalAmountCapital = object.totalAmountCapital
32+ dueBlockHeight = object.dueBlockHeight
33+ expireBlockHeight = object.expireBlockHeight
34+ banker = object.banker
35+ gas = object.gas
36+ radio =BigNumber(object.totalAmountCapital).div(object.totalAmountBill).toNumber()
3437 }
3538
39+
3640 return {
3741 depositProgram,
3842 profitProgram,
--- a/src/components/layout/bytomWrap.jsx
+++ b/src/components/layout/bytomWrap.jsx
@@ -18,7 +18,22 @@ export default function(WrappedComponent) {
1818 ) {
1919 return <WrappedComponent {...this.props} />
2020 }
21- else if (( window.bytom )) {
21+ else if (( bytom && !this.props.bytomConnection)) {
22+ return (
23+ <div className="columns">
24+ <div className="column" />
25+ <div className="column is-two-thirds">
26+ <h1 className="title">
27+ Authenticate <strong>Bytom-Chrome-Extension</strong>.
28+ </h1>
29+ <p className="lead">
30+ Please Authenticate the connection request.
31+ </p>
32+ </div>
33+ <div className="column" />
34+ </div>
35+ )
36+ } else if (( bytom )) {
2237 return (
2338 <div className="columns">
2439 <div className="column" />
@@ -62,6 +77,7 @@ export default function(WrappedComponent) {
6277 }
6378 const mapStateToProps = state => ({
6479 bytom: state.bytom,
80+ bytomConnection: state.bytomConnection
6581 })
6682 return connect(mapStateToProps)(BytomWrap)
6783 }
--- /dev/null
+++ b/src/components/layout/constants/index.jsx
@@ -0,0 +1,39 @@
1+import React, {
2+ Component
3+} from 'react'
4+import GetContractArgs from "../../constants";
5+import {connect} from "react-redux";
6+
7+class Constants extends Component {
8+
9+ render () {
10+ return (
11+ <header className="masthead bg-primary text-white">
12+ <div className="container">
13+ <table>
14+ <tbody>
15+ <tr>
16+ <td>
17+ <span className="mr-5">Deposit Asset ID: </span>
18+ </td>
19+ <td>{GetContractArgs(this.props.bytom).assetDeposited}</td>
20+ </tr>
21+ <tr>
22+ <td>
23+ <span className="mr-5">Bill Asset ID: </span>
24+ </td>
25+ <td>{GetContractArgs(this.props.bytom).assetBill}</td>
26+ </tr>
27+ </tbody>
28+ </table>
29+ </div>
30+ </header>
31+ )
32+ }
33+
34+}
35+const mapStateToProps = state => ({
36+ bytom: state.bytom
37+})
38+
39+export default connect(mapStateToProps)(Constants)
--- a/src/components/layout/header/NetworkInfo.jsx
+++ b/src/components/layout/header/NetworkInfo.jsx
@@ -1,8 +1,9 @@
11 import React, { Component } from 'react'
22 import { NavLink } from 'react-router-dom'
33 import jdenticon from "jdenticon"
4+import {connect} from "react-redux";
45
5-const NetworkInfo = class extends Component {
6+class NetworkInfo extends Component {
67
78 constructor (props) {
89 super(props)
@@ -12,9 +13,10 @@ const NetworkInfo = class extends Component {
1213 }
1314
1415 componentDidMount() {
16+ const bytom = this.props.bytom
1517 if (
16- window.bytom
17- && window.bytom.default_account
18+ bytom
19+ && bytom.default_account
1820 ) {
1921 this.setState({ account: bytom.default_account })
2022 }
@@ -22,10 +24,10 @@ const NetworkInfo = class extends Component {
2224
2325 render() {
2426 const account = this.state.account
25-
27+ const bytom = this.props.bytom
2628 if (
27- window.bytom
28- && window.bytom.default_account
29+ bytom
30+ && bytom.default_account
2931 ) {
3032 const svg = jdenticon.toSvg(account.alias, 40)
3133 return (
@@ -33,7 +35,7 @@ const NetworkInfo = class extends Component {
3335 <div className="nav-item d-flex ">
3436 <NavLink exact activeClassName="active" className="d-flex nav-link rounded js-scroll-trigger" to='/account'>
3537 <div className="mr-2" dangerouslySetInnerHTML={{__html:svg}} />
36- <div className="mt-auto mb-auto ">{window.bytom.default_account.alias}</div>
38+ <div className="mt-auto mb-auto ">{bytom.default_account.alias}</div>
3739 </NavLink>
3840 </div>
3941 </div>
@@ -44,5 +46,8 @@ const NetworkInfo = class extends Component {
4446 }
4547 }
4648
49+const mapStateToProps = state => ({
50+ bytom: state.bytom
51+})
4752
48-export default NetworkInfo
53+export default connect(mapStateToProps)(NetworkInfo)
--- a/src/components/layout/header/index.jsx
+++ b/src/components/layout/header/index.jsx
@@ -2,7 +2,7 @@ import React, { Component } from 'react'
22 import { NavLink } from 'react-router-dom'
33 import NetworkInfo from './NetworkInfo'
44
5-const Header = class extends Component {
5+class Header extends Component {
66
77 constructor (props) {
88 super(props)
--- a/src/reducers/rotateReducer.js
+++ b/src/reducers/rotateReducer.js
@@ -15,6 +15,11 @@ export default (state, action) => {
1515 ...state,
1616 bytom: action.bytom
1717 };
18+ case "UPDATE_BYTOM_CONNECTION":
19+ return {
20+ ...state,
21+ bytomConnection: action.bytomConnection
22+ };
1823 default:
1924 return state
2025 }
--- a/src/store.js
+++ b/src/store.js
@@ -2,7 +2,7 @@ import rotateReducer from "./reducers/rotateReducer"
22 import { createStore, applyMiddleware, compose } from 'redux'
33 import thunkMiddleware from 'redux-thunk'
44
5-function configureStore(state = { account: '' , depositAssetBalance:'',billAssetBalance:'', bytom:''}) {
5+function configureStore(state = { account: '' , depositAssetBalance:'',billAssetBalance:'', bytom:'', bytomConnection:false}) {
66 return createStore(
77 rotateReducer,
88 state,
Show on old repository browser