-
Notifications
You must be signed in to change notification settings - Fork 504
/
sagas.js
201 lines (180 loc) · 6.76 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { put, call, select } from 'redux-saga/effects'
import { delay } from 'redux-saga'
import * as A from './actions'
import * as actions from '../../actions'
import * as selectors from '../../selectors.js'
import * as modalActions from '../../modals/actions'
import * as sendBtcActions from '../../components/sendBtc/actions'
import * as sendBtcSelectors from '../../components/sendBtc/selectors'
import settings from 'config'
import * as C from 'services/AlertService'
import { promptForSecondPassword } from 'services/SagaService'
import { path, prop } from 'ramda'
export default ({ coreSagas }) => {
const logLocation = 'modules/sfox/sagas'
const setBankManually = function * (action) {
try {
yield put(A.sfoxLoading())
yield call(coreSagas.data.sfox.setBankManually, action.payload)
const accounts = yield select(selectors.core.data.sfox.getAccounts)
if (accounts.error) {
throw new Error(JSON.parse(accounts.error).error)
}
yield put(A.sfoxSuccess())
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'setBankManually', e))
yield put(A.sfoxFailure(e))
}
}
const sfoxSignup = function * () {
try {
yield put(A.sfoxLoading())
yield call(coreSagas.data.sfox.signup)
const profile = yield select(selectors.core.data.sfox.getProfile)
if (!profile.error) {
yield put(A.sfoxSuccess())
yield put(A.nextStep('verify'))
} else {
yield put(A.sfoxNotAsked())
throw new Error(JSON.parse(profile.error).error)
}
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'sfoxSignup', e))
yield put(A.sfoxFailure(e))
}
}
const setProfile = function * (payload) {
try {
yield call(coreSagas.data.sfox.setProfile, payload)
const profile = yield select(selectors.core.data.sfox.getProfile)
if (profile.error) {
throw new Error(profile.error)
} else {
if (profile.data.verificationStatus.required_docs.length) {
yield put(A.nextStep('upload'))
} else {
yield put(A.nextStep('funding'))
}
}
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'setProfile', e))
yield put(A.setVerifyError(e))
}
}
const upload = function * (payload) {
try {
yield call(coreSagas.data.sfox.uploadDoc, payload)
const profile = yield select(selectors.core.data.sfox.getProfile)
if (!profile.data.verificationStatus.required_docs.length) {
yield put(A.nextStep('funding'))
}
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'upload', e))
yield put(actions.alerts.displayError(C.DOCUMENT_UPLOAD_ERROR))
}
}
const setBank = function * (payload) {
try {
yield call(coreSagas.data.sfox.setBankAccount, payload)
yield put(actions.alerts.displaySuccess(C.BANK_ACCOUNT_SET_SUCCESS))
yield put(modalActions.closeAllModals())
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'setBank', e))
}
}
const submitMicroDeposits = function * (payload) {
try {
yield put(A.sfoxLoading())
const result = yield call(coreSagas.data.sfox.verifyMicroDeposits, payload)
if (result.status === 'active') {
yield put(A.sfoxSuccess())
yield call(delay, 1000)
// yield put(modalActions.closeAllModals())
} else {
yield put(A.sfoxNotAsked())
throw new Error(result)
}
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'submitMicroDeposits', e))
yield put(A.sfoxFailure(e))
}
}
const submitQuote = function * (action) {
try {
yield put(A.sfoxLoading())
const nextAddressData = yield call(prepareAddress)
const trade = yield call(coreSagas.data.sfox.handleTrade, action.payload, nextAddressData)
if (prop('message', trade) || prop('name', trade) === 'AssertionError') {
if (trade.message === 'QUOTE_EXPIRED') {
throw new Error("The quote has expired. If this continues to happen, we recommend automatically setting your computer's date & time.")
}
throw new Error(trade.message)
}
yield put(A.sfoxSuccess())
yield put(actions.form.change('buySellTabStatus', 'status', 'order_history'))
yield put(modalActions.showModal('SfoxTradeDetails', { trade }))
} catch (e) {
yield put(A.sfoxFailure(e))
yield put(actions.logs.logErrorMessage(logLocation, 'submitQuote', e))
}
}
const prepareAddress = function * () {
try {
const state = yield select()
const defaultIdx = selectors.core.wallet.getDefaultAccountIndex(state)
const receiveR = selectors.core.common.btc.getNextAvailableReceiveAddress(settings.NETWORK_BITCOIN, defaultIdx, state)
const receiveIdxR = selectors.core.common.btc.getNextAvailableReceiveIndex(settings.NETWORK_BITCOIN, defaultIdx, state)
return {
address: receiveR.getOrElse(),
index: receiveIdxR.getOrElse(),
accountIndex: defaultIdx
}
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'prepareAddress', e))
}
}
const submitSellQuote = function * (action) {
const q = action.payload
try {
yield put(A.sfoxLoading())
const trade = yield call(coreSagas.data.sfox.handleSellTrade, q)
let p = yield select(sendBtcSelectors.getPayment)
let payment = yield coreSagas.payment.btc.create({ payment: p.getOrElse({}), network: settings.NETWORK_BITCOIN })
payment = yield payment.amount(parseInt(trade.sendAmount))
// QA Tool: manually set a "to" address on the payment object for testing sell
const qaState = yield select()
const qaAddress = path(['qa', 'qaSellAddress'], qaState)
if (qaAddress) {
payment = yield payment.to(qaAddress)
} else {
payment = yield payment.to(trade.receiveAddress)
}
payment = yield payment.description(`Exchange Trade SFX-${trade.id}`)
try {
payment = yield payment.build()
} catch (e) {
yield put(actions.logs.logErrorMessage(logLocation, 'submitSellQuote', e))
}
const password = yield call(promptForSecondPassword)
payment = yield payment.sign(password)
payment = yield payment.publish()
yield put(sendBtcActions.sendBtcPaymentUpdatedSuccess(payment.value()))
yield put(A.sfoxSuccess())
yield put(actions.form.change('buySellTabStatus', 'status', 'order_history'))
} catch (e) {
yield put(A.sfoxFailure(e))
yield put(actions.logs.logErrorMessage(logLocation, 'submitSellQuote', e))
}
}
return {
prepareAddress,
setBankManually,
setBank,
sfoxSignup,
setProfile,
submitMicroDeposits,
submitQuote,
submitSellQuote,
upload
}
}