-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathUnitsStore.ts
190 lines (166 loc) · 5.98 KB
/
UnitsStore.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
186
187
188
189
190
import { action, observable } from 'mobx';
import SettingsStore from './SettingsStore';
import FiatStore from './FiatStore';
import FeeUtils from './../utils/FeeUtils';
type Units = 'sats' | 'BTC' | 'fiat';
// 100_000_000
export const SATS_PER_BTC = 100000000;
interface ValueDisplayProps {
amount: string;
unit: Units;
symbol?: string;
negative?: boolean;
plural?: boolean;
rtl?: boolean;
space?: boolean;
}
export default class UnitsStore {
@observable public units: Units = 'sats';
settingsStore: SettingsStore;
fiatStore: FiatStore;
constructor(settingsStore: SettingsStore, fiatStore: FiatStore) {
this.settingsStore = settingsStore;
this.fiatStore = fiatStore;
}
@action
public changeUnits = () => (this.units = this.getNextUnit());
public getNextUnit = () => {
const { settings } = this.settingsStore;
const { fiatEnabled } = settings;
if (!fiatEnabled) {
return this.units === 'sats' ? 'BTC' : 'sats';
} else {
switch (this.units) {
case 'sats':
return 'BTC';
case 'BTC':
return 'fiat';
default:
return 'sats';
}
}
};
@action
public resetUnits = () => {
this.units = 'sats';
};
@action getUnformattedAmount = (
value: string | number = 0,
fixedUnits?: string
): ValueDisplayProps => {
const { settings } = this.settingsStore;
const { fiat, display } = settings;
const showAllDecimalPlaces: boolean =
(display && display.showAllDecimalPlaces) || false;
const units = fixedUnits || this.units;
const sats = Number(value);
const negative = sats < 0;
const absValueSats = Math.abs(sats);
if (units === 'BTC') {
return {
amount: FeeUtils.toFixed(
absValueSats / SATS_PER_BTC,
showAllDecimalPlaces
),
unit: 'BTC',
negative,
space: false
};
} else if (units === 'sats') {
return {
amount: this.fiatStore.numberWithCommas(absValueSats),
unit: 'sats',
negative,
plural: !(Number(value) === 1 || Number(value) === -1)
};
} else {
const currency = fiat;
// TODO: is this the right place to catch this?
if (!currency) {
return {
amount: 'Disabled',
unit: 'fiat',
symbol: '$'
};
}
if (this.fiatStore.fiatRates) {
const fiatEntry = this.fiatStore.fiatRates.filter(
(entry: any) => entry.code === fiat
)[0];
if (!fiatEntry?.rate) {
return {
error: 'Rate for selected currency not available'
};
}
const rate = (fiatEntry && fiatEntry.rate) || 0;
const { symbol, space, rtl, separatorSwap } =
this.fiatStore.getSymbol();
const amount = (
FeeUtils.toFixed(absValueSats / SATS_PER_BTC) * rate
).toFixed(2);
return {
amount: separatorSwap
? this.fiatStore.numberWithDecimals(amount)
: this.fiatStore.numberWithCommas(amount),
unit: 'fiat',
symbol,
negative,
plural: false,
rtl,
space
};
} else {
return { error: 'Error fetching fiat rates' };
}
}
};
@action
public getAmount = (value: string | number = 0, fixedUnits?: string) => {
const { settings } = this.settingsStore;
const { fiat } = settings;
const units = fixedUnits || this.units;
const [wholeSats] = value.toString().split('.');
if (units === 'BTC') {
// handle negative values
const valueToProcess = (wholeSats && wholeSats.toString()) || '0';
if (valueToProcess.includes('-')) {
const processedValue = valueToProcess.split('-')[1];
return `-₿${FeeUtils.toFixed(
Number(processedValue) / SATS_PER_BTC
)}`;
}
return `₿${FeeUtils.toFixed(
Number(wholeSats || 0) / SATS_PER_BTC
)}`;
} else if (units === 'sats') {
const sats = `${this.fiatStore.numberWithCommas(value) || 0} ${
Number(value) === 1 || Number(value) === -1 ? 'sat' : 'sats'
}`;
return sats;
} else if (units === 'fiat' && fiat) {
if (this.fiatStore.fiatRates) {
const fiatEntry = this.fiatStore.fiatRates.filter(
(entry: any) => entry.code === fiat
)[0];
const { code } = fiatEntry;
const rate = (fiatEntry && fiatEntry.rate) || 0;
const { symbol, space, rtl, separatorSwap } =
this.fiatStore.symbolLookup(code);
const amount = (
FeeUtils.toFixed(Number(wholeSats || 0) / SATS_PER_BTC) *
rate
).toFixed(2);
const formattedAmount = separatorSwap
? this.fiatStore.numberWithDecimals(amount)
: this.fiatStore.numberWithCommas(amount);
if (rtl) {
return `${formattedAmount}${space ? ' ' : ''}${symbol}`;
} else {
return `${symbol}${space ? ' ' : ''}${formattedAmount}`;
}
} else {
return '$N/A';
}
}
};
}