- list utxo from buffer server
- create the custom contract transaction
- lock utxo
-- call `window.bytom.advancedTransfer` to create the advanced Transaction
+- call `window.bytom.send_advanced_transaction` to create the advanced Transaction
- update balance if success.
### Input and Output Object
import React, { Component } from "react";
import '../styles/App.css';
-import { Switch, Route } from 'react-router-dom';
+import { Switch, Route, withRouter } from 'react-router-dom';
import Profit from './layout/profit'
import Saving from './layout/save'
import Footer from './layout/footer'
import Header from './layout/header'
import Account from './layout/account'
+import action from './action'
import bytomWrap from './layout/bytomWrap'
-
+import {connect} from "react-redux";
import GetContractArgs from "./constants";
+import Bytom from 'bytom-js-sdk'
+
+class App extends Component {
+ componentDidMount(){
+ const { bytom, setBytom } = this.props;
+ if(!bytom){
+ document.addEventListener('chromeBytomLoaded', bytomExtension => {
+ console.log('bytomloaded');
+ const bytom = window.bytom;
+ setBytom(bytom);
+ this.bytomLoaded(bytom);
+ });
+ }else {
+ this.bytomLoaded(bytom);
+ }
+ }
+
+ bytomLoaded (bytom){
+ let bytomPollInterval = 3 * 1000;
+ let networks = {
+ solonet: 'http://app.bycoin.io:3000/',
+ testnet: 'http://app.bycoin.io:3020/',
+ mainnet: 'https://api.bycoin.im:8000/'
+ };
+
+ try {
+ const BYTOM_ACCOUNT = bytom.default_account
+
+ const bytomAPI = new Bytom(networks, '')
+ bytomAPI.setNetType(bytom.net)
+
+ global.bytomAPI = bytomAPI
-const App = () => (
- <div>
- <Header />
- <Constants />
- <section className="portfolio" id="portfolio">
- <div className="container">
- <Main />
+ // Check to see if the user has signed in/out of their
+ // bytom wallet or switched accounts
+ let accountInterval = setInterval(function () {
+ if (BYTOM_ACCOUNT.accountId !== bytom.default_account.accountId) {
+ location.reload(true);
+ }
+ }, bytomPollInterval);
+ } catch (err) {
+ console.log(err);
+ }
+ }
+
+ render() {
+ return (
+ <div>
+ <Header />
+ <Constants />
+ <section className="portfolio" id="portfolio">
+ <div className="container">
+ <Main />
+ </div>
+ </section>
+ <Footer />
</div>
- </section>
- <Footer />
- </div>
-);
+ )
+ }
+}
const Constants = () =>(
<header className="masthead bg-primary text-white">
</Switch>
);
-export default App;
+
+const mapStateToProps = state => ({
+ bytom: state.bytom,
+})
+
+const mapDispatchToProps = dispatch => ({
+ setBytom: (bytom) => dispatch(action.setBytom(bytom)),
+})
+
+export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App))
\ No newline at end of file
--- /dev/null
+const setBytom = (bytom) => {
+ return (dispatch) => {
+ dispatch({
+ type: "UPDATE_BYTOM",
+ bytom
+ })
+ }
+}
+
+let actions = {
+ setBytom,
+}
+
+export default actions
let radio
const GetContractArgs = function() {
- if(window.bytom && window.bytom.defaultAccount && window.bytom.defaultAccount.net){
- let network = window.bytom.defaultAccount.net
+ if(window.bytom && window.bytom.default_account && window.bytom.net){
+ let network = window.bytom.net
const object = config[network]
if(object){
}
componentDidMount() {
+ const bytom = this.props.bytom
if (
- window.bytom
- && window.bytom.defaultAccount
+ bytom
+ && bytom.default_account
) {
- const account = window.bytom.defaultAccount
+ const account = bytom.default_account
this.setState({ account })
if(account){
- this.props.updateBalances(account.guid)
+ this.props.updateBalances(account.accountId)
this.listBalance(account, GetContractArgs().assetDeposited)
}
}
const mapStateToProps = state => ({
depositAssetBalance: state.depositAssetBalance,
- billAssetBalance: state.billAssetBalance
+ billAssetBalance: state.billAssetBalance,
+ bytom: state.bytom
})
const mapDispatchToProps = dispatch => ({
import React, { Component } from 'react'
+import {connect} from "react-redux";
// This is a Higher Order Component (HOC) that wraps up any components that require
// an unlocked Bytom account instance
render () {
let contents = <div />
+ const bytom = this.props.bytom
if (
- window.bytom
- && window.bytom.defaultAccount
+ bytom
+ && bytom.default_account
) {
return <WrappedComponent {...this.props} />
}
}
}
-
- return BytomWrap;
-
+ const mapStateToProps = state => ({
+ bytom: state.bytom,
+ })
+ return connect(mapStateToProps)(BytomWrap)
}
componentDidMount() {
if (
window.bytom
- && window.bytom.defaultAccount
+ && window.bytom.default_account
) {
- this.setState({ account: window.bytom.defaultAccount })
+ this.setState({ account: bytom.default_account })
}
}
const account = this.state.account
if (
- account
+ window.bytom
+ && window.bytom.default_account
) {
const svg = jdenticon.toSvg(account.alias, 40)
return (
<div className="nav-item d-flex ">
<NavLink exact activeClassName="active" className="d-flex nav-link rounded js-scroll-trigger" to='/account'>
<div className="mr-2" dangerouslySetInnerHTML={{__html:svg}} />
- <div className="mt-auto mb-auto ">{account.alias}</div>
+ <div className="mt-auto mb-auto ">{window.bytom.default_account.alias}</div>
</NavLink>
</div>
</div>
}
}
+
export default NetworkInfo
}
function updateDatatbaseBalance(resp, saver, amountBill, account){
- const transactionHash = resp.message.result.data.transaction_hash
+ const transactionHash = resp.transaction_hash
const radio = BigNumber( GetContractArgs().radio )
const profitAmount = radio.multipliedBy(amountBill).toNumber()
return updateBalances({
import { FixedLimitProfit } from './action'
import GetContractArgs from "../../constants";
import BigNumber from 'bignumber.js'
+import {connect} from "react-redux";
class Profit extends React.Component {
}
componentDidMount() {
+ const bytom = this.props.bytom
if (
- window.bytom
- && window.bytom.defaultAccount
+ bytom
+ && bytom.default_account
) {
- this.setState({ account: window.bytom.defaultAccount })
+ this.setState({ account: bytom.default_account })
}
}
}
}
-export default Profit
+const mapStateToProps = state => ({
+ bytom: state.bytom,
+})
+
+export default connect(mapStateToProps)(Profit)
function updateDatatbaseBalance(resp, amount, address){
return updateBalances({
- "tx_id": resp.message.result.data.transaction_hash,
+ "tx_id": resp.transaction_hash,
address,
"asset": GetContractArgs().assetDeposited,
"amount": -amount
}).then(()=>{
return updateBalances({
- "tx_id": resp.message.result.data.transaction_hash,
+ "tx_id": resp.transaction_hash,
address,
"asset": GetContractArgs().assetBill,
"amount": amount
import React from 'react'
import { FixedLimitDeposit} from './action'
import GetContractArgs from '../../constants'
+import {connect} from "react-redux";
class Save extends React.Component {
}
componentDidMount() {
+ const bytom = this.props.bytom
if (
- window.bytom
- && window.bytom.defaultAccount
+ bytom
+ && bytom.default_account
) {
- this.setState({ account: window.bytom.defaultAccount })
+ this.setState({ account: bytom.default_account })
}
}
)
}
}
+const mapStateToProps = state => ({
+ bytom: state.bytom,
+})
-export default Save
+export default connect(mapStateToProps)(Save)
export function listDappUTXO(params)
{
let url
- switch (window.bytom.defaultAccount.net){
+ switch (window.bytom.net){
case "testnet":
url = "/dapptestnet/list-utxos"
break
export function updateUtxo(params)
{
let url
- switch (window.bytom.defaultAccount.net) {
+ switch (window.bytom.net) {
case "testnet":
url = "/dapptestnet/update-utxo"
break
export function updateBalances(params)
{
let url
- switch (window.bytom.defaultAccount.net) {
+ switch (window.bytom.net) {
case "testnet":
url = "/dapptestnet/update-balance"
break
export function listBalances(params)
{
let url
- switch (window.bytom.defaultAccount.net) {
+ switch (window.bytom.net) {
case "testnet":
url = "/dapptestnet/list-balances"
break
.then(()=>{
//Transactions
- window.bytom.advancedTransfer(input, output, GetContractArgs().gas*100000000, args, 1)
+ return window.bytom.send_advanced_transaction({input, output, gas: GetContractArgs().gas*100000000, args})
.then((resp) => {
- if(resp.action === 'reject'){
- reject('user reject the request')
- }else if(resp.action === 'success'){
-
//Update Balance
return updateDatatbaseBalance(resp, ...updateParameters).then(()=>{
resolve()
}).catch(err => {
throw err
})
- }
})
.catch(err => {
- throw err
+ throw err.message
})
})
.catch(err => {
import { Provider } from "react-redux";
import configureStore from "./store";
-import Bytom from 'bytom-js-sdk'
-
require("babel-core/register");
require("babel-polyfill");
-window.addEventListener('load', async function() {
- if (typeof window.bytom !== 'undefined') {
- let bytomPollInterval = 3 * 1000;
-
- let networks = {
- solonet: 'http://app.bycoin.io:3000/',
- testnet: 'http://app.bycoin.io:3020/',
- mainnet: 'https://api.bycoin.im:8000/'
- };
-
- try {
- window.bytom.defaultAccount = await window.bytom.request('currentAccount')
-
- const bytom = new Bytom(networks, '')
- bytom.setNetType(window.bytom.defaultAccount.net)
-
- global.bytomAPI = bytom
-
- // Check to see if the user has signed in/out of their
- // bytom wallet or switched accounts
- let accountInterval = setInterval(async function() {
- const account = await window.bytom.request('currentAccount')
- if ( account.guid !== window.bytom.defaultAccount.guid) {
- location.reload(true);
- }
- }, bytomPollInterval);
- } catch (err) {
- console.log(err);
- }
-
- }
-
- ReactDOM.render((
- <Provider store={configureStore()}>
- <BrowserRouter>
- <App />
- </BrowserRouter>
- </Provider>
- ), document.getElementById('root'));
-});
+ReactDOM.render((
+ <Provider store={configureStore()}>
+ <BrowserRouter>
+ <App />
+ </BrowserRouter>
+ </Provider>
+), document.getElementById('root'));
...state,
depositAssetBalance: action.depositAssetBalance
};
+ case "UPDATE_BYTOM":
+ return {
+ ...state,
+ bytom: action.bytom
+ };
default:
return state
}
import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
-function configureStore(state = { account: '' , depositAssetBalance:'',billAssetBalance:''}) {
+function configureStore(state = { account: '' , depositAssetBalance:'',billAssetBalance:'', bytom:''}) {
return createStore(
rotateReducer,
state,