-
Notifications
You must be signed in to change notification settings - Fork 502
/
sagas.js
405 lines (364 loc) · 12 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import ExchangeDelegate from '../../../exchange/delegate'
import { delay } from 'redux-saga'
import { apply, call, put, select } from 'redux-saga/effects'
import * as A from './actions'
import * as S from './selectors'
import * as walletActions from '../../wallet/actions'
import * as buySellSelectors from '../../kvStore/buySell/selectors'
import { coinifyService } from '../../../exchange/service'
import * as buySellA from '../../kvStore/buySell/actions'
import { equals, head, prop, sort, path, prepend } from 'ramda'
export default ({ api, options }) => {
const getCoinify = function * () {
const state = yield select()
const delegate = new ExchangeDelegate(state, api, 'coinify')
const value = yield select(buySellSelectors.getMetadata)
const walletOptions = state.walletOptionsPath.data
let coinify = yield apply(coinifyService, coinifyService.refresh,
[value, delegate, walletOptions])
yield apply(coinify, coinify.profile.fetch)
yield put(A.coinifyFetchProfileSuccess(coinify))
return coinify
}
let coinify
const refreshCoinify = function * () {
yield put(A.coinifyFetchProfileLoading())
const state = yield select()
const delegate = new ExchangeDelegate(state, api, 'coinify')
const value = yield select(buySellSelectors.getMetadata)
const walletOptions = state.walletOptionsPath.data
coinify = yield apply(coinifyService, coinifyService.refresh,
[value, delegate, walletOptions])
yield apply(coinify, coinify.profile.fetch)
yield put(A.coinifyFetchProfileSuccess(coinify))
}
const init = function * () {
try {
const val = yield select(buySellSelectors.getMetadata)
if (!path(['data', 'value', 'coinify', 'offline_token'], val)) return
yield call(refreshCoinify)
} catch (e) {
console.warn(e)
}
}
const coinifyFetchProfile = function * (noLoad) {
try {
yield put(A.coinifyFetchProfileLoading())
yield apply(coinify, coinify.profile.fetch)
yield put(A.coinifyFetchProfileSuccess(coinify.profile))
} catch (e) {
yield put(A.coinifyFetchProfileFailure(e))
}
}
const fetchQuote = function * (data) {
try {
const coinify = yield select(S.getProfile)
yield put(A.fetchQuoteLoading())
const { amount, baseCurrency, quoteCurrency, type } = data.quote
const getQuote = type === 'sell'
? coinify.data.getSellQuote
: coinify.data.getBuyQuote
const quote = yield apply(coinify.data, getQuote,
[Math.floor(amount), baseCurrency, quoteCurrency])
yield put(A.fetchQuoteSuccess(quote))
return quote
} catch (e) {
yield put(A.fetchQuoteFailure(e))
}
}
const refreshBuyQuote = function * () {
try {
const quote = yield select(S.getQuote)
const qData = path(['data'], quote)
let quotePayload = {
amount: qData.baseCurrency === 'BTC'
? (qData.baseAmount * -1) / 1e8
: (qData.baseAmount * -1) * 100,
baseCurrency: qData.baseCurrency,
quoteCurrency: qData.quoteCurrency,
type: 'buy'
}
const refreshedQuote = yield call(fetchQuote, {quote: quotePayload})
yield call(getPaymentMediums, {payload: refreshedQuote})
} catch (e) {
yield put(A.fetchQuoteFailure(e))
}
}
const refreshSellQuote = function * () {
try {
const quote = yield select(S.getQuote)
const qData = path(['data'], quote)
let quotePayload = {
baseCurrency: qData.baseCurrency,
quoteCurrency: qData.quoteCurrency,
type: 'sell'
}
if (qData.baseCurrency !== 'BTC') quotePayload['amount'] = qData.baseAmount * 100
else quotePayload['amount'] = qData.baseAmount / 1e8
const refreshedQuote = yield call(fetchQuote, { quote: quotePayload })
yield call(getPaymentMediums, { payload: refreshedQuote })
} catch (e) {
yield put(A.fetchQuoteFailure(e))
}
}
const fetchQuoteAndMediums = function * (data) {
try {
const { amt, baseCurrency, quoteCurrency, medium, type } = data.payload
const getQuote = type === 'sell'
? coinify.data.getSellQuote
: coinify.data.getBuyQuote
const quote = yield apply(coinify, getQuote, [amt, baseCurrency, quoteCurrency])
const mediums = yield apply(quote, quote.getPaymentMediums)
const account = yield apply(mediums[medium], mediums[medium].getAccounts)
yield put(A.fetchQuoteSuccess(quote))
yield put(A.getPaymentMediumsSuccess(mediums))
yield put(A.getMediumAccountsSuccess(account))
} catch (e) {
yield put(A.fetchQuoteFailure(e))
}
}
const fetchRateQuote = function * (data) {
try {
yield put(A.fetchQuoteLoading())
const { currency, type } = data.payload
const getQuote = type === 'sell' ? coinify.getSellQuote : coinify.getBuyQuote
const quote = yield apply(coinify, getQuote, [-1e8, 'BTC', currency])
yield put(A.fetchQuoteSuccess(quote))
} catch (e) {
yield put(A.fetchQuoteFailure(e))
}
}
const fetchTrades = function * (coinifyObj) {
try {
const payload = prop('payload', coinifyObj)
const coinify = !payload ? yield call(getCoinify) : payload
const trades = yield apply(coinify, coinify.getTrades)
yield put(A.fetchTradesSuccess(trades))
} catch (e) {
yield put(A.fetchTradesFailure(e))
}
}
const resetProfile = function * () {
yield put(A.resetProfile())
}
const getPaymentMediums = function * (data) {
try {
const quote = data.payload
yield put(A.getPaymentMediumsLoading())
const mediums = yield apply(quote, quote.getPaymentMediums)
yield put(A.getPaymentMediumsSuccess(mediums))
return mediums
} catch (e) {
yield put(A.getPaymentMediumsFailure(e))
}
}
const getMediumAccounts = function * (data) {
try {
const medium = data.payload
const account = yield apply(medium, medium.getAccounts)
yield put(A.getMediumAccountsSuccess(account))
} catch (e) {
yield put(A.getMediumAccountsFailure(e))
}
}
// Used in sell to get mediums with accounts
const getMediumsWithBankAccounts = function * (data) {
try {
const quote = data.payload
yield put(A.getPaymentMediumsLoading())
const mediums = yield apply(quote, quote.getPaymentMediums)
const medium = prop('bank', mediums)
yield apply(medium, medium.getBankAccounts)
yield put(A.getPaymentMediumsSuccess(mediums))
} catch (e) {
console.log(e)
yield put(A.getPaymentMediumsFailure(e))
}
}
const addBankAccount = function * (data) {
try {
const { medium, account } = data.payload
const bankAccount = yield apply(medium, medium.addBankAccount, [account])
yield put(A.setBankAccount(bankAccount))
} catch (e) {
console.log(e)
}
}
const deleteBankAccount = function * (data) {
try {
const account = data.payload
yield apply(account, account.delete)
} catch (e) {
console.log(e)
}
}
const signup = function * (data) {
const countryCode = data
let fiatCurrency
if (countryCode === 'DK') fiatCurrency = 'DKK'
else if (countryCode === 'GB') fiatCurrency = 'GBP'
else fiatCurrency = 'EUR'
try {
const coinify = yield call(getCoinify)
const signupResponse = yield apply(coinify, coinify.signup, [countryCode, fiatCurrency])
yield put(buySellA.coinifySetProfileBuySell(signupResponse))
yield put(A.coinifySetToken(signupResponse))
} catch (e) {
yield put(A.coinifySignupFailure(e))
}
}
const buy = function * (data, addressData) {
const { quote, medium } = data.payload
try {
yield put(A.handleTradeLoading())
const mediums = yield apply(quote, quote.getPaymentMediums)
const accounts = yield apply(mediums[medium], mediums[medium].getAccounts)
const buyResult = yield apply(accounts[0], accounts[0].buy)
yield put(A.handleTradeSuccess(buyResult))
const coinifyObj = yield call(getCoinify)
yield put(A.fetchTrades(coinifyObj))
// get current kvstore trades
const kvTrades = yield select(buySellSelectors.getCoinifyTrades)
// prepend new trade
const newTrades = prepend(buyResult, kvTrades)
// set new trades to metadata
yield put(buySellA.setCoinifyTradesBuySell(newTrades))
yield call(labelAddressForBuy, buyResult, addressData)
return buyResult
} catch (e) {
yield put(A.handleTradeFailure(e))
}
}
const labelAddressForBuy = function * (trade, addressData) {
try {
trade._account_index = addressData.accountIndex
trade._receive_index = addressData.index
const id = trade.tradeSubscriptionId || trade.id
yield put(walletActions.setHdAddressLabel(addressData.accountIndex, addressData.index, `Coinify order #${id}`))
} catch (e) {
yield put(A.handleTradeFailure(e))
}
}
const sell = function * () {
try {
yield put(A.handleTradeLoading())
const account = yield select(S.getAccount)
const sellResult = yield apply(account, account.sell)
yield put(A.handleTradeSuccess(sellResult))
yield put(A.fetchTrades())
yield call(getCoinify)
return sellResult
} catch (e) {
yield put(A.handleTradeFailure(e))
}
}
const cancelTrade = function * ({trade}) {
try {
yield apply(trade, trade.cancel)
yield call(fetchTrades)
if (prop('tradeSubscriptionId', trade)) {
yield call(fetchSubscriptions)
}
} catch (e) {
console.log('issue cancelling trade', e)
}
}
const triggerKYC = function * () {
try {
yield put(A.handleTradeLoading())
const coinify = yield call(getCoinify)
const kyc = yield apply(coinify, coinify.triggerKYC)
yield put(A.handleTradeSuccess(kyc))
yield put(A.getKyc())
return kyc
} catch (e) {
yield put(A.handleTradeFailure(e))
}
}
const getKYC = function * () {
try {
yield put(A.getKYCLoading())
const coinify = yield call(getCoinify)
const kycs = yield apply(coinify, coinify.getKYCs)
const byTime = (a, b) => prop('createdAt', b) - prop('createdAt', a)
const kyc = head(sort(byTime, kycs))
yield put(A.getKYCSuccess(kyc))
return kyc
} catch (e) {
yield put(A.getKYCFailure(e))
}
}
const pollKYCPending = function * () {
try {
const kyc = yield select(S.getKyc)
let status = kyc.map(prop('state')).getOrElse(undefined)
while (equals(status, 'pending')) {
yield call(delay, 1000)
const kycR = yield select(S.getKyc)
const kyc = kycR.getOrElse(undefined)
if (!kyc) {
return
}
yield apply(kyc, kyc.refresh)
status = prop('state', kyc)
yield put(A.getKYCSuccess(kyc))
}
} catch (e) {
yield put(A.getKYCFailure(e))
}
}
const kycAsTrade = function * (data) {
const { kyc } = data
try {
yield put(A.handleTradeSuccess(kyc))
} catch (e) {
yield put(A.handleTradeFailure(e))
}
}
const fetchSubscriptions = function * () {
try {
const coinify = yield call(getCoinify)
yield put(A.fetchSubscriptionsLoading())
const subs = yield apply(coinify, coinify.getSubscriptions)
yield put(A.fetchSubscriptionsSuccess(subs))
} catch (e) {
yield put(A.fetchSubscriptionsFailure(e))
}
}
const cancelSubscription = function * ({ id }) {
try {
yield call(refreshCoinify)
const cancelSub = yield apply(coinify, coinify.cancelSubscription, [id])
yield call(fetchSubscriptions)
return cancelSub
} catch (e) {
console.warn(e)
}
}
return {
signup,
buy,
sell,
init,
coinifyFetchProfile,
fetchTrades,
fetchSubscriptions,
fetchQuote,
fetchRateQuote,
resetProfile,
getPaymentMediums,
getMediumAccounts,
getMediumsWithBankAccounts,
addBankAccount,
deleteBankAccount,
fetchQuoteAndMediums,
cancelTrade,
cancelSubscription,
triggerKYC,
getKYC,
kycAsTrade,
pollKYCPending,
refreshBuyQuote,
refreshSellQuote
}
}