Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

#99 Refactor redux store #101

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
6e9799b
remove cookie store and session storage
Apr 8, 2019
0f7d68b
eslint fix
Apr 8, 2019
6003d8d
display all safes and not owners only in safe list
Apr 8, 2019
3b27729
load active tokens from localstorage when visiting safe view
Apr 8, 2019
8108b6b
add an action to fetch token balances
Apr 8, 2019
8a8b297
action for fetching active token balances wip
Apr 9, 2019
4f59865
reducer fix, fetching balances and updating balances fixes
Apr 9, 2019
1125536
tokens fetch fixes, disable eth switch in tokens modal
Apr 9, 2019
56c6892
split fetching tokens function
Apr 9, 2019
f598344
fix token fetch
Apr 10, 2019
93cf045
update safe and token models
Apr 10, 2019
a158fe4
add extended token selector
Apr 10, 2019
78883ea
extended token selector fix, dep bump
Apr 11, 2019
fe35b81
refactor tokens reducer
Apr 11, 2019
f94e598
balance fetch fixes
Apr 11, 2019
be295d1
tokens balance refactor wip
Apr 11, 2019
5f142bc
add eth balance to safe model
Apr 11, 2019
3530d2f
remove eth from list of active tokens
Apr 11, 2019
50a315b
show eth balance in balance tab
Apr 12, 2019
f797e2d
use addSafe instead updateSafe while adding a new safe
Apr 12, 2019
a6249de
add SafeToken record
Apr 12, 2019
49f0004
update readme
Apr 12, 2019
f8d1e68
Update safe tokens wip
Apr 12, 2019
413cfa3
reducer fix
Apr 12, 2019
595731d
fix displaying active tokens
Apr 12, 2019
910382c
Active tokens WIP, needs debugging: tokens get washed after safe update
Apr 12, 2019
f486068
rewrite fetchSafe function to support updates
mmv08 Apr 15, 2019
3c5d7fc
fix token balance updating
mmv08 Apr 15, 2019
fc3e1e4
eslint fix
mmv08 Apr 15, 2019
400a19f
fix token balance updating
mmv08 Apr 15, 2019
d7127fb
rewrite active tokens logic to use updateSafe action
mmv08 Apr 15, 2019
81e05c1
remove unused vars, add comment for generateProps func
mmv08 Apr 15, 2019
0b6be00
fix checkbox displaying in manage tokens modal
mmv08 Apr 15, 2019
2c8a79e
remove cdu, otherwise it will fail on many token updates because of i…
Apr 15, 2019
7df11f3
update fix
Apr 15, 2019
b9b936d
activeTokens refactor (again)
mmv08 Apr 16, 2019
d8fbdd3
conflict ifx
mmv08 Apr 16, 2019
76b41a4
merge fixes
mmv08 Apr 16, 2019
4c1197a
fix token balance selector
mmv08 Apr 16, 2019
3373b83
storage middleware wip, add optimization comment
Apr 16, 2019
7e49f8d
fix storing safe in localstorage
Apr 16, 2019
dc185e8
fix safe loading from storage wip
mmv08 Apr 17, 2019
e3d7984
fix safe loading from storage
mmv08 Apr 17, 2019
81fbb3b
add selector for all active tokens
mmv08 Apr 17, 2019
cdaa6c8
fix loading tokens from storage
mmv08 Apr 17, 2019
289f5f6
token redux-related stuff cleanup after refactor
mmv08 Apr 17, 2019
43e8521
eslint fix
mmv08 Apr 17, 2019
c633165
rename setActiveTokens to saveActiveTokens
mmv08 Apr 17, 2019
84af7ae
eslint fixes
mmv08 Apr 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-final-form": "^4.1.0",
"react-hot-loader": "^4.8.2",
"react-hot-loader": "4.8.4",
"react-infinite-scroll-component": "^4.5.2",
"react-redux": "^6.0.1",
"react-redux": "7.0.2",
"react-router-dom": "^4.3.1",
"recompose": "^0.30.0",
"redux": "^4.0.1",
Expand Down Expand Up @@ -117,11 +117,11 @@
"@babel/preset-flow": "^7.0.0-beta.40",
"@babel/preset-react": "^7.0.0-beta.40",
"@sambego/storybook-state": "^1.0.7",
"@storybook/addon-actions": "^5.0.6",
"@storybook/addon-knobs": "^5.0.6",
"@storybook/addon-links": "^5.0.6",
"@storybook/react": "^5.0.6",
"autoprefixer": "^9.4.10",
"@storybook/addon-actions": "5.0.9",
"@storybook/addon-knobs": "5.0.9",
"@storybook/addon-links": "5.0.9",
"@storybook/react": "5.0.9",
"autoprefixer": "9.5.1",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.1.0",
Expand All @@ -134,38 +134,38 @@
"detect-port": "^1.2.2",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-flowtype": "^3.4.2",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-flowtype": "3.6.1",
"eslint-plugin-import": "2.17.2",
"eslint-plugin-jest": "^22.3.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"ethereumjs-abi": "^0.6.7",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"file-loader": "^3.0.1",
"flow-bin": "0.96.0",
"flow-bin": "0.97.0",
"fs-extra": "^7.0.1",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.0.4",
"jest": "^24.1.0",
"json-loader": "^0.5.7",
"mini-css-extract-plugin": "^0.5.0",
"mini-css-extract-plugin": "0.6.0",
"postcss-loader": "^3.0.0",
"postcss-mixins": "^6.2.0",
"postcss-simple-vars": "^5.0.2",
"pre-commit": "^1.2.2",
"prettier-eslint-cli": "^4.7.1",
"run-with-testrpc": "^0.3.0",
"run-with-testrpc": "0.3.1",
"storybook-host": "^5.0.3",
"storybook-router": "^0.3.3",
"style-loader": "^0.23.1",
"truffle": "^5.0.10",
"truffle": "5.0.12",
"truffle-contract": "^4.0.11",
"truffle-solidity-loader": "^0.1.10",
"truffle-solidity-loader": "0.1.12",
"uglifyjs-webpack-plugin": "^2.1.2",
"webpack": "^4.1.1",
"webpack-bundle-analyzer": "^3.1.0",
"webpack-bundle-analyzer": "3.3.2",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.1.0",
"webpack-dev-server": "3.3.1",
"webpack-manifest-plugin": "^2.0.0-rc.2"
}
}
14 changes: 8 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ These instructions will get you a copy of the project up and running on your loc
What things you need to install the software and how to install them

```
npm install truffle // recommended usage of -g flag
npm install ganache-cli // recommended usage of -g flag
npm install flow-type // recommended usage of -g flag
yarn add truffle // recommended usage of -g flag
yarn add ganache-cli // recommended usage of -g flag
yarn add flow-type // recommended usage of -g flag
git clone https://github.com/gnosis/safe-contracts.git
```

We use [yarn](https://yarnpkg.com) in our infrastacture, so we decided to go with yarn in the README

### Installing

A step by step series of examples that tell you have to get a development env running
Expand All @@ -29,14 +31,14 @@ ganache-cli -b 3
Start the project in the other one
```
cd safe-contracts && truffle compile && truffle migrate && cd ..
npm install
npm start
yarn install
yarn start
```

## Running the tests

```
npm test
yarn test
```


Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import ReactDOM from 'react-dom'
import Root from '~/components/Root'
import { store } from '~/store'
import loadSafesFromStorage from '~/routes/safe/store/actions/loadSafesFromStorage'
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'

store.dispatch(loadActiveTokens())
store.dispatch(loadSafesFromStorage())

ReactDOM.render(<Root />, document.getElementById('root'))
4 changes: 2 additions & 2 deletions src/logic/safe/safeFrontendOperations.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @flow
import { List } from 'immutable'
import { type Transaction } from '~/routes/safe/store/model/transaction'
import { type Transaction } from '~/routes/safe/store/models/transaction'
import { executeTransaction, approveTransaction } from '~/logic/safe/safeBlockchainOperations'
import { EMPTY_DATA } from '~/logic/wallets/ethTransactions'
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { type Safe } from '~/routes/safe/store/model/safe'
import { type Safe } from '~/routes/safe/store/models/safe'
import { getGnosisSafeContract } from '~/logic/contracts/safeContracts'
import { storeSubject } from '~/utils/storage/transactions'

Expand Down
2 changes: 1 addition & 1 deletion src/logic/safe/utils/safeStorage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import { type Owner } from '~/routes/safe/store/model/owner'
import { type Owner } from '~/routes/safe/store/models/owner'
import { List, Map } from 'immutable'
import { loadFromStorage, saveToStorage } from '~/utils/storage'

Expand Down
11 changes: 5 additions & 6 deletions src/logic/tokens/store/actions/addToken.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @flow
import { createAction } from 'redux-actions'
import { type Token } from '~/logic/tokens/store/model/token'
import { setActiveTokens, getActiveTokens, setToken } from '~/logic/tokens/utils/tokensStorage'
import { saveActiveTokens, getActiveTokens, setToken } from '~/logic/tokens/utils/tokensStorage'
import type { Dispatch as ReduxDispatch } from 'redux'
import { type GlobalState } from '~/store/index'
import { type GlobalState } from '~/store/'

export const ADD_TOKEN = 'ADD_TOKEN'

Expand All @@ -14,17 +14,16 @@ type AddTokenProps = {

export const addToken = createAction<string, *, *>(
ADD_TOKEN,
(safeAddress: string, token: Token): AddTokenProps => ({
safeAddress,
(token: Token): AddTokenProps => ({
token,
}),
)

const saveToken = (safeAddress: string, token: Token) => async (dispatch: ReduxDispatch<GlobalState>) => {
dispatch(addToken(safeAddress, token))
dispatch(addToken(token))

const activeTokens = await getActiveTokens(safeAddress)
await setActiveTokens(safeAddress, activeTokens.push(token.toJS()))
await saveActiveTokens(safeAddress, activeTokens.push(token.toJS()))
setToken(safeAddress, token)
}

Expand Down
21 changes: 0 additions & 21 deletions src/logic/tokens/store/actions/disableToken.js

This file was deleted.

22 changes: 0 additions & 22 deletions src/logic/tokens/store/actions/enableToken.js

This file was deleted.

65 changes: 12 additions & 53 deletions src/logic/tokens/store/actions/fetchTokens.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// @flow
import { List, Map } from 'immutable'
import { List } from 'immutable'
import contract from 'truffle-contract'
import axios from 'axios'
import { BigNumber } from 'bignumber.js'
import type { Dispatch as ReduxDispatch } from 'redux'
import StandardToken from '@gnosis.pm/util-contracts/build/contracts/GnosisStandardToken.json'
import HumanFriendlyToken from '@gnosis.pm/util-contracts/build/contracts/HumanFriendlyToken.json'
import { getWeb3 } from '~/logic/wallets/getWeb3'
import { type GlobalState } from '~/store/index'
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
import { makeToken, type TokenProps } from '~/logic/tokens/store/model/token'
import { ensureOnce } from '~/utils/singleton'
import { getActiveTokens, getTokens } from '~/logic/tokens/utils/tokensStorage'
import { getSafeEthToken } from '~/logic/tokens/utils/tokenHelpers'
import saveTokens from './saveTokens'
import { getRelayUrl } from '~/config/index'

Expand All @@ -34,66 +31,28 @@ export const getHumanFriendlyToken = ensureOnce(createHumanFriendlyTokenContract

export const getStandardTokenContract = ensureOnce(createStandardTokenContract)

export const calculateBalanceOf = async (tokenAddress: string, address: string, decimals: number) => {
const erc20Token = await getStandardTokenContract()
let balance = 0

try {
const token = await erc20Token.at(tokenAddress)
balance = await token.balanceOf(address)
} catch (err) {
console.error('Failed to fetch token balances: ', err)
}

return new BigNumber(balance).div(10 ** decimals).toString()
}

export const fetchTokensData = async () => {
const fetchTokenList = async () => {
const apiUrl = getRelayUrl()
const url = `${apiUrl}/tokens`
const errMsg = 'Error querying safe balances'
return axios.get(url, errMsg)
}

export const fetchTokens = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
const tokens: List<TokenProps> = await getActiveTokens(safeAddress)
const ethBalance = await getSafeEthToken(safeAddress)
const customTokens = await getTokens(safeAddress)
const {
data: { results },
} = await fetchTokensData()

export const fetchTokens = () => async (dispatch: ReduxDispatch<GlobalState>) => {
try {
const balancesRecords = await Promise.all(
results.map(async (item: TokenProps) => {
const status = tokens.findIndex(activeToken => activeToken.name === item.name) !== -1
const funds = status ? await calculateBalanceOf(item.address, safeAddress, item.decimals) : '0'

return makeToken({ ...item, status, funds })
}),
)

const customTokenRecords = await Promise.all(
customTokens.map(async (item: TokenProps) => {
const status = tokens.findIndex(activeToken => activeToken.name === item.name) !== -1
const funds = status ? await calculateBalanceOf(item.address, safeAddress, item.decimals) : '0'
const {
data: { results: tokenList },
} = await fetchTokenList()

return makeToken({ ...item, status, funds })
}),
)
const tokens = List(tokenList.map((token: TokenProps) => makeToken(token)))

const balances: Map<string, Token> = Map().withMutations((map) => {
balancesRecords.forEach(record => map.set(record.address, record))
customTokenRecords.forEach(record => map.set(record.address, record))

map.set(ethBalance.address, ethBalance)
})

return dispatch(saveTokens(safeAddress, balances))
dispatch(saveTokens(tokens))
} catch (err) {
// eslint-disable-next-line
console.log('Error fetching tokens... ' + err)
console.log('Error fetching token list ' + err)

return Promise.resolve()
}
}

export default fetchTokens
24 changes: 24 additions & 0 deletions src/logic/tokens/store/actions/loadActiveTokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @flow
import type { Dispatch as ReduxDispatch } from 'redux'
import { Map, List } from 'immutable'
import { type TokenProps, type Token, makeToken } from '~/logic/tokens/store/model/token'
import { type GlobalState } from '~/store/index'
import { getActiveTokens } from '~/logic/tokens/utils/tokensStorage'
import saveTokens from './saveTokens'

const loadActiveTokens = () => async (dispatch: ReduxDispatch<GlobalState>) => {
try {
const tokens: Map<string, TokenProps> = await getActiveTokens()

const tokenRecordsList: List<Token> = List(
Object.values(tokens).map(token => makeToken(token)),
)

dispatch(saveTokens(tokenRecordsList))
} catch (err) {
// eslint-disable-next-line
console.error('Error while loading active tokens from storage:', err)
}
}

export default loadActiveTokens
26 changes: 4 additions & 22 deletions src/logic/tokens/store/actions/saveTokens.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
// @flow
import { Map, List } from 'immutable'
import { Map } from 'immutable'
import { createAction } from 'redux-actions'
import type { Dispatch as ReduxDispatch } from 'redux'
import { type Token } from '~/logic/tokens/store/model/token'
import { ensureOnceAsync } from '~/utils/singleton'
import { type GlobalState } from '~/store/index'
import { setActiveTokens } from '~/logic/tokens/utils/tokensStorage'
import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelpers'

export const ADD_TOKENS = 'ADD_TOKENS'

const setTokensOnce = ensureOnceAsync(setActiveTokens)

type TokenProps = {
safeAddress: string,
tokens: Map<string, Token>,
}

export const addTokens = createAction<string, *, *>(
const addTokens = createAction<string, *, *>(
ADD_TOKENS,
(safeAddress: string, tokens: Map<string, Token>): TokenProps => ({
safeAddress,
(tokens: Map<string, Token>): TokenProps => ({
tokens,
}),
)

const saveTokens = (safeAddress: string, tokens: Map<string, Token>) => async (
dispatch: ReduxDispatch<GlobalState>,
) => {
dispatch(addTokens(safeAddress, tokens))

const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
await setTokensOnce(safeAddress, activeAddresses)
}

export default saveTokens
export default addTokens
Loading