WA-232 Partial commit fetching balances

This commit is contained in:
apanizo 2018-06-27 17:28:53 +02:00
parent 91f3855e97
commit 3e2636b02b
17 changed files with 6968 additions and 101 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 21.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Ebene_7" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50" style="enable-background:new 0 0 50 50;" xml:space="preserve">
<g id="ZWC4wa.tif">
<g>
<g>
<path d="M13.5,26c3.7-6.1,7.2-12,11.1-18.4C28.3,14,31.9,19.9,35.6,26c-3.7,2.2-7.4,4.4-11.1,6.6C20.9,30.4,17.2,28.2,13.5,26z
M25.1,20.8c2.7,1.2,5.4,2.5,8.5,3.9c-2.9-4.8-5.6-9.3-8.5-14.1C25.1,14.2,25.1,17.4,25.1,20.8z M15.5,24.7
c3.1-1.4,5.8-2.7,8.4-3.8c0-3.4,0-6.6,0-10.2C21,15.4,18.4,19.9,15.5,24.7z M33.8,25.9c-3-1.4-5.8-2.6-8.7-3.9c0,3.1,0,6,0,9.1
C28,29.3,30.8,27.7,33.8,25.9z M23.9,22c-3,1.4-5.7,2.6-8.6,3.9c3,1.8,5.8,3.4,8.6,5.1C23.9,27.9,23.9,25,23.9,22z"/>
<path d="M24.5,33.3c3.5-2,6.9-4,12.1-7.1c-4.9,6.9-8.4,11.8-12.1,17.1C20.8,38,17.2,33,12.4,26.2C17.8,29.4,21.2,31.4,24.5,33.3z
M25.1,40.7c2.7-3.8,5.1-7.3,7.9-11.2c-3,1.8-5.5,3.3-7.9,4.7C25.1,36.4,25.1,38.4,25.1,40.7z M23.9,34.2
c-2.4-1.4-4.9-2.9-7.9-4.7c2.8,4,5.2,7.4,7.9,11.1C23.9,38.3,23.9,36.4,23.9,34.2z"/>
</g>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -16,12 +16,12 @@ type Props = Actions & {
userAccount: string,
}
type State = {
export type OpenState = {
safeAddress: string,
safeTx: string,
}
const createSafe = async (values: Object, userAccount: string, addSafe: AddSafe): Promise<State> => {
export const createSafe = async (values: Object, userAccount: string, addSafe: AddSafe): Promise<OpenState> => {
const accounts = getAccountsFrom(values)
const numConfirmations = getThresholdFrom(values)
const name = getSafeNameFrom(values)
@ -43,7 +43,7 @@ const createSafe = async (values: Object, userAccount: string, addSafe: AddSafe)
return { safeAddress: safeContract.address, safeTx: safe }
}
class Open extends React.Component<Props, State> {
class Open extends React.Component<Props, OpenState> {
constructor() {
super()

View File

@ -1,13 +1,13 @@
// @flow
import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
import fetchBalance from '~/routes/safe/store/actions/fetchBalance'
import fetchBalances from '~/routes/safe/store/actions/fetchBalances'
export type Actions = {
fetchSafe: typeof fetchSafe,
fetchBalance: typeof fetchBalance,
fetchBalances: typeof fetchBalances,
}
export default {
fetchSafe,
fetchBalance,
fetchBalances,
}

View File

@ -14,16 +14,26 @@ type Props = Actions & SelectorProps & {
class SafeView extends React.PureComponent<Props> {
componentDidMount() {
this.intervalId = setInterval(() => {
const { safe, fetchSafe, fetchBalance } = this.props
if (!safe) { return }
const safeAddress: string = safe.get('address')
fetchBalance(safeAddress)
if (safe) {
fetchSafe(safe)
const { safe, fetchSafe } = this.props
if (!safe) {
return
}
fetchSafe(safe)
}, 1500)
}
componentDidUpdate(prevProps) {
if (prevProps.safe) {
return
}
if (this.props.safe) {
const safeAddress = this.props.safe.get('address')
this.props.fetchBalances(safeAddress)
}
}
componentWillUnmount() {
clearInterval(this.intervalId)
}

View File

@ -1,19 +0,0 @@
// @flow
import { createAction } from 'redux-actions'
export const ADD_BALANCE = 'ADD_BALANCE'
type BalanceProps = {
safeAddress: string,
funds: string,
}
const addBalance = createAction(
ADD_BALANCE,
(safeAddress: string, funds: string): BalanceProps => ({
safeAddress,
funds,
}),
)
export default addBalance

View File

@ -0,0 +1,21 @@
// @flow
import { Map } from 'immutable'
import { createAction } from 'redux-actions'
import { type Balance } from '~/routes/safe/store/model/balance'
export const ADD_BALANCES = 'ADD_BALANCES'
type BalanceProps = {
safeAddress: string,
balances: Map<string, Balance>,
}
const addBalances = createAction(
ADD_BALANCES,
(safeAddress: string, balances: Map<string, Balance>): BalanceProps => ({
safeAddress,
balances,
}),
)
export default addBalances

View File

@ -1,11 +0,0 @@
// @flow
import type { Dispatch as ReduxDispatch } from 'redux'
import { getBalanceInEtherOf } from '~/wallets/getWeb3'
import { type GlobalState } from '~/store/index'
import addBalance from './addBalance'
export default (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
const balance = await getBalanceInEtherOf(safeAddress)
return dispatch(addBalance(safeAddress, balance))
}

View File

@ -0,0 +1,41 @@
// @flow
import { Map } from 'immutable'
import type { Dispatch as ReduxDispatch } from 'redux'
import { getBalanceInEtherOf } from '~/wallets/getWeb3'
import { type GlobalState } from '~/store/index'
import { makeBalance, type Balance } from '~/routes/safe/store/model/balance'
import addBalances from './addBalances'
export default (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
const balance = await getBalanceInEtherOf(safeAddress)
const ethBalance = makeBalance({
address: '0',
name: 'Ether',
symbol: 'ETH',
decimals: 18,
logoUrl: 'assets/icons/icon_etherTokens.svg',
funds: balance,
})
const header = new Headers({
'Access-Control-Allow-Origin': '*',
})
const sentData = {
mode: 'cors',
header,
}
const response = await fetch('https://gist.githubusercontent.com/rmeissner/98911fcf74b0ea9731e2dae2441c97a4/raw/', sentData)
if (!response.ok) {
throw new Error('Error querying safe balances')
}
const json = await response.json()
const balances: Map<string, Balance> = Map().withMutations((map) => {
json.forEach(item => map.set(item.symbol, makeBalance(item)))
map.set('ETH', ethBalance)
})
return dispatch(addBalances(safeAddress, balances))
}

View File

@ -0,0 +1,23 @@
// @flow
import { Record } from 'immutable'
import type { RecordFactory, RecordOf } from 'immutable'
export type BalanceProps = {
address: string,
name: string,
symbol: string,
decimals: number,
logoUrl: string,
funds: string,
}
export const makeBalance: RecordFactory<BalanceProps> = Record({
address: '',
name: '',
symbol: '',
decimals: 0,
logoUrl: '',
funds: '0',
})
export type Balance = RecordOf<BalanceProps>

View File

@ -1,13 +1,14 @@
// @flow
import { Map } from 'immutable'
import { handleActions, type ActionType } from 'redux-actions'
import addBalance, { ADD_BALANCE } from '~/routes/safe/store/actions/addBalance'
import addBalances, { ADD_BALANCES } from '~/routes/safe/store/actions/addBalances'
import { type Balance } from '~/routes/safe/store/model/balance'
export const BALANCE_REDUCER_ID = 'balances'
export type State = Map<string, string>
export type State = Map<string, Map<string, Balance>>
export default handleActions({
[ADD_BALANCE]: (state: State, action: ActionType<typeof addBalance>): State =>
state.set(action.payload.safeAddress, action.payload.funds),
[ADD_BALANCES]: (state: State, action: ActionType<typeof addBalances>): State =>
state.set(action.payload.safeAddress, action.payload.balances),
}, Map())

View File

@ -10,6 +10,7 @@ import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
import { type State as TransactionsState, TRANSACTIONS_REDUCER_ID } from '~/routes/safe/store/reducer/transactions'
import { type Transaction } from '~/routes/safe/store/model/transaction'
import { type Confirmation } from '~/routes/safe/store/model/confirmation'
import { type Balance } from '~/routes/safe/store/model/balance'
export type RouterProps = {
match: Match,
@ -81,15 +82,15 @@ export const safeSelector: Selector<GlobalState, RouterProps, SafeSelectorProps>
},
)
export const balanceSelector: Selector<GlobalState, RouterProps, string> = createSelector(
export const balanceSelector: Selector<GlobalState, RouterProps, Map<string, Balance>> = createSelector(
balancesSelector,
safeParamAddressSelector,
(balances: Map<string, string>, address: string) => {
(balances: Map<string, Map<string, Balance>>, address: string) => {
if (!address) {
return '0'
return Map()
}
return balances.get(address) || '0'
return balances.get(address) || Map()
},
)

View File

@ -1,44 +0,0 @@
// @flow
import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
import fetchBalance from '~/routes/safe/store/actions/fetchBalance'
import { aNewStore } from '~/store'
import { addEtherTo } from '~/test/utils/etherMovements'
import { aDeployedSafe } from './builder/deployedSafe.builder'
const balanceReducerTests = () => {
describe('Safe Actions[fetchBalance]', () => {
let store
beforeEach(async () => {
store = aNewStore()
})
it('reducer should return 0 to just deployed safe', async () => {
// GIVEN
const address = await aDeployedSafe(store)
// WHEN
await store.dispatch(fetchBalance(address))
// THEN
const balances = store.getState()[BALANCE_REDUCER_ID]
expect(balances).not.toBe(undefined)
expect(balances.get(address)).toBe('0')
})
it('reducer should return 1.3456 ETH as funds to safe with 1.3456 ETH', async () => {
// GIVEN
const address = await aDeployedSafe(store)
// WHEN
await addEtherTo(address, '1.3456')
await store.dispatch(fetchBalance(address))
// THEN
const balances = store.getState()[BALANCE_REDUCER_ID]
expect(balances).not.toBe(undefined)
expect(balances.get(address)).toBe('1.3456')
})
})
}
export default balanceReducerTests

View File

@ -1,5 +1,6 @@
// @flow
import addBalance from '~/routes/safe/store/actions/addBalance'
/*
import addBalances from '~/routes/safe/store/actions/addBalances'
import { aNewStore } from '~/store'
import { buildMathPropsFrom } from '~/test/utils/buildReactRouterProps'
import { balanceSelector } from '../selectors'
@ -26,7 +27,7 @@ const balanceSelectorTests = () => {
const store = aNewStore()
// WHEN
await store.dispatch(addBalance('bar', '1'))
await store.dispatch(addBalances('bar', '1'))
const balance = balanceSelector(store.getState(), { match })
// THEN
@ -40,7 +41,7 @@ const balanceSelectorTests = () => {
const store = aNewStore()
// WHEN
await store.dispatch(addBalance(safeAddress, '1.3456'))
await store.dispatch(addBalances(safeAddress, '1.3456'))
const balance = balanceSelector(store.getState(), { match })
// THEN
@ -50,3 +51,4 @@ const balanceSelectorTests = () => {
}
export default balanceSelectorTests
*/

View File

@ -1,7 +1,6 @@
// @flow
import balanceReducerTests from './balance.reducer'
import safeReducerTests from './safe.reducer'
import balanceSelectorTests from './balance.selector'
// import balanceSelectorTests from './balance.selector'
import safeSelectorTests from './safe.selector'
import grantedSelectorTests from './granted.selector'
import confirmationsSelectorTests from './confirmations.selector'
@ -10,13 +9,12 @@ import transactionsSelectorTests from './transactions.selector'
describe('Safe Test suite', () => {
// ACTIONS AND REDUCERS
safeReducerTests()
balanceReducerTests()
// SAFE SELECTOR
safeSelectorTests()
// BALANCE SELECTOR
balanceSelectorTests()
// balanceSelectorTests()
// GRANTED SELECTOR
grantedSelectorTests()

View File

@ -0,0 +1,107 @@
// @flow
import { makeSafe, type Safe } from '~/routes/safe/store/model/safe'
import { buildOwnersFrom, buildDailyLimitFrom } from '~/routes/safe/store/actions'
import { FIELD_NAME, FIELD_CONFIRMATIONS, FIELD_OWNERS, getOwnerNameBy, getOwnerAddressBy, FIELD_DAILY_LIMIT } from '~/routes/open/components/fields'
import { getWeb3, getProviderInfo } from '~/wallets/getWeb3'
import { promisify } from '~/utils/promisify'
import addSafe from '~/routes/safe/store/actions/addSafe'
import { createSafe, type OpenState } from '~/routes/open/container/Open'
import { type GlobalState } from '~/store/index'
import { makeProvider } from '~/wallets/store/model/provider'
import addProvider from '~/wallets/store/actions/addProvider'
class SafeBuilder {
safe: Safe
constructor() {
this.safe = makeSafe()
}
withAddress(address: string) {
this.safe = this.safe.set('address', address)
return this
}
withName(name: string) {
this.safe = this.safe.set('name', name)
return this
}
withConfirmations(confirmations: number) {
this.safe = this.safe.set('threshold', confirmations)
return this
}
withDailyLimit(limit: number, spentToday: number = 0) {
const dailyLimit = buildDailyLimitFrom(limit, spentToday)
this.safe = this.safe.set('dailyLimit', dailyLimit)
return this
}
withOwner(names: string[], adresses: string[]) {
const owners = buildOwnersFrom(names, adresses)
this.safe = this.safe.set('owners', owners)
return this
}
get() {
return this.safe
}
}
const aSafe = () => new SafeBuilder()
export class SafeFactory {
static oneOwnerSafe = (ownerAddress: string = '0x03db1a8b26d08df23337e9276a36b474510f0023') => aSafe()
.withAddress('0x03db1a8b26d08df23337e9276a36b474510f0025')
.withName('Adol ICO Safe')
.withConfirmations(1)
.withDailyLimit(10)
.withOwner(['Adol Metamask'], [ownerAddress])
.get()
static twoOwnersSafe = (firstOwner: string = '0x03db1a8b26d08df23337e9276a36b474510f0023', secondOwner: string = '0x03db1a8b26d08df23337e9276a36b474510f0024') => aSafe()
.withAddress('0x03db1a8b26d08df23337e9276a36b474510f0026')
.withName('Adol & Tobias Safe')
.withConfirmations(2)
.withOwner(
['Adol Metamask', 'Tobias Metamask'],
[firstOwner, secondOwner],
)
.withDailyLimit(10, 1.34)
.get()
static dailyLimitSafe = (dailyLimit: number, spentToday: number) => aSafe()
.withAddress('0x03db1a8b26d08df23337e9276a36b474510f0027')
.withName('Adol & Tobias Safe')
.withConfirmations(2)
.withOwner(
['Adol Metamask', 'Tobias Metamask'],
['0x03db1a8b26d08df23337e9276a36b474510f0023', '0x03db1a8b26d08df23337e9276a36b474510f0024'],
)
.withDailyLimit(dailyLimit, spentToday)
.get()
}
export const aMinedSafe = async (store: Store<GlobalState>): Promise<string> => {
const provider = await getProviderInfo()
const walletRecord = makeProvider(provider)
store.dispatch(addProvider(walletRecord))
const accounts = await promisify(cb => getWeb3().eth.getAccounts(cb))
const form = {
[FIELD_NAME]: 'Safe Name',
[FIELD_CONFIRMATIONS]: '1',
[FIELD_OWNERS]: '1',
[getOwnerNameBy(0)]: 'Adol Metamask 1',
[getOwnerAddressBy(0)]: accounts[0],
[FIELD_DAILY_LIMIT]: '5',
}
const addSafeFn: any = (...args) => store.dispatch(addSafe(...args))
const openSafeProps: OpenState = await createSafe(form, accounts[0], addSafeFn)
return openSafeProps.safeAddress
}
export default aSafe

View File

@ -0,0 +1,87 @@
// @flow
import contract from 'truffle-contract'
import { Map } from 'immutable'
import { BALANCE_REDUCER_ID } from '~/routes/safe/store/reducer/balances'
import fetchBalances from '~/routes/safe/store/actions/fetchBalances'
import { aNewStore } from '~/store'
import { aMinedSafe } from '~/test/builder/safe.redux.builder'
import { type Balance } from '~/routes/safe/store/model/balance'
import { addEtherTo } from '~/test/utils/etherMovements'
import Token from '#/test/Token.json'
import { getWeb3 } from '~/wallets/getWeb3'
import { promisify } from '~/utils/promisify'
describe('Safe Actions[fetchBalance]', () => {
let store
let address: string
beforeEach(async () => {
store = aNewStore()
address = await aMinedSafe(store)
})
it('reducer should return 0 to just deployed safe', async () => {
// GIVEN
const tokenList = ['WE', '<3', 'GNO', 'OMG', 'RDN']
// WHEN
await store.dispatch(fetchBalances(address))
// THEN
const balances: Map<string, Map<string, Balance>> | typeof undefined = store.getState()[BALANCE_REDUCER_ID]
if (!balances) throw new Error()
const safeBalances: Map<string, Balance> | typeof undefined = balances.get(address)
if (!safeBalances) throw new Error()
expect(safeBalances.size).toBe(6)
tokenList.forEach((token: string) => {
const record = safeBalances.get(token)
if (!record) throw new Error()
expect(record.get('funds')).toBe('0')
})
})
it('reducer should return 0.03456 ETH as funds to safe with 0.03456 ETH', async () => {
// WHEN
await addEtherTo(address, '0.03456')
await store.dispatch(fetchBalances(address))
// THEN
const balances: Map<string, Map<string, Balance>> | typeof undefined = store.getState()[BALANCE_REDUCER_ID]
if (!balances) throw new Error()
const safeBalances: Map<string, Balance> | typeof undefined = balances.get(address)
if (!safeBalances) throw new Error()
expect(safeBalances.size).toBe(6)
const ethBalance = safeBalances.get('ETH')
if (!ethBalance) throw new Error()
expect(ethBalance.get('funds')).toBe('0.03456')
})
it('reducer should return 2 GNO when safe has 2 GNO', async () => {
// GIVEN
const web3 = getWeb3()
const token = contract(Token)
token.setProvider(web3.currentProvider)
const accounts = await promisify(cb => getWeb3().eth.getAccounts(cb))
const myToken = await token.new({ from: accounts[0], gas: '5000000' })
await myToken.transfer(address, 100, { from: accounts[0], gas: '5000000' })
// console.log(await myToken.totalSupply())
// WHEN
await store.dispatch(fetchBalances(address))
// THEN
const balances: Map<string, Map<string, Balance>> | typeof undefined = store.getState()[BALANCE_REDUCER_ID]
if (!balances) throw new Error()
const safeBalances: Map<string, Balance> | typeof undefined = balances.get(address)
if (!safeBalances) throw new Error()
expect(safeBalances.size).toBe(6)
const ethBalance = safeBalances.get('ETH')
if (!ethBalance) throw new Error()
expect(ethBalance.get('funds')).toBe('0.03456')
})
})