-
Notifications
You must be signed in to change notification settings - Fork 504
/
sagas.js
65 lines (59 loc) · 2.05 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
import { call, put, select } from 'redux-saga/effects'
import { compose, isNil } from 'ramda'
import { set } from 'ramda-lens'
import * as A from './actions'
import { KVStoreEntry } from '../../../types'
import { getMetadataXpriv } from '../root/selectors'
import { derivationMap, SHAPESHIFT } from '../config'
const taskToPromise = t => new Promise((resolve, reject) => t.fork(reject, resolve))
export default ({ api }) => {
const callTask = function * (task) {
return yield call(compose(taskToPromise, () => task))
}
const fetchShapeShift = function * () {
const typeId = derivationMap[SHAPESHIFT]
const mxpriv = yield select(getMetadataXpriv)
const kv = KVStoreEntry.fromMetadataXpriv(mxpriv, typeId)
const newkv = yield callTask(api.fetchKVStore(kv))
yield put(A.setShapeShift(newkv))
}
const fetchShapeshiftTrade = function * (address) {
try {
const tradeDetails = yield call(api.getTradeStatus, address)
yield put(A.fetchShapeshiftTradeSuccess(tradeDetails))
} catch (e) {
yield put(A.fetchShapeshiftTradeFailure(e.message))
}
}
const createShapeshift = function * (kv) {
const newShapeshiftEntry = {
trades: [],
USAState: null
}
const newkv = set(KVStoreEntry.value, newShapeshiftEntry, kv)
yield put(A.createMetadataShapeshift(newkv))
}
const fetchMetadataShapeshift = function * () {
try {
const typeId = derivationMap[SHAPESHIFT]
const mxpriv = yield select(getMetadataXpriv)
const kv = KVStoreEntry.fromMetadataXpriv(mxpriv, typeId)
yield put(A.fetchMetadataShapeshiftLoading())
const newkv = yield callTask(api.fetchKVStore(kv))
yield put(A.fetchMetadataShapeshiftSuccess(newkv))
if (isNil(newkv.value)) {
yield call(createShapeshift, newkv)
} else {
yield put(A.fetchMetadataShapeshiftSuccess(newkv))
}
} catch (e) {
yield put(A.fetchMetadataShapeshiftFailure(e.message))
}
}
return {
createShapeshift,
fetchShapeShift,
fetchShapeshiftTrade,
fetchMetadataShapeshift
}
}