@exodus/fiat-balances 6.2.2 → 6.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/atoms/index.js +8 -0
- package/atoms/non-dust-balance-asset-names-atom.js +5 -0
- package/index.js +44 -0
- package/module/fiat-balances.js +251 -0
- package/module/index.js +1 -249
- package/package.json +11 -9
- package/plugin/index.js +33 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [6.3.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@6.3.0...@exodus/fiat-balances@6.3.1) (2023-07-21)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @exodus/fiat-balances
|
|
9
|
+
|
|
10
|
+
## [6.3.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@6.2.2...@exodus/fiat-balances@6.3.0) (2023-07-10)
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
- export `fiatBalances` factory ([#2314](https://github.com/ExodusMovement/exodus-hydra/issues/2314)) ([da0dd61](https://github.com/ExodusMovement/exodus-hydra/commit/da0dd616d09188924d348f11437853bbfb438b54))
|
|
15
|
+
|
|
6
16
|
## [6.2.2](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/fiat-balances@6.2.1...@exodus/fiat-balances@6.2.2) (2023-06-21)
|
|
7
17
|
|
|
8
18
|
### Performance Improvements
|
package/atoms/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import createFiatBalancesAtom from './fiat-balances'
|
|
2
2
|
import createOptimisticFiatBalancesAtom from './optimistic-fiat-balances'
|
|
3
|
+
import createNonDustBalanceAssetNamesAtom from './non-dust-balance-asset-names-atom'
|
|
3
4
|
|
|
4
5
|
export const fiatBalancesAtomDefinition = {
|
|
5
6
|
id: 'fiatBalancesAtom',
|
|
@@ -14,3 +15,10 @@ export const optimisticFiatBalancesAtomDefinition = {
|
|
|
14
15
|
factory: createOptimisticFiatBalancesAtom,
|
|
15
16
|
dependencies: [],
|
|
16
17
|
}
|
|
18
|
+
|
|
19
|
+
export const nonDustBalanceAssetNamesAtomDefinition = {
|
|
20
|
+
id: 'nonDustBalanceAssetNamesAtom',
|
|
21
|
+
type: 'atom',
|
|
22
|
+
factory: createNonDustBalanceAssetNamesAtom,
|
|
23
|
+
dependencies: [],
|
|
24
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import fiatBalancesDefinition from './module'
|
|
2
|
+
import {
|
|
3
|
+
fiatBalancesAtomDefinition,
|
|
4
|
+
nonDustBalanceAssetNamesAtomDefinition,
|
|
5
|
+
optimisticFiatBalancesAtomDefinition,
|
|
6
|
+
} from './atoms'
|
|
7
|
+
import fiatBalancesPluginDefinition from './plugin'
|
|
8
|
+
|
|
9
|
+
const fiatBalances = ({ optimistic }) => ({
|
|
10
|
+
id: 'fiatBalances',
|
|
11
|
+
definitions: [
|
|
12
|
+
{
|
|
13
|
+
definition: fiatBalancesDefinition,
|
|
14
|
+
writesAtoms: ['fiatBalancesAtom'],
|
|
15
|
+
},
|
|
16
|
+
{ definition: fiatBalancesAtomDefinition },
|
|
17
|
+
{ definition: fiatBalancesPluginDefinition },
|
|
18
|
+
{ definition: nonDustBalanceAssetNamesAtomDefinition },
|
|
19
|
+
{
|
|
20
|
+
if: optimistic,
|
|
21
|
+
definition: optimisticFiatBalancesAtomDefinition,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
if: optimistic,
|
|
25
|
+
definition: {
|
|
26
|
+
...fiatBalancesDefinition,
|
|
27
|
+
id: 'optimisticFiatBalances',
|
|
28
|
+
},
|
|
29
|
+
aliases: [
|
|
30
|
+
{
|
|
31
|
+
implementationId: 'optimisticBalancesAtom',
|
|
32
|
+
interfaceId: 'balancesAtom',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
implementationId: 'optimisticFiatBalancesAtom',
|
|
36
|
+
interfaceId: 'fiatBalancesAtom',
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
writesAtoms: ['optimisticFiatBalancesAtom'],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
export default fiatBalances
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { conversionByRate } from '@exodus/currency'
|
|
2
|
+
import ExodusModule from '@exodus/module'
|
|
3
|
+
import { isEqual, set } from 'lodash'
|
|
4
|
+
import fiatCurrencies from '@exodus/fiat-currencies'
|
|
5
|
+
import { combine } from '@exodus/atoms'
|
|
6
|
+
|
|
7
|
+
// not super efficient, so we're assuming small objects, like { [walletAccount]: xyz }
|
|
8
|
+
const areKeysSame = (a, b) => isEqual(Object.keys(a).sort(), Object.keys(b).sort())
|
|
9
|
+
|
|
10
|
+
const MODULE_ID = 'fiatBalances'
|
|
11
|
+
|
|
12
|
+
class FiatBalances extends ExodusModule {
|
|
13
|
+
#assetsModule
|
|
14
|
+
#balanceFields
|
|
15
|
+
#fiatCurrency
|
|
16
|
+
#cachedRates
|
|
17
|
+
#latestAssetBalances = null
|
|
18
|
+
#balances = Object.create(null)
|
|
19
|
+
#fiatBalancesAtom
|
|
20
|
+
#inputAtom
|
|
21
|
+
#balancesAtom
|
|
22
|
+
// #balances.totals: Object,
|
|
23
|
+
// #balances.byWalletAccount: Object,
|
|
24
|
+
// #balances.byAssetSource: Object,
|
|
25
|
+
// #balances.byBaseAssetSource: Object,
|
|
26
|
+
|
|
27
|
+
constructor({
|
|
28
|
+
assetsModule,
|
|
29
|
+
logger,
|
|
30
|
+
balancesAtom,
|
|
31
|
+
config: { balanceFields },
|
|
32
|
+
fiatBalancesAtom,
|
|
33
|
+
ratesAtom,
|
|
34
|
+
currencyAtom,
|
|
35
|
+
}) {
|
|
36
|
+
super({ name: MODULE_ID, logger })
|
|
37
|
+
|
|
38
|
+
this.#assetsModule = assetsModule
|
|
39
|
+
this.#balanceFields = balanceFields
|
|
40
|
+
this.#fiatBalancesAtom = fiatBalancesAtom
|
|
41
|
+
this.#balancesAtom = balancesAtom
|
|
42
|
+
this.#inputAtom = combine({
|
|
43
|
+
rates: ratesAtom,
|
|
44
|
+
currency: currencyAtom,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
load = () => {
|
|
49
|
+
this.#inputAtom.observe(({ currency, rates }) => {
|
|
50
|
+
this.#fiatCurrency = currency
|
|
51
|
+
this.#cachedRates = rates
|
|
52
|
+
this.#maybeRecompute()
|
|
53
|
+
})
|
|
54
|
+
this.#balancesAtom.observe(this.#setBalances)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#isReady = () => {
|
|
58
|
+
return Boolean(
|
|
59
|
+
this.#cachedRates &&
|
|
60
|
+
this.#fiatCurrency &&
|
|
61
|
+
this.#latestAssetBalances &&
|
|
62
|
+
this.#cachedRates[this.#fiatCurrency]
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// seems cheap, worth memoizing?
|
|
67
|
+
#getCreateConversion = ({ assetName }) => {
|
|
68
|
+
const fiatCurrency = fiatCurrencies[this.#fiatCurrency]
|
|
69
|
+
const rates = this.#cachedRates[this.#fiatCurrency]
|
|
70
|
+
const asset = this.#assetsModule.getAsset(assetName)
|
|
71
|
+
let fiatRate
|
|
72
|
+
// HACK: Super hacky, terrible, the rates state object should have the prices split up by sub-objects per fiat
|
|
73
|
+
if (fiatCurrency.defaultUnit.unitName === 'USD') {
|
|
74
|
+
fiatRate = rates?.[asset.ticker]?.priceUSD || 0
|
|
75
|
+
} else {
|
|
76
|
+
fiatRate = rates?.[asset.ticker]?.price || 0
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return conversionByRate(asset.currency, fiatCurrency, fiatRate)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#convertToFiat = ({ assetName, value }) => {
|
|
83
|
+
return this.#getCreateConversion({ assetName })(value)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#maybeRecompute = () => {
|
|
87
|
+
if (this.#isReady()) this.#recompute()
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#recompute = async () => {
|
|
91
|
+
this.#reset()
|
|
92
|
+
|
|
93
|
+
const balances = this.#latestAssetBalances
|
|
94
|
+
const { byAssetSource, byBaseAssetSource, byWalletAccount } = this.#balances
|
|
95
|
+
const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
|
|
96
|
+
|
|
97
|
+
Object.keys(balances).forEach((walletAccount) => {
|
|
98
|
+
const walletAccountBalances = balances[walletAccount]
|
|
99
|
+
this.#balanceFields.forEach((field) => {
|
|
100
|
+
let fieldTotal = fiatZero
|
|
101
|
+
const entries = Object.entries(walletAccountBalances).filter(([balances]) => balances)
|
|
102
|
+
|
|
103
|
+
entries.forEach(([assetName, assetBalances]) => {
|
|
104
|
+
const balance = assetBalances?.[field]
|
|
105
|
+
if (!balance) return
|
|
106
|
+
|
|
107
|
+
if (!byAssetSource?.[walletAccount]?.[assetName]?.[field]) {
|
|
108
|
+
set(byAssetSource, [walletAccount, assetName, field], fiatZero)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
|
|
112
|
+
if (!byBaseAssetSource?.[walletAccount]?.[baseAssetName]?.[field]) {
|
|
113
|
+
set(byBaseAssetSource, [walletAccount, baseAssetName, field], fiatZero)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const fiatValue = this.#convertToFiat({ assetName, value: balance })
|
|
117
|
+
// eslint-disable-next-line standard/computed-property-even-spacing
|
|
118
|
+
byAssetSource[walletAccount][assetName][field] =
|
|
119
|
+
byAssetSource[walletAccount][assetName][field].add(fiatValue)
|
|
120
|
+
|
|
121
|
+
// eslint-disable-next-line standard/computed-property-even-spacing
|
|
122
|
+
byBaseAssetSource[walletAccount][baseAssetName][field] =
|
|
123
|
+
byBaseAssetSource[walletAccount][baseAssetName][field].add(fiatValue)
|
|
124
|
+
|
|
125
|
+
fieldTotal = fieldTotal.add(fiatValue)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
set(byWalletAccount, [walletAccount, field], fieldTotal)
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
this.#recomputeWalletTotals()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#setBalances = ({ balances, oldBalances, changes }) => {
|
|
136
|
+
const needsRecompute =
|
|
137
|
+
!changes ||
|
|
138
|
+
this.#latestAssetBalances !== oldBalances ||
|
|
139
|
+
// adding/removing walletAccounts is fairly rare, recompute to reduce code complexity
|
|
140
|
+
!oldBalances ||
|
|
141
|
+
!areKeysSame(oldBalances, balances)
|
|
142
|
+
|
|
143
|
+
this.#latestAssetBalances = balances
|
|
144
|
+
if (!this.#isReady()) return
|
|
145
|
+
|
|
146
|
+
if (needsRecompute) return this.#recompute()
|
|
147
|
+
|
|
148
|
+
this._logger.log('recomputing based on new balances')
|
|
149
|
+
|
|
150
|
+
const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
|
|
151
|
+
|
|
152
|
+
// TODO set changes to fiatBalancesAtom as well
|
|
153
|
+
const { byWalletAccount, byAssetSource, byBaseAssetSource } = this.#balances
|
|
154
|
+
Object.entries(changes).forEach(([walletAccount, walletAccountBalancesChanges]) => {
|
|
155
|
+
const walletAccountBalances = byWalletAccount[walletAccount]
|
|
156
|
+
Object.entries(walletAccountBalancesChanges).forEach(
|
|
157
|
+
([assetName, assetSourceBalancesChanges]) => {
|
|
158
|
+
const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
|
|
159
|
+
this.#balanceFields.forEach((field) => {
|
|
160
|
+
// we may not be interested in all fields from `balances`
|
|
161
|
+
if (!assetSourceBalancesChanges[field]) return
|
|
162
|
+
|
|
163
|
+
const { from, to } = assetSourceBalancesChanges[field]
|
|
164
|
+
walletAccountBalances[field] = walletAccountBalances[field] || fiatZero
|
|
165
|
+
|
|
166
|
+
const fromFiatValue = this.#convertToFiat({ assetName, value: from })
|
|
167
|
+
const toFiatValue = this.#convertToFiat({ assetName, value: to })
|
|
168
|
+
walletAccountBalances[field] = walletAccountBalances[field]
|
|
169
|
+
.sub(fromFiatValue)
|
|
170
|
+
.add(toFiatValue)
|
|
171
|
+
|
|
172
|
+
//
|
|
173
|
+
;[
|
|
174
|
+
{
|
|
175
|
+
byAssetSource,
|
|
176
|
+
assetName,
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
byAssetSource: byBaseAssetSource,
|
|
180
|
+
assetName: baseAssetName,
|
|
181
|
+
},
|
|
182
|
+
].forEach(({ byAssetSource, assetName }) => {
|
|
183
|
+
if (!byAssetSource[walletAccount]?.[assetName]?.[field]) {
|
|
184
|
+
set(byAssetSource, [walletAccount, assetName, field], fiatZero)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
|
|
188
|
+
byAssetSource[walletAccount][assetName][field] = byAssetSource[walletAccount][
|
|
189
|
+
assetName
|
|
190
|
+
][field]
|
|
191
|
+
.sub(fromFiatValue)
|
|
192
|
+
.add(toFiatValue)
|
|
193
|
+
})
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
)
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
this.#recomputeWalletTotals()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#recomputeWalletTotals = () => {
|
|
203
|
+
this.#balanceFields.forEach((field) => {
|
|
204
|
+
this.#balances.totals[field] = Object.entries(this.#balances.byWalletAccount).reduce(
|
|
205
|
+
(total, [walletAccount, walletAccountBalances]) =>
|
|
206
|
+
walletAccountBalances[field] ? total.add(walletAccountBalances[field]) : total,
|
|
207
|
+
fiatCurrencies[this.#fiatCurrency].ZERO
|
|
208
|
+
)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
this.#flush()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#reset = () => {
|
|
215
|
+
const totals = Object.create(null)
|
|
216
|
+
const byWalletAccount = Object.create(null)
|
|
217
|
+
const byAssetSource = Object.create(null)
|
|
218
|
+
const byBaseAssetSource = Object.create(null)
|
|
219
|
+
this.#balances = Object.assign(Object.create(null), {
|
|
220
|
+
totals,
|
|
221
|
+
byWalletAccount,
|
|
222
|
+
byAssetSource,
|
|
223
|
+
byBaseAssetSource,
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
#flush = () => {
|
|
228
|
+
this._logger.log('fiat balances updated')
|
|
229
|
+
const data = { balances: this.#balances }
|
|
230
|
+
this.#fiatBalancesAtom.set((current) => (isEqual(current, data) ? current : data))
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const createFiatBalances = (...args) => new FiatBalances(...args)
|
|
235
|
+
|
|
236
|
+
const fiatBalancesDefinition = {
|
|
237
|
+
id: MODULE_ID,
|
|
238
|
+
type: 'module',
|
|
239
|
+
factory: createFiatBalances,
|
|
240
|
+
dependencies: [
|
|
241
|
+
'assetsModule',
|
|
242
|
+
'logger',
|
|
243
|
+
'balancesAtom',
|
|
244
|
+
'fiatBalancesAtom',
|
|
245
|
+
'config',
|
|
246
|
+
'ratesAtom',
|
|
247
|
+
'currencyAtom',
|
|
248
|
+
],
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export default fiatBalancesDefinition
|
package/module/index.js
CHANGED
|
@@ -1,249 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import ExodusModule from '@exodus/module'
|
|
3
|
-
import { isEqual, set } from 'lodash'
|
|
4
|
-
import fiatCurrencies from '@exodus/fiat-currencies'
|
|
5
|
-
import { combine } from '@exodus/atoms'
|
|
6
|
-
|
|
7
|
-
// not super efficient, so we're assuming small objects, like { [walletAccount]: xyz }
|
|
8
|
-
const areKeysSame = (a, b) => isEqual(Object.keys(a).sort(), Object.keys(b).sort())
|
|
9
|
-
|
|
10
|
-
const MODULE_ID = 'fiatBalances'
|
|
11
|
-
|
|
12
|
-
class FiatBalances extends ExodusModule {
|
|
13
|
-
#assetsModule
|
|
14
|
-
#balanceFields
|
|
15
|
-
#fiatCurrency
|
|
16
|
-
#cachedRates
|
|
17
|
-
#latestAssetBalances = null
|
|
18
|
-
#balances = Object.create(null)
|
|
19
|
-
#fiatBalancesAtom
|
|
20
|
-
#inputAtom
|
|
21
|
-
#balancesAtom
|
|
22
|
-
// #balances.totals: Object,
|
|
23
|
-
// #balances.byWalletAccount: Object,
|
|
24
|
-
// #balances.byAssetSource: Object,
|
|
25
|
-
// #balances.byBaseAssetSource: Object,
|
|
26
|
-
|
|
27
|
-
constructor({
|
|
28
|
-
assetsModule,
|
|
29
|
-
logger,
|
|
30
|
-
balancesAtom,
|
|
31
|
-
config: { balanceFields },
|
|
32
|
-
fiatBalancesAtom,
|
|
33
|
-
ratesAtom,
|
|
34
|
-
currencyAtom,
|
|
35
|
-
}) {
|
|
36
|
-
super({ name: MODULE_ID, logger })
|
|
37
|
-
|
|
38
|
-
this.#assetsModule = assetsModule
|
|
39
|
-
this.#balanceFields = balanceFields
|
|
40
|
-
this.#fiatBalancesAtom = fiatBalancesAtom
|
|
41
|
-
this.#balancesAtom = balancesAtom
|
|
42
|
-
this.#inputAtom = combine({
|
|
43
|
-
rates: ratesAtom,
|
|
44
|
-
currency: currencyAtom,
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
load = () => {
|
|
49
|
-
this.#inputAtom.observe(({ currency, rates }) => {
|
|
50
|
-
this.#fiatCurrency = currency
|
|
51
|
-
this.#cachedRates = rates
|
|
52
|
-
this.#maybeRecompute()
|
|
53
|
-
})
|
|
54
|
-
this.#balancesAtom.observe(this.#setBalances)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
#isReady = () => {
|
|
58
|
-
return Boolean(
|
|
59
|
-
this.#cachedRates &&
|
|
60
|
-
this.#fiatCurrency &&
|
|
61
|
-
this.#latestAssetBalances &&
|
|
62
|
-
this.#cachedRates[this.#fiatCurrency]
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// seems cheap, worth memoizing?
|
|
67
|
-
#getCreateConversion = ({ assetName }) => {
|
|
68
|
-
const fiatCurrency = fiatCurrencies[this.#fiatCurrency]
|
|
69
|
-
const rates = this.#cachedRates[this.#fiatCurrency]
|
|
70
|
-
const asset = this.#assetsModule.getAsset(assetName)
|
|
71
|
-
let fiatRate
|
|
72
|
-
// HACK: Super hacky, terrible, the rates state object should have the prices split up by sub-objects per fiat
|
|
73
|
-
if (fiatCurrency.defaultUnit.unitName === 'USD') {
|
|
74
|
-
fiatRate = rates?.[asset.ticker]?.priceUSD || 0
|
|
75
|
-
} else {
|
|
76
|
-
fiatRate = rates?.[asset.ticker]?.price || 0
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return conversionByRate(asset.currency, fiatCurrency, fiatRate)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
#convertToFiat = ({ assetName, value }) => {
|
|
83
|
-
return this.#getCreateConversion({ assetName })(value)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
#maybeRecompute = () => {
|
|
87
|
-
if (this.#isReady()) this.#recompute()
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
#recompute = async () => {
|
|
91
|
-
this.#reset()
|
|
92
|
-
|
|
93
|
-
const balances = this.#latestAssetBalances
|
|
94
|
-
const { byAssetSource, byBaseAssetSource, byWalletAccount } = this.#balances
|
|
95
|
-
const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
|
|
96
|
-
|
|
97
|
-
Object.keys(balances).forEach((walletAccount) => {
|
|
98
|
-
const walletAccountBalances = balances[walletAccount]
|
|
99
|
-
this.#balanceFields.forEach((field) => {
|
|
100
|
-
let fieldTotal = fiatZero
|
|
101
|
-
const entries = Object.entries(walletAccountBalances).filter(([balances]) => balances)
|
|
102
|
-
|
|
103
|
-
entries.forEach(([assetName, assetBalances]) => {
|
|
104
|
-
const balance = assetBalances?.[field]
|
|
105
|
-
if (!balance) return
|
|
106
|
-
|
|
107
|
-
if (!byAssetSource?.[walletAccount]?.[assetName]?.[field]) {
|
|
108
|
-
set(byAssetSource, [walletAccount, assetName, field], fiatZero)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
|
|
112
|
-
if (!byBaseAssetSource?.[walletAccount]?.[baseAssetName]?.[field]) {
|
|
113
|
-
set(byBaseAssetSource, [walletAccount, baseAssetName, field], fiatZero)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const fiatValue = this.#convertToFiat({ assetName, value: balance })
|
|
117
|
-
// eslint-disable-next-line standard/computed-property-even-spacing
|
|
118
|
-
byAssetSource[walletAccount][assetName][field] =
|
|
119
|
-
byAssetSource[walletAccount][assetName][field].add(fiatValue)
|
|
120
|
-
|
|
121
|
-
// eslint-disable-next-line standard/computed-property-even-spacing
|
|
122
|
-
byBaseAssetSource[walletAccount][baseAssetName][field] =
|
|
123
|
-
byBaseAssetSource[walletAccount][baseAssetName][field].add(fiatValue)
|
|
124
|
-
|
|
125
|
-
fieldTotal = fieldTotal.add(fiatValue)
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
set(byWalletAccount, [walletAccount, field], fieldTotal)
|
|
129
|
-
})
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
this.#recomputeWalletTotals()
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
#setBalances = ({ balances, oldBalances, changes }) => {
|
|
136
|
-
const needsRecompute =
|
|
137
|
-
!changes ||
|
|
138
|
-
this.#latestAssetBalances !== oldBalances ||
|
|
139
|
-
// adding/removing walletAccounts is fairly rare, recompute to reduce code complexity
|
|
140
|
-
!oldBalances ||
|
|
141
|
-
!areKeysSame(oldBalances, balances)
|
|
142
|
-
|
|
143
|
-
this.#latestAssetBalances = balances
|
|
144
|
-
if (!this.#isReady()) return
|
|
145
|
-
|
|
146
|
-
if (needsRecompute) return this.#recompute()
|
|
147
|
-
|
|
148
|
-
this._logger.log('recomputing based on new balances')
|
|
149
|
-
|
|
150
|
-
const fiatZero = fiatCurrencies[this.#fiatCurrency].ZERO
|
|
151
|
-
|
|
152
|
-
// TODO set changes to fiatBalancesAtom as well
|
|
153
|
-
const { byWalletAccount, byAssetSource, byBaseAssetSource } = this.#balances
|
|
154
|
-
Object.entries(changes).forEach(([walletAccount, walletAccountBalancesChanges]) => {
|
|
155
|
-
const walletAccountBalances = byWalletAccount[walletAccount]
|
|
156
|
-
Object.entries(walletAccountBalancesChanges).forEach(
|
|
157
|
-
([assetName, assetSourceBalancesChanges]) => {
|
|
158
|
-
const baseAssetName = this.#assetsModule.getAsset(assetName).baseAsset.name
|
|
159
|
-
this.#balanceFields.forEach((field) => {
|
|
160
|
-
// we may not be interested in all fields from `balances`
|
|
161
|
-
if (!assetSourceBalancesChanges[field]) return
|
|
162
|
-
|
|
163
|
-
const { from, to } = assetSourceBalancesChanges[field]
|
|
164
|
-
walletAccountBalances[field] = walletAccountBalances[field] || fiatZero
|
|
165
|
-
|
|
166
|
-
const fromFiatValue = this.#convertToFiat({ assetName, value: from })
|
|
167
|
-
const toFiatValue = this.#convertToFiat({ assetName, value: to })
|
|
168
|
-
walletAccountBalances[field] = walletAccountBalances[field]
|
|
169
|
-
.sub(fromFiatValue)
|
|
170
|
-
.add(toFiatValue)
|
|
171
|
-
|
|
172
|
-
//
|
|
173
|
-
;[
|
|
174
|
-
{
|
|
175
|
-
byAssetSource,
|
|
176
|
-
assetName,
|
|
177
|
-
},
|
|
178
|
-
{
|
|
179
|
-
byAssetSource: byBaseAssetSource,
|
|
180
|
-
assetName: baseAssetName,
|
|
181
|
-
},
|
|
182
|
-
].forEach(({ byAssetSource, assetName }) => {
|
|
183
|
-
if (!byAssetSource[walletAccount]?.[assetName]?.[field]) {
|
|
184
|
-
set(byAssetSource, [walletAccount, assetName, field], fiatZero)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
|
|
188
|
-
byAssetSource[walletAccount][assetName][field] = byAssetSource[walletAccount][
|
|
189
|
-
assetName
|
|
190
|
-
][field]
|
|
191
|
-
.sub(fromFiatValue)
|
|
192
|
-
.add(toFiatValue)
|
|
193
|
-
})
|
|
194
|
-
})
|
|
195
|
-
}
|
|
196
|
-
)
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
this.#recomputeWalletTotals()
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
#recomputeWalletTotals = () => {
|
|
203
|
-
this.#balanceFields.forEach((field) => {
|
|
204
|
-
this.#balances.totals[field] = Object.entries(this.#balances.byWalletAccount).reduce(
|
|
205
|
-
(total, [walletAccount, walletAccountBalances]) =>
|
|
206
|
-
walletAccountBalances[field] ? total.add(walletAccountBalances[field]) : total,
|
|
207
|
-
fiatCurrencies[this.#fiatCurrency].ZERO
|
|
208
|
-
)
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
this.#flush()
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
#reset = () => {
|
|
215
|
-
const totals = Object.create(null)
|
|
216
|
-
const byWalletAccount = Object.create(null)
|
|
217
|
-
const byAssetSource = Object.create(null)
|
|
218
|
-
const byBaseAssetSource = Object.create(null)
|
|
219
|
-
this.#balances = Object.assign(Object.create(null), {
|
|
220
|
-
totals,
|
|
221
|
-
byWalletAccount,
|
|
222
|
-
byAssetSource,
|
|
223
|
-
byBaseAssetSource,
|
|
224
|
-
})
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
#flush = () => {
|
|
228
|
-
this._logger.log('fiat balances updated')
|
|
229
|
-
const data = { balances: this.#balances }
|
|
230
|
-
this.#fiatBalancesAtom.set((current) => (isEqual(current, data) ? current : data))
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const createFiatBalances = (...args) => new FiatBalances(...args)
|
|
235
|
-
|
|
236
|
-
export default {
|
|
237
|
-
id: MODULE_ID,
|
|
238
|
-
type: 'module',
|
|
239
|
-
factory: createFiatBalances,
|
|
240
|
-
dependencies: [
|
|
241
|
-
'assetsModule',
|
|
242
|
-
'logger',
|
|
243
|
-
'balancesAtom',
|
|
244
|
-
'fiatBalancesAtom',
|
|
245
|
-
'config',
|
|
246
|
-
'ratesAtom',
|
|
247
|
-
'currencyAtom',
|
|
248
|
-
],
|
|
249
|
-
}
|
|
1
|
+
export default from './fiat-balances'
|
package/package.json
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/fiat-balances",
|
|
3
|
-
"version": "6.
|
|
4
|
-
"description": "
|
|
5
|
-
"author": "Exodus Movement Inc",
|
|
3
|
+
"version": "6.3.1",
|
|
4
|
+
"description": "Track asset and portfolio fiat balances based on changes in crypto balances and the user's preferred fiat currency",
|
|
5
|
+
"author": "Exodus Movement Inc.",
|
|
6
6
|
"license": "UNLICENSED",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "jest",
|
|
9
9
|
"lint": "eslint . --ignore-path ../../.gitignore",
|
|
10
10
|
"lint:fix": "yarn lint --fix"
|
|
11
11
|
},
|
|
12
|
+
"main": "index.js",
|
|
12
13
|
"files": [
|
|
13
14
|
"module",
|
|
14
15
|
"atoms",
|
|
16
|
+
"plugin",
|
|
15
17
|
"README.md",
|
|
16
18
|
"CHANGELOG.md",
|
|
17
|
-
"!**/__tests__"
|
|
19
|
+
"!**/__tests__/**"
|
|
18
20
|
],
|
|
19
21
|
"repository": {
|
|
20
22
|
"type": "git",
|
|
21
23
|
"url": "git+https://github.com/ExodusMovement/exodus-hydra.git"
|
|
22
24
|
},
|
|
23
|
-
"homepage": "https://github.com/ExodusMovement/exodus-hydra/tree/master/
|
|
25
|
+
"homepage": "https://github.com/ExodusMovement/exodus-hydra/tree/master/features/fiat-balances",
|
|
24
26
|
"bugs": {
|
|
25
27
|
"url": "https://github.com/ExodusMovement/exodus-hydra/issues?q=is%3Aissue+is%3Aopen+label%3Afiat-balances"
|
|
26
28
|
},
|
|
@@ -31,17 +33,17 @@
|
|
|
31
33
|
"lodash": "^4.17.21"
|
|
32
34
|
},
|
|
33
35
|
"devDependencies": {
|
|
34
|
-
"@exodus/atoms": "^5.
|
|
35
|
-
"@exodus/balances": "^
|
|
36
|
+
"@exodus/atoms": "^5.2.3",
|
|
37
|
+
"@exodus/balances": "^7.0.0",
|
|
36
38
|
"@exodus/basic-utils": "^2.0.0",
|
|
37
39
|
"@exodus/blockchain-metadata": "*",
|
|
38
40
|
"@exodus/enabled-assets": "^6.0.2",
|
|
39
41
|
"@exodus/models": "^8.9.0",
|
|
40
42
|
"@exodus/storage-memory": "^2.1.0",
|
|
41
43
|
"@exodus/wallet-accounts": "*",
|
|
42
|
-
"eslint": "^8.
|
|
44
|
+
"eslint": "^8.44.0",
|
|
43
45
|
"events": "^3.3.0",
|
|
44
46
|
"jest": "^29.1.2"
|
|
45
47
|
},
|
|
46
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "2408d9e51f2d1f145df0adb8ddeb7bf5970bba39"
|
|
47
49
|
}
|
package/plugin/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const createFiatBalancesPlugin = ({
|
|
2
|
+
port,
|
|
3
|
+
fiatBalancesAtom,
|
|
4
|
+
optimisticFiatBalancesAtom,
|
|
5
|
+
fiatBalances,
|
|
6
|
+
optimisticFiatBalances,
|
|
7
|
+
}) => ({
|
|
8
|
+
onLoad() {
|
|
9
|
+
fiatBalances.load()
|
|
10
|
+
optimisticFiatBalances?.load()
|
|
11
|
+
},
|
|
12
|
+
onUnlock() {
|
|
13
|
+
fiatBalancesAtom.get().then((value) => port.emit('fiatBalancesAtom', value))
|
|
14
|
+
optimisticFiatBalancesAtom
|
|
15
|
+
?.get()
|
|
16
|
+
.then((value) => port.emit('optimisticFiatBalancesAtom', value))
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const fiatBalancesPluginDefinition = {
|
|
21
|
+
id: 'fiatBalancesPlugin',
|
|
22
|
+
type: 'plugin',
|
|
23
|
+
factory: createFiatBalancesPlugin,
|
|
24
|
+
dependencies: [
|
|
25
|
+
'fiatBalances',
|
|
26
|
+
'port',
|
|
27
|
+
'fiatBalancesAtom',
|
|
28
|
+
'optimisticFiatBalances?',
|
|
29
|
+
'optimisticFiatBalancesAtom?',
|
|
30
|
+
],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default fiatBalancesPluginDefinition
|