@sovryn-zero/lib-base 0.1.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.
Files changed (84) hide show
  1. package/.eslintrc.json +17 -0
  2. package/.mocharc.yml +1 -0
  3. package/LICENSE +905 -0
  4. package/README.md +23 -0
  5. package/api-extractor.json +4 -0
  6. package/dist/index.d.ts +14 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +26 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/src/Decimal.d.ts +89 -0
  11. package/dist/src/Decimal.d.ts.map +1 -0
  12. package/dist/src/Decimal.js +361 -0
  13. package/dist/src/Decimal.js.map +1 -0
  14. package/dist/src/Fees.d.ts +82 -0
  15. package/dist/src/Fees.d.ts.map +1 -0
  16. package/dist/src/Fees.js +123 -0
  17. package/dist/src/Fees.js.map +1 -0
  18. package/dist/src/LiquityStore.d.ts +209 -0
  19. package/dist/src/LiquityStore.d.ts.map +1 -0
  20. package/dist/src/LiquityStore.js +209 -0
  21. package/dist/src/LiquityStore.js.map +1 -0
  22. package/dist/src/ObservableLiquity.d.ts +15 -0
  23. package/dist/src/ObservableLiquity.d.ts.map +1 -0
  24. package/dist/src/ObservableLiquity.js +3 -0
  25. package/dist/src/ObservableLiquity.js.map +1 -0
  26. package/dist/src/PopulatableLiquity.d.ts +125 -0
  27. package/dist/src/PopulatableLiquity.d.ts.map +1 -0
  28. package/dist/src/PopulatableLiquity.js +3 -0
  29. package/dist/src/PopulatableLiquity.js.map +1 -0
  30. package/dist/src/ReadableLiquity.d.ts +156 -0
  31. package/dist/src/ReadableLiquity.d.ts.map +1 -0
  32. package/dist/src/ReadableLiquity.js +3 -0
  33. package/dist/src/ReadableLiquity.js.map +1 -0
  34. package/dist/src/SendableLiquity.d.ts +156 -0
  35. package/dist/src/SendableLiquity.d.ts.map +1 -0
  36. package/dist/src/SendableLiquity.js +20 -0
  37. package/dist/src/SendableLiquity.js.map +1 -0
  38. package/dist/src/StabilityDeposit.d.ts +59 -0
  39. package/dist/src/StabilityDeposit.d.ts.map +1 -0
  40. package/dist/src/StabilityDeposit.js +80 -0
  41. package/dist/src/StabilityDeposit.js.map +1 -0
  42. package/dist/src/TransactableLiquity.d.ts +414 -0
  43. package/dist/src/TransactableLiquity.d.ts.map +1 -0
  44. package/dist/src/TransactableLiquity.js +18 -0
  45. package/dist/src/TransactableLiquity.js.map +1 -0
  46. package/dist/src/Trove.d.ts +367 -0
  47. package/dist/src/Trove.d.ts.map +1 -0
  48. package/dist/src/Trove.js +423 -0
  49. package/dist/src/Trove.js.map +1 -0
  50. package/dist/src/ZEROStake.d.ts +52 -0
  51. package/dist/src/ZEROStake.d.ts.map +1 -0
  52. package/dist/src/ZEROStake.js +74 -0
  53. package/dist/src/ZEROStake.js.map +1 -0
  54. package/dist/src/_CachedReadableLiquity.d.ts +55 -0
  55. package/dist/src/_CachedReadableLiquity.d.ts.map +1 -0
  56. package/dist/src/_CachedReadableLiquity.js +93 -0
  57. package/dist/src/_CachedReadableLiquity.js.map +1 -0
  58. package/dist/src/constants.d.ts +61 -0
  59. package/dist/src/constants.d.ts.map +1 -0
  60. package/dist/src/constants.js +64 -0
  61. package/dist/src/constants.js.map +1 -0
  62. package/dist/tsdoc-metadata.json +11 -0
  63. package/etc/lib-base.api.md +788 -0
  64. package/index.ts +13 -0
  65. package/package.json +52 -0
  66. package/src/Decimal.ts +456 -0
  67. package/src/Fees.ts +160 -0
  68. package/src/LiquityStore.ts +563 -0
  69. package/src/ObservableLiquity.ts +32 -0
  70. package/src/PopulatableLiquity.ts +280 -0
  71. package/src/ReadableLiquity.ts +175 -0
  72. package/src/SendableLiquity.ts +251 -0
  73. package/src/StabilityDeposit.ts +126 -0
  74. package/src/TransactableLiquity.ts +471 -0
  75. package/src/Trove.ts +824 -0
  76. package/src/ZEROStake.ts +99 -0
  77. package/src/_CachedReadableLiquity.ts +186 -0
  78. package/src/constants.ts +68 -0
  79. package/test/Decimal.test.ts +212 -0
  80. package/test/StabilityDeposit.test.ts +30 -0
  81. package/test/Trove.test.ts +143 -0
  82. package/test/ZEROStake.test.ts +24 -0
  83. package/tsconfig.dist.json +8 -0
  84. package/tsconfig.json +5 -0
@@ -0,0 +1,280 @@
1
+ import { Decimal, Decimalish } from "./Decimal";
2
+ import { TroveAdjustmentParams, TroveCreationParams } from "./Trove";
3
+ import { LiquityReceipt, SendableLiquity, SentLiquityTransaction } from "./SendableLiquity";
4
+
5
+ import {
6
+ CollateralGainTransferDetails,
7
+ LiquidationDetails,
8
+ RedemptionDetails,
9
+ StabilityDepositChangeDetails,
10
+ StabilityPoolGainsWithdrawalDetails,
11
+ TroveAdjustmentDetails,
12
+ TroveClosureDetails,
13
+ TroveCreationDetails
14
+ } from "./TransactableLiquity";
15
+
16
+ /**
17
+ * A transaction that has been prepared for sending.
18
+ *
19
+ * @remarks
20
+ * Implemented by {@link @sovryn-zero/lib-ethers#PopulatedEthersLiquityTransaction}.
21
+ *
22
+ * @public
23
+ */
24
+ export interface PopulatedLiquityTransaction<
25
+ P = unknown,
26
+ T extends SentLiquityTransaction = SentLiquityTransaction
27
+ > {
28
+ /** Implementation-specific populated transaction object. */
29
+ readonly rawPopulatedTransaction: P;
30
+
31
+ /**
32
+ * Send the transaction.
33
+ *
34
+ * @returns An object that implements {@link @sovryn-zero/lib-base#SentLiquityTransaction}.
35
+ */
36
+ send(): Promise<T>;
37
+ }
38
+
39
+ /**
40
+ * A redemption transaction that has been prepared for sending.
41
+ *
42
+ * @remarks
43
+ * The Zero protocol fulfills redemptions by repaying the debt of Troves in ascending order of
44
+ * their collateralization ratio, and taking a portion of their collateral in exchange. Due to the
45
+ * {@link @sovryn-zero/lib-base#ZUSD_MINIMUM_DEBT | minimum debt} requirement that Troves must fulfill,
46
+ * some ZUSD amounts are not possible to redeem exactly.
47
+ *
48
+ * When {@link @sovryn-zero/lib-base#PopulatableLiquity.redeemZUSD | redeemZUSD()} is called with an
49
+ * amount that can't be fully redeemed, the amount will be truncated (see the `redeemableZUSDAmount`
50
+ * property). When this happens, the redeemer can either redeem the truncated amount by sending the
51
+ * transaction unchanged, or prepare a new transaction by
52
+ * {@link @sovryn-zero/lib-base#PopulatedRedemption.increaseAmountByMinimumNetDebt | increasing the amount}
53
+ * to the next lowest possible value, which is the sum of the truncated amount and
54
+ * {@link @sovryn-zero/lib-base#ZUSD_MINIMUM_NET_DEBT}.
55
+ *
56
+ * @public
57
+ */
58
+ export interface PopulatedRedemption<P = unknown, S = unknown, R = unknown>
59
+ extends PopulatedLiquityTransaction<
60
+ P,
61
+ SentLiquityTransaction<S, LiquityReceipt<R, RedemptionDetails>>
62
+ > {
63
+ /** Amount of ZUSD the redeemer is trying to redeem. */
64
+ readonly attemptedZUSDAmount: Decimal;
65
+
66
+ /** Maximum amount of ZUSD that is currently redeemable from `attemptedZUSDAmount`. */
67
+ readonly redeemableZUSDAmount: Decimal;
68
+
69
+ /** Whether `redeemableZUSDAmount` is less than `attemptedZUSDAmount`. */
70
+ readonly isTruncated: boolean;
71
+
72
+ /**
73
+ * Prepare a new transaction by increasing the attempted amount to the next lowest redeemable
74
+ * value.
75
+ *
76
+ * @param maxRedemptionRate - Maximum acceptable
77
+ * {@link @sovryn-zero/lib-base#Fees.redemptionRate | redemption rate} to
78
+ * use in the new transaction.
79
+ *
80
+ * @remarks
81
+ * If `maxRedemptionRate` is omitted, the original transaction's `maxRedemptionRate` is reused
82
+ * unless that was also omitted, in which case the current redemption rate (based on the increased
83
+ * amount) plus 0.1% is used as maximum acceptable rate.
84
+ */
85
+ increaseAmountByMinimumNetDebt(
86
+ maxRedemptionRate?: Decimalish
87
+ ): Promise<PopulatedRedemption<P, S, R>>;
88
+ }
89
+
90
+ /** @internal */
91
+ export type _PopulatableFrom<T, P> = {
92
+ [M in keyof T]: T[M] extends (...args: infer A) => Promise<infer U>
93
+ ? U extends SentLiquityTransaction
94
+ ? (...args: A) => Promise<PopulatedLiquityTransaction<P, U>>
95
+ : never
96
+ : never;
97
+ };
98
+
99
+ /**
100
+ * Prepare Zero transactions for sending.
101
+ *
102
+ * @remarks
103
+ * The functions return an object implementing {@link PopulatedLiquityTransaction}, which can be
104
+ * used to send the transaction and get a {@link SentLiquityTransaction}.
105
+ *
106
+ * Implemented by {@link @sovryn-zero/lib-ethers#PopulatableEthersLiquity}.
107
+ *
108
+ * @public
109
+ */
110
+ export interface PopulatableLiquity<R = unknown, S = unknown, P = unknown>
111
+ extends _PopulatableFrom<SendableLiquity<R, S>, P> {
112
+ // Methods re-declared for documentation purposes
113
+
114
+ /** {@inheritDoc TransactableLiquity.openTrove} */
115
+ openTrove(
116
+ params: TroveCreationParams<Decimalish>,
117
+ maxBorrowingRate?: Decimalish
118
+ ): Promise<
119
+ PopulatedLiquityTransaction<
120
+ P,
121
+ SentLiquityTransaction<S, LiquityReceipt<R, TroveCreationDetails>>
122
+ >
123
+ >;
124
+
125
+ /** {@inheritDoc TransactableLiquity.closeTrove} */
126
+ closeTrove(): Promise<
127
+ PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, TroveClosureDetails>>>
128
+ >;
129
+
130
+ /** {@inheritDoc TransactableLiquity.adjustTrove} */
131
+ adjustTrove(
132
+ params: TroveAdjustmentParams<Decimalish>,
133
+ maxBorrowingRate?: Decimalish
134
+ ): Promise<
135
+ PopulatedLiquityTransaction<
136
+ P,
137
+ SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>
138
+ >
139
+ >;
140
+
141
+ /** {@inheritDoc TransactableLiquity.depositCollateral} */
142
+ depositCollateral(
143
+ amount: Decimalish
144
+ ): Promise<
145
+ PopulatedLiquityTransaction<
146
+ P,
147
+ SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>
148
+ >
149
+ >;
150
+
151
+ /** {@inheritDoc TransactableLiquity.withdrawCollateral} */
152
+ withdrawCollateral(
153
+ amount: Decimalish
154
+ ): Promise<
155
+ PopulatedLiquityTransaction<
156
+ P,
157
+ SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>
158
+ >
159
+ >;
160
+
161
+ /** {@inheritDoc TransactableLiquity.borrowZUSD} */
162
+ borrowZUSD(
163
+ amount: Decimalish,
164
+ maxBorrowingRate?: Decimalish
165
+ ): Promise<
166
+ PopulatedLiquityTransaction<
167
+ P,
168
+ SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>
169
+ >
170
+ >;
171
+
172
+ /** {@inheritDoc TransactableLiquity.repayZUSD} */
173
+ repayZUSD(
174
+ amount: Decimalish
175
+ ): Promise<
176
+ PopulatedLiquityTransaction<
177
+ P,
178
+ SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>
179
+ >
180
+ >;
181
+
182
+ /** @internal */
183
+ setPrice(
184
+ price: Decimalish
185
+ ): Promise<PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>>;
186
+
187
+ /** {@inheritDoc TransactableLiquity.liquidate} */
188
+ liquidate(
189
+ address: string | string[]
190
+ ): Promise<
191
+ PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, LiquidationDetails>>>
192
+ >;
193
+
194
+ /** {@inheritDoc TransactableLiquity.liquidateUpTo} */
195
+ liquidateUpTo(
196
+ maximumNumberOfTrovesToLiquidate: number
197
+ ): Promise<
198
+ PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, LiquidationDetails>>>
199
+ >;
200
+
201
+ /** {@inheritDoc TransactableLiquity.depositZUSDInStabilityPool} */
202
+ depositZUSDInStabilityPool(
203
+ amount: Decimalish,
204
+ frontendTag?: string
205
+ ): Promise<
206
+ PopulatedLiquityTransaction<
207
+ P,
208
+ SentLiquityTransaction<S, LiquityReceipt<R, StabilityDepositChangeDetails>>
209
+ >
210
+ >;
211
+
212
+ /** {@inheritDoc TransactableLiquity.withdrawZUSDFromStabilityPool} */
213
+ withdrawZUSDFromStabilityPool(
214
+ amount: Decimalish
215
+ ): Promise<
216
+ PopulatedLiquityTransaction<
217
+ P,
218
+ SentLiquityTransaction<S, LiquityReceipt<R, StabilityDepositChangeDetails>>
219
+ >
220
+ >;
221
+
222
+ /** {@inheritDoc TransactableLiquity.withdrawGainsFromStabilityPool} */
223
+ withdrawGainsFromStabilityPool(): Promise<
224
+ PopulatedLiquityTransaction<
225
+ P,
226
+ SentLiquityTransaction<S, LiquityReceipt<R, StabilityPoolGainsWithdrawalDetails>>
227
+ >
228
+ >;
229
+
230
+ /** {@inheritDoc TransactableLiquity.transferCollateralGainToTrove} */
231
+ transferCollateralGainToTrove(): Promise<
232
+ PopulatedLiquityTransaction<
233
+ P,
234
+ SentLiquityTransaction<S, LiquityReceipt<R, CollateralGainTransferDetails>>
235
+ >
236
+ >;
237
+
238
+ /** {@inheritDoc TransactableLiquity.sendZUSD} */
239
+ sendZUSD(
240
+ toAddress: string,
241
+ amount: Decimalish
242
+ ): Promise<PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>>;
243
+
244
+ /** {@inheritDoc TransactableLiquity.sendZERO} */
245
+ sendZERO(
246
+ toAddress: string,
247
+ amount: Decimalish
248
+ ): Promise<PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>>;
249
+
250
+ /** {@inheritDoc TransactableLiquity.redeemZUSD} */
251
+ redeemZUSD(
252
+ amount: Decimalish,
253
+ maxRedemptionRate?: Decimalish
254
+ ): Promise<PopulatedRedemption<P, S, R>>;
255
+
256
+ /** {@inheritDoc TransactableLiquity.claimCollateralSurplus} */
257
+ claimCollateralSurplus(): Promise<
258
+ PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>
259
+ >;
260
+
261
+ /** {@inheritDoc TransactableLiquity.stakeZERO} */
262
+ stakeZERO(
263
+ amount: Decimalish
264
+ ): Promise<PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>>;
265
+
266
+ /** {@inheritDoc TransactableLiquity.unstakeZERO} */
267
+ unstakeZERO(
268
+ amount: Decimalish
269
+ ): Promise<PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>>;
270
+
271
+ /** {@inheritDoc TransactableLiquity.withdrawGainsFromStaking} */
272
+ withdrawGainsFromStaking(): Promise<
273
+ PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>
274
+ >;
275
+
276
+ /** {@inheritDoc TransactableLiquity.registerFrontend} */
277
+ registerFrontend(
278
+ kickbackRate: Decimalish
279
+ ): Promise<PopulatedLiquityTransaction<P, SentLiquityTransaction<S, LiquityReceipt<R, void>>>>;
280
+ }
@@ -0,0 +1,175 @@
1
+ import { Decimal } from "./Decimal";
2
+ import { Trove, TroveWithPendingRedistribution, UserTrove } from "./Trove";
3
+ import { StabilityDeposit } from "./StabilityDeposit";
4
+ import { Fees } from "./Fees";
5
+ import { ZEROStake } from "./ZEROStake";
6
+
7
+ /**
8
+ * Represents whether an address has been registered as a Zero frontend.
9
+ *
10
+ * @remarks
11
+ * Returned by the {@link ReadableLiquity.getFrontendStatus | getFrontendStatus()} function.
12
+ *
13
+ * When `status` is `"registered"`, `kickbackRate` gives the frontend's kickback rate as a
14
+ * {@link Decimal} between 0 and 1.
15
+ *
16
+ * @public
17
+ */
18
+ export type FrontendStatus =
19
+ | { status: "unregistered" }
20
+ | { status: "registered"; kickbackRate: Decimal };
21
+
22
+ /**
23
+ * Parameters of the {@link ReadableLiquity.(getTroves:2) | getTroves()} function.
24
+ *
25
+ * @public
26
+ */
27
+ export interface TroveListingParams {
28
+ /** Number of Troves to retrieve. */
29
+ readonly first: number;
30
+
31
+ /** How the Troves should be sorted. */
32
+ readonly sortedBy: "ascendingCollateralRatio" | "descendingCollateralRatio";
33
+
34
+ /** Index of the first Trove to retrieve from the sorted list. */
35
+ readonly startingAt?: number;
36
+
37
+ /**
38
+ * When set to `true`, the retrieved Troves won't include the liquidation shares received since
39
+ * the last time they were directly modified.
40
+ *
41
+ * @remarks
42
+ * Changes the type of returned Troves to {@link TroveWithPendingRedistribution}.
43
+ */
44
+ readonly beforeRedistribution?: boolean;
45
+ }
46
+
47
+ /**
48
+ * Read the state of the Zero protocol.
49
+ *
50
+ * @remarks
51
+ * Implemented by {@link @sovryn-zero/lib-ethers#EthersLiquity}.
52
+ *
53
+ * @public
54
+ */
55
+ export interface ReadableLiquity {
56
+ /**
57
+ * Get the total collateral and debt per stake that has been liquidated through redistribution.
58
+ *
59
+ * @remarks
60
+ * Needed when dealing with instances of {@link @sovryn-zero/lib-base#TroveWithPendingRedistribution}.
61
+ */
62
+ getTotalRedistributed(): Promise<Trove>;
63
+
64
+ /**
65
+ * Get a Trove in its state after the last direct modification.
66
+ *
67
+ * @param address - Address that owns the Trove.
68
+ *
69
+ * @remarks
70
+ * The current state of a Trove can be fetched using
71
+ * {@link @sovryn-zero/lib-base#ReadableLiquity.getTrove | getTrove()}.
72
+ */
73
+ getTroveBeforeRedistribution(address?: string): Promise<TroveWithPendingRedistribution>;
74
+
75
+ /**
76
+ * Get the current state of a Trove.
77
+ *
78
+ * @param address - Address that owns the Trove.
79
+ */
80
+ getTrove(address?: string): Promise<UserTrove>;
81
+
82
+ /**
83
+ * Get number of Troves that are currently open.
84
+ */
85
+ getNumberOfTroves(): Promise<number>;
86
+
87
+ /**
88
+ * Get the current price of the native currency (e.g. Ether) in USD.
89
+ */
90
+ getPrice(): Promise<Decimal>;
91
+
92
+ /**
93
+ * Get the total amount of collateral and debt in the Zero system.
94
+ */
95
+ getTotal(): Promise<Trove>;
96
+
97
+ /**
98
+ * Get the current state of a Stability Deposit.
99
+ *
100
+ * @param address - Address that owns the Stability Deposit.
101
+ */
102
+ getStabilityDeposit(address?: string): Promise<StabilityDeposit>;
103
+
104
+ /**
105
+ * Get the remaining ZERO that will be collectively rewarded to stability depositors.
106
+ */
107
+ getRemainingStabilityPoolZEROReward(): Promise<Decimal>;
108
+
109
+ /**
110
+ * Get the total amount of ZUSD currently deposited in the Stability Pool.
111
+ */
112
+ getZUSDInStabilityPool(): Promise<Decimal>;
113
+
114
+ /**
115
+ * Get the amount of ZUSD held by an address.
116
+ *
117
+ * @param address - Address whose balance should be retrieved.
118
+ */
119
+ getZUSDBalance(address?: string): Promise<Decimal>;
120
+
121
+ /**
122
+ * Get the amount of ZERO held by an address.
123
+ *
124
+ * @param address - Address whose balance should be retrieved.
125
+ */
126
+ getZEROBalance(address?: string): Promise<Decimal>;
127
+
128
+ /**
129
+ * Get the amount of leftover collateral available for withdrawal by an address.
130
+ *
131
+ * @remarks
132
+ * When a Trove gets liquidated or redeemed, any collateral it has above 110% (in case of
133
+ * liquidation) or 100% collateralization (in case of redemption) gets sent to a pool, where it
134
+ * can be withdrawn from using
135
+ * {@link @sovryn-zero/lib-base#TransactableLiquity.claimCollateralSurplus | claimCollateralSurplus()}.
136
+ */
137
+ getCollateralSurplusBalance(address?: string): Promise<Decimal>;
138
+
139
+ /** @internal */
140
+ getTroves(
141
+ params: TroveListingParams & { beforeRedistribution: true }
142
+ ): Promise<TroveWithPendingRedistribution[]>;
143
+
144
+ /**
145
+ * Get a slice from the list of Troves.
146
+ *
147
+ * @param params - Controls how the list is sorted, and where the slice begins and ends.
148
+ * @returns Pairs of owner addresses and their Troves.
149
+ */
150
+ getTroves(params: TroveListingParams): Promise<UserTrove[]>;
151
+
152
+ /**
153
+ * Get a calculator for current fees.
154
+ */
155
+ getFees(): Promise<Fees>;
156
+
157
+ /**
158
+ * Get the current state of an ZERO Stake.
159
+ *
160
+ * @param address - Address that owns the ZERO Stake.
161
+ */
162
+ getZEROStake(address?: string): Promise<ZEROStake>;
163
+
164
+ /**
165
+ * Get the total amount of ZERO currently staked.
166
+ */
167
+ getTotalStakedZERO(): Promise<Decimal>;
168
+
169
+ /**
170
+ * Check whether an address is registered as a Zero frontend, and what its kickback rate is.
171
+ *
172
+ * @param address - Address to check.
173
+ */
174
+ getFrontendStatus(address?: string): Promise<FrontendStatus>;
175
+ }
@@ -0,0 +1,251 @@
1
+ import { Decimalish } from "./Decimal";
2
+ import { TroveAdjustmentParams, TroveCreationParams } from "./Trove";
3
+
4
+ import {
5
+ CollateralGainTransferDetails,
6
+ LiquidationDetails,
7
+ RedemptionDetails,
8
+ StabilityDepositChangeDetails,
9
+ StabilityPoolGainsWithdrawalDetails,
10
+ TransactableLiquity,
11
+ TroveAdjustmentDetails,
12
+ TroveClosureDetails,
13
+ TroveCreationDetails
14
+ } from "./TransactableLiquity";
15
+
16
+ /**
17
+ * A transaction that has already been sent.
18
+ *
19
+ * @remarks
20
+ * Implemented by {@link @sovryn-zero/lib-ethers#SentEthersLiquityTransaction}.
21
+ *
22
+ * @public
23
+ */
24
+ export interface SentLiquityTransaction<S = unknown, T extends LiquityReceipt = LiquityReceipt> {
25
+ /** Implementation-specific sent transaction object. */
26
+ readonly rawSentTransaction: S;
27
+
28
+ /**
29
+ * Check whether the transaction has been mined, and whether it was successful.
30
+ *
31
+ * @remarks
32
+ * Unlike {@link @sovryn-zero/lib-base#SentLiquityTransaction.waitForReceipt | waitForReceipt()},
33
+ * this function doesn't wait for the transaction to be mined.
34
+ */
35
+ getReceipt(): Promise<T>;
36
+
37
+ /**
38
+ * Wait for the transaction to be mined, and check whether it was successful.
39
+ *
40
+ * @returns Either a {@link @sovryn-zero/lib-base#FailedReceipt} or a
41
+ * {@link @sovryn-zero/lib-base#SuccessfulReceipt}.
42
+ */
43
+ waitForReceipt(): Promise<Extract<T, MinedReceipt>>;
44
+ }
45
+
46
+ /**
47
+ * Indicates that the transaction hasn't been mined yet.
48
+ *
49
+ * @remarks
50
+ * Returned by {@link SentLiquityTransaction.getReceipt}.
51
+ *
52
+ * @public
53
+ */
54
+ export type PendingReceipt = { status: "pending" };
55
+
56
+ /** @internal */
57
+ export const _pendingReceipt: PendingReceipt = { status: "pending" };
58
+
59
+ /**
60
+ * Indicates that the transaction has been mined, but it failed.
61
+ *
62
+ * @remarks
63
+ * The `rawReceipt` property is an implementation-specific transaction receipt object.
64
+ *
65
+ * Returned by {@link SentLiquityTransaction.getReceipt} and
66
+ * {@link SentLiquityTransaction.waitForReceipt}.
67
+ *
68
+ * @public
69
+ */
70
+ export type FailedReceipt<R = unknown> = { status: "failed"; rawReceipt: R };
71
+
72
+ /** @internal */
73
+ export const _failedReceipt = <R>(rawReceipt: R): FailedReceipt<R> => ({
74
+ status: "failed",
75
+ rawReceipt
76
+ });
77
+
78
+ /**
79
+ * Indicates that the transaction has succeeded.
80
+ *
81
+ * @remarks
82
+ * The `rawReceipt` property is an implementation-specific transaction receipt object.
83
+ *
84
+ * The `details` property may contain more information about the transaction.
85
+ * See the return types of {@link TransactableLiquity} functions for the exact contents of `details`
86
+ * for each type of Zero transaction.
87
+ *
88
+ * Returned by {@link SentLiquityTransaction.getReceipt} and
89
+ * {@link SentLiquityTransaction.waitForReceipt}.
90
+ *
91
+ * @public
92
+ */
93
+ export type SuccessfulReceipt<R = unknown, D = unknown> = {
94
+ status: "succeeded";
95
+ rawReceipt: R;
96
+ details: D;
97
+ };
98
+
99
+ /** @internal */
100
+ export const _successfulReceipt = <R, D>(
101
+ rawReceipt: R,
102
+ details: D,
103
+ toString?: () => string
104
+ ): SuccessfulReceipt<R, D> => ({
105
+ status: "succeeded",
106
+ rawReceipt,
107
+ details,
108
+ ...(toString ? { toString } : {})
109
+ });
110
+
111
+ /**
112
+ * Either a {@link FailedReceipt} or a {@link SuccessfulReceipt}.
113
+ *
114
+ * @public
115
+ */
116
+ export type MinedReceipt<R = unknown, D = unknown> = FailedReceipt<R> | SuccessfulReceipt<R, D>;
117
+
118
+ /**
119
+ * One of either a {@link PendingReceipt}, a {@link FailedReceipt} or a {@link SuccessfulReceipt}.
120
+ *
121
+ * @public
122
+ */
123
+ export type LiquityReceipt<R = unknown, D = unknown> = PendingReceipt | MinedReceipt<R, D>;
124
+
125
+ /** @internal */
126
+ export type _SendableFrom<T, R, S> = {
127
+ [M in keyof T]: T[M] extends (...args: infer A) => Promise<infer D>
128
+ ? (...args: A) => Promise<SentLiquityTransaction<S, LiquityReceipt<R, D>>>
129
+ : never;
130
+ };
131
+
132
+ /**
133
+ * Send Zero transactions.
134
+ *
135
+ * @remarks
136
+ * The functions return an object implementing {@link SentLiquityTransaction}, which can be used
137
+ * to monitor the transaction and get its details when it succeeds.
138
+ *
139
+ * Implemented by {@link @sovryn-zero/lib-ethers#SendableEthersLiquity}.
140
+ *
141
+ * @public
142
+ */
143
+ export interface SendableLiquity<R = unknown, S = unknown>
144
+ extends _SendableFrom<TransactableLiquity, R, S> {
145
+ // Methods re-declared for documentation purposes
146
+
147
+ /** {@inheritDoc TransactableLiquity.openTrove} */
148
+ openTrove(
149
+ params: TroveCreationParams<Decimalish>,
150
+ maxBorrowingRate?: Decimalish
151
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveCreationDetails>>>;
152
+
153
+ /** {@inheritDoc TransactableLiquity.closeTrove} */
154
+ closeTrove(): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveClosureDetails>>>;
155
+
156
+ /** {@inheritDoc TransactableLiquity.adjustTrove} */
157
+ adjustTrove(
158
+ params: TroveAdjustmentParams<Decimalish>,
159
+ maxBorrowingRate?: Decimalish
160
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>>;
161
+
162
+ /** {@inheritDoc TransactableLiquity.depositCollateral} */
163
+ depositCollateral(
164
+ amount: Decimalish
165
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>>;
166
+
167
+ /** {@inheritDoc TransactableLiquity.withdrawCollateral} */
168
+ withdrawCollateral(
169
+ amount: Decimalish
170
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>>;
171
+
172
+ /** {@inheritDoc TransactableLiquity.borrowZUSD} */
173
+ borrowZUSD(
174
+ amount: Decimalish,
175
+ maxBorrowingRate?: Decimalish
176
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>>;
177
+
178
+ /** {@inheritDoc TransactableLiquity.repayZUSD} */
179
+ repayZUSD(
180
+ amount: Decimalish
181
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, TroveAdjustmentDetails>>>;
182
+
183
+ /** @internal */
184
+ setPrice(price: Decimalish): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
185
+
186
+ /** {@inheritDoc TransactableLiquity.liquidate} */
187
+ liquidate(
188
+ address: string | string[]
189
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, LiquidationDetails>>>;
190
+
191
+ /** {@inheritDoc TransactableLiquity.liquidateUpTo} */
192
+ liquidateUpTo(
193
+ maximumNumberOfTrovesToLiquidate: number
194
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, LiquidationDetails>>>;
195
+
196
+ /** {@inheritDoc TransactableLiquity.depositZUSDInStabilityPool} */
197
+ depositZUSDInStabilityPool(
198
+ amount: Decimalish,
199
+ frontendTag?: string
200
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, StabilityDepositChangeDetails>>>;
201
+
202
+ /** {@inheritDoc TransactableLiquity.withdrawZUSDFromStabilityPool} */
203
+ withdrawZUSDFromStabilityPool(
204
+ amount: Decimalish
205
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, StabilityDepositChangeDetails>>>;
206
+
207
+ /** {@inheritDoc TransactableLiquity.withdrawGainsFromStabilityPool} */
208
+ withdrawGainsFromStabilityPool(): Promise<
209
+ SentLiquityTransaction<S, LiquityReceipt<R, StabilityPoolGainsWithdrawalDetails>>
210
+ >;
211
+
212
+ /** {@inheritDoc TransactableLiquity.transferCollateralGainToTrove} */
213
+ transferCollateralGainToTrove(): Promise<
214
+ SentLiquityTransaction<S, LiquityReceipt<R, CollateralGainTransferDetails>>
215
+ >;
216
+
217
+ /** {@inheritDoc TransactableLiquity.sendZUSD} */
218
+ sendZUSD(
219
+ toAddress: string,
220
+ amount: Decimalish
221
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
222
+
223
+ /** {@inheritDoc TransactableLiquity.sendZERO} */
224
+ sendZERO(
225
+ toAddress: string,
226
+ amount: Decimalish
227
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
228
+
229
+ /** {@inheritDoc TransactableLiquity.redeemZUSD} */
230
+ redeemZUSD(
231
+ amount: Decimalish,
232
+ maxRedemptionRate?: Decimalish
233
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, RedemptionDetails>>>;
234
+
235
+ /** {@inheritDoc TransactableLiquity.claimCollateralSurplus} */
236
+ claimCollateralSurplus(): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
237
+
238
+ /** {@inheritDoc TransactableLiquity.stakeZERO} */
239
+ stakeZERO(amount: Decimalish): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
240
+
241
+ /** {@inheritDoc TransactableLiquity.unstakeZERO} */
242
+ unstakeZERO(amount: Decimalish): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
243
+
244
+ /** {@inheritDoc TransactableLiquity.withdrawGainsFromStaking} */
245
+ withdrawGainsFromStaking(): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
246
+
247
+ /** {@inheritDoc TransactableLiquity.registerFrontend} */
248
+ registerFrontend(
249
+ kickbackRate: Decimalish
250
+ ): Promise<SentLiquityTransaction<S, LiquityReceipt<R, void>>>;
251
+ }