Loëck Vézien
7 years ago
committed by
GitHub
28 changed files with 263 additions and 485 deletions
@ -1,24 +1,14 @@ |
|||
// @flow
|
|||
|
|||
import axios from 'axios' |
|||
import { fetchCurrentCounterValues } from '@ledgerhq/wallet-common/lib/api/countervalue' |
|||
|
|||
type SendFunction = (type: string, data: *) => void |
|||
|
|||
export default async (send: SendFunction, { counterValue, currencies }: Object) => { |
|||
const data = await axios |
|||
.get( |
|||
`https://min-api.cryptocompare.com/data/pricemulti?extraParams=ledger-test&fsyms=${currencies.join( |
|||
',', |
|||
)}&tsyms=${counterValue}`,
|
|||
) |
|||
.then(({ data }) => |
|||
currencies.reduce((result, code) => { |
|||
result.push({ |
|||
symbol: `${code}-${counterValue}`, |
|||
value: data[code][counterValue], |
|||
}) |
|||
return result |
|||
}, []), |
|||
) |
|||
send('counterValues.update', data) |
|||
try { |
|||
const data = await fetchCurrentCounterValues(currencies, counterValue) |
|||
send('counterValues.update', data) |
|||
} catch (err) { |
|||
console.error(err) // eslint-disable-line no-console
|
|||
} |
|||
} |
|||
|
@ -1,73 +1,41 @@ |
|||
// @flow
|
|||
|
|||
import { handleActions } from 'redux-actions' |
|||
|
|||
export type CounterValuesState = { |
|||
[string]: { |
|||
byDate: Object, |
|||
list: Array<[string, number]>, |
|||
}, |
|||
} |
|||
|
|||
import merge from 'lodash/merge' |
|||
import get from 'lodash/get' |
|||
import { |
|||
makeCalculateCounterValue, |
|||
makeReverseCounterValue, |
|||
formatCounterValueDay, |
|||
} from '@ledgerhq/wallet-common/lib/helpers/countervalue' |
|||
import type { CalculateCounterValue } from '@ledgerhq/wallet-common/lib/types' |
|||
|
|||
import type { State } from 'reducers' |
|||
|
|||
export type CounterValuesState = {} |
|||
const state: CounterValuesState = {} |
|||
|
|||
const handlers = { |
|||
UPDATE_COUNTER_VALUES: ( |
|||
state: CounterValuesState, |
|||
{ payload: counterValues }: { payload: CounterValuesState }, |
|||
): CounterValuesState => ({ |
|||
...state, |
|||
...counterValues, |
|||
}), |
|||
UPDATE_LAST_COUNTER_VALUE: ( |
|||
state: CounterValuesState, |
|||
{ payload: { symbol, value } }: { payload: { symbol: string, value: number } }, |
|||
): CounterValuesState => { |
|||
// We update only last value (newer)
|
|||
if (state[symbol]) { |
|||
const [date] = state[symbol].list[0] |
|||
// [0] date, [1] value, only update value
|
|||
state[symbol].list[0][1] = value |
|||
// Keep the same value for byDate object
|
|||
state[symbol].byDate[date] = value |
|||
|
|||
// Update reference for a proper update
|
|||
return { ...state } |
|||
} |
|||
|
|||
return state |
|||
}, |
|||
UPDATE_COUNTER_VALUES: (state, { payload: counterValues }) => merge(state, counterValues), |
|||
} |
|||
|
|||
export function getLastCounterValueBySymbol( |
|||
symbol: string, |
|||
state: { counterValues: CounterValuesState }, |
|||
): number { |
|||
return state.counterValues[symbol].list[0][1] |
|||
} |
|||
|
|||
export function serializeCounterValues(counterValues: Object) { |
|||
return Object.keys(counterValues).reduce((result, key) => { |
|||
const counterValue = counterValues[key].sort(([dateA], [dateB]) => (dateA < dateB ? 1 : -1)) |
|||
|
|||
result[key] = { |
|||
byDate: counterValue.reduce((r, [date, value]) => { |
|||
r[date] = value |
|||
return r |
|||
}, {}), |
|||
list: counterValue, |
|||
const getPairHistory = state => (coinTicker, fiat) => { |
|||
const byDate = get(state, `counterValues.${coinTicker}.${fiat}`) |
|||
return date => { |
|||
if (!byDate) { |
|||
return 0 |
|||
} |
|||
|
|||
return result |
|||
}, {}) |
|||
if (!date) { |
|||
return byDate.latest || 0 |
|||
} |
|||
return byDate[formatCounterValueDay(date)] || 0 |
|||
} |
|||
} |
|||
|
|||
export function deserializeCounterValues(counterValues: Object) { |
|||
return Object.keys(counterValues).reduce((result, key) => { |
|||
const counterValue = counterValues[key] |
|||
result[key] = counterValue.list |
|||
return result |
|||
}, {}) |
|||
} |
|||
export const calculateCounterValueSelector = (state: State): CalculateCounterValue => |
|||
makeCalculateCounterValue(getPairHistory(state)) |
|||
|
|||
export const reverseCounterValueSelector = (state: State): CalculateCounterValue => |
|||
makeReverseCounterValue(getPairHistory(state)) |
|||
|
|||
export default handleActions(handlers, state) |
|||
|
Loading…
Reference in new issue