@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,143 @@
1
+ import assert from "assert";
2
+ import { describe, it } from "mocha";
3
+ import fc from "fast-check";
4
+
5
+ import {
6
+ ZUSD_LIQUIDATION_RESERVE,
7
+ ZUSD_MINIMUM_DEBT,
8
+ MAXIMUM_BORROWING_RATE
9
+ } from "../src/constants";
10
+
11
+ import { Decimal, Difference } from "../src/Decimal";
12
+ import { Trove, _emptyTrove } from "../src/Trove";
13
+
14
+ const liquidationReserve = Number(ZUSD_LIQUIDATION_RESERVE);
15
+ const maximumBorrowingRate = Number(MAXIMUM_BORROWING_RATE);
16
+
17
+ const maxDebt = 10 * Number(ZUSD_MINIMUM_DEBT);
18
+
19
+ const trove = ({ collateral = 0, debt = 0 }) =>
20
+ new Trove(Decimal.from(collateral), Decimal.from(debt));
21
+
22
+ const onlyCollateral = () => fc.record({ collateral: fc.float({ min: 0.1 }) }).map(trove);
23
+
24
+ const onlyDebt = () =>
25
+ fc.record({ debt: fc.float({ min: liquidationReserve, max: maxDebt }) }).map(trove);
26
+
27
+ const bothCollateralAndDebt = () =>
28
+ fc
29
+ .record({
30
+ collateral: fc.float({ min: 0.1 }),
31
+ debt: fc.float({ min: liquidationReserve, max: maxDebt })
32
+ })
33
+ .map(trove);
34
+
35
+ const arbitraryTrove = () => fc.record({ collateral: fc.float(), debt: fc.float() }).map(trove);
36
+
37
+ const validTrove = () =>
38
+ fc
39
+ .record({ collateral: fc.float(), debt: fc.float({ min: liquidationReserve, max: maxDebt }) })
40
+ .map(trove);
41
+
42
+ const validNonEmptyTrove = () => validTrove().filter(t => !t.isEmpty);
43
+
44
+ const roughlyEqual = (a: Trove, b: Trove) =>
45
+ a.collateral.eq(b.collateral) && !!Difference.between(a.debt, b.debt).absoluteValue?.lt(1e-9);
46
+
47
+ describe("Trove", () => {
48
+ it("applying undefined diff should yield the same Trove", () => {
49
+ const trove = new Trove(Decimal.from(1), Decimal.from(111));
50
+
51
+ assert(trove.apply(undefined) === trove);
52
+ });
53
+
54
+ it("applying diff of empty from `b` to `a` should yield empty", () => {
55
+ fc.assert(
56
+ fc.property(validNonEmptyTrove(), validNonEmptyTrove(), (a, b) =>
57
+ a.apply(b.whatChanged(_emptyTrove)).equals(_emptyTrove)
58
+ )
59
+ );
60
+ });
61
+
62
+ it("applying what changed should preserve zeroings", () => {
63
+ fc.assert(
64
+ fc.property(
65
+ arbitraryTrove(),
66
+ bothCollateralAndDebt(),
67
+ onlyCollateral(),
68
+ (a, b, c) => a.apply(b.whatChanged(c)).debt.isZero
69
+ )
70
+ );
71
+
72
+ fc.assert(
73
+ fc.property(
74
+ arbitraryTrove(),
75
+ bothCollateralAndDebt(),
76
+ onlyDebt(),
77
+ (a, b, c) => a.apply(b.whatChanged(c)).collateral.isZero
78
+ )
79
+ );
80
+ });
81
+
82
+ it("applying diff of `b` from `a` to `a` should yield `b` when borrowing rate is 0", () => {
83
+ fc.assert(
84
+ fc.property(validTrove(), arbitraryTrove(), (a, b) =>
85
+ a.apply(a.whatChanged(b, 0), 0).equals(b)
86
+ )
87
+ );
88
+ });
89
+
90
+ it("applying diff of `b` from `a` to `a` should roughly yield `b` when borrowing rate is non-0", () => {
91
+ fc.assert(
92
+ fc.property(validTrove(), arbitraryTrove(), fc.float({ max: 0.5 }), (a, b, c) =>
93
+ roughlyEqual(a.apply(a.whatChanged(b, c), c), b)
94
+ )
95
+ );
96
+ });
97
+
98
+ it("applying an adjustment should never throw", () => {
99
+ fc.assert(
100
+ fc.property(validNonEmptyTrove(), validNonEmptyTrove(), validNonEmptyTrove(), (a, b, c) => {
101
+ a.apply(b.whatChanged(c));
102
+ })
103
+ );
104
+ });
105
+
106
+ describe("whatChanged()", () => {
107
+ it("should not define zeros on adjustment", () => {
108
+ fc.assert(
109
+ fc.property(validNonEmptyTrove(), validNonEmptyTrove(), (a, b) => {
110
+ const change = a.whatChanged(b);
111
+
112
+ return (
113
+ change === undefined ||
114
+ (change.type === "adjustment" &&
115
+ !change.params.depositCollateral?.isZero &&
116
+ !change.params.withdrawCollateral?.isZero &&
117
+ !change.params.borrowZUSD?.isZero &&
118
+ !change.params.repayZUSD?.isZero)
119
+ );
120
+ })
121
+ );
122
+ });
123
+
124
+ it("should recreate a Trove with minimum debt at any borrowing rate", () => {
125
+ fc.assert(
126
+ fc.property(fc.float({ max: maximumBorrowingRate }), borrowingRate => {
127
+ const withMinimumDebt = Trove.recreate(
128
+ new Trove(Decimal.ONE, ZUSD_MINIMUM_DEBT),
129
+ borrowingRate
130
+ );
131
+
132
+ const ret = Trove.create(withMinimumDebt, borrowingRate).debt.gte(ZUSD_MINIMUM_DEBT);
133
+
134
+ if (!ret) {
135
+ console.log(`${Trove.create(withMinimumDebt, borrowingRate).debt}`);
136
+ }
137
+
138
+ return ret;
139
+ })
140
+ );
141
+ });
142
+ });
143
+ });
@@ -0,0 +1,24 @@
1
+ import { describe, it } from "mocha";
2
+ import fc from "fast-check";
3
+
4
+ import { Decimal } from "../src/Decimal";
5
+ import { ZEROStake } from "../src/ZEROStake";
6
+
7
+ const arbitraryStake = () =>
8
+ fc
9
+ .tuple(fc.float(), fc.float(), fc.float())
10
+ .map(([a, b, c]) => new ZEROStake(Decimal.from(a), Decimal.from(b), Decimal.from(c)));
11
+
12
+ const nonZeroStake = () => arbitraryStake().filter(({ stakedZERO }) => !stakedZERO.isZero);
13
+
14
+ describe("ZEROStake", () => {
15
+ it("applying diff of `b` from `a` to `a` should always yield `b`", () => {
16
+ fc.assert(fc.property(arbitraryStake(), fc.float(), (a, b) => a.apply(a.whatChanged(b)).eq(b)));
17
+ });
18
+
19
+ it("applying what changed should preserve zeroing", () => {
20
+ fc.assert(
21
+ fc.property(arbitraryStake(), nonZeroStake(), (a, b) => a.apply(b.whatChanged(0)).eq(0))
22
+ );
23
+ });
24
+ });
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist"
5
+ },
6
+ "include": ["src"],
7
+ "files": ["./index.ts"]
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": ["src", "test"],
4
+ "compilerOptions": { "noErrorTruncation": true }
5
+ }