4
4
NodeTier ,
5
5
} from 'types/generated/auctioneerrpc/auctioneer_pb' ;
6
6
import { LeaseDuration } from 'types/state' ;
7
+ import Big from 'big.js' ;
7
8
import debounce from 'lodash/debounce' ;
8
9
import { annualPercentRate , toBasisPoints , toPercent } from 'util/bigmath' ;
9
10
import { BLOCKS_PER_DAY } from 'util/constants' ;
@@ -22,8 +23,8 @@ export default class OrderFormView {
22
23
23
24
/** the currently selected type of the order */
24
25
orderType : OrderType = OrderType . Bid ;
25
- amount = 0 ;
26
- premium = 0 ;
26
+ amount = Big ( 0 ) ;
27
+ premium = Big ( 0 ) ;
27
28
duration = 0 ;
28
29
minChanSize = DEFAULT_MIN_CHAN_SIZE ;
29
30
maxBatchFeeRate = DEFAULT_MAX_BATCH_FEE ;
@@ -33,8 +34,8 @@ export default class OrderFormView {
33
34
addlOptionsVisible = false ;
34
35
35
36
/** quoted fees */
36
- executionFee = 0 ;
37
- worstChainFee = 0 ;
37
+ executionFee = Big ( 0 ) ;
38
+ worstChainFee = Big ( 0 ) ;
38
39
quoteLoading = false ;
39
40
40
41
constructor ( store : Store ) {
@@ -52,16 +53,16 @@ export default class OrderFormView {
52
53
53
54
/** the error message if the amount is invalid */
54
55
get amountError ( ) {
55
- if ( ! this . amount ) return '' ;
56
- if ( this . amount % ONE_UNIT !== 0 ) {
56
+ if ( this . amount . eq ( 0 ) ) return '' ;
57
+ if ( ! this . amount . mod ( ONE_UNIT ) . eq ( 0 ) ) {
57
58
return l ( 'errorMultiple' ) ;
58
59
}
59
60
return '' ;
60
61
}
61
62
62
63
/** the error message if the premium is invalid */
63
64
get premiumError ( ) {
64
- if ( ! this . premium || ! this . amount ) return '' ;
65
+ if ( this . premium . eq ( 0 ) || this . amount . eq ( 0 ) ) return '' ;
65
66
if ( this . perBlockFixedRate < 1 ) {
66
67
return l ( 'premiumLowError' ) ;
67
68
}
@@ -74,7 +75,7 @@ export default class OrderFormView {
74
75
if ( this . minChanSize % ONE_UNIT !== 0 ) {
75
76
return l ( 'errorMultiple' ) ;
76
77
}
77
- if ( this . amount && this . minChanSize > this . amount ) {
78
+ if ( ! this . amount . eq ( 0 ) && this . amount . lt ( this . minChanSize ) ) {
78
79
return l ( 'errorLiquidity' ) ;
79
80
}
80
81
return '' ;
@@ -145,7 +146,7 @@ export default class OrderFormView {
145
146
146
147
/** the per block fixed rate */
147
148
get perBlockFixedRate ( ) {
148
- if ( [ this . amount , this . premium ] . includes ( 0 ) ) return 0 ;
149
+ if ( this . amount . eq ( 0 ) || this . premium . eq ( 0 ) ) return 0 ;
149
150
150
151
return this . _store . api . pool . calcFixedRate (
151
152
this . amount ,
@@ -156,13 +157,13 @@ export default class OrderFormView {
156
157
157
158
/** the premium interest of the amount in basis points */
158
159
get interestBps ( ) {
159
- if ( [ this . amount , this . premium ] . includes ( 0 ) ) return 0 ;
160
- return toBasisPoints ( this . premium / this . amount ) ;
160
+ if ( this . amount . eq ( 0 ) || this . premium . eq ( 0 ) ) return 0 ;
161
+ return toBasisPoints ( this . premium . div ( this . amount ) . toNumber ( ) ) ;
161
162
}
162
163
163
164
/** the APR given the amount and premium */
164
165
get apr ( ) {
165
- if ( [ this . amount , this . premium ] . includes ( 0 ) ) return 0 ;
166
+ if ( this . amount . eq ( 0 ) || this . premium . eq ( 0 ) ) return 0 ;
166
167
const termInDays = this . derivedDuration / BLOCKS_PER_DAY ;
167
168
const apr = annualPercentRate ( this . amount , this . premium , termInDays ) ;
168
169
return toPercent ( apr ) ;
@@ -177,7 +178,9 @@ export default class OrderFormView {
177
178
/** determines if the current values are all valid */
178
179
get isValid ( ) {
179
180
return (
180
- ! [ this . amount , this . premium , this . minChanSize , this . maxBatchFeeRate ] . includes ( 0 ) &&
181
+ ! this . amount . eq ( 0 ) &&
182
+ ! this . premium . eq ( 0 ) &&
183
+ ! [ this . minChanSize , this . maxBatchFeeRate ] . includes ( 0 ) &&
181
184
! this . amountError &&
182
185
! this . minChanSizeError &&
183
186
! this . feeRateError
@@ -189,12 +192,12 @@ export default class OrderFormView {
189
192
}
190
193
191
194
setAmount ( amount : number ) {
192
- this . amount = amount ;
195
+ this . amount = Big ( amount ) ;
193
196
this . fetchQuote ( ) ;
194
197
}
195
198
196
199
setPremium ( premium : number ) {
197
- this . premium = premium ;
200
+ this . premium = Big ( premium ) ;
198
201
this . fetchQuote ( ) ;
199
202
}
200
203
@@ -219,18 +222,18 @@ export default class OrderFormView {
219
222
220
223
setSuggestedPremium ( ) {
221
224
try {
222
- if ( ! this . amount ) throw new Error ( 'Must specify amount first' ) ;
225
+ if ( this . amount . eq ( 0 ) ) throw new Error ( 'Must specify amount first' ) ;
223
226
const prevBatch = this . _store . batchStore . sortedBatches [ 0 ] ;
224
227
if ( ! prevBatch ) throw new Error ( 'Previous batch not found' ) ;
225
228
const prevFixedRate = prevBatch . clearingPriceRate ;
226
229
// get the percentage rate of the previous batch and apply to the current amount
227
230
const prevPctRate = this . _store . api . pool . calcPctRate (
228
- prevFixedRate ,
229
- this . derivedDuration ,
231
+ Big ( prevFixedRate ) ,
232
+ Big ( this . derivedDuration ) ,
230
233
) ;
231
- const suggested = this . amount * prevPctRate ;
234
+ const suggested = this . amount . mul ( prevPctRate ) ;
232
235
// round to the nearest 10 to offset lose of precision in calculating percentages
233
- this . premium = Math . round ( suggested / 10 ) * 10 ;
236
+ this . premium = suggested . div ( 10 ) . round ( ) . mul ( 10 ) ;
234
237
this . fetchQuote ( ) ;
235
238
} catch ( error ) {
236
239
this . _store . appView . handleError ( error , 'Unable to suggest premium' ) ;
@@ -243,7 +246,7 @@ export default class OrderFormView {
243
246
244
247
/** requests a quote for an order to obtain accurate fees */
245
248
async quoteOrder ( ) {
246
- const minUnitsMatch = Math . floor ( this . minChanSize / ONE_UNIT ) ;
249
+ const minUnitsMatch = + Big ( this . minChanSize ) . div ( ONE_UNIT ) . round ( 0 , Big . roundDown ) ;
247
250
const satsPerKWeight = this . _store . api . pool . satsPerVByteToKWeight (
248
251
this . maxBatchFeeRate ,
249
252
) ;
@@ -260,8 +263,8 @@ export default class OrderFormView {
260
263
) ;
261
264
262
265
runInAction ( ( ) => {
263
- this . executionFee = totalExecutionFeeSat ;
264
- this . worstChainFee = worstCaseChainFeeSat ;
266
+ this . executionFee = Big ( totalExecutionFeeSat ) ;
267
+ this . worstChainFee = Big ( worstCaseChainFeeSat ) ;
265
268
this . quoteLoading = false ;
266
269
} ) ;
267
270
}
@@ -276,8 +279,8 @@ export default class OrderFormView {
276
279
fetchQuote ( ) {
277
280
if ( ! this . isValid ) {
278
281
runInAction ( ( ) => {
279
- this . executionFee = 0 ;
280
- this . worstChainFee = 0 ;
282
+ this . executionFee = Big ( 0 ) ;
283
+ this . worstChainFee = Big ( 0 ) ;
281
284
this . quoteLoading = false ;
282
285
} ) ;
283
286
return ;
@@ -303,11 +306,11 @@ export default class OrderFormView {
303
306
) ;
304
307
runInAction ( ( ) => {
305
308
if ( nonce ) {
306
- this . amount = 0 ;
307
- this . premium = 0 ;
309
+ this . amount = Big ( 0 ) ;
310
+ this . premium = Big ( 0 ) ;
308
311
this . duration = 0 ;
309
- this . executionFee = 0 ;
310
- this . worstChainFee = 0 ;
312
+ this . executionFee = Big ( 0 ) ;
313
+ this . worstChainFee = Big ( 0 ) ;
311
314
// persist the additional options so they can be used for future orders
312
315
this . _store . settingsStore . setOrderSettings (
313
316
this . minChanSize ,
0 commit comments