@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.
@@ -1,209 +1,209 @@
1
- import { Decimal } from "./Decimal";
2
- import { StabilityDeposit } from "./StabilityDeposit";
3
- import { Trove, TroveWithPendingRedistribution, UserTrove } from "./Trove";
4
- import { Fees } from "./Fees";
5
- import { ZEROStake } from "./ZEROStake";
6
- import { FrontendStatus } from "./ReadableLiquity";
7
- /**
8
- * State variables read from the blockchain.
9
- *
10
- * @public
11
- */
12
- export interface LiquityStoreBaseState {
13
- /** Status of currently used frontend. */
14
- frontend: FrontendStatus;
15
- /** Status of user's own frontend. */
16
- ownFrontend: FrontendStatus;
17
- /** Number of Troves that are currently open. */
18
- numberOfTroves: number;
19
- /** User's native currency balance (e.g. Ether). */
20
- accountBalance: Decimal;
21
- /** User's ZUSD token balance. */
22
- zusdBalance: Decimal;
23
- /** User's NUE token balance. */
24
- nueBalance: Decimal;
25
- /** User's ZERO token balance. */
26
- zeroBalance: Decimal;
27
- /**
28
- * Amount of leftover collateral available for withdrawal to the user.
29
- *
30
- * @remarks
31
- * See {@link ReadableLiquity.getCollateralSurplusBalance | getCollateralSurplusBalance()} for
32
- * more information.
33
- */
34
- collateralSurplusBalance: Decimal;
35
- /** Current price of the native currency (e.g. Ether) in USD. */
36
- price: Decimal;
37
- /** Total amount of ZUSD currently deposited in the Stability Pool. */
38
- zusdInStabilityPool: Decimal;
39
- /** Total collateral and debt in the Zero system. */
40
- total: Trove;
41
- /**
42
- * Total collateral and debt per stake that has been liquidated through redistribution.
43
- *
44
- * @remarks
45
- * Needed when dealing with instances of {@link TroveWithPendingRedistribution}.
46
- */
47
- totalRedistributed: Trove;
48
- /**
49
- * User's Trove in its state after the last direct modification.
50
- *
51
- * @remarks
52
- * The current state of the user's Trove can be found as
53
- * {@link LiquityStoreDerivedState.trove | trove}.
54
- */
55
- troveBeforeRedistribution: TroveWithPendingRedistribution;
56
- /** User's stability deposit. */
57
- stabilityDeposit: StabilityDeposit;
58
- /** Remaining ZERO that will be collectively rewarded to stability depositors. */
59
- remainingStabilityPoolZEROReward: Decimal;
60
- /** @internal */
61
- _feesInNormalMode: Fees;
62
- /** User's ZERO stake. */
63
- zeroStake: ZEROStake;
64
- /** Total amount of ZERO currently staked. */
65
- totalStakedZERO: Decimal;
66
- /** @internal */
67
- _riskiestTroveBeforeRedistribution: TroveWithPendingRedistribution;
68
- }
69
- /**
70
- * State variables derived from {@link LiquityStoreBaseState}.
71
- *
72
- * @public
73
- */
74
- export interface LiquityStoreDerivedState {
75
- /** Current state of user's Trove */
76
- trove: UserTrove;
77
- /** Calculator for current fees. */
78
- fees: Fees;
79
- /**
80
- * Current borrowing rate.
81
- *
82
- * @remarks
83
- * A value between 0 and 1.
84
- *
85
- * @example
86
- * For example a value of 0.01 amounts to a borrowing fee of 1% of the borrowed amount.
87
- */
88
- borrowingRate: Decimal;
89
- /**
90
- * Current redemption rate.
91
- *
92
- * @remarks
93
- * Note that the actual rate paid by a redemption transaction will depend on the amount of ZUSD
94
- * being redeemed.
95
- *
96
- * Use {@link Fees.redemptionRate} to calculate a precise redemption rate.
97
- */
98
- redemptionRate: Decimal;
99
- /**
100
- * Whether there are any Troves with collateral ratio below the
101
- * {@link MINIMUM_COLLATERAL_RATIO | minimum}.
102
- */
103
- haveUndercollateralizedTroves: boolean;
104
- }
105
- /**
106
- * Type of {@link LiquityStore}'s {@link LiquityStore.state | state}.
107
- *
108
- * @remarks
109
- * It combines all properties of {@link LiquityStoreBaseState} and {@link LiquityStoreDerivedState}
110
- * with optional extra state added by the particular `LiquityStore` implementation.
111
- *
112
- * The type parameter `T` may be used to type the extra state.
113
- *
114
- * @public
115
- */
116
- export declare type LiquityStoreState<T = unknown> = LiquityStoreBaseState & LiquityStoreDerivedState & T;
117
- /**
118
- * Parameters passed to {@link LiquityStore} listeners.
119
- *
120
- * @remarks
121
- * Use the {@link LiquityStore.subscribe | subscribe()} function to register a listener.
122
-
123
- * @public
124
- */
125
- export interface LiquityStoreListenerParams<T = unknown> {
126
- /** The entire previous state. */
127
- newState: LiquityStoreState<T>;
128
- /** The entire new state. */
129
- oldState: LiquityStoreState<T>;
130
- /** Only the state variables that have changed. */
131
- stateChange: Partial<LiquityStoreState<T>>;
132
- }
133
- /**
134
- * Abstract base class of Zero data store implementations.
135
- *
136
- * @remarks
137
- * The type parameter `T` may be used to type extra state added to {@link LiquityStoreState} by the
138
- * subclass.
139
- *
140
- * Implemented by {@link @sovryn-zero/lib-ethers#BlockPolledLiquityStore}.
141
- *
142
- * @public
143
- */
144
- export declare abstract class LiquityStore<T = unknown> {
145
- /** Turn console logging on/off. */
146
- logging: boolean;
147
- /**
148
- * Called after the state is fetched for the first time.
149
- *
150
- * @remarks
151
- * See {@link LiquityStore.start | start()}.
152
- */
153
- onLoaded?: () => void;
154
- /** @internal */
155
- protected _loaded: boolean;
156
- private _baseState?;
157
- private _derivedState?;
158
- private _extraState?;
159
- private _updateTimeoutId;
160
- private _listeners;
161
- /**
162
- * The current store state.
163
- *
164
- * @remarks
165
- * Should not be accessed before the store is loaded. Assign a function to
166
- * {@link LiquityStore.onLoaded | onLoaded} to get a callback when this happens.
167
- *
168
- * See {@link LiquityStoreState} for the list of properties returned.
169
- */
170
- get state(): LiquityStoreState<T>;
171
- /** @internal */
172
- protected abstract _doStart(): () => void;
173
- /**
174
- * Start monitoring the blockchain for Zero state changes.
175
- *
176
- * @remarks
177
- * The {@link LiquityStore.onLoaded | onLoaded} callback will be called after the state is fetched
178
- * for the first time.
179
- *
180
- * Use the {@link LiquityStore.subscribe | subscribe()} function to register listeners.
181
- *
182
- * @returns Function to stop the monitoring.
183
- */
184
- start(): () => void;
185
- private _cancelUpdateIfScheduled;
186
- private _scheduleUpdate;
187
- private _logUpdate;
188
- private _updateIfChanged;
189
- private _silentlyUpdateIfChanged;
190
- private _updateFees;
191
- private _reduce;
192
- private _derive;
193
- private _reduceDerived;
194
- /** @internal */
195
- protected abstract _reduceExtra(extraState: T, extraStateUpdate: Partial<T>): T;
196
- private _notify;
197
- /**
198
- * Register a state change listener.
199
- *
200
- * @param listener - Function that will be called whenever state changes.
201
- * @returns Function to unregister this listener.
202
- */
203
- subscribe(listener: (params: LiquityStoreListenerParams<T>) => void): () => void;
204
- /** @internal */
205
- protected _load(baseState: LiquityStoreBaseState, extraState?: T): void;
206
- /** @internal */
207
- protected _update(baseStateUpdate?: Partial<LiquityStoreBaseState>, extraStateUpdate?: Partial<T>): void;
208
- }
1
+ import { Decimal } from "./Decimal";
2
+ import { StabilityDeposit } from "./StabilityDeposit";
3
+ import { Trove, TroveWithPendingRedistribution, UserTrove } from "./Trove";
4
+ import { Fees } from "./Fees";
5
+ import { ZEROStake } from "./ZEROStake";
6
+ import { FrontendStatus } from "./ReadableLiquity";
7
+ /**
8
+ * State variables read from the blockchain.
9
+ *
10
+ * @public
11
+ */
12
+ export interface LiquityStoreBaseState {
13
+ /** Status of currently used frontend. */
14
+ frontend: FrontendStatus;
15
+ /** Status of user's own frontend. */
16
+ ownFrontend: FrontendStatus;
17
+ /** Number of Troves that are currently open. */
18
+ numberOfTroves: number;
19
+ /** User's native currency balance (e.g. Ether). */
20
+ accountBalance: Decimal;
21
+ /** User's ZUSD token balance. */
22
+ zusdBalance: Decimal;
23
+ /** User's NUE token balance. */
24
+ nueBalance: Decimal;
25
+ /** User's ZERO token balance. */
26
+ zeroBalance: Decimal;
27
+ /**
28
+ * Amount of leftover collateral available for withdrawal to the user.
29
+ *
30
+ * @remarks
31
+ * See {@link ReadableLiquity.getCollateralSurplusBalance | getCollateralSurplusBalance()} for
32
+ * more information.
33
+ */
34
+ collateralSurplusBalance: Decimal;
35
+ /** Current price of the native currency (e.g. Ether) in USD. */
36
+ price: Decimal;
37
+ /** Total amount of ZUSD currently deposited in the Stability Pool. */
38
+ zusdInStabilityPool: Decimal;
39
+ /** Total collateral and debt in the Zero system. */
40
+ total: Trove;
41
+ /**
42
+ * Total collateral and debt per stake that has been liquidated through redistribution.
43
+ *
44
+ * @remarks
45
+ * Needed when dealing with instances of {@link TroveWithPendingRedistribution}.
46
+ */
47
+ totalRedistributed: Trove;
48
+ /**
49
+ * User's Trove in its state after the last direct modification.
50
+ *
51
+ * @remarks
52
+ * The current state of the user's Trove can be found as
53
+ * {@link LiquityStoreDerivedState.trove | trove}.
54
+ */
55
+ troveBeforeRedistribution: TroveWithPendingRedistribution;
56
+ /** User's stability deposit. */
57
+ stabilityDeposit: StabilityDeposit;
58
+ /** Remaining ZERO that will be collectively rewarded to stability depositors. */
59
+ remainingStabilityPoolZEROReward: Decimal;
60
+ /** @internal */
61
+ _feesInNormalMode: Fees;
62
+ /** User's ZERO stake. */
63
+ zeroStake: ZEROStake;
64
+ /** Total amount of ZERO currently staked. */
65
+ totalStakedZERO: Decimal;
66
+ /** @internal */
67
+ _riskiestTroveBeforeRedistribution: TroveWithPendingRedistribution;
68
+ }
69
+ /**
70
+ * State variables derived from {@link LiquityStoreBaseState}.
71
+ *
72
+ * @public
73
+ */
74
+ export interface LiquityStoreDerivedState {
75
+ /** Current state of user's Trove */
76
+ trove: UserTrove;
77
+ /** Calculator for current fees. */
78
+ fees: Fees;
79
+ /**
80
+ * Current borrowing rate.
81
+ *
82
+ * @remarks
83
+ * A value between 0 and 1.
84
+ *
85
+ * @example
86
+ * For example a value of 0.01 amounts to a borrowing fee of 1% of the borrowed amount.
87
+ */
88
+ borrowingRate: Decimal;
89
+ /**
90
+ * Current redemption rate.
91
+ *
92
+ * @remarks
93
+ * Note that the actual rate paid by a redemption transaction will depend on the amount of ZUSD
94
+ * being redeemed.
95
+ *
96
+ * Use {@link Fees.redemptionRate} to calculate a precise redemption rate.
97
+ */
98
+ redemptionRate: Decimal;
99
+ /**
100
+ * Whether there are any Troves with collateral ratio below the
101
+ * {@link MINIMUM_COLLATERAL_RATIO | minimum}.
102
+ */
103
+ haveUndercollateralizedTroves: boolean;
104
+ }
105
+ /**
106
+ * Type of {@link LiquityStore}'s {@link LiquityStore.state | state}.
107
+ *
108
+ * @remarks
109
+ * It combines all properties of {@link LiquityStoreBaseState} and {@link LiquityStoreDerivedState}
110
+ * with optional extra state added by the particular `LiquityStore` implementation.
111
+ *
112
+ * The type parameter `T` may be used to type the extra state.
113
+ *
114
+ * @public
115
+ */
116
+ export declare type LiquityStoreState<T = unknown> = LiquityStoreBaseState & LiquityStoreDerivedState & T;
117
+ /**
118
+ * Parameters passed to {@link LiquityStore} listeners.
119
+ *
120
+ * @remarks
121
+ * Use the {@link LiquityStore.subscribe | subscribe()} function to register a listener.
122
+
123
+ * @public
124
+ */
125
+ export interface LiquityStoreListenerParams<T = unknown> {
126
+ /** The entire previous state. */
127
+ newState: LiquityStoreState<T>;
128
+ /** The entire new state. */
129
+ oldState: LiquityStoreState<T>;
130
+ /** Only the state variables that have changed. */
131
+ stateChange: Partial<LiquityStoreState<T>>;
132
+ }
133
+ /**
134
+ * Abstract base class of Zero data store implementations.
135
+ *
136
+ * @remarks
137
+ * The type parameter `T` may be used to type extra state added to {@link LiquityStoreState} by the
138
+ * subclass.
139
+ *
140
+ * Implemented by {@link @sovryn-zero/lib-ethers#BlockPolledLiquityStore}.
141
+ *
142
+ * @public
143
+ */
144
+ export declare abstract class LiquityStore<T = unknown> {
145
+ /** Turn console logging on/off. */
146
+ logging: boolean;
147
+ /**
148
+ * Called after the state is fetched for the first time.
149
+ *
150
+ * @remarks
151
+ * See {@link LiquityStore.start | start()}.
152
+ */
153
+ onLoaded?: () => void;
154
+ /** @internal */
155
+ protected _loaded: boolean;
156
+ private _baseState?;
157
+ private _derivedState?;
158
+ private _extraState?;
159
+ private _updateTimeoutId;
160
+ private _listeners;
161
+ /**
162
+ * The current store state.
163
+ *
164
+ * @remarks
165
+ * Should not be accessed before the store is loaded. Assign a function to
166
+ * {@link LiquityStore.onLoaded | onLoaded} to get a callback when this happens.
167
+ *
168
+ * See {@link LiquityStoreState} for the list of properties returned.
169
+ */
170
+ get state(): LiquityStoreState<T>;
171
+ /** @internal */
172
+ protected abstract _doStart(): () => void;
173
+ /**
174
+ * Start monitoring the blockchain for Zero state changes.
175
+ *
176
+ * @remarks
177
+ * The {@link LiquityStore.onLoaded | onLoaded} callback will be called after the state is fetched
178
+ * for the first time.
179
+ *
180
+ * Use the {@link LiquityStore.subscribe | subscribe()} function to register listeners.
181
+ *
182
+ * @returns Function to stop the monitoring.
183
+ */
184
+ start(): () => void;
185
+ private _cancelUpdateIfScheduled;
186
+ private _scheduleUpdate;
187
+ private _logUpdate;
188
+ private _updateIfChanged;
189
+ private _silentlyUpdateIfChanged;
190
+ private _updateFees;
191
+ private _reduce;
192
+ private _derive;
193
+ private _reduceDerived;
194
+ /** @internal */
195
+ protected abstract _reduceExtra(extraState: T, extraStateUpdate: Partial<T>): T;
196
+ private _notify;
197
+ /**
198
+ * Register a state change listener.
199
+ *
200
+ * @param listener - Function that will be called whenever state changes.
201
+ * @returns Function to unregister this listener.
202
+ */
203
+ subscribe(listener: (params: LiquityStoreListenerParams<T>) => void): () => void;
204
+ /** @internal */
205
+ protected _load(baseState: LiquityStoreBaseState, extraState?: T): void;
206
+ /** @internal */
207
+ protected _update(baseStateUpdate?: Partial<LiquityStoreBaseState>, extraStateUpdate?: Partial<T>): void;
208
+ }
209
209
  //# sourceMappingURL=LiquityStore.d.ts.map