Skip to content

Commit 680b749

Browse files
committed
mobx: update types in the mobx views
1 parent b8900c9 commit 680b749

File tree

8 files changed

+47
-40
lines changed

8 files changed

+47
-40
lines changed

app/src/store/views/appView.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { makeAutoObservable, observable, toJS } from 'mobx';
22
import { SwapState } from 'types/generated/loop_pb';
33
import { Alert } from 'types/state';
4+
import Big from 'big.js';
45
import { AuthenticationError } from 'util/errors';
56
import { prefixTranslation } from 'util/translate';
67
import { Store } from 'store';
@@ -140,7 +141,7 @@ export default class AppView {
140141
// set the timestamp far in the future so it doesn't automatically disappear
141142
// from the Processing Loops list after 5 mins
142143
const tomorrow = Date.now() + 24 * 60 * 60 * 1000;
143-
this._store.swapStore.sortedSwaps[0].lastUpdateTime = tomorrow * 1000 * 1000;
144+
this._store.swapStore.sortedSwaps[0].lastUpdateTime = Big(tomorrow * 1000 * 1000);
144145
} else if (step === 22 /* swap-progress */) {
145146
// #22 is the swap-progress step
146147
// force the swap to be 100% complete

app/src/store/views/buildSwapView.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,10 @@ class BuildSwapView {
489489
const deadline =
490490
this._store.nodeStore.network === 'regtest' ? 0 : Date.now() + thirtyMins;
491491
// convert the selected channel ids to numbers
492-
const chanIds = this.selectedChanIds.map(v => parseInt(v));
493492
res = await this._store.api.loop.loopOut(
494493
amount,
495494
quote,
496-
chanIds,
495+
this.selectedChanIds,
497496
deadline,
498497
this.confTarget,
499498
this.loopOutAddress,

app/src/store/views/closeAccountView.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { makeAutoObservable } from 'mobx';
2+
import Big from 'big.js';
23
import { ellipseInside } from 'util/strings';
34
import { Store } from 'store';
45

app/src/store/views/fundAccountView.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { makeAutoObservable } from 'mobx';
2+
import Big from 'big.js';
23
import { prefixTranslation } from 'util/translate';
34
import { Store } from 'store';
45

app/src/store/views/fundNewAccountView.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export default class FundNewAccountView {
1313
private _store: Store;
1414

1515
// editable form fields
16-
amount = 0;
16+
amount = Big(0);
1717
confTarget = DEFAULT_CONF_TARGET;
1818
expireBlocks = DEFAULT_EXPIRE_BLOCKS;
1919
// response from quote
20-
minerFee = 0;
20+
minerFee = Big(0);
2121

2222
constructor(store: Store) {
2323
makeAutoObservable(this, {}, { deep: false, autoBind: true });
@@ -49,9 +49,9 @@ export default class FundNewAccountView {
4949

5050
/** the error message if the amount is invalid */
5151
get amountError() {
52-
if (!this.amount) return '';
52+
if (this.amount.eq(0)) return '';
5353
const accountMinimum = 100000;
54-
if (this.amount < accountMinimum) {
54+
if (this.amount.lt(accountMinimum)) {
5555
return l('amountTooLow', { accountMinimum });
5656
}
5757
if (this.walletBalance.lt(this.amount)) {
@@ -93,7 +93,7 @@ export default class FundNewAccountView {
9393
//
9494

9595
setAmount(amount: number) {
96-
this.amount = amount;
96+
this.amount = Big(amount);
9797
}
9898

9999
setConfTarget(confTarget: number) {
@@ -110,7 +110,7 @@ export default class FundNewAccountView {
110110

111111
/** shows the summary view */
112112
cancel() {
113-
this.amount = 0;
113+
this.amount = Big(0);
114114
this.confTarget = DEFAULT_CONF_TARGET;
115115
this.expireBlocks = DEFAULT_EXPIRE_BLOCKS;
116116
this._store.accountSectionView.showSummary();
@@ -126,7 +126,7 @@ export default class FundNewAccountView {
126126
);
127127

128128
runInAction(() => {
129-
this.minerFee = minerFeeTotal;
129+
this.minerFee = Big(minerFeeTotal);
130130
});
131131

132132
this._store.accountSectionView.showFundNewConfirm();

app/src/store/views/leaseView.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { makeAutoObservable } from 'mobx';
22
import { SortParams } from 'types/state';
3+
import Big from 'big.js';
34
import { annualPercentRate, toPercent } from 'util/bigmath';
45
import { BLOCKS_PER_DAY } from 'util/constants';
56
import { formatSats } from 'util/formatters';
@@ -40,7 +41,7 @@ export default class LeaseView {
4041
get apr() {
4142
const { channelAmtSat, premiumSat, channelDurationBlocks } = this.lease;
4243
const termInDays = channelDurationBlocks / BLOCKS_PER_DAY;
43-
return annualPercentRate(+channelAmtSat, +premiumSat, termInDays);
44+
return annualPercentRate(channelAmtSat, premiumSat, termInDays);
4445
}
4546

4647
/** the annual percentage rate of this lease as a percentage */

app/src/store/views/orderFormView.ts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
NodeTier,
55
} from 'types/generated/auctioneerrpc/auctioneer_pb';
66
import { LeaseDuration } from 'types/state';
7+
import Big from 'big.js';
78
import debounce from 'lodash/debounce';
89
import { annualPercentRate, toBasisPoints, toPercent } from 'util/bigmath';
910
import { BLOCKS_PER_DAY } from 'util/constants';
@@ -22,8 +23,8 @@ export default class OrderFormView {
2223

2324
/** the currently selected type of the order */
2425
orderType: OrderType = OrderType.Bid;
25-
amount = 0;
26-
premium = 0;
26+
amount = Big(0);
27+
premium = Big(0);
2728
duration = 0;
2829
minChanSize = DEFAULT_MIN_CHAN_SIZE;
2930
maxBatchFeeRate = DEFAULT_MAX_BATCH_FEE;
@@ -33,8 +34,8 @@ export default class OrderFormView {
3334
addlOptionsVisible = false;
3435

3536
/** quoted fees */
36-
executionFee = 0;
37-
worstChainFee = 0;
37+
executionFee = Big(0);
38+
worstChainFee = Big(0);
3839
quoteLoading = false;
3940

4041
constructor(store: Store) {
@@ -52,16 +53,16 @@ export default class OrderFormView {
5253

5354
/** the error message if the amount is invalid */
5455
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)) {
5758
return l('errorMultiple');
5859
}
5960
return '';
6061
}
6162

6263
/** the error message if the premium is invalid */
6364
get premiumError() {
64-
if (!this.premium || !this.amount) return '';
65+
if (this.premium.eq(0) || this.amount.eq(0)) return '';
6566
if (this.perBlockFixedRate < 1) {
6667
return l('premiumLowError');
6768
}
@@ -74,7 +75,7 @@ export default class OrderFormView {
7475
if (this.minChanSize % ONE_UNIT !== 0) {
7576
return l('errorMultiple');
7677
}
77-
if (this.amount && this.minChanSize > this.amount) {
78+
if (!this.amount.eq(0) && this.amount.lt(this.minChanSize)) {
7879
return l('errorLiquidity');
7980
}
8081
return '';
@@ -145,7 +146,7 @@ export default class OrderFormView {
145146

146147
/** the per block fixed rate */
147148
get perBlockFixedRate() {
148-
if ([this.amount, this.premium].includes(0)) return 0;
149+
if (this.amount.eq(0) || this.premium.eq(0)) return 0;
149150

150151
return this._store.api.pool.calcFixedRate(
151152
this.amount,
@@ -156,13 +157,13 @@ export default class OrderFormView {
156157

157158
/** the premium interest of the amount in basis points */
158159
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());
161162
}
162163

163164
/** the APR given the amount and premium */
164165
get apr() {
165-
if ([this.amount, this.premium].includes(0)) return 0;
166+
if (this.amount.eq(0) || this.premium.eq(0)) return 0;
166167
const termInDays = this.derivedDuration / BLOCKS_PER_DAY;
167168
const apr = annualPercentRate(this.amount, this.premium, termInDays);
168169
return toPercent(apr);
@@ -177,7 +178,9 @@ export default class OrderFormView {
177178
/** determines if the current values are all valid */
178179
get isValid() {
179180
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) &&
181184
!this.amountError &&
182185
!this.minChanSizeError &&
183186
!this.feeRateError
@@ -189,12 +192,12 @@ export default class OrderFormView {
189192
}
190193

191194
setAmount(amount: number) {
192-
this.amount = amount;
195+
this.amount = Big(amount);
193196
this.fetchQuote();
194197
}
195198

196199
setPremium(premium: number) {
197-
this.premium = premium;
200+
this.premium = Big(premium);
198201
this.fetchQuote();
199202
}
200203

@@ -219,18 +222,18 @@ export default class OrderFormView {
219222

220223
setSuggestedPremium() {
221224
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');
223226
const prevBatch = this._store.batchStore.sortedBatches[0];
224227
if (!prevBatch) throw new Error('Previous batch not found');
225228
const prevFixedRate = prevBatch.clearingPriceRate;
226229
// get the percentage rate of the previous batch and apply to the current amount
227230
const prevPctRate = this._store.api.pool.calcPctRate(
228-
prevFixedRate,
229-
this.derivedDuration,
231+
Big(prevFixedRate),
232+
Big(this.derivedDuration),
230233
);
231-
const suggested = this.amount * prevPctRate;
234+
const suggested = this.amount.mul(prevPctRate);
232235
// 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);
234237
this.fetchQuote();
235238
} catch (error) {
236239
this._store.appView.handleError(error, 'Unable to suggest premium');
@@ -243,7 +246,7 @@ export default class OrderFormView {
243246

244247
/** requests a quote for an order to obtain accurate fees */
245248
async quoteOrder() {
246-
const minUnitsMatch = Math.floor(this.minChanSize / ONE_UNIT);
249+
const minUnitsMatch = +Big(this.minChanSize).div(ONE_UNIT).round(0, Big.roundDown);
247250
const satsPerKWeight = this._store.api.pool.satsPerVByteToKWeight(
248251
this.maxBatchFeeRate,
249252
);
@@ -260,8 +263,8 @@ export default class OrderFormView {
260263
);
261264

262265
runInAction(() => {
263-
this.executionFee = totalExecutionFeeSat;
264-
this.worstChainFee = worstCaseChainFeeSat;
266+
this.executionFee = Big(totalExecutionFeeSat);
267+
this.worstChainFee = Big(worstCaseChainFeeSat);
265268
this.quoteLoading = false;
266269
});
267270
}
@@ -276,8 +279,8 @@ export default class OrderFormView {
276279
fetchQuote() {
277280
if (!this.isValid) {
278281
runInAction(() => {
279-
this.executionFee = 0;
280-
this.worstChainFee = 0;
282+
this.executionFee = Big(0);
283+
this.worstChainFee = Big(0);
281284
this.quoteLoading = false;
282285
});
283286
return;
@@ -303,11 +306,11 @@ export default class OrderFormView {
303306
);
304307
runInAction(() => {
305308
if (nonce) {
306-
this.amount = 0;
307-
this.premium = 0;
309+
this.amount = Big(0);
310+
this.premium = Big(0);
308311
this.duration = 0;
309-
this.executionFee = 0;
310-
this.worstChainFee = 0;
312+
this.executionFee = Big(0);
313+
this.worstChainFee = Big(0);
311314
// persist the additional options so they can be used for future orders
312315
this._store.settingsStore.setOrderSettings(
313316
this.minChanSize,

app/src/store/views/renewAccountView.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { makeAutoObservable } from 'mobx';
2+
import Big from 'big.js';
23
import { Store } from 'store';
34

45
// default expiration to ~90 days

0 commit comments

Comments
 (0)