@sovryn-zero/lib-base 0.1.0 → 0.2.0
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/dist/index.d.ts +13 -13
- package/dist/index.js +25 -25
- package/dist/src/Decimal.d.ts +88 -88
- package/dist/src/Decimal.js +360 -360
- package/dist/src/Fees.d.ts +81 -81
- package/dist/src/Fees.js +122 -122
- package/dist/src/LiquityStore.d.ts +208 -208
- package/dist/src/LiquityStore.js +208 -208
- package/dist/src/ObservableLiquity.d.ts +14 -14
- package/dist/src/ObservableLiquity.js +2 -2
- package/dist/src/PopulatableLiquity.d.ts +124 -124
- package/dist/src/PopulatableLiquity.js +2 -2
- package/dist/src/ReadableLiquity.d.ts +155 -155
- package/dist/src/ReadableLiquity.js +2 -2
- package/dist/src/SendableLiquity.d.ts +155 -155
- package/dist/src/SendableLiquity.js +19 -19
- package/dist/src/StabilityDeposit.d.ts +58 -58
- package/dist/src/StabilityDeposit.js +79 -79
- package/dist/src/TransactableLiquity.d.ts +413 -413
- package/dist/src/TransactableLiquity.js +17 -17
- package/dist/src/Trove.d.ts +366 -366
- package/dist/src/Trove.js +422 -422
- package/dist/src/ZEROStake.d.ts +51 -51
- package/dist/src/ZEROStake.js +73 -73
- package/dist/src/_CachedReadableLiquity.d.ts +54 -54
- package/dist/src/_CachedReadableLiquity.js +92 -92
- package/dist/src/constants.d.ts +60 -60
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +63 -63
- package/dist/src/constants.js.map +1 -1
- package/package.json +2 -2
- package/src/constants.ts +3 -3
package/dist/src/LiquityStore.js
CHANGED
@@ -1,209 +1,209 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.LiquityStore = void 0;
|
7
|
-
const assert_1 = __importDefault(require("assert"));
|
8
|
-
const strictEquals = (a, b) => a === b;
|
9
|
-
const eq = (a, b) => a.eq(b);
|
10
|
-
const equals = (a, b) => a.equals(b);
|
11
|
-
const frontendStatusEquals = (a, b) => a.status === "unregistered"
|
12
|
-
? b.status === "unregistered"
|
13
|
-
: b.status === "registered" && a.kickbackRate.eq(b.kickbackRate);
|
14
|
-
const showFrontendStatus = (x) => x.status === "unregistered"
|
15
|
-
? '{ status: "unregistered" }'
|
16
|
-
: `{ status: "registered", kickbackRate: ${x.kickbackRate} }`;
|
17
|
-
const wrap = (f) => (...args) => f(...args);
|
18
|
-
const difference = (a, b) => Object.fromEntries(Object.entries(a).filter(([key, value]) => value !== b[key]));
|
19
|
-
/**
|
20
|
-
* Abstract base class of Zero data store implementations.
|
21
|
-
*
|
22
|
-
* @remarks
|
23
|
-
* The type parameter `T` may be used to type extra state added to {@link LiquityStoreState} by the
|
24
|
-
* subclass.
|
25
|
-
*
|
26
|
-
* Implemented by {@link @sovryn-zero/lib-ethers#BlockPolledLiquityStore}.
|
27
|
-
*
|
28
|
-
* @public
|
29
|
-
*/
|
30
|
-
class LiquityStore {
|
31
|
-
constructor() {
|
32
|
-
/** Turn console logging on/off. */
|
33
|
-
this.logging = false;
|
34
|
-
/** @internal */
|
35
|
-
this._loaded = false;
|
36
|
-
this._listeners = new Set();
|
37
|
-
}
|
38
|
-
/**
|
39
|
-
* The current store state.
|
40
|
-
*
|
41
|
-
* @remarks
|
42
|
-
* Should not be accessed before the store is loaded. Assign a function to
|
43
|
-
* {@link LiquityStore.onLoaded | onLoaded} to get a callback when this happens.
|
44
|
-
*
|
45
|
-
* See {@link LiquityStoreState} for the list of properties returned.
|
46
|
-
*/
|
47
|
-
get state() {
|
48
|
-
return Object.assign({}, this._baseState, this._derivedState, this._extraState);
|
49
|
-
}
|
50
|
-
/**
|
51
|
-
* Start monitoring the blockchain for Zero state changes.
|
52
|
-
*
|
53
|
-
* @remarks
|
54
|
-
* The {@link LiquityStore.onLoaded | onLoaded} callback will be called after the state is fetched
|
55
|
-
* for the first time.
|
56
|
-
*
|
57
|
-
* Use the {@link LiquityStore.subscribe | subscribe()} function to register listeners.
|
58
|
-
*
|
59
|
-
* @returns Function to stop the monitoring.
|
60
|
-
*/
|
61
|
-
start() {
|
62
|
-
const doStop = this._doStart();
|
63
|
-
return () => {
|
64
|
-
doStop();
|
65
|
-
this._cancelUpdateIfScheduled();
|
66
|
-
};
|
67
|
-
}
|
68
|
-
_cancelUpdateIfScheduled() {
|
69
|
-
if (this._updateTimeoutId !== undefined) {
|
70
|
-
clearTimeout(this._updateTimeoutId);
|
71
|
-
}
|
72
|
-
}
|
73
|
-
_scheduleUpdate() {
|
74
|
-
this._cancelUpdateIfScheduled();
|
75
|
-
this._updateTimeoutId = setTimeout(() => {
|
76
|
-
this._updateTimeoutId = undefined;
|
77
|
-
this._update();
|
78
|
-
}, 30000);
|
79
|
-
}
|
80
|
-
_logUpdate(name, next, show) {
|
81
|
-
if (this.logging) {
|
82
|
-
console.log(`${name} updated to ${show ? show(next) : next}`);
|
83
|
-
}
|
84
|
-
return next;
|
85
|
-
}
|
86
|
-
_updateIfChanged(equals, name, prev, next, show) {
|
87
|
-
return next !== undefined && !equals(prev, next) ? this._logUpdate(name, next, show) : prev;
|
88
|
-
}
|
89
|
-
_silentlyUpdateIfChanged(equals, prev, next) {
|
90
|
-
return next !== undefined && !equals(prev, next) ? next : prev;
|
91
|
-
}
|
92
|
-
_updateFees(name, prev, next) {
|
93
|
-
if (next && !next.equals(prev)) {
|
94
|
-
// Filter out fee update spam that happens on every new block by only logging when string
|
95
|
-
// representation changes.
|
96
|
-
if (`${next}` !== `${prev}`) {
|
97
|
-
this._logUpdate(name, next);
|
98
|
-
}
|
99
|
-
return next;
|
100
|
-
}
|
101
|
-
else {
|
102
|
-
return prev;
|
103
|
-
}
|
104
|
-
}
|
105
|
-
_reduce(baseState, baseStateUpdate) {
|
106
|
-
return {
|
107
|
-
frontend: this._updateIfChanged(frontendStatusEquals, "frontend", baseState.frontend, baseStateUpdate.frontend, showFrontendStatus),
|
108
|
-
ownFrontend: this._updateIfChanged(frontendStatusEquals, "ownFrontend", baseState.ownFrontend, baseStateUpdate.ownFrontend, showFrontendStatus),
|
109
|
-
numberOfTroves: this._updateIfChanged(strictEquals, "numberOfTroves", baseState.numberOfTroves, baseStateUpdate.numberOfTroves),
|
110
|
-
accountBalance: this._updateIfChanged(eq, "accountBalance", baseState.accountBalance, baseStateUpdate.accountBalance),
|
111
|
-
zusdBalance: this._updateIfChanged(eq, "zusdBalance", baseState.zusdBalance, baseStateUpdate.zusdBalance),
|
112
|
-
nueBalance: this._updateIfChanged(eq, "nueBalance", baseState.nueBalance, baseStateUpdate.nueBalance),
|
113
|
-
zeroBalance: this._updateIfChanged(eq, "zeroBalance", baseState.zeroBalance, baseStateUpdate.zeroBalance),
|
114
|
-
collateralSurplusBalance: this._updateIfChanged(eq, "collateralSurplusBalance", baseState.collateralSurplusBalance, baseStateUpdate.collateralSurplusBalance),
|
115
|
-
price: this._updateIfChanged(eq, "price", baseState.price, baseStateUpdate.price),
|
116
|
-
zusdInStabilityPool: this._updateIfChanged(eq, "zusdInStabilityPool", baseState.zusdInStabilityPool, baseStateUpdate.zusdInStabilityPool),
|
117
|
-
total: this._updateIfChanged(equals, "total", baseState.total, baseStateUpdate.total),
|
118
|
-
totalRedistributed: this._updateIfChanged(equals, "totalRedistributed", baseState.totalRedistributed, baseStateUpdate.totalRedistributed),
|
119
|
-
troveBeforeRedistribution: this._updateIfChanged(equals, "troveBeforeRedistribution", baseState.troveBeforeRedistribution, baseStateUpdate.troveBeforeRedistribution),
|
120
|
-
stabilityDeposit: this._updateIfChanged(equals, "stabilityDeposit", baseState.stabilityDeposit, baseStateUpdate.stabilityDeposit),
|
121
|
-
remainingStabilityPoolZEROReward: this._silentlyUpdateIfChanged(eq, baseState.remainingStabilityPoolZEROReward, baseStateUpdate.remainingStabilityPoolZEROReward),
|
122
|
-
_feesInNormalMode: this._silentlyUpdateIfChanged(equals, baseState._feesInNormalMode, baseStateUpdate._feesInNormalMode),
|
123
|
-
zeroStake: this._updateIfChanged(equals, "zeroStake", baseState.zeroStake, baseStateUpdate.zeroStake),
|
124
|
-
totalStakedZERO: this._updateIfChanged(eq, "totalStakedZERO", baseState.totalStakedZERO, baseStateUpdate.totalStakedZERO),
|
125
|
-
_riskiestTroveBeforeRedistribution: this._silentlyUpdateIfChanged(equals, baseState._riskiestTroveBeforeRedistribution, baseStateUpdate._riskiestTroveBeforeRedistribution)
|
126
|
-
};
|
127
|
-
}
|
128
|
-
_derive({ troveBeforeRedistribution, totalRedistributed, _feesInNormalMode, total, price, _riskiestTroveBeforeRedistribution }) {
|
129
|
-
const fees = _feesInNormalMode._setRecoveryMode(total.collateralRatioIsBelowCritical(price));
|
130
|
-
return {
|
131
|
-
trove: troveBeforeRedistribution.applyRedistribution(totalRedistributed),
|
132
|
-
fees,
|
133
|
-
borrowingRate: fees.borrowingRate(),
|
134
|
-
redemptionRate: fees.redemptionRate(),
|
135
|
-
haveUndercollateralizedTroves: _riskiestTroveBeforeRedistribution
|
136
|
-
.applyRedistribution(totalRedistributed)
|
137
|
-
.collateralRatioIsBelowMinimum(price)
|
138
|
-
};
|
139
|
-
}
|
140
|
-
_reduceDerived(derivedState, derivedStateUpdate) {
|
141
|
-
return {
|
142
|
-
fees: this._updateFees("fees", derivedState.fees, derivedStateUpdate.fees),
|
143
|
-
trove: this._updateIfChanged(equals, "trove", derivedState.trove, derivedStateUpdate.trove),
|
144
|
-
borrowingRate: this._silentlyUpdateIfChanged(eq, derivedState.borrowingRate, derivedStateUpdate.borrowingRate),
|
145
|
-
redemptionRate: this._silentlyUpdateIfChanged(eq, derivedState.redemptionRate, derivedStateUpdate.redemptionRate),
|
146
|
-
haveUndercollateralizedTroves: this._updateIfChanged(strictEquals, "haveUndercollateralizedTroves", derivedState.haveUndercollateralizedTroves, derivedStateUpdate.haveUndercollateralizedTroves)
|
147
|
-
};
|
148
|
-
}
|
149
|
-
_notify(params) {
|
150
|
-
// Iterate on a copy of `_listeners`, to avoid notifying any new listeners subscribed by
|
151
|
-
// existing listeners, as that could result in infinite loops.
|
152
|
-
//
|
153
|
-
// Before calling a listener from our copy of `_listeners`, check if it has been removed from
|
154
|
-
// the original set. This way we avoid calling listeners that have already been unsubscribed
|
155
|
-
// by an earlier listener callback.
|
156
|
-
[...this._listeners].forEach(listener => {
|
157
|
-
if (this._listeners.has(listener)) {
|
158
|
-
listener(params);
|
159
|
-
}
|
160
|
-
});
|
161
|
-
}
|
162
|
-
/**
|
163
|
-
* Register a state change listener.
|
164
|
-
*
|
165
|
-
* @param listener - Function that will be called whenever state changes.
|
166
|
-
* @returns Function to unregister this listener.
|
167
|
-
*/
|
168
|
-
subscribe(listener) {
|
169
|
-
const uniqueListener = wrap(listener);
|
170
|
-
this._listeners.add(uniqueListener);
|
171
|
-
return () => {
|
172
|
-
this._listeners.delete(uniqueListener);
|
173
|
-
};
|
174
|
-
}
|
175
|
-
/** @internal */
|
176
|
-
_load(baseState, extraState) {
|
177
|
-
assert_1.default(!this._loaded);
|
178
|
-
this._baseState = baseState;
|
179
|
-
this._derivedState = this._derive(baseState);
|
180
|
-
this._extraState = extraState;
|
181
|
-
this._loaded = true;
|
182
|
-
this._scheduleUpdate();
|
183
|
-
if (this.onLoaded) {
|
184
|
-
this.onLoaded();
|
185
|
-
}
|
186
|
-
}
|
187
|
-
/** @internal */
|
188
|
-
_update(baseStateUpdate, extraStateUpdate) {
|
189
|
-
assert_1.default(this._baseState && this._derivedState);
|
190
|
-
const oldState = this.state;
|
191
|
-
if (baseStateUpdate) {
|
192
|
-
this._baseState = this._reduce(this._baseState, baseStateUpdate);
|
193
|
-
}
|
194
|
-
// Always running this lets us derive state based on passage of time, like baseRate decay
|
195
|
-
this._derivedState = this._reduceDerived(this._derivedState, this._derive(this._baseState));
|
196
|
-
if (extraStateUpdate) {
|
197
|
-
assert_1.default(this._extraState);
|
198
|
-
this._extraState = this._reduceExtra(this._extraState, extraStateUpdate);
|
199
|
-
}
|
200
|
-
this._scheduleUpdate();
|
201
|
-
this._notify({
|
202
|
-
newState: this.state,
|
203
|
-
oldState,
|
204
|
-
stateChange: difference(this.state, oldState)
|
205
|
-
});
|
206
|
-
}
|
207
|
-
}
|
208
|
-
exports.LiquityStore = LiquityStore;
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.LiquityStore = void 0;
|
7
|
+
const assert_1 = __importDefault(require("assert"));
|
8
|
+
const strictEquals = (a, b) => a === b;
|
9
|
+
const eq = (a, b) => a.eq(b);
|
10
|
+
const equals = (a, b) => a.equals(b);
|
11
|
+
const frontendStatusEquals = (a, b) => a.status === "unregistered"
|
12
|
+
? b.status === "unregistered"
|
13
|
+
: b.status === "registered" && a.kickbackRate.eq(b.kickbackRate);
|
14
|
+
const showFrontendStatus = (x) => x.status === "unregistered"
|
15
|
+
? '{ status: "unregistered" }'
|
16
|
+
: `{ status: "registered", kickbackRate: ${x.kickbackRate} }`;
|
17
|
+
const wrap = (f) => (...args) => f(...args);
|
18
|
+
const difference = (a, b) => Object.fromEntries(Object.entries(a).filter(([key, value]) => value !== b[key]));
|
19
|
+
/**
|
20
|
+
* Abstract base class of Zero data store implementations.
|
21
|
+
*
|
22
|
+
* @remarks
|
23
|
+
* The type parameter `T` may be used to type extra state added to {@link LiquityStoreState} by the
|
24
|
+
* subclass.
|
25
|
+
*
|
26
|
+
* Implemented by {@link @sovryn-zero/lib-ethers#BlockPolledLiquityStore}.
|
27
|
+
*
|
28
|
+
* @public
|
29
|
+
*/
|
30
|
+
class LiquityStore {
|
31
|
+
constructor() {
|
32
|
+
/** Turn console logging on/off. */
|
33
|
+
this.logging = false;
|
34
|
+
/** @internal */
|
35
|
+
this._loaded = false;
|
36
|
+
this._listeners = new Set();
|
37
|
+
}
|
38
|
+
/**
|
39
|
+
* The current store state.
|
40
|
+
*
|
41
|
+
* @remarks
|
42
|
+
* Should not be accessed before the store is loaded. Assign a function to
|
43
|
+
* {@link LiquityStore.onLoaded | onLoaded} to get a callback when this happens.
|
44
|
+
*
|
45
|
+
* See {@link LiquityStoreState} for the list of properties returned.
|
46
|
+
*/
|
47
|
+
get state() {
|
48
|
+
return Object.assign({}, this._baseState, this._derivedState, this._extraState);
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Start monitoring the blockchain for Zero state changes.
|
52
|
+
*
|
53
|
+
* @remarks
|
54
|
+
* The {@link LiquityStore.onLoaded | onLoaded} callback will be called after the state is fetched
|
55
|
+
* for the first time.
|
56
|
+
*
|
57
|
+
* Use the {@link LiquityStore.subscribe | subscribe()} function to register listeners.
|
58
|
+
*
|
59
|
+
* @returns Function to stop the monitoring.
|
60
|
+
*/
|
61
|
+
start() {
|
62
|
+
const doStop = this._doStart();
|
63
|
+
return () => {
|
64
|
+
doStop();
|
65
|
+
this._cancelUpdateIfScheduled();
|
66
|
+
};
|
67
|
+
}
|
68
|
+
_cancelUpdateIfScheduled() {
|
69
|
+
if (this._updateTimeoutId !== undefined) {
|
70
|
+
clearTimeout(this._updateTimeoutId);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
_scheduleUpdate() {
|
74
|
+
this._cancelUpdateIfScheduled();
|
75
|
+
this._updateTimeoutId = setTimeout(() => {
|
76
|
+
this._updateTimeoutId = undefined;
|
77
|
+
this._update();
|
78
|
+
}, 30000);
|
79
|
+
}
|
80
|
+
_logUpdate(name, next, show) {
|
81
|
+
if (this.logging) {
|
82
|
+
console.log(`${name} updated to ${show ? show(next) : next}`);
|
83
|
+
}
|
84
|
+
return next;
|
85
|
+
}
|
86
|
+
_updateIfChanged(equals, name, prev, next, show) {
|
87
|
+
return next !== undefined && !equals(prev, next) ? this._logUpdate(name, next, show) : prev;
|
88
|
+
}
|
89
|
+
_silentlyUpdateIfChanged(equals, prev, next) {
|
90
|
+
return next !== undefined && !equals(prev, next) ? next : prev;
|
91
|
+
}
|
92
|
+
_updateFees(name, prev, next) {
|
93
|
+
if (next && !next.equals(prev)) {
|
94
|
+
// Filter out fee update spam that happens on every new block by only logging when string
|
95
|
+
// representation changes.
|
96
|
+
if (`${next}` !== `${prev}`) {
|
97
|
+
this._logUpdate(name, next);
|
98
|
+
}
|
99
|
+
return next;
|
100
|
+
}
|
101
|
+
else {
|
102
|
+
return prev;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
_reduce(baseState, baseStateUpdate) {
|
106
|
+
return {
|
107
|
+
frontend: this._updateIfChanged(frontendStatusEquals, "frontend", baseState.frontend, baseStateUpdate.frontend, showFrontendStatus),
|
108
|
+
ownFrontend: this._updateIfChanged(frontendStatusEquals, "ownFrontend", baseState.ownFrontend, baseStateUpdate.ownFrontend, showFrontendStatus),
|
109
|
+
numberOfTroves: this._updateIfChanged(strictEquals, "numberOfTroves", baseState.numberOfTroves, baseStateUpdate.numberOfTroves),
|
110
|
+
accountBalance: this._updateIfChanged(eq, "accountBalance", baseState.accountBalance, baseStateUpdate.accountBalance),
|
111
|
+
zusdBalance: this._updateIfChanged(eq, "zusdBalance", baseState.zusdBalance, baseStateUpdate.zusdBalance),
|
112
|
+
nueBalance: this._updateIfChanged(eq, "nueBalance", baseState.nueBalance, baseStateUpdate.nueBalance),
|
113
|
+
zeroBalance: this._updateIfChanged(eq, "zeroBalance", baseState.zeroBalance, baseStateUpdate.zeroBalance),
|
114
|
+
collateralSurplusBalance: this._updateIfChanged(eq, "collateralSurplusBalance", baseState.collateralSurplusBalance, baseStateUpdate.collateralSurplusBalance),
|
115
|
+
price: this._updateIfChanged(eq, "price", baseState.price, baseStateUpdate.price),
|
116
|
+
zusdInStabilityPool: this._updateIfChanged(eq, "zusdInStabilityPool", baseState.zusdInStabilityPool, baseStateUpdate.zusdInStabilityPool),
|
117
|
+
total: this._updateIfChanged(equals, "total", baseState.total, baseStateUpdate.total),
|
118
|
+
totalRedistributed: this._updateIfChanged(equals, "totalRedistributed", baseState.totalRedistributed, baseStateUpdate.totalRedistributed),
|
119
|
+
troveBeforeRedistribution: this._updateIfChanged(equals, "troveBeforeRedistribution", baseState.troveBeforeRedistribution, baseStateUpdate.troveBeforeRedistribution),
|
120
|
+
stabilityDeposit: this._updateIfChanged(equals, "stabilityDeposit", baseState.stabilityDeposit, baseStateUpdate.stabilityDeposit),
|
121
|
+
remainingStabilityPoolZEROReward: this._silentlyUpdateIfChanged(eq, baseState.remainingStabilityPoolZEROReward, baseStateUpdate.remainingStabilityPoolZEROReward),
|
122
|
+
_feesInNormalMode: this._silentlyUpdateIfChanged(equals, baseState._feesInNormalMode, baseStateUpdate._feesInNormalMode),
|
123
|
+
zeroStake: this._updateIfChanged(equals, "zeroStake", baseState.zeroStake, baseStateUpdate.zeroStake),
|
124
|
+
totalStakedZERO: this._updateIfChanged(eq, "totalStakedZERO", baseState.totalStakedZERO, baseStateUpdate.totalStakedZERO),
|
125
|
+
_riskiestTroveBeforeRedistribution: this._silentlyUpdateIfChanged(equals, baseState._riskiestTroveBeforeRedistribution, baseStateUpdate._riskiestTroveBeforeRedistribution)
|
126
|
+
};
|
127
|
+
}
|
128
|
+
_derive({ troveBeforeRedistribution, totalRedistributed, _feesInNormalMode, total, price, _riskiestTroveBeforeRedistribution }) {
|
129
|
+
const fees = _feesInNormalMode._setRecoveryMode(total.collateralRatioIsBelowCritical(price));
|
130
|
+
return {
|
131
|
+
trove: troveBeforeRedistribution.applyRedistribution(totalRedistributed),
|
132
|
+
fees,
|
133
|
+
borrowingRate: fees.borrowingRate(),
|
134
|
+
redemptionRate: fees.redemptionRate(),
|
135
|
+
haveUndercollateralizedTroves: _riskiestTroveBeforeRedistribution
|
136
|
+
.applyRedistribution(totalRedistributed)
|
137
|
+
.collateralRatioIsBelowMinimum(price)
|
138
|
+
};
|
139
|
+
}
|
140
|
+
_reduceDerived(derivedState, derivedStateUpdate) {
|
141
|
+
return {
|
142
|
+
fees: this._updateFees("fees", derivedState.fees, derivedStateUpdate.fees),
|
143
|
+
trove: this._updateIfChanged(equals, "trove", derivedState.trove, derivedStateUpdate.trove),
|
144
|
+
borrowingRate: this._silentlyUpdateIfChanged(eq, derivedState.borrowingRate, derivedStateUpdate.borrowingRate),
|
145
|
+
redemptionRate: this._silentlyUpdateIfChanged(eq, derivedState.redemptionRate, derivedStateUpdate.redemptionRate),
|
146
|
+
haveUndercollateralizedTroves: this._updateIfChanged(strictEquals, "haveUndercollateralizedTroves", derivedState.haveUndercollateralizedTroves, derivedStateUpdate.haveUndercollateralizedTroves)
|
147
|
+
};
|
148
|
+
}
|
149
|
+
_notify(params) {
|
150
|
+
// Iterate on a copy of `_listeners`, to avoid notifying any new listeners subscribed by
|
151
|
+
// existing listeners, as that could result in infinite loops.
|
152
|
+
//
|
153
|
+
// Before calling a listener from our copy of `_listeners`, check if it has been removed from
|
154
|
+
// the original set. This way we avoid calling listeners that have already been unsubscribed
|
155
|
+
// by an earlier listener callback.
|
156
|
+
[...this._listeners].forEach(listener => {
|
157
|
+
if (this._listeners.has(listener)) {
|
158
|
+
listener(params);
|
159
|
+
}
|
160
|
+
});
|
161
|
+
}
|
162
|
+
/**
|
163
|
+
* Register a state change listener.
|
164
|
+
*
|
165
|
+
* @param listener - Function that will be called whenever state changes.
|
166
|
+
* @returns Function to unregister this listener.
|
167
|
+
*/
|
168
|
+
subscribe(listener) {
|
169
|
+
const uniqueListener = wrap(listener);
|
170
|
+
this._listeners.add(uniqueListener);
|
171
|
+
return () => {
|
172
|
+
this._listeners.delete(uniqueListener);
|
173
|
+
};
|
174
|
+
}
|
175
|
+
/** @internal */
|
176
|
+
_load(baseState, extraState) {
|
177
|
+
assert_1.default(!this._loaded);
|
178
|
+
this._baseState = baseState;
|
179
|
+
this._derivedState = this._derive(baseState);
|
180
|
+
this._extraState = extraState;
|
181
|
+
this._loaded = true;
|
182
|
+
this._scheduleUpdate();
|
183
|
+
if (this.onLoaded) {
|
184
|
+
this.onLoaded();
|
185
|
+
}
|
186
|
+
}
|
187
|
+
/** @internal */
|
188
|
+
_update(baseStateUpdate, extraStateUpdate) {
|
189
|
+
assert_1.default(this._baseState && this._derivedState);
|
190
|
+
const oldState = this.state;
|
191
|
+
if (baseStateUpdate) {
|
192
|
+
this._baseState = this._reduce(this._baseState, baseStateUpdate);
|
193
|
+
}
|
194
|
+
// Always running this lets us derive state based on passage of time, like baseRate decay
|
195
|
+
this._derivedState = this._reduceDerived(this._derivedState, this._derive(this._baseState));
|
196
|
+
if (extraStateUpdate) {
|
197
|
+
assert_1.default(this._extraState);
|
198
|
+
this._extraState = this._reduceExtra(this._extraState, extraStateUpdate);
|
199
|
+
}
|
200
|
+
this._scheduleUpdate();
|
201
|
+
this._notify({
|
202
|
+
newState: this.state,
|
203
|
+
oldState,
|
204
|
+
stateChange: difference(this.state, oldState)
|
205
|
+
});
|
206
|
+
}
|
207
|
+
}
|
208
|
+
exports.LiquityStore = LiquityStore;
|
209
209
|
//# sourceMappingURL=LiquityStore.js.map
|
@@ -1,15 +1,15 @@
|
|
1
|
-
import { Decimal } from "./Decimal";
|
2
|
-
import { Trove, TroveWithPendingRedistribution } from "./Trove";
|
3
|
-
import { StabilityDeposit } from "./StabilityDeposit";
|
4
|
-
/** @alpha */
|
5
|
-
export interface ObservableLiquity {
|
6
|
-
watchTotalRedistributed(onTotalRedistributedChanged: (totalRedistributed: Trove) => void): () => void;
|
7
|
-
watchTroveWithoutRewards(onTroveChanged: (trove: TroveWithPendingRedistribution) => void, address?: string): () => void;
|
8
|
-
watchNumberOfTroves(onNumberOfTrovesChanged: (numberOfTroves: number) => void): () => void;
|
9
|
-
watchPrice(onPriceChanged: (price: Decimal) => void): () => void;
|
10
|
-
watchTotal(onTotalChanged: (total: Trove) => void): () => void;
|
11
|
-
watchStabilityDeposit(onStabilityDepositChanged: (stabilityDeposit: StabilityDeposit) => void, address?: string): () => void;
|
12
|
-
watchZUSDInStabilityPool(onZUSDInStabilityPoolChanged: (zusdInStabilityPool: Decimal) => void): () => void;
|
13
|
-
watchZUSDBalance(onZUSDBalanceChanged: (balance: Decimal) => void, address?: string): () => void;
|
14
|
-
}
|
1
|
+
import { Decimal } from "./Decimal";
|
2
|
+
import { Trove, TroveWithPendingRedistribution } from "./Trove";
|
3
|
+
import { StabilityDeposit } from "./StabilityDeposit";
|
4
|
+
/** @alpha */
|
5
|
+
export interface ObservableLiquity {
|
6
|
+
watchTotalRedistributed(onTotalRedistributedChanged: (totalRedistributed: Trove) => void): () => void;
|
7
|
+
watchTroveWithoutRewards(onTroveChanged: (trove: TroveWithPendingRedistribution) => void, address?: string): () => void;
|
8
|
+
watchNumberOfTroves(onNumberOfTrovesChanged: (numberOfTroves: number) => void): () => void;
|
9
|
+
watchPrice(onPriceChanged: (price: Decimal) => void): () => void;
|
10
|
+
watchTotal(onTotalChanged: (total: Trove) => void): () => void;
|
11
|
+
watchStabilityDeposit(onStabilityDepositChanged: (stabilityDeposit: StabilityDeposit) => void, address?: string): () => void;
|
12
|
+
watchZUSDInStabilityPool(onZUSDInStabilityPoolChanged: (zusdInStabilityPool: Decimal) => void): () => void;
|
13
|
+
watchZUSDBalance(onZUSDBalanceChanged: (balance: Decimal) => void, address?: string): () => void;
|
14
|
+
}
|
15
15
|
//# sourceMappingURL=ObservableLiquity.d.ts.map
|
@@ -1,3 +1,3 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
//# sourceMappingURL=ObservableLiquity.js.map
|