@sovryn-zero/lib-base 0.1.0 → 0.2.1

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,82 +1,82 @@
1
- import { Decimal, Decimalish } from "./Decimal";
2
- /**
3
- * Calculator for fees.
4
- *
5
- * @remarks
6
- * Returned by the {@link ReadableLiquity.getFees | getFees()} function.
7
- *
8
- * @public
9
- */
10
- export declare class Fees {
11
- private readonly _baseRateWithoutDecay;
12
- private readonly _minuteDecayFactor;
13
- private readonly _beta;
14
- private readonly _lastFeeOperation;
15
- private readonly _timeOfLatestBlock;
16
- private readonly _recoveryMode;
17
- /** @internal */
18
- constructor(baseRateWithoutDecay: Decimalish, minuteDecayFactor: Decimalish, beta: Decimalish, lastFeeOperation: Date, timeOfLatestBlock: Date, recoveryMode: boolean);
19
- /** @internal */
20
- _setRecoveryMode(recoveryMode: boolean): Fees;
21
- /**
22
- * Compare to another instance of `Fees`.
23
- */
24
- equals(that: Fees): boolean;
25
- /** @internal */
26
- toString(): string;
27
- /** @internal */
28
- baseRate(when?: Date): Decimal;
29
- /**
30
- * Calculate the current borrowing rate.
31
- *
32
- * @param when - Optional timestamp that can be used to calculate what the borrowing rate would
33
- * decay to at a point of time in the future.
34
- *
35
- * @remarks
36
- * By default, the fee is calculated at the time of the latest block. This can be overridden using
37
- * the `when` parameter.
38
- *
39
- * To calculate the borrowing fee in ZUSD, multiply the borrowed ZUSD amount by the borrowing rate.
40
- *
41
- * @example
42
- * ```typescript
43
- * const fees = await zero.getFees();
44
- *
45
- * const borrowedZUSDAmount = 100;
46
- * const borrowingRate = fees.borrowingRate();
47
- * const borrowingFeeZUSD = borrowingRate.mul(borrowedZUSDAmount);
48
- * ```
49
- */
50
- borrowingRate(when?: Date): Decimal;
51
- /**
52
- * Calculate the current redemption rate.
53
- *
54
- * @param redeemedFractionOfSupply - The amount of ZUSD being redeemed divided by the total supply.
55
- * @param when - Optional timestamp that can be used to calculate what the redemption rate would
56
- * decay to at a point of time in the future.
57
- *
58
- * @remarks
59
- * By default, the fee is calculated at the time of the latest block. This can be overridden using
60
- * the `when` parameter.
61
-
62
- * Unlike the borrowing rate, the redemption rate depends on the amount being redeemed. To be more
63
- * precise, it depends on the fraction of the redeemed amount compared to the total ZUSD supply,
64
- * which must be passed as a parameter.
65
- *
66
- * To calculate the redemption fee in ZUSD, multiply the redeemed ZUSD amount with the redemption
67
- * rate.
68
- *
69
- * @example
70
- * ```typescript
71
- * const fees = await zero.getFees();
72
- * const total = await zero.getTotal();
73
- *
74
- * const redeemedZUSDAmount = Decimal.from(100);
75
- * const redeemedFractionOfSupply = redeemedZUSDAmount.div(total.debt);
76
- * const redemptionRate = fees.redemptionRate(redeemedFractionOfSupply);
77
- * const redemptionFeeZUSD = redemptionRate.mul(redeemedZUSDAmount);
78
- * ```
79
- */
80
- redemptionRate(redeemedFractionOfSupply?: Decimalish, when?: Date): Decimal;
81
- }
1
+ import { Decimal, Decimalish } from "./Decimal";
2
+ /**
3
+ * Calculator for fees.
4
+ *
5
+ * @remarks
6
+ * Returned by the {@link ReadableLiquity.getFees | getFees()} function.
7
+ *
8
+ * @public
9
+ */
10
+ export declare class Fees {
11
+ private readonly _baseRateWithoutDecay;
12
+ private readonly _minuteDecayFactor;
13
+ private readonly _beta;
14
+ private readonly _lastFeeOperation;
15
+ private readonly _timeOfLatestBlock;
16
+ private readonly _recoveryMode;
17
+ /** @internal */
18
+ constructor(baseRateWithoutDecay: Decimalish, minuteDecayFactor: Decimalish, beta: Decimalish, lastFeeOperation: Date, timeOfLatestBlock: Date, recoveryMode: boolean);
19
+ /** @internal */
20
+ _setRecoveryMode(recoveryMode: boolean): Fees;
21
+ /**
22
+ * Compare to another instance of `Fees`.
23
+ */
24
+ equals(that: Fees): boolean;
25
+ /** @internal */
26
+ toString(): string;
27
+ /** @internal */
28
+ baseRate(when?: Date): Decimal;
29
+ /**
30
+ * Calculate the current borrowing rate.
31
+ *
32
+ * @param when - Optional timestamp that can be used to calculate what the borrowing rate would
33
+ * decay to at a point of time in the future.
34
+ *
35
+ * @remarks
36
+ * By default, the fee is calculated at the time of the latest block. This can be overridden using
37
+ * the `when` parameter.
38
+ *
39
+ * To calculate the borrowing fee in ZUSD, multiply the borrowed ZUSD amount by the borrowing rate.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const fees = await zero.getFees();
44
+ *
45
+ * const borrowedZUSDAmount = 100;
46
+ * const borrowingRate = fees.borrowingRate();
47
+ * const borrowingFeeZUSD = borrowingRate.mul(borrowedZUSDAmount);
48
+ * ```
49
+ */
50
+ borrowingRate(when?: Date): Decimal;
51
+ /**
52
+ * Calculate the current redemption rate.
53
+ *
54
+ * @param redeemedFractionOfSupply - The amount of ZUSD being redeemed divided by the total supply.
55
+ * @param when - Optional timestamp that can be used to calculate what the redemption rate would
56
+ * decay to at a point of time in the future.
57
+ *
58
+ * @remarks
59
+ * By default, the fee is calculated at the time of the latest block. This can be overridden using
60
+ * the `when` parameter.
61
+
62
+ * Unlike the borrowing rate, the redemption rate depends on the amount being redeemed. To be more
63
+ * precise, it depends on the fraction of the redeemed amount compared to the total ZUSD supply,
64
+ * which must be passed as a parameter.
65
+ *
66
+ * To calculate the redemption fee in ZUSD, multiply the redeemed ZUSD amount with the redemption
67
+ * rate.
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const fees = await zero.getFees();
72
+ * const total = await zero.getTotal();
73
+ *
74
+ * const redeemedZUSDAmount = Decimal.from(100);
75
+ * const redeemedFractionOfSupply = redeemedZUSDAmount.div(total.debt);
76
+ * const redemptionRate = fees.redemptionRate(redeemedFractionOfSupply);
77
+ * const redemptionFeeZUSD = redemptionRate.mul(redeemedZUSDAmount);
78
+ * ```
79
+ */
80
+ redemptionRate(redeemedFractionOfSupply?: Decimalish, when?: Date): Decimal;
81
+ }
82
82
  //# sourceMappingURL=Fees.d.ts.map
package/dist/src/Fees.js CHANGED
@@ -1,123 +1,123 @@
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.Fees = void 0;
7
- const assert_1 = __importDefault(require("assert"));
8
- const Decimal_1 = require("./Decimal");
9
- const constants_1 = require("./constants");
10
- /**
11
- * Calculator for fees.
12
- *
13
- * @remarks
14
- * Returned by the {@link ReadableLiquity.getFees | getFees()} function.
15
- *
16
- * @public
17
- */
18
- class Fees {
19
- /** @internal */
20
- constructor(baseRateWithoutDecay, minuteDecayFactor, beta, lastFeeOperation, timeOfLatestBlock, recoveryMode) {
21
- this._baseRateWithoutDecay = Decimal_1.Decimal.from(baseRateWithoutDecay);
22
- this._minuteDecayFactor = Decimal_1.Decimal.from(minuteDecayFactor);
23
- this._beta = Decimal_1.Decimal.from(beta);
24
- this._lastFeeOperation = lastFeeOperation;
25
- this._timeOfLatestBlock = timeOfLatestBlock;
26
- this._recoveryMode = recoveryMode;
27
- assert_1.default(this._minuteDecayFactor.lt(1));
28
- }
29
- /** @internal */
30
- _setRecoveryMode(recoveryMode) {
31
- return new Fees(this._baseRateWithoutDecay, this._minuteDecayFactor, this._beta, this._lastFeeOperation, this._timeOfLatestBlock, recoveryMode);
32
- }
33
- /**
34
- * Compare to another instance of `Fees`.
35
- */
36
- equals(that) {
37
- return (this._baseRateWithoutDecay.eq(that._baseRateWithoutDecay) &&
38
- this._minuteDecayFactor.eq(that._minuteDecayFactor) &&
39
- this._beta.eq(that._beta) &&
40
- this._lastFeeOperation.getTime() === that._lastFeeOperation.getTime() &&
41
- this._timeOfLatestBlock.getTime() === that._timeOfLatestBlock.getTime() &&
42
- this._recoveryMode === that._recoveryMode);
43
- }
44
- /** @internal */
45
- toString() {
46
- return (`{ baseRateWithoutDecay: ${this._baseRateWithoutDecay}` +
47
- `, lastFeeOperation: "${this._lastFeeOperation.toLocaleString()}"` +
48
- `, recoveryMode: ${this._recoveryMode} } `);
49
- }
50
- /** @internal */
51
- baseRate(when = this._timeOfLatestBlock) {
52
- const millisecondsSinceLastFeeOperation = Math.max(when.getTime() - this._lastFeeOperation.getTime(), 0 // Clamp negative elapsed time to 0, in case the client's time is in the past.
53
- // We will calculate slightly higher than actual fees, which is fine.
54
- );
55
- const minutesSinceLastFeeOperation = Math.floor(millisecondsSinceLastFeeOperation / 60000);
56
- return this._minuteDecayFactor.pow(minutesSinceLastFeeOperation).mul(this._baseRateWithoutDecay);
57
- }
58
- /**
59
- * Calculate the current borrowing rate.
60
- *
61
- * @param when - Optional timestamp that can be used to calculate what the borrowing rate would
62
- * decay to at a point of time in the future.
63
- *
64
- * @remarks
65
- * By default, the fee is calculated at the time of the latest block. This can be overridden using
66
- * the `when` parameter.
67
- *
68
- * To calculate the borrowing fee in ZUSD, multiply the borrowed ZUSD amount by the borrowing rate.
69
- *
70
- * @example
71
- * ```typescript
72
- * const fees = await zero.getFees();
73
- *
74
- * const borrowedZUSDAmount = 100;
75
- * const borrowingRate = fees.borrowingRate();
76
- * const borrowingFeeZUSD = borrowingRate.mul(borrowedZUSDAmount);
77
- * ```
78
- */
79
- borrowingRate(when) {
80
- return this._recoveryMode
81
- ? Decimal_1.Decimal.ZERO
82
- : Decimal_1.Decimal.min(constants_1.MINIMUM_BORROWING_RATE.add(this.baseRate(when)), constants_1.MAXIMUM_BORROWING_RATE);
83
- }
84
- /**
85
- * Calculate the current redemption rate.
86
- *
87
- * @param redeemedFractionOfSupply - The amount of ZUSD being redeemed divided by the total supply.
88
- * @param when - Optional timestamp that can be used to calculate what the redemption rate would
89
- * decay to at a point of time in the future.
90
- *
91
- * @remarks
92
- * By default, the fee is calculated at the time of the latest block. This can be overridden using
93
- * the `when` parameter.
94
-
95
- * Unlike the borrowing rate, the redemption rate depends on the amount being redeemed. To be more
96
- * precise, it depends on the fraction of the redeemed amount compared to the total ZUSD supply,
97
- * which must be passed as a parameter.
98
- *
99
- * To calculate the redemption fee in ZUSD, multiply the redeemed ZUSD amount with the redemption
100
- * rate.
101
- *
102
- * @example
103
- * ```typescript
104
- * const fees = await zero.getFees();
105
- * const total = await zero.getTotal();
106
- *
107
- * const redeemedZUSDAmount = Decimal.from(100);
108
- * const redeemedFractionOfSupply = redeemedZUSDAmount.div(total.debt);
109
- * const redemptionRate = fees.redemptionRate(redeemedFractionOfSupply);
110
- * const redemptionFeeZUSD = redemptionRate.mul(redeemedZUSDAmount);
111
- * ```
112
- */
113
- redemptionRate(redeemedFractionOfSupply = Decimal_1.Decimal.ZERO, when) {
114
- redeemedFractionOfSupply = Decimal_1.Decimal.from(redeemedFractionOfSupply);
115
- let baseRate = this.baseRate(when);
116
- if (redeemedFractionOfSupply.nonZero) {
117
- baseRate = redeemedFractionOfSupply.div(this._beta).add(baseRate);
118
- }
119
- return Decimal_1.Decimal.min(constants_1.MINIMUM_REDEMPTION_RATE.add(baseRate), Decimal_1.Decimal.ONE);
120
- }
121
- }
122
- exports.Fees = Fees;
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.Fees = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ const Decimal_1 = require("./Decimal");
9
+ const constants_1 = require("./constants");
10
+ /**
11
+ * Calculator for fees.
12
+ *
13
+ * @remarks
14
+ * Returned by the {@link ReadableLiquity.getFees | getFees()} function.
15
+ *
16
+ * @public
17
+ */
18
+ class Fees {
19
+ /** @internal */
20
+ constructor(baseRateWithoutDecay, minuteDecayFactor, beta, lastFeeOperation, timeOfLatestBlock, recoveryMode) {
21
+ this._baseRateWithoutDecay = Decimal_1.Decimal.from(baseRateWithoutDecay);
22
+ this._minuteDecayFactor = Decimal_1.Decimal.from(minuteDecayFactor);
23
+ this._beta = Decimal_1.Decimal.from(beta);
24
+ this._lastFeeOperation = lastFeeOperation;
25
+ this._timeOfLatestBlock = timeOfLatestBlock;
26
+ this._recoveryMode = recoveryMode;
27
+ assert_1.default(this._minuteDecayFactor.lt(1));
28
+ }
29
+ /** @internal */
30
+ _setRecoveryMode(recoveryMode) {
31
+ return new Fees(this._baseRateWithoutDecay, this._minuteDecayFactor, this._beta, this._lastFeeOperation, this._timeOfLatestBlock, recoveryMode);
32
+ }
33
+ /**
34
+ * Compare to another instance of `Fees`.
35
+ */
36
+ equals(that) {
37
+ return (this._baseRateWithoutDecay.eq(that._baseRateWithoutDecay) &&
38
+ this._minuteDecayFactor.eq(that._minuteDecayFactor) &&
39
+ this._beta.eq(that._beta) &&
40
+ this._lastFeeOperation.getTime() === that._lastFeeOperation.getTime() &&
41
+ this._timeOfLatestBlock.getTime() === that._timeOfLatestBlock.getTime() &&
42
+ this._recoveryMode === that._recoveryMode);
43
+ }
44
+ /** @internal */
45
+ toString() {
46
+ return (`{ baseRateWithoutDecay: ${this._baseRateWithoutDecay}` +
47
+ `, lastFeeOperation: "${this._lastFeeOperation.toLocaleString()}"` +
48
+ `, recoveryMode: ${this._recoveryMode} } `);
49
+ }
50
+ /** @internal */
51
+ baseRate(when = this._timeOfLatestBlock) {
52
+ const millisecondsSinceLastFeeOperation = Math.max(when.getTime() - this._lastFeeOperation.getTime(), 0 // Clamp negative elapsed time to 0, in case the client's time is in the past.
53
+ // We will calculate slightly higher than actual fees, which is fine.
54
+ );
55
+ const minutesSinceLastFeeOperation = Math.floor(millisecondsSinceLastFeeOperation / 60000);
56
+ return this._minuteDecayFactor.pow(minutesSinceLastFeeOperation).mul(this._baseRateWithoutDecay);
57
+ }
58
+ /**
59
+ * Calculate the current borrowing rate.
60
+ *
61
+ * @param when - Optional timestamp that can be used to calculate what the borrowing rate would
62
+ * decay to at a point of time in the future.
63
+ *
64
+ * @remarks
65
+ * By default, the fee is calculated at the time of the latest block. This can be overridden using
66
+ * the `when` parameter.
67
+ *
68
+ * To calculate the borrowing fee in ZUSD, multiply the borrowed ZUSD amount by the borrowing rate.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const fees = await zero.getFees();
73
+ *
74
+ * const borrowedZUSDAmount = 100;
75
+ * const borrowingRate = fees.borrowingRate();
76
+ * const borrowingFeeZUSD = borrowingRate.mul(borrowedZUSDAmount);
77
+ * ```
78
+ */
79
+ borrowingRate(when) {
80
+ return this._recoveryMode
81
+ ? Decimal_1.Decimal.ZERO
82
+ : Decimal_1.Decimal.min(constants_1.MINIMUM_BORROWING_RATE.add(this.baseRate(when)), constants_1.MAXIMUM_BORROWING_RATE);
83
+ }
84
+ /**
85
+ * Calculate the current redemption rate.
86
+ *
87
+ * @param redeemedFractionOfSupply - The amount of ZUSD being redeemed divided by the total supply.
88
+ * @param when - Optional timestamp that can be used to calculate what the redemption rate would
89
+ * decay to at a point of time in the future.
90
+ *
91
+ * @remarks
92
+ * By default, the fee is calculated at the time of the latest block. This can be overridden using
93
+ * the `when` parameter.
94
+
95
+ * Unlike the borrowing rate, the redemption rate depends on the amount being redeemed. To be more
96
+ * precise, it depends on the fraction of the redeemed amount compared to the total ZUSD supply,
97
+ * which must be passed as a parameter.
98
+ *
99
+ * To calculate the redemption fee in ZUSD, multiply the redeemed ZUSD amount with the redemption
100
+ * rate.
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * const fees = await zero.getFees();
105
+ * const total = await zero.getTotal();
106
+ *
107
+ * const redeemedZUSDAmount = Decimal.from(100);
108
+ * const redeemedFractionOfSupply = redeemedZUSDAmount.div(total.debt);
109
+ * const redemptionRate = fees.redemptionRate(redeemedFractionOfSupply);
110
+ * const redemptionFeeZUSD = redemptionRate.mul(redeemedZUSDAmount);
111
+ * ```
112
+ */
113
+ redemptionRate(redeemedFractionOfSupply = Decimal_1.Decimal.ZERO, when) {
114
+ redeemedFractionOfSupply = Decimal_1.Decimal.from(redeemedFractionOfSupply);
115
+ let baseRate = this.baseRate(when);
116
+ if (redeemedFractionOfSupply.nonZero) {
117
+ baseRate = redeemedFractionOfSupply.div(this._beta).add(baseRate);
118
+ }
119
+ return Decimal_1.Decimal.min(constants_1.MINIMUM_REDEMPTION_RATE.add(baseRate), Decimal_1.Decimal.ONE);
120
+ }
121
+ }
122
+ exports.Fees = Fees;
123
123
  //# sourceMappingURL=Fees.js.map