-
Notifications
You must be signed in to change notification settings - Fork 504
/
sagas.js
111 lines (102 loc) · 3.57 KB
/
sagas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { call, put, select, take } from 'redux-saga/effects'
import { dissoc, isNil, length, mapObjIndexed, path, sum, values } from 'ramda'
import { convertFeeToWei } from '../../../utils/ethereum'
import * as A from './actions'
import * as S from './selectors'
import * as AT from './actionTypes'
import * as selectors from '../../selectors'
export default ({ api }) => {
const fetchData = function * (action) {
try {
yield put(A.fetchDataLoading())
const context = yield select(selectors.core.kvStore.ethereum.getContext)
const data = yield call(api.getEthereumData, context)
const latestBlock = yield call(api.getEthereumLatestBlock)
yield call(accountSaga, data, latestBlock)
} catch (e) {
yield put(A.fetchDataFailure(e.message))
}
}
const fetchFee = function * () {
try {
yield put(A.fetchFeeLoading())
const data = yield call(api.getEthereumFee)
const weiData = convertFeeToWei(data)
yield put(A.fetchFeeSuccess(weiData))
} catch (e) {
yield put(A.fetchFeeFailure(e.message))
}
}
const fetchLatestBlock = function * () {
try {
yield put(A.fetchLatestBlockLoading())
const data = yield call(api.getEthereumLatestBlock)
yield put(A.fetchLatestBlockSuccess(data))
} catch (e) {
yield put(A.fetchLatestBlockFailure(e.message))
}
}
const fetchRates = function * () {
try {
yield put(A.fetchRatesLoading())
const data = yield call(api.getEthereumTicker)
yield put(A.fetchRatesSuccess(data))
} catch (e) {
yield put(A.fetchRatesFailure(e.message))
}
}
const watchTransactions = function * () {
while (true) {
const action = yield take(AT.FETCH_ETHEREUM_TRANSACTIONS)
yield call(fetchTransactions, action)
}
}
const fetchTransactions = function * (action) {
const { payload } = action
const { reset } = payload
try {
const defaultAccountR = yield select(selectors.kvStore.ethereum.getContext)
const address = defaultAccountR.getOrFail('Could not get ethereum context.')
const pages = reset ? [] : yield select(S.getTransactions)
const nextPage = length(pages)
yield put(A.fetchTransactionsLoading())
const data = yield call(api.getEthereumTransactions, address, nextPage)
const latestBlock = yield call(api.getEthereumLatestBlock)
yield call(accountSaga, data, latestBlock)
const txs = path([address, 'txns'], data)
if (isNil(txs)) return
yield put(A.fetchTransactionsSuccess(txs, reset))
} catch (e) {
yield put(A.fetchTransactionsFailure(e.message))
}
}
const accountSaga = function * (data, latestBlock) {
// Accounts treatments
const finalBalance = sum(values(data).map(obj => obj.balance))
const totalReceived = sum(values(data).map(obj => obj.totalReceived))
const totalSent = sum(values(data).map(obj => obj.totalSent))
const nTx = sum(values(data).map(obj => obj.txn_count))
const addresses = mapObjIndexed((num, key, obj) => dissoc('txns', num), data)
// const transactions = mapObjIndexed((num, key, obj) => sortBy(compose(negate, prop('timeStamp')), prop('txns', num)), data)
const ethereumData = {
addresses,
info: {
n_tx: nTx,
total_received: totalReceived,
total_sent: totalSent,
final_balance: finalBalance
},
latest_block: latestBlock
// transactions
}
yield put(A.fetchDataSuccess(ethereumData))
}
return {
fetchFee,
fetchData,
fetchRates,
fetchLatestBlock,
fetchTransactions,
watchTransactions
}
}