Commit 20538d34 authored by Uciel's avatar Uciel

renamed web3 reducer

parent fe19f9ea
...@@ -14,16 +14,16 @@ import Purchase from './components/Purchase'; ...@@ -14,16 +14,16 @@ import Purchase from './components/Purchase';
import About from './components/About'; import About from './components/About';
import Links from './components/Links'; import Links from './components/Links';
import Transactions from './components/Transactions'; import Transactions from './components/Transactions';
import SelectToken from './components/SelectToken' // import SelectToken from './components/SelectToken'
// enter redux // enter redux
import { bindActionCreators } from 'redux' import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { subscribe } from 'redux-subscriber'; import { subscribe } from 'redux-subscriber';
// redux actions // redux actions
import { initializeGlobalWeb3 } from './actions/global-actions'; // import { initializeGlobalWeb3 } from './actions/global-actions';
import { uniExchangeContractReady, swtExchangeContractReady } from './actions/exchangeContract-actions'; import { uniExchangeContractReady, swtExchangeContractReady } from './actions/exchangeContract-actions';
import { uniTokenContractReady, swtTokenContractReady } from './actions/tokenContract-actions'; import { uniTokenContractReady, swtTokenContractReady } from './actions/tokenContract-actions';
import { setWeb3ConnectionStatus, setCurrentMaskAddress, metamaskLocked, metamaskUnlocked, setInteractionState, factoryContractReady, toggleAbout } from './actions/web3-actions'; import { initializeGlobalWeb3, setWeb3ConnectionStatus, setCurrentMaskAddress, metamaskLocked, metamaskUnlocked, setInteractionState, factoryContractReady, toggleAbout } from './actions/web3-actions';
import { setInputBalance, setOutputBalance, setInvariant1, setInvariant2, setMarketEth1, setMarketEth2, setMarketTokens1, setMarketTokens2, setAllowanceApprovalState } from './actions/exchange-actions'; import { setInputBalance, setOutputBalance, setInvariant1, setInvariant2, setMarketEth1, setMarketEth2, setMarketTokens1, setMarketTokens2, setAllowanceApprovalState } from './actions/exchange-actions';
// enter d3 & misc. tools // enter d3 & misc. tools
import './App.css'; import './App.css';
...@@ -75,11 +75,11 @@ class App extends Component { ...@@ -75,11 +75,11 @@ class App extends Component {
if(state.web3Store.connected === true && !state.web3Store.metamaskLocked) { if(state.web3Store.connected === true && !state.web3Store.metamaskLocked) {
console.log('successfully connected to metamask', state.web3Store.currentMaskAddress); console.log('successfully connected to metamask', state.web3Store.currentMaskAddress);
setInterval(this.getMarketInfo, 15000); setInterval(this.getMarketInfo, 15000);
// setInterval(this.getAccountInfo, 15000); setInterval(this.getAccountInfo, 15000);
// setInterval(this.getUserAddress, 10000); setInterval(this.getUserAddress, 10000);
} else { } else {
console.log('web3 not connected, getting user address') console.log('web3 not connected, getting user address')
// setInterval(this.getUserAddress, 500); setInterval(this.getUserAddress, 500);
} }
}) })
} }
...@@ -107,11 +107,13 @@ class App extends Component { ...@@ -107,11 +107,13 @@ class App extends Component {
getUserAddress = () => { getUserAddress = () => {
localweb3.eth.getAccounts((error, result) => { localweb3.eth.getAccounts((error, result) => {
if (result.length > 0) { if (result.length > 0) {
console.log('getUserAddress metamask unlocked')
this.props.setCurrentMaskAddress(result[0]); this.props.setCurrentMaskAddress(result[0]);
this.props.metamaskUnlocked(); this.props.metamaskUnlocked();
this.props.setWeb3ConnectionStatus(true); this.props.setWeb3ConnectionStatus(true);
} }
else { else {
console.log('getUserAddress metamask Locked ')
this.props.metamaskLocked(); this.props.metamaskLocked();
this.props.setWeb3ConnectionStatus(false); this.props.setWeb3ConnectionStatus(false);
this.props.setInteractionState('locked'); this.props.setInteractionState('locked');
...@@ -375,8 +377,8 @@ class App extends Component { ...@@ -375,8 +377,8 @@ class App extends Component {
inputTokenValue={this.props.exchange.inputToken.value} inputTokenValue={this.props.exchange.inputToken.value}
exchangeFee={this.props.exchange.fee} exchangeFee={this.props.exchange.fee}
/> />
<Purchase <Purchase
symbolToExchangeContract={this.symbolToExchangeContract} symbolToExchangeContract={this.symbolToExchangeContract}
symbolToTokenAddress={this.symbolToTokenAddress} symbolToTokenAddress={this.symbolToTokenAddress}
/> />
<About toggleAbout={this.toggleAbout} location={this}/> <About toggleAbout={this.toggleAbout} location={this}/>
......
import { import {
INITIALIZE_GLOBAL_WEB3,
SET_WEB3_CONNECTION_STATUS, SET_WEB3_CONNECTION_STATUS,
SET_CURRENT_MASK_ADDRESS, SET_CURRENT_MASK_ADDRESS,
METAMASK_LOCKED, METAMASK_LOCKED,
...@@ -94,4 +95,10 @@ export const setExchangeType = (exchangeType) => ({ ...@@ -94,4 +95,10 @@ export const setExchangeType = (exchangeType) => ({
export const toggleAbout = (toggle) => ({ export const toggleAbout = (toggle) => ({
type: TOGGLE_ABOUT, type: TOGGLE_ABOUT,
aboutToggle: toggle aboutToggle: toggle
}) });
\ No newline at end of file
export const initializeGlobalWeb3 = (web3) => ({
type: INITIALIZE_GLOBAL_WEB3,
web3
});
\ No newline at end of file
import React, { Component }from 'react'; import React, { Component }from 'react';
import { bindActionCreators } from 'redux' import { bindActionCreators } from 'redux'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { setWeb3ConnectionStatus, setInteractionState, setNetworkMessage } from '../actions/web3-actions'; import { subscribe } from 'redux-subscriber';
import { setWeb3ConnectionStatus, setInteractionState, setNetworkMessage, metamaskLocked } from '../actions/web3-actions';
class NetworkStatus extends Component { class NetworkStatus extends Component {
componentDidMount(){ componentDidMount(){
if (this.props.global.web3 !== undefined){ const interactionStateSubscriber = subscribe('web3Store.metamaskLocked', state => {
this.checkNetwork(); if (state.web3Store.metamaskLocked !== true) {
} console.log('metamask is unlocked')
this.checkNetwork();
} else {
console.log('metamask is locked')
}
})
// if (this.props.web3Store.web3 !== undefined){
// this.checkNetwork();
// }
} }
checkNetwork = () => { checkNetwork = () => {
this.props.global.web3.eth.net.getNetworkType((err, networkId) => { this.props.web3Store.web3.eth.net.getNetworkType((err, networkId) => {
console.log("Connected to " + networkId) console.log("Connected to " + networkId)
let interactionState = networkId === 'rinkeby' ? 'connected' : 'disconnected'; let interactionState = networkId === 'rinkeby' ? 'connected' : 'disconnected';
let connectionStatus = networkId === 'rinkeby' ? true : false; let connectionStatus = networkId === 'rinkeby' ? true : false;
...@@ -64,6 +73,7 @@ const mapDispatchToProps = (dispatch) => { ...@@ -64,6 +73,7 @@ const mapDispatchToProps = (dispatch) => {
setWeb3ConnectionStatus, setWeb3ConnectionStatus,
setInteractionState, setInteractionState,
setNetworkMessage, setNetworkMessage,
metamaskLocked
}, dispatch) }, dispatch)
} }
......
...@@ -6,7 +6,7 @@ import { setExchangeInputValue, setExchangeOutputValue } from '../actions/exchan ...@@ -6,7 +6,7 @@ import { setExchangeInputValue, setExchangeOutputValue } from '../actions/exchan
class Purchase extends Component { class Purchase extends Component {
purchaseTokens = async () => { purchaseTokens = async () => {
await this.props.setBlockTimestamp(this.props.global.web3); await this.props.setBlockTimestamp(this.props.web3Store.web3);
if (this.props.web3Store.exchangeType === 'ETH to Token') { if (this.props.web3Store.exchangeType === 'ETH to Token') {
this.ethToTokenPurchase(); this.ethToTokenPurchase();
} else if (this.props.web3Store.exchangeType === 'Token to ETH') { } else if (this.props.web3Store.exchangeType === 'Token to ETH') {
...@@ -19,9 +19,9 @@ class Purchase extends Component { ...@@ -19,9 +19,9 @@ class Purchase extends Component {
ethToTokenPurchase = () => { ethToTokenPurchase = () => {
var exchange = this.props.symbolToExchangeContract(this.props.exchange.outputToken.value); var exchange = this.props.symbolToExchangeContract(this.props.exchange.outputToken.value);
var minTokens = (this.props.exchange.outputValue/10**18).toString(); var minTokens = (this.props.exchange.outputValue/10**18).toString();
var minTokensInt = this.props.global.web3.utils.toWei(minTokens); var minTokensInt = this.props.web3Store.web3.utils.toWei(minTokens);
var ethSold = this.props.exchange.inputValue; var ethSold = this.props.exchange.inputValue;
var weiSold = this.props.global.web3.utils.toWei(ethSold); var weiSold = this.props.web3Store.web3.utils.toWei(ethSold);
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
// console.log(minTokensInt, weiSold, timeout); // console.log(minTokensInt, weiSold, timeout);
...@@ -53,9 +53,9 @@ class Purchase extends Component { ...@@ -53,9 +53,9 @@ class Purchase extends Component {
tokenToEthPurchase = () => { tokenToEthPurchase = () => {
var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value); var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value);
var minEth = (this.props.exchange.outputValue/10**18).toString(); var minEth = (this.props.exchange.outputValue/10**18).toString();
var minEthInt = this.props.global.web3.utils.toWei(minEth); var minEthInt = this.props.web3Store.web3.utils.toWei(minEth);
var tokensSold = this.props.exchange.inputValue; var tokensSold = this.props.exchange.inputValue;
var tokensSoldInt = this.props.global.web3.utils.toWei(tokensSold); var tokensSoldInt = this.props.web3Store.web3.utils.toWei(tokensSold);
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
exchange.methods.tokenToEthSwap(tokensSoldInt, minEthInt, timeout).send({from: this.props.web3Store.currentMaskAddress}) exchange.methods.tokenToEthSwap(tokensSoldInt, minEthInt, timeout).send({from: this.props.web3Store.currentMaskAddress})
...@@ -78,9 +78,9 @@ class Purchase extends Component { ...@@ -78,9 +78,9 @@ class Purchase extends Component {
var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value); var exchange = this.props.symbolToExchangeContract(this.props.exchange.inputToken.value);
var tokenOutAddress = this.props.symbolToTokenAddress(this.props.exchange.outputToken.value); var tokenOutAddress = this.props.symbolToTokenAddress(this.props.exchange.outputToken.value);
var minTokens = (this.props.exchange.outputValue/10**18).toString(); var minTokens = (this.props.exchange.outputValue/10**18).toString();
var minTokensInt = this.props.global.web3.utils.toWei(minTokens); var minTokensInt = this.props.web3Store.web3.utils.toWei(minTokens);
var tokensSold = this.props.exchange.inputValue; var tokensSold = this.props.exchange.inputValue;
var tokensSoldInt = this.props.global.web3.utils.toWei(tokensSold); var tokensSoldInt = this.props.web3Store.web3.utils.toWei(tokensSold);
var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins var timeout = this.props.web3Store.blockTimestamp + 300; //current block time + 5mins
console.log('tokenOutAddress', tokenOutAddress); console.log('tokenOutAddress', tokenOutAddress);
console.log('minTokensInt', minTokensInt); console.log('minTokensInt', minTokensInt);
......
import { INITIALIZE_GLOBAL_WEB3 } from '../constants'; // import { INITIALIZE_GLOBAL_WEB3 } from '../constants';
export default (state = {}, action) => { // export default (state = {}, action) => {
const { globalWeb3 } = action; // const { globalWeb3 } = action;
switch(action.type) { // switch(action.type) {
case INITIALIZE_GLOBAL_WEB3: // case INITIALIZE_GLOBAL_WEB3:
return Object.assign({}, state, { web3: globalWeb3 }); // return Object.assign({}, state, { web3: globalWeb3 });
default: return state // default: return state
} // }
} // }
\ No newline at end of file \ No newline at end of file
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import global from './global-reducer'; // import global from './global-reducer';
import web3Store from './web3-reducer'; import web3Store from './web3-reducer';
import exchangeContracts from './exchangeContract-reducer'; import exchangeContracts from './exchangeContract-reducer';
import tokenContracts from './tokenContract-reducer'; import tokenContracts from './tokenContract-reducer';
import exchange from './exchange-reducer'; import exchange from './exchange-reducer';
export default combineReducers({ export default combineReducers({
global,
web3Store, web3Store,
exchangeContracts, exchangeContracts,
tokenContracts, tokenContracts,
......
...@@ -14,8 +14,10 @@ import { ...@@ -14,8 +14,10 @@ import {
} from '../constants'; } from '../constants';
export default (state = {}, action) => { export default (state = {}, action) => {
const { connected, currentMaskAddress, metamaskLocked, interaction, factoryContract, networkMessage, timestamp, exchangeType, globalWeb3, aboutToggle } = action const { connected, currentMaskAddress, metamaskLocked, interaction, factoryContract, networkMessage, timestamp, exchangeType, web3, aboutToggle } = action
switch (action.type) { switch (action.type) {
case INITIALIZE_GLOBAL_WEB3:
return Object.assign({}, state, { web3: web3 });
case SET_WEB3_CONNECTION_STATUS: case SET_WEB3_CONNECTION_STATUS:
return Object.assign({}, state, { connected: connected }); return Object.assign({}, state, { connected: connected });
case SET_CURRENT_MASK_ADDRESS: case SET_CURRENT_MASK_ADDRESS:
...@@ -34,8 +36,6 @@ export default (state = {}, action) => { ...@@ -34,8 +36,6 @@ export default (state = {}, action) => {
return Object.assign({}, state, { blockTimestamp: timestamp }); return Object.assign({}, state, { blockTimestamp: timestamp });
case SET_EXCHANGE_TYPE: case SET_EXCHANGE_TYPE:
return Object.assign({}, state, { exchangeType: exchangeType }); return Object.assign({}, state, { exchangeType: exchangeType });
case INITIALIZE_GLOBAL_WEB3:
return Object.assign({}, state, { globalWeb3: globalWeb3 });
case TOGGLE_ABOUT: case TOGGLE_ABOUT:
return Object.assign({}, state, { aboutToggle: aboutToggle }) return Object.assign({}, state, { aboutToggle: aboutToggle })
default: return state; default: return state;
......
export default { export default {
global: {},
web3Store: { web3Store: {
web3: {},
connected: false, connected: false,
aboutToggle: false, aboutToggle: false,
globalWeb3: {}, globalWeb3: {},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment