Skip to content

Commit

Permalink
[BACKMERGE] /bug/fix profits and balance dashboard (#359)
Browse files Browse the repository at this point in the history
* PM-618: Display WETH for Dashboard/Header (#334)

* Bug: Fix problems with collateralToken, allowing to enter contractName to use as collateralToken (#340)

* PM-618: Display WETH for Dashboard/Header

* Refactor collateralToken

* Implement ToS Acceptance localstorage load/save

* Update config for staging

* reset local config, update tests, fix collateralToken for all sources

* update all configs

* Fix failing imports & pass tests
  • Loading branch information
mmv08 authored May 23, 2018
1 parent 3ad4f07 commit 46eeef9
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 140 deletions.
6 changes: 3 additions & 3 deletions .bootstraprc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
useFlexbox: true
styleLoaders:
- style-loader
- css-loader
- postcss-loader
- sass-loader
- css-loader?sourceMap
- postcss-loader?sourceMap
- sass-loader?sourceMap
styles:
grid: true
buttons: true
Expand Down
9 changes: 6 additions & 3 deletions config/environments/olympia/development/interface.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
}
},
"collateralToken": {
"address": "0x979861df79c7408553aaf20c01cfb3f81ccf9341",
"symbol": "OLY",
"icon": "/assets/img/icons/gno_token.svg"
"source": "address",
"options": {
"address": "0x979861df79c7408553aaf20c01cfb3f81ccf9341",
"symbol": "OLY",
"icon": "/assets/img/icons/gno_token.svg"
}
},
"providers": {
"default": "METAMASK",
Expand Down
9 changes: 6 additions & 3 deletions config/environments/olympia/production/interface.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
}
},
"collateralToken": {
"address": "0x979861df79c7408553aaf20c01cfb3f81ccf9341",
"symbol": "OLY",
"icon": "/assets/img/icons/gno_token.svg"
"source": "address",
"options": {
"address": "0x979861df79c7408553aaf20c01cfb3f81ccf9341",
"symbol": "OLY",
"icon": "/assets/img/icons/gno_token.svg"
}
},
"providers": {
"default": "METAMASK",
Expand Down
9 changes: 6 additions & 3 deletions config/environments/olympia/staging/interface.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
}
},
"collateralToken": {
"address": "0x979861df79c7408553aaf20c01cfb3f81ccf9341",
"symbol": "OLY",
"icon": "/assets/img/icons/gno_token.svg"
"source": "address",
"options": {
"address": "0x979861df79c7408553aaf20c01cfb3f81ccf9341",
"symbol": "OLY",
"icon": "/assets/img/icons/gno_token.svg"
}
},
"providers": {
"default": "METAMASK",
Expand Down
9 changes: 6 additions & 3 deletions config/environments/staging/interface.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
"requireTOSAccept": true
},
"collateralToken": {
"address": "0xd19bce9f7693598a9fa1f94c548b20887a33f141",
"symbol": "WETH",
"icon": "/assets/img/icons/icon_etherTokens.svg"
"source": "contract",
"options": {
"contractName": "EtherToken",
"symbol": "WETH",
"icon": "/assets/img/icons/icon_etherTokens.svg"
}
},
"footer": {
"enabled": true,
Expand Down
2 changes: 2 additions & 0 deletions src/api/gnosis.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@ export const getGnosisConnection = async () => {
}

export const getROGnosisConnection = async () => gnosisROInstance || undefined

export default Gnosis
4 changes: 2 additions & 2 deletions src/components/Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class Dashboard extends Component {
let tradesHoldingsSection = <div className="dashboardWidgets dashboardWidgets--financial" />
const predictedProfitFormatted = Decimal(accountPredictiveAssets).toDP(4, 1).toString()
if (hasWallet) {
metricsSection = <Metrics tokens={collateralToken.amount} tokenSymbol={collateralToken.symbol} tokenIcon={collateralToken.icon} predictedProfit={predictedProfitFormatted} />
metricsSection = <Metrics tokens={collateralToken.balance} tokenSymbol={collateralToken.symbol} tokenIcon={collateralToken.icon} predictedProfit={predictedProfitFormatted} />

tradesHoldingsSection = (
<div className="dashboardWidgets dashboardWidgets--financial">
Expand Down Expand Up @@ -402,7 +402,7 @@ Dashboard.propTypes = {
fetchTournamentUserData: PropTypes.func.isRequired,
collateralToken: PropTypes.shape({
symbol: PropTypes.string,
amount: PropTypes.string,
balance: PropTypes.string,
address: PropTypes.string,
}).isRequired,
}
Expand Down
26 changes: 18 additions & 8 deletions src/containers/HeaderContainer/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import { requestTokenBalance } from 'store/actions/blockchain'
import { requestTokenBalance, TOKEN_SOURCE_ETH } from 'store/actions/blockchain'
import { openModal } from 'store/actions/modal'
import { requestMainnetAddress } from 'store/actions/account'

import { initProviders } from 'integrations/store/actions'
import { WALLET_PROVIDER } from 'integrations/constants'

import { getCollateralToken } from 'utils/features'
import { getCollateralToken } from 'store/selectors/blockchain'

/**
* Requests the configured tournaments collateralToken balance. If none is set, does nothing
* @param {function} dispatch
* @param {function} getState
*/
const requestTournamentTokenBalance = account => (dispatch) => {
const tournamentToken = getCollateralToken()
const requestCollateralTokenBalance = account => (dispatch, getState) => {
const state = getState()
const collateralToken = getCollateralToken(state)

if (!tournamentToken) {
return null
// no collateral token defined yet - this information might be asynchronous, if the
// defined collateral token is inside a contract.
if (!collateralToken || !collateralToken.source) {
return undefined
}

return dispatch(requestTokenBalance(tournamentToken.address, account))
// if the collateralToken source is the ETH balance from the users wallet, we don't need
// to start a request to fetch the balance, as it is auto updating in the current provider
if (collateralToken.source === TOKEN_SOURCE_ETH) {
return undefined
}

return dispatch(requestTokenBalance(collateralToken.address, account))
}

export default {
requestMainnetAddress,
requestTokenBalance: requestTournamentTokenBalance,
requestTokenBalance: requestCollateralTokenBalance,
openModal: modalName => dispatch => dispatch(openModal({ modalName })),
initUport: () => dispatch => dispatch(initProviders({ providers: [WALLET_PROVIDER.UPORT] })),
}
62 changes: 24 additions & 38 deletions src/containers/HeaderContainer/store/selectors.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,55 @@
import Decimal from 'decimal.js'

import {
getCurrentAccount,
getCurrentNetwork,
getCurrentBalance,
getActiveProvider,
checkWalletConnection,
hasAcceptedTermsAndConditions,
isConnectedToCorrectNetwork,
getTargetNetworkId,
getRegisteredMainnetAddress,
isMetamaskLocked,
hasAcceptedTermsAndConditions,
} from 'integrations/store/selectors'

import {
getLogoConfig,
isFeatureEnabled,
getFeatureConfig,
getCollateralToken,
} from 'utils/features'

import { getTokenAmount } from 'store/selectors/blockchain'
import { getCollateralToken } from 'store/selectors/blockchain'

import { meSelector } from 'routes/Scoreboard/store/selectors'

const collateralToken = getCollateralToken()
const gameGuideConfig = getFeatureConfig('gameGuide')
const logoConfig = getLogoConfig('logo')

/**
* Returns either the balance of the counfigured token of the current balance of ether
* @param {*} state
*/
const getCurrentTokenBalance = (state) => {
if (!collateralToken) {
return getCurrentBalance(state)
}

const amount = getTokenAmount(state, collateralToken.address)

if (!amount) {
return Decimal(0)
export default (state) => {
const collateralToken = getCollateralToken(state)

return {
hasWallet: checkWalletConnection(state),
currentAccount: getCurrentAccount(state),
currentNetwork: getCurrentNetwork(state),
currentProvider: getActiveProvider(state),
isConnectedToCorrectNetwork: isConnectedToCorrectNetwork(state),
targetNetworkId: getTargetNetworkId(state),
mainnetAddress: getRegisteredMainnetAddress(state),
lockedMetamask: isMetamaskLocked(state),
userTournamentInfo: meSelector(state),
logoPath: logoConfig.regular,
smallLogoPath: logoConfig.small,
showScoreboard: isFeatureEnabled('scoreboard'),
showGameGuide: isFeatureEnabled('gameGuide'),
gameGuideType: gameGuideConfig.type,
gameGuideURL: gameGuideConfig.url,
tokenAddress: collateralToken.address,
tokenBalance: collateralToken.balance,
acceptedTOS: hasAcceptedTermsAndConditions(state),
}

return amount.toString()
}

export default state => ({
hasWallet: checkWalletConnection(state),
currentAccount: getCurrentAccount(state),
currentNetwork: getCurrentNetwork(state),
currentProvider: getActiveProvider(state),
isConnectedToCorrectNetwork: isConnectedToCorrectNetwork(state),
targetNetworkId: getTargetNetworkId(state),
mainnetAddress: getRegisteredMainnetAddress(state),
lockedMetamask: isMetamaskLocked(state),
userTournamentInfo: meSelector(state),
logoPath: logoConfig.regular,
smallLogoPath: logoConfig.small,
showScoreboard: isFeatureEnabled('scoreboard'),
showGameGuide: isFeatureEnabled('gameGuide'),
gameGuideType: gameGuideConfig.type,
gameGuideURL: gameGuideConfig.url,
tokenAddress: collateralToken && collateralToken.address,
tokenBalance: getCurrentTokenBalance(state),
acceptedTOS: hasAcceptedTermsAndConditions(state),
})

1 change: 0 additions & 1 deletion src/integrations/store/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { WALLET_PROVIDER } from 'integrations/constants'
import { getConfiguration, getFeatureConfig } from 'utils/features'
import { formValueSelector } from 'redux-form'

const config = getConfiguration()

Expand Down
24 changes: 1 addition & 23 deletions src/integrations/uport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { WALLET_PROVIDER } from 'integrations/constants'
import InjectedWeb3 from 'integrations/injectedWeb3'
import { fetchTournamentUserData } from 'routes/Scoreboard/store/actions'
import { weiToEth, hexWithoutPrefix } from 'utils/helpers'
import { getCollateralToken, getProviderIntegrationConfig } from 'utils/features'
import { getProviderIntegrationConfig } from 'utils/features'
import initUportConnector, { connect, connectorLogOut, isUserConnected } from './connector'

const collateralToken = getCollateralToken()
export const { notificationsEnabled = false } = getProviderIntegrationConfig('uport')

class Uport extends InjectedWeb3 {
Expand Down Expand Up @@ -86,27 +85,6 @@ class Uport extends InjectedWeb3 {
return accounts && accounts.length ? accounts[0] : null
}

/**
* Returns the balance of olympia tokens for the current default account in Wei
* @async
* @param {string} account - Useraccount to get the balance for
* @returns {Promise<string>} - Accountbalance in WEI for current account
*/
async getBalance(account) {
const userAccount = account || this.account
if (!userAccount) {
throw new Error('No Account available')
}

const balance = await getTokenBalance(collateralToken.address, userAccount)

if (typeof balance !== 'undefined') {
return weiToEth(balance.toString())
}

throw new Error('Invalid Balance')
}

async logout() {
await super.logout()
connectorLogOut()
Expand Down
21 changes: 13 additions & 8 deletions src/routes/MarketList/store/actions/fetchMarkets.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { List } from 'immutable'
import { requestFromRestAPI } from 'api/utils/fetch'
import { hexWithoutPrefix } from 'utils/helpers'
import { getConfiguration, getCollateralToken } from 'utils/features'
import { getConfiguration } from 'utils/features'
import { OUTCOME_TYPES } from 'utils/constants'
import { BoundsRecord, CategoricalMarketRecord, ScalarMarketRecord, OutcomeRecord } from 'store/models'
import { getCollateralToken } from 'store/selectors/blockchain'
import addMarkets from './addMarkets'

const config = getConfiguration()
const { address: collateralTokenConfigAddress } = getCollateralToken() || {}
const whitelisted = config.whitelist || {}

const addresses = Object.keys(whitelisted).map(hexWithoutPrefix)
Expand Down Expand Up @@ -145,21 +145,26 @@ export const extractMarkets = markets =>
return builder.call(builder, market)
})

export const processMarketResponse = (dispatch, response) => {
export const processMarketResponse = (dispatch, state, response) => {
if (!response || !response.results) {
dispatch(addMarkets([]))
return
}

const applicationCollateralToken = getCollateralToken(state)

let marketRecords = extractMarkets(response.results)

if (collateralTokenConfigAddress) {
marketRecords = marketRecords.filter(({ collateralToken }) => collateralToken === hexWithoutPrefix(collateralTokenConfigAddress))
if (applicationCollateralToken.address) {
marketRecords = marketRecords.filter(({ collateralToken }) => collateralToken === hexWithoutPrefix(applicationCollateralToken.address))
}

dispatch(addMarkets(marketRecords))
}

export default () => dispatch =>
requestFromRestAPI('markets', { creator: addresses.join() })
.then(response => processMarketResponse(dispatch, response))
export default () => async (dispatch, getState) => {
const response = await requestFromRestAPI('markets', { creator: addresses.join() })
const state = getState()

return processMarketResponse(dispatch, state, response)
}
10 changes: 6 additions & 4 deletions src/routes/MarketList/store/test/market.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { combineReducers, createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import { List } from 'immutable'
import marketReducer, { REDUCER_ID } from 'store/reducers/market'
import blockchainReducer from 'store/reducers/blockchain'
import { processMarketResponse } from '../actions/fetchMarkets'
import { marketListSelector } from '../selectors'
import { oneMarketData, twoMarketData, realData, MarketFactory } from './builder/index.builder'
Expand All @@ -12,6 +13,7 @@ const marketReducerTests = () => {
beforeEach(() => {
const reducers = combineReducers({
[REDUCER_ID]: marketReducer,
blockchain: blockchainReducer,
})
const middlewares = [
thunk,
Expand All @@ -28,7 +30,7 @@ const marketReducerTests = () => {
const emptyResponse = { }

// WHEN
processMarketResponse(store.dispatch, emptyResponse)
processMarketResponse(store.dispatch, store.getState(), emptyResponse)

// THEN
const emptyList = List([])
Expand All @@ -41,7 +43,7 @@ const marketReducerTests = () => {
const threeMarketsResponse = realData

// WHEN
processMarketResponse(store.dispatch, threeMarketsResponse)
processMarketResponse(store.dispatch, store.getState(), threeMarketsResponse)

// THEN
const markets = marketListSelector(store.getState())
Expand All @@ -58,11 +60,11 @@ const marketReducerTests = () => {
it('should replace markets in store when fetch data', () => {
// GIVEN
const kittiesResponse = oneMarketData
processMarketResponse(store.dispatch, kittiesResponse)
processMarketResponse(store.dispatch, store.getState(), kittiesResponse)

// WHEN
const etherAndGasMarketsResponse = twoMarketData
processMarketResponse(store.dispatch, etherAndGasMarketsResponse)
processMarketResponse(store.dispatch, store.getState(), etherAndGasMarketsResponse)

// THEN
const markets = marketListSelector(store.getState())
Expand Down
Loading

0 comments on commit 46eeef9

Please sign in to comment.