-
Notifications
You must be signed in to change notification settings - Fork 503
/
sagas.utils.ts
185 lines (172 loc) · 5.24 KB
/
sagas.utils.ts
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
import * as A from './actions'
import * as S from './selectors'
import { ADDRESS_TYPES } from 'blockchain-wallet-v4/src/redux/payment/btc/utils'
import { call, CallEffect, put, select } from 'redux-saga/effects'
import {
CoinType,
FiatType,
PaymentType,
PaymentValue,
RemoteDataType
} from 'core/types'
import { convertBaseToStandard } from '../exchange/services'
import { Exchange } from 'blockchain-wallet-v4/src'
import { INVALID_COIN_TYPE } from 'blockchain-wallet-v4/src/model'
import { promptForSecondPassword } from 'services/SagaService'
import { RatesType } from '../borrow/types'
import { selectors } from 'data'
export default ({ coreSagas, networks }: { coreSagas: any; networks: any }) => {
const buildAndPublishPayment = function * (
coin: CoinType,
payment: PaymentType,
destination: string
): Generator<PaymentType | CallEffect, boolean, any> {
let paymentError
try {
payment = yield payment.to(destination, ADDRESS_TYPES.ADDRESS)
payment = yield payment.build()
// ask for second password
const password = yield call(promptForSecondPassword)
payment = yield payment.sign(password)
payment = yield payment.publish()
} catch (e) {
throw e
}
return !paymentError
}
const createLimits = function * (payment: PaymentType) {
try {
const coin = S.getCoinType(yield select())
const limitsR = S.getInterestLimits(yield select())
const limits = limitsR.getOrFail('NO_LIMITS_AVAILABLE')
const balance = payment.value().effectiveBalance
const ratesR = S.getRates(yield select())
const rates = ratesR.getOrElse({} as RatesType)
const userCurrency = (yield select(
selectors.core.settings.getCurrency
)).getOrFail('Failed to get user currency')
const walletCurrencyR = S.getWalletCurrency(yield select())
const walletCurrency = walletCurrencyR.getOrElse({} as FiatType)
let maxFiat
switch (coin) {
case 'BTC':
maxFiat = Exchange.convertBtcToFiat({
value: balance,
fromUnit: 'SAT',
toCurrency: userCurrency,
rates
}).value
break
case 'ETH':
maxFiat = Exchange.convertEthToFiat({
value: balance,
fromUnit: 'WEI',
toCurrency: userCurrency,
rates
}).value
break
case 'PAX':
maxFiat = Exchange.convertPaxToFiat({
value: balance,
fromUnit: 'WEI',
toCurrency: userCurrency,
rates
}).value
break
case 'USDT':
maxFiat = Exchange.convertUsdtToFiat({
value: balance,
fromUnit: 'WEI',
toCurrency: userCurrency,
rates
}).value
break
default:
throw new Error(INVALID_COIN_TYPE)
}
const minFiat = limits[coin]?.minDepositAmount || 100
const maxCoin = Exchange.convertCoinToCoin({
value: balance,
coin,
baseToStandard: true
}).value
const minCoin = Exchange.convertFiatToCoin(
Number(convertBaseToStandard('FIAT', minFiat)),
coin,
walletCurrency,
rates
)
yield put(
A.setDepositLimits({
maxFiat: maxFiat,
minFiat: Number(convertBaseToStandard('FIAT', minFiat)), // default unit is cents, convert to standard
maxCoin: maxCoin,
minCoin: minCoin
})
)
} catch (e) {
yield put(A.setPaymentFailure(e))
}
}
const createPayment = function * (index?: number) {
let payment
const coin = S.getCoinType(yield select())
switch (coin) {
case 'BTC':
payment = coreSagas.payment.btc.create({
network: networks.btc
})
payment = yield payment.init()
payment = yield payment.from(index, ADDRESS_TYPES.ACCOUNT)
payment = yield payment.fee('regular')
break
case 'ETH':
payment = coreSagas.payment.eth.create({
network: networks.eth
})
payment = yield payment.init({ isErc20: false, coin })
payment = yield payment.from()
payment = yield payment.fee('priority')
break
case 'USDT':
case 'PAX':
payment = coreSagas.payment.eth.create({
network: networks.eth
})
payment = yield payment.init({ isErc20: true, coin })
payment = yield payment.from()
payment = yield payment.fee('priority')
break
default:
throw new Error(INVALID_COIN_TYPE)
}
return payment
}
const paymentGetOrElse = (
coin: CoinType,
paymentR: RemoteDataType<string | Error, PaymentValue>
): PaymentType => {
switch (coin) {
case 'USDT':
case 'PAX':
case 'ETH':
return coreSagas.payment.eth.create({
payment: paymentR.getOrElse(<PaymentType>{}),
network: networks.eth
})
case 'BTC':
return coreSagas.payment.btc.create({
payment: paymentR.getOrElse(<PaymentType>{}),
network: networks.btc
})
default:
throw new Error(INVALID_COIN_TYPE)
}
}
return {
buildAndPublishPayment,
createLimits,
createPayment,
paymentGetOrElse
}
}