@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.
- package/.eslintrc.json +17 -0
- package/.mocharc.yml +1 -0
- package/LICENSE +905 -0
- package/README.md +23 -0
- package/api-extractor.json +4 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/src/Decimal.d.ts +89 -0
- package/dist/src/Decimal.d.ts.map +1 -0
- package/dist/src/Decimal.js +361 -0
- package/dist/src/Decimal.js.map +1 -0
- package/dist/src/Fees.d.ts +82 -0
- package/dist/src/Fees.d.ts.map +1 -0
- package/dist/src/Fees.js +123 -0
- package/dist/src/Fees.js.map +1 -0
- package/dist/src/LiquityStore.d.ts +209 -0
- package/dist/src/LiquityStore.d.ts.map +1 -0
- package/dist/src/LiquityStore.js +209 -0
- package/dist/src/LiquityStore.js.map +1 -0
- package/dist/src/ObservableLiquity.d.ts +15 -0
- package/dist/src/ObservableLiquity.d.ts.map +1 -0
- package/dist/src/ObservableLiquity.js +3 -0
- package/dist/src/ObservableLiquity.js.map +1 -0
- package/dist/src/PopulatableLiquity.d.ts +125 -0
- package/dist/src/PopulatableLiquity.d.ts.map +1 -0
- package/dist/src/PopulatableLiquity.js +3 -0
- package/dist/src/PopulatableLiquity.js.map +1 -0
- package/dist/src/ReadableLiquity.d.ts +156 -0
- package/dist/src/ReadableLiquity.d.ts.map +1 -0
- package/dist/src/ReadableLiquity.js +3 -0
- package/dist/src/ReadableLiquity.js.map +1 -0
- package/dist/src/SendableLiquity.d.ts +156 -0
- package/dist/src/SendableLiquity.d.ts.map +1 -0
- package/dist/src/SendableLiquity.js +20 -0
- package/dist/src/SendableLiquity.js.map +1 -0
- package/dist/src/StabilityDeposit.d.ts +59 -0
- package/dist/src/StabilityDeposit.d.ts.map +1 -0
- package/dist/src/StabilityDeposit.js +80 -0
- package/dist/src/StabilityDeposit.js.map +1 -0
- package/dist/src/TransactableLiquity.d.ts +414 -0
- package/dist/src/TransactableLiquity.d.ts.map +1 -0
- package/dist/src/TransactableLiquity.js +18 -0
- package/dist/src/TransactableLiquity.js.map +1 -0
- package/dist/src/Trove.d.ts +367 -0
- package/dist/src/Trove.d.ts.map +1 -0
- package/dist/src/Trove.js +423 -0
- package/dist/src/Trove.js.map +1 -0
- package/dist/src/ZEROStake.d.ts +52 -0
- package/dist/src/ZEROStake.d.ts.map +1 -0
- package/dist/src/ZEROStake.js +74 -0
- package/dist/src/ZEROStake.js.map +1 -0
- package/dist/src/_CachedReadableLiquity.d.ts +55 -0
- package/dist/src/_CachedReadableLiquity.d.ts.map +1 -0
- package/dist/src/_CachedReadableLiquity.js +93 -0
- package/dist/src/_CachedReadableLiquity.js.map +1 -0
- package/dist/src/constants.d.ts +61 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +64 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/etc/lib-base.api.md +788 -0
- package/index.ts +13 -0
- package/package.json +52 -0
- package/src/Decimal.ts +456 -0
- package/src/Fees.ts +160 -0
- package/src/LiquityStore.ts +563 -0
- package/src/ObservableLiquity.ts +32 -0
- package/src/PopulatableLiquity.ts +280 -0
- package/src/ReadableLiquity.ts +175 -0
- package/src/SendableLiquity.ts +251 -0
- package/src/StabilityDeposit.ts +126 -0
- package/src/TransactableLiquity.ts +471 -0
- package/src/Trove.ts +824 -0
- package/src/ZEROStake.ts +99 -0
- package/src/_CachedReadableLiquity.ts +186 -0
- package/src/constants.ts +68 -0
- package/test/Decimal.test.ts +212 -0
- package/test/StabilityDeposit.test.ts +30 -0
- package/test/Trove.test.ts +143 -0
- package/test/ZEROStake.test.ts +24 -0
- package/tsconfig.dist.json +8 -0
- package/tsconfig.json +5 -0
package/index.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
export * from "./src/constants";
|
2
|
+
export * from "./src/Decimal";
|
3
|
+
export * from "./src/Trove";
|
4
|
+
export * from "./src/StabilityDeposit";
|
5
|
+
export * from "./src/ZEROStake";
|
6
|
+
export * from "./src/Fees";
|
7
|
+
export * from "./src/ReadableLiquity";
|
8
|
+
export * from "./src/ObservableLiquity";
|
9
|
+
export * from "./src/TransactableLiquity";
|
10
|
+
export * from "./src/SendableLiquity";
|
11
|
+
export * from "./src/PopulatableLiquity";
|
12
|
+
export * from "./src/LiquityStore";
|
13
|
+
export * from "./src/_CachedReadableLiquity";
|
package/package.json
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
{
|
2
|
+
"name": "@sovryn-zero/lib-base",
|
3
|
+
"version": "0.1.0",
|
4
|
+
"description": "Sovryn Zero SDK shared interfaces",
|
5
|
+
"keywords": [
|
6
|
+
"Sovryn",
|
7
|
+
"Zero",
|
8
|
+
"SDK",
|
9
|
+
"DeFi",
|
10
|
+
"blockchain",
|
11
|
+
"finance",
|
12
|
+
"BTC",
|
13
|
+
"bitcoin",
|
14
|
+
"RSK",
|
15
|
+
"rootstock"
|
16
|
+
],
|
17
|
+
"homepage": "https://github.com/DistributedCollective/zero#readme",
|
18
|
+
"license": "MIT",
|
19
|
+
"author": "Sovryn",
|
20
|
+
"main": "dist/index.js",
|
21
|
+
"types": "dist/index.d.ts",
|
22
|
+
"repository": "https://github.com/DistributedCollective/zero",
|
23
|
+
"private": false,
|
24
|
+
"publishConfig": {
|
25
|
+
"access": "public",
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
27
|
+
},
|
28
|
+
"scripts": {
|
29
|
+
"prepare": "run-s prepare:*",
|
30
|
+
"prepare:eslint": "eslint src",
|
31
|
+
"prepare:tsc": "tsc --project tsconfig.dist.json",
|
32
|
+
"prepare:api": "api-extractor run --local",
|
33
|
+
"test": "mocha --require ts-node/register",
|
34
|
+
"do-pack": "yarn prepare && yarn pack"
|
35
|
+
},
|
36
|
+
"dependencies": {
|
37
|
+
"@ethersproject/bignumber": "5.0.15"
|
38
|
+
},
|
39
|
+
"devDependencies": {
|
40
|
+
"@microsoft/api-extractor": "7.13.2",
|
41
|
+
"@types/mocha": "8.2.1",
|
42
|
+
"@typescript-eslint/eslint-plugin": "4.17.0",
|
43
|
+
"@typescript-eslint/parser": "4.18.0",
|
44
|
+
"eslint": "7.22.0",
|
45
|
+
"eslint-plugin-tsdoc": "0.2.11",
|
46
|
+
"fast-check": "2.13.0",
|
47
|
+
"mocha": "8.3.2",
|
48
|
+
"npm-run-all": "4.1.5",
|
49
|
+
"ts-node": "9.1.1",
|
50
|
+
"typescript": "4.1.5"
|
51
|
+
}
|
52
|
+
}
|
package/src/Decimal.ts
ADDED
@@ -0,0 +1,456 @@
|
|
1
|
+
import assert from "assert";
|
2
|
+
|
3
|
+
import { BigNumber } from "@ethersproject/bignumber";
|
4
|
+
|
5
|
+
const getDigits = (numDigits: number) => TEN.pow(numDigits);
|
6
|
+
|
7
|
+
const MAX_UINT_256 = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
8
|
+
const PRECISION = 18;
|
9
|
+
const ONE = BigNumber.from(1);
|
10
|
+
const TEN = BigNumber.from(10);
|
11
|
+
const DIGITS = getDigits(PRECISION);
|
12
|
+
|
13
|
+
const stringRepresentationFormat = /^[0-9]*(\.[0-9]*)?(e[-+]?[0-9]+)?$/;
|
14
|
+
const trailingZeros = /0*$/;
|
15
|
+
const magnitudes = ["", "K", "M", "B", "T"];
|
16
|
+
|
17
|
+
const roundedMul = (x: BigNumber, y: BigNumber) => x.mul(y).add(Decimal.HALF.hex).div(DIGITS);
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Types that can be converted into a Decimal.
|
21
|
+
*
|
22
|
+
* @public
|
23
|
+
*/
|
24
|
+
export type Decimalish = Decimal | number | string;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Fixed-point decimal bignumber with 18 digits of precision.
|
28
|
+
*
|
29
|
+
* @remarks
|
30
|
+
* Used by Zero libraries to precisely represent native currency (e.g. Ether), ZUSD and ZERO
|
31
|
+
* amounts, as well as derived metrics like collateral ratios.
|
32
|
+
*
|
33
|
+
* @public
|
34
|
+
*/
|
35
|
+
export class Decimal {
|
36
|
+
static readonly INFINITY = Decimal.fromBigNumberString(MAX_UINT_256);
|
37
|
+
static readonly ZERO = Decimal.from(0);
|
38
|
+
static readonly HALF = Decimal.from(0.5);
|
39
|
+
static readonly ONE = Decimal.from(1);
|
40
|
+
|
41
|
+
private readonly _bigNumber: BigNumber;
|
42
|
+
|
43
|
+
/** @internal */
|
44
|
+
get hex(): string {
|
45
|
+
return this._bigNumber.toHexString();
|
46
|
+
}
|
47
|
+
|
48
|
+
/** @internal */
|
49
|
+
get bigNumber(): string {
|
50
|
+
return this._bigNumber.toString();
|
51
|
+
}
|
52
|
+
|
53
|
+
private constructor(bigNumber: BigNumber) {
|
54
|
+
if (bigNumber.isNegative()) {
|
55
|
+
throw new Error("negatives not supported by Decimal");
|
56
|
+
}
|
57
|
+
|
58
|
+
this._bigNumber = bigNumber;
|
59
|
+
}
|
60
|
+
|
61
|
+
static fromBigNumberString(bigNumberString: string): Decimal {
|
62
|
+
return new Decimal(BigNumber.from(bigNumberString));
|
63
|
+
}
|
64
|
+
|
65
|
+
private static _fromString(representation: string): Decimal {
|
66
|
+
if (!representation || !representation.match(stringRepresentationFormat)) {
|
67
|
+
throw new Error(`bad decimal format: "${representation}"`);
|
68
|
+
}
|
69
|
+
|
70
|
+
if (representation.includes("e")) {
|
71
|
+
// eslint-disable-next-line prefer-const
|
72
|
+
let [coefficient, exponent] = representation.split("e");
|
73
|
+
|
74
|
+
if (exponent.startsWith("-")) {
|
75
|
+
return new Decimal(
|
76
|
+
Decimal._fromString(coefficient)._bigNumber.div(
|
77
|
+
TEN.pow(BigNumber.from(exponent.substr(1)))
|
78
|
+
)
|
79
|
+
);
|
80
|
+
}
|
81
|
+
|
82
|
+
if (exponent.startsWith("+")) {
|
83
|
+
exponent = exponent.substr(1);
|
84
|
+
}
|
85
|
+
|
86
|
+
return new Decimal(
|
87
|
+
Decimal._fromString(coefficient)._bigNumber.mul(TEN.pow(BigNumber.from(exponent)))
|
88
|
+
);
|
89
|
+
}
|
90
|
+
|
91
|
+
if (!representation.includes(".")) {
|
92
|
+
return new Decimal(BigNumber.from(representation).mul(DIGITS));
|
93
|
+
}
|
94
|
+
|
95
|
+
// eslint-disable-next-line prefer-const
|
96
|
+
let [characteristic, mantissa] = representation.split(".");
|
97
|
+
|
98
|
+
if (mantissa.length < PRECISION) {
|
99
|
+
mantissa += "0".repeat(PRECISION - mantissa.length);
|
100
|
+
} else {
|
101
|
+
mantissa = mantissa.substr(0, PRECISION);
|
102
|
+
}
|
103
|
+
|
104
|
+
return new Decimal(
|
105
|
+
BigNumber.from(characteristic || 0)
|
106
|
+
.mul(DIGITS)
|
107
|
+
.add(mantissa)
|
108
|
+
);
|
109
|
+
}
|
110
|
+
|
111
|
+
static from(decimalish: Decimalish): Decimal {
|
112
|
+
switch (typeof decimalish) {
|
113
|
+
case "object":
|
114
|
+
if (decimalish instanceof Decimal) {
|
115
|
+
return decimalish;
|
116
|
+
} else {
|
117
|
+
throw new Error("invalid Decimalish value");
|
118
|
+
}
|
119
|
+
case "string":
|
120
|
+
return Decimal._fromString(decimalish);
|
121
|
+
case "number":
|
122
|
+
return Decimal._fromString(decimalish.toString());
|
123
|
+
default:
|
124
|
+
throw new Error("invalid Decimalish value");
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
private _toStringWithAutomaticPrecision() {
|
129
|
+
const characteristic = this._bigNumber.div(DIGITS);
|
130
|
+
const mantissa = this._bigNumber.mod(DIGITS);
|
131
|
+
|
132
|
+
if (mantissa.isZero()) {
|
133
|
+
return characteristic.toString();
|
134
|
+
} else {
|
135
|
+
const paddedMantissa = mantissa.toString().padStart(PRECISION, "0");
|
136
|
+
const trimmedMantissa = paddedMantissa.replace(trailingZeros, "");
|
137
|
+
return characteristic.toString() + "." + trimmedMantissa;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
private _roundUp(precision: number) {
|
142
|
+
const halfDigit = getDigits(PRECISION - 1 - precision).mul(5);
|
143
|
+
return this._bigNumber.add(halfDigit);
|
144
|
+
}
|
145
|
+
|
146
|
+
private _toStringWithPrecision(precision: number) {
|
147
|
+
if (precision < 0) {
|
148
|
+
throw new Error("precision must not be negative");
|
149
|
+
}
|
150
|
+
|
151
|
+
const value = precision < PRECISION ? this._roundUp(precision) : this._bigNumber;
|
152
|
+
const characteristic = value.div(DIGITS);
|
153
|
+
const mantissa = value.mod(DIGITS);
|
154
|
+
|
155
|
+
if (precision === 0) {
|
156
|
+
return characteristic.toString();
|
157
|
+
} else {
|
158
|
+
const paddedMantissa = mantissa.toString().padStart(PRECISION, "0");
|
159
|
+
const trimmedMantissa = paddedMantissa.substr(0, precision);
|
160
|
+
return characteristic.toString() + "." + trimmedMantissa;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
toString(precision?: number): string {
|
165
|
+
if (this.infinite) {
|
166
|
+
return "∞";
|
167
|
+
} else if (precision !== undefined) {
|
168
|
+
return this._toStringWithPrecision(precision);
|
169
|
+
} else {
|
170
|
+
return this._toStringWithAutomaticPrecision();
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
prettify(precision = 2): string {
|
175
|
+
const [characteristic, mantissa] = parseFloat(this.toString(precision)).toString().split(".");
|
176
|
+
const prettyCharacteristic = characteristic.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
|
177
|
+
|
178
|
+
return mantissa !== undefined ? prettyCharacteristic + "." + mantissa : prettyCharacteristic;
|
179
|
+
}
|
180
|
+
|
181
|
+
shorten(): string {
|
182
|
+
const characteristicLength = this.toString(0).length;
|
183
|
+
const magnitude = Math.min(Math.floor((characteristicLength - 1) / 3), magnitudes.length - 1);
|
184
|
+
|
185
|
+
const precision = Math.max(3 * (magnitude + 1) - characteristicLength, 0);
|
186
|
+
const normalized = this.div(new Decimal(getDigits(PRECISION + 3 * magnitude)));
|
187
|
+
|
188
|
+
return normalized.prettify(precision) + magnitudes[magnitude];
|
189
|
+
}
|
190
|
+
|
191
|
+
add(addend: Decimalish): Decimal {
|
192
|
+
return new Decimal(this._bigNumber.add(Decimal.from(addend)._bigNumber));
|
193
|
+
}
|
194
|
+
|
195
|
+
sub(subtrahend: Decimalish): Decimal {
|
196
|
+
return new Decimal(this._bigNumber.sub(Decimal.from(subtrahend)._bigNumber));
|
197
|
+
}
|
198
|
+
|
199
|
+
mul(multiplier: Decimalish): Decimal {
|
200
|
+
return new Decimal(this._bigNumber.mul(Decimal.from(multiplier)._bigNumber).div(DIGITS));
|
201
|
+
}
|
202
|
+
|
203
|
+
div(divider: Decimalish): Decimal {
|
204
|
+
divider = Decimal.from(divider);
|
205
|
+
|
206
|
+
if (divider.isZero) {
|
207
|
+
return Decimal.INFINITY;
|
208
|
+
}
|
209
|
+
|
210
|
+
return new Decimal(this._bigNumber.mul(DIGITS).div(divider._bigNumber));
|
211
|
+
}
|
212
|
+
|
213
|
+
/** @internal */
|
214
|
+
_divCeil(divider: Decimalish): Decimal {
|
215
|
+
divider = Decimal.from(divider);
|
216
|
+
|
217
|
+
if (divider.isZero) {
|
218
|
+
return Decimal.INFINITY;
|
219
|
+
}
|
220
|
+
|
221
|
+
return new Decimal(
|
222
|
+
this._bigNumber.mul(DIGITS).add(divider._bigNumber.sub(ONE)).div(divider._bigNumber)
|
223
|
+
);
|
224
|
+
}
|
225
|
+
|
226
|
+
mulDiv(multiplier: Decimalish, divider: Decimalish): Decimal {
|
227
|
+
multiplier = Decimal.from(multiplier);
|
228
|
+
divider = Decimal.from(divider);
|
229
|
+
|
230
|
+
if (divider.isZero) {
|
231
|
+
return Decimal.INFINITY;
|
232
|
+
}
|
233
|
+
|
234
|
+
return new Decimal(this._bigNumber.mul(multiplier._bigNumber).div(divider._bigNumber));
|
235
|
+
}
|
236
|
+
|
237
|
+
pow(exponent: number): Decimal {
|
238
|
+
assert(Number.isInteger(exponent));
|
239
|
+
assert(0 <= exponent && exponent <= 0xffffffff); // Ensure we're safe to use bitwise ops
|
240
|
+
|
241
|
+
if (exponent === 0) {
|
242
|
+
return Decimal.ONE;
|
243
|
+
}
|
244
|
+
|
245
|
+
if (exponent === 1) {
|
246
|
+
return this;
|
247
|
+
}
|
248
|
+
|
249
|
+
let x = this._bigNumber;
|
250
|
+
let y = DIGITS;
|
251
|
+
|
252
|
+
for (; exponent > 1; exponent >>>= 1) {
|
253
|
+
if (exponent & 1) {
|
254
|
+
y = roundedMul(x, y);
|
255
|
+
}
|
256
|
+
|
257
|
+
x = roundedMul(x, x);
|
258
|
+
}
|
259
|
+
|
260
|
+
return new Decimal(roundedMul(x, y));
|
261
|
+
}
|
262
|
+
|
263
|
+
get isZero(): boolean {
|
264
|
+
return this._bigNumber.isZero();
|
265
|
+
}
|
266
|
+
|
267
|
+
get zero(): this | undefined {
|
268
|
+
if (this.isZero) {
|
269
|
+
return this;
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
get nonZero(): this | undefined {
|
274
|
+
if (!this.isZero) {
|
275
|
+
return this;
|
276
|
+
}
|
277
|
+
}
|
278
|
+
|
279
|
+
get infinite(): this | undefined {
|
280
|
+
if (this.eq(Decimal.INFINITY)) {
|
281
|
+
return this;
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
285
|
+
get finite(): this | undefined {
|
286
|
+
if (!this.eq(Decimal.INFINITY)) {
|
287
|
+
return this;
|
288
|
+
}
|
289
|
+
}
|
290
|
+
|
291
|
+
/** @internal */
|
292
|
+
get absoluteValue(): this {
|
293
|
+
return this;
|
294
|
+
}
|
295
|
+
|
296
|
+
lt(that: Decimalish): boolean {
|
297
|
+
return this._bigNumber.lt(Decimal.from(that)._bigNumber);
|
298
|
+
}
|
299
|
+
|
300
|
+
eq(that: Decimalish): boolean {
|
301
|
+
return this._bigNumber.eq(Decimal.from(that)._bigNumber);
|
302
|
+
}
|
303
|
+
|
304
|
+
gt(that: Decimalish): boolean {
|
305
|
+
return this._bigNumber.gt(Decimal.from(that)._bigNumber);
|
306
|
+
}
|
307
|
+
|
308
|
+
gte(that: Decimalish): boolean {
|
309
|
+
return this._bigNumber.gte(Decimal.from(that)._bigNumber);
|
310
|
+
}
|
311
|
+
|
312
|
+
lte(that: Decimalish): boolean {
|
313
|
+
return this._bigNumber.lte(Decimal.from(that)._bigNumber);
|
314
|
+
}
|
315
|
+
|
316
|
+
static min(a: Decimalish, b: Decimalish): Decimal {
|
317
|
+
a = Decimal.from(a);
|
318
|
+
b = Decimal.from(b);
|
319
|
+
|
320
|
+
return a.lt(b) ? a : b;
|
321
|
+
}
|
322
|
+
|
323
|
+
static max(a: Decimalish, b: Decimalish): Decimal {
|
324
|
+
a = Decimal.from(a);
|
325
|
+
b = Decimal.from(b);
|
326
|
+
|
327
|
+
return a.gt(b) ? a : b;
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
type DifferenceRepresentation = { sign: "" | "+" | "-"; absoluteValue: Decimal };
|
332
|
+
|
333
|
+
/** @alpha */
|
334
|
+
export class Difference {
|
335
|
+
private _number?: DifferenceRepresentation;
|
336
|
+
|
337
|
+
private constructor(number?: DifferenceRepresentation) {
|
338
|
+
this._number = number;
|
339
|
+
}
|
340
|
+
|
341
|
+
static between(d1: Decimalish | undefined, d2: Decimalish | undefined): Difference {
|
342
|
+
if (d1 === undefined || d2 === undefined) {
|
343
|
+
return new Difference(undefined);
|
344
|
+
}
|
345
|
+
|
346
|
+
d1 = Decimal.from(d1);
|
347
|
+
d2 = Decimal.from(d2);
|
348
|
+
|
349
|
+
if (d1.infinite && d2.infinite) {
|
350
|
+
return new Difference(undefined);
|
351
|
+
} else if (d1.infinite) {
|
352
|
+
return new Difference({ sign: "+", absoluteValue: d1 });
|
353
|
+
} else if (d2.infinite) {
|
354
|
+
return new Difference({ sign: "-", absoluteValue: d2 });
|
355
|
+
} else if (d1.gt(d2)) {
|
356
|
+
return new Difference({ sign: "+", absoluteValue: Decimal.from(d1).sub(d2) });
|
357
|
+
} else if (d2.gt(d1)) {
|
358
|
+
return new Difference({ sign: "-", absoluteValue: Decimal.from(d2).sub(d1) });
|
359
|
+
} else {
|
360
|
+
return new Difference({ sign: "", absoluteValue: Decimal.ZERO });
|
361
|
+
}
|
362
|
+
}
|
363
|
+
|
364
|
+
toString(precision?: number): string {
|
365
|
+
if (!this._number) {
|
366
|
+
return "N/A";
|
367
|
+
}
|
368
|
+
|
369
|
+
return this._number.sign + this._number.absoluteValue.toString(precision);
|
370
|
+
}
|
371
|
+
|
372
|
+
prettify(precision?: number): string {
|
373
|
+
if (!this._number) {
|
374
|
+
return this.toString();
|
375
|
+
}
|
376
|
+
|
377
|
+
return this._number.sign + this._number.absoluteValue.prettify(precision);
|
378
|
+
}
|
379
|
+
|
380
|
+
mul(multiplier: Decimalish): Difference {
|
381
|
+
return new Difference(
|
382
|
+
this._number && {
|
383
|
+
sign: this._number.sign,
|
384
|
+
absoluteValue: this._number.absoluteValue.mul(multiplier)
|
385
|
+
}
|
386
|
+
);
|
387
|
+
}
|
388
|
+
|
389
|
+
get nonZero(): this | undefined {
|
390
|
+
return this._number?.absoluteValue.nonZero && this;
|
391
|
+
}
|
392
|
+
|
393
|
+
get positive(): this | undefined {
|
394
|
+
return this._number?.sign === "+" ? this : undefined;
|
395
|
+
}
|
396
|
+
|
397
|
+
get negative(): this | undefined {
|
398
|
+
return this._number?.sign === "-" ? this : undefined;
|
399
|
+
}
|
400
|
+
|
401
|
+
get absoluteValue(): Decimal | undefined {
|
402
|
+
return this._number?.absoluteValue;
|
403
|
+
}
|
404
|
+
|
405
|
+
get infinite(): this | undefined {
|
406
|
+
return this._number?.absoluteValue.infinite && this;
|
407
|
+
}
|
408
|
+
|
409
|
+
get finite(): this | undefined {
|
410
|
+
return this._number?.absoluteValue.finite && this;
|
411
|
+
}
|
412
|
+
}
|
413
|
+
|
414
|
+
/** @alpha */
|
415
|
+
export class Percent<
|
416
|
+
T extends {
|
417
|
+
infinite?: T | undefined;
|
418
|
+
absoluteValue?: A | undefined;
|
419
|
+
mul?(hundred: 100): T;
|
420
|
+
toString(precision?: number): string;
|
421
|
+
},
|
422
|
+
A extends {
|
423
|
+
gte(n: string): boolean;
|
424
|
+
}
|
425
|
+
> {
|
426
|
+
private _percent: T;
|
427
|
+
|
428
|
+
public constructor(ratio: T) {
|
429
|
+
this._percent = ratio.infinite || (ratio.mul && ratio.mul(100)) || ratio;
|
430
|
+
}
|
431
|
+
|
432
|
+
nonZeroish(precision: number): this | undefined {
|
433
|
+
const zeroish = `0.${"0".repeat(precision)}5`;
|
434
|
+
|
435
|
+
if (this._percent.absoluteValue?.gte(zeroish)) {
|
436
|
+
return this;
|
437
|
+
}
|
438
|
+
}
|
439
|
+
|
440
|
+
toString(precision: number): string {
|
441
|
+
return (
|
442
|
+
this._percent.toString(precision) +
|
443
|
+
(this._percent.absoluteValue && !this._percent.infinite ? "%" : "")
|
444
|
+
);
|
445
|
+
}
|
446
|
+
|
447
|
+
prettify(): string {
|
448
|
+
if (this._percent.absoluteValue?.gte("1000")) {
|
449
|
+
return this.toString(0);
|
450
|
+
} else if (this._percent.absoluteValue?.gte("10")) {
|
451
|
+
return this.toString(1);
|
452
|
+
} else {
|
453
|
+
return this.toString(2);
|
454
|
+
}
|
455
|
+
}
|
456
|
+
}
|
package/src/Fees.ts
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
import assert from "assert";
|
2
|
+
|
3
|
+
import { Decimal, Decimalish } from "./Decimal";
|
4
|
+
|
5
|
+
import {
|
6
|
+
MAXIMUM_BORROWING_RATE,
|
7
|
+
MINIMUM_BORROWING_RATE,
|
8
|
+
MINIMUM_REDEMPTION_RATE
|
9
|
+
} from "./constants";
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Calculator for fees.
|
13
|
+
*
|
14
|
+
* @remarks
|
15
|
+
* Returned by the {@link ReadableLiquity.getFees | getFees()} function.
|
16
|
+
*
|
17
|
+
* @public
|
18
|
+
*/
|
19
|
+
export class Fees {
|
20
|
+
private readonly _baseRateWithoutDecay: Decimal;
|
21
|
+
private readonly _minuteDecayFactor: Decimal;
|
22
|
+
private readonly _beta: Decimal;
|
23
|
+
private readonly _lastFeeOperation: Date;
|
24
|
+
private readonly _timeOfLatestBlock: Date;
|
25
|
+
private readonly _recoveryMode: boolean;
|
26
|
+
|
27
|
+
/** @internal */
|
28
|
+
constructor(
|
29
|
+
baseRateWithoutDecay: Decimalish,
|
30
|
+
minuteDecayFactor: Decimalish,
|
31
|
+
beta: Decimalish,
|
32
|
+
lastFeeOperation: Date,
|
33
|
+
timeOfLatestBlock: Date,
|
34
|
+
recoveryMode: boolean
|
35
|
+
) {
|
36
|
+
this._baseRateWithoutDecay = Decimal.from(baseRateWithoutDecay);
|
37
|
+
this._minuteDecayFactor = Decimal.from(minuteDecayFactor);
|
38
|
+
this._beta = Decimal.from(beta);
|
39
|
+
this._lastFeeOperation = lastFeeOperation;
|
40
|
+
this._timeOfLatestBlock = timeOfLatestBlock;
|
41
|
+
this._recoveryMode = recoveryMode;
|
42
|
+
|
43
|
+
assert(this._minuteDecayFactor.lt(1));
|
44
|
+
}
|
45
|
+
|
46
|
+
/** @internal */
|
47
|
+
_setRecoveryMode(recoveryMode: boolean): Fees {
|
48
|
+
return new Fees(
|
49
|
+
this._baseRateWithoutDecay,
|
50
|
+
this._minuteDecayFactor,
|
51
|
+
this._beta,
|
52
|
+
this._lastFeeOperation,
|
53
|
+
this._timeOfLatestBlock,
|
54
|
+
recoveryMode
|
55
|
+
);
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
* Compare to another instance of `Fees`.
|
60
|
+
*/
|
61
|
+
equals(that: Fees): boolean {
|
62
|
+
return (
|
63
|
+
this._baseRateWithoutDecay.eq(that._baseRateWithoutDecay) &&
|
64
|
+
this._minuteDecayFactor.eq(that._minuteDecayFactor) &&
|
65
|
+
this._beta.eq(that._beta) &&
|
66
|
+
this._lastFeeOperation.getTime() === that._lastFeeOperation.getTime() &&
|
67
|
+
this._timeOfLatestBlock.getTime() === that._timeOfLatestBlock.getTime() &&
|
68
|
+
this._recoveryMode === that._recoveryMode
|
69
|
+
);
|
70
|
+
}
|
71
|
+
|
72
|
+
/** @internal */
|
73
|
+
toString(): string {
|
74
|
+
return (
|
75
|
+
`{ baseRateWithoutDecay: ${this._baseRateWithoutDecay}` +
|
76
|
+
`, lastFeeOperation: "${this._lastFeeOperation.toLocaleString()}"` +
|
77
|
+
`, recoveryMode: ${this._recoveryMode} } `
|
78
|
+
);
|
79
|
+
}
|
80
|
+
|
81
|
+
/** @internal */
|
82
|
+
baseRate(when = this._timeOfLatestBlock): Decimal {
|
83
|
+
const millisecondsSinceLastFeeOperation = Math.max(
|
84
|
+
when.getTime() - this._lastFeeOperation.getTime(),
|
85
|
+
0 // Clamp negative elapsed time to 0, in case the client's time is in the past.
|
86
|
+
// We will calculate slightly higher than actual fees, which is fine.
|
87
|
+
);
|
88
|
+
|
89
|
+
const minutesSinceLastFeeOperation = Math.floor(millisecondsSinceLastFeeOperation / 60000);
|
90
|
+
|
91
|
+
return this._minuteDecayFactor.pow(minutesSinceLastFeeOperation).mul(this._baseRateWithoutDecay);
|
92
|
+
}
|
93
|
+
|
94
|
+
/**
|
95
|
+
* Calculate the current borrowing rate.
|
96
|
+
*
|
97
|
+
* @param when - Optional timestamp that can be used to calculate what the borrowing rate would
|
98
|
+
* decay to at a point of time in the future.
|
99
|
+
*
|
100
|
+
* @remarks
|
101
|
+
* By default, the fee is calculated at the time of the latest block. This can be overridden using
|
102
|
+
* the `when` parameter.
|
103
|
+
*
|
104
|
+
* To calculate the borrowing fee in ZUSD, multiply the borrowed ZUSD amount by the borrowing rate.
|
105
|
+
*
|
106
|
+
* @example
|
107
|
+
* ```typescript
|
108
|
+
* const fees = await zero.getFees();
|
109
|
+
*
|
110
|
+
* const borrowedZUSDAmount = 100;
|
111
|
+
* const borrowingRate = fees.borrowingRate();
|
112
|
+
* const borrowingFeeZUSD = borrowingRate.mul(borrowedZUSDAmount);
|
113
|
+
* ```
|
114
|
+
*/
|
115
|
+
borrowingRate(when?: Date): Decimal {
|
116
|
+
return this._recoveryMode
|
117
|
+
? Decimal.ZERO
|
118
|
+
: Decimal.min(MINIMUM_BORROWING_RATE.add(this.baseRate(when)), MAXIMUM_BORROWING_RATE);
|
119
|
+
}
|
120
|
+
|
121
|
+
/**
|
122
|
+
* Calculate the current redemption rate.
|
123
|
+
*
|
124
|
+
* @param redeemedFractionOfSupply - The amount of ZUSD being redeemed divided by the total supply.
|
125
|
+
* @param when - Optional timestamp that can be used to calculate what the redemption rate would
|
126
|
+
* decay to at a point of time in the future.
|
127
|
+
*
|
128
|
+
* @remarks
|
129
|
+
* By default, the fee is calculated at the time of the latest block. This can be overridden using
|
130
|
+
* the `when` parameter.
|
131
|
+
|
132
|
+
* Unlike the borrowing rate, the redemption rate depends on the amount being redeemed. To be more
|
133
|
+
* precise, it depends on the fraction of the redeemed amount compared to the total ZUSD supply,
|
134
|
+
* which must be passed as a parameter.
|
135
|
+
*
|
136
|
+
* To calculate the redemption fee in ZUSD, multiply the redeemed ZUSD amount with the redemption
|
137
|
+
* rate.
|
138
|
+
*
|
139
|
+
* @example
|
140
|
+
* ```typescript
|
141
|
+
* const fees = await zero.getFees();
|
142
|
+
* const total = await zero.getTotal();
|
143
|
+
*
|
144
|
+
* const redeemedZUSDAmount = Decimal.from(100);
|
145
|
+
* const redeemedFractionOfSupply = redeemedZUSDAmount.div(total.debt);
|
146
|
+
* const redemptionRate = fees.redemptionRate(redeemedFractionOfSupply);
|
147
|
+
* const redemptionFeeZUSD = redemptionRate.mul(redeemedZUSDAmount);
|
148
|
+
* ```
|
149
|
+
*/
|
150
|
+
redemptionRate(redeemedFractionOfSupply: Decimalish = Decimal.ZERO, when?: Date): Decimal {
|
151
|
+
redeemedFractionOfSupply = Decimal.from(redeemedFractionOfSupply);
|
152
|
+
let baseRate = this.baseRate(when);
|
153
|
+
|
154
|
+
if (redeemedFractionOfSupply.nonZero) {
|
155
|
+
baseRate = redeemedFractionOfSupply.div(this._beta).add(baseRate);
|
156
|
+
}
|
157
|
+
|
158
|
+
return Decimal.min(MINIMUM_REDEMPTION_RATE.add(baseRate), Decimal.ONE);
|
159
|
+
}
|
160
|
+
}
|