@morpho-org/blue-sdk 3.0.4 → 3.0.6

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.
@@ -31,10 +31,6 @@ export declare class Holding implements IHolding {
31
31
  * The token in which this holding is denominated.
32
32
  */
33
33
  readonly token: Address;
34
- /**
35
- * The balance of the user for this token.
36
- */
37
- balance: bigint;
38
34
  /**
39
35
  * Whether the user is allowed to transfer this holding's balance.
40
36
  */
@@ -54,5 +50,14 @@ export declare class Holding implements IHolding {
54
50
  * `undefined` if the token does not support ERC-2612.
55
51
  */
56
52
  erc2612Nonce?: bigint;
53
+ /**
54
+ * Allows to customize the setter behavior in child classes.
55
+ */
56
+ protected _balance: bigint;
57
57
  constructor({ user, token, erc20Allowances, permit2BundlerAllowance, balance, erc2612Nonce, canTransfer, }: IHolding);
58
+ /**
59
+ * The balance of the user for this token.
60
+ */
61
+ get balance(): bigint;
62
+ set balance(value: bigint);
58
63
  }
@@ -16,10 +16,6 @@ class Holding {
16
16
  * The token in which this holding is denominated.
17
17
  */
18
18
  token;
19
- /**
20
- * The balance of the user for this token.
21
- */
22
- balance;
23
19
  /**
24
20
  * Whether the user is allowed to transfer this holding's balance.
25
21
  */
@@ -37,10 +33,14 @@ class Holding {
37
33
  * `undefined` if the token does not support ERC-2612.
38
34
  */
39
35
  erc2612Nonce;
36
+ /**
37
+ * Allows to customize the setter behavior in child classes.
38
+ */
39
+ _balance;
40
40
  constructor({ user, token, erc20Allowances, permit2BundlerAllowance, balance, erc2612Nonce, canTransfer, }) {
41
41
  this.user = user;
42
42
  this.token = token;
43
- this.balance = balance;
43
+ this._balance = balance;
44
44
  this.canTransfer = canTransfer;
45
45
  this.erc20Allowances = (0, morpho_ts_1.fromEntries)((0, morpho_ts_1.entries)(erc20Allowances).map(([address, allowance]) => [
46
46
  address,
@@ -54,5 +54,14 @@ class Holding {
54
54
  if (erc2612Nonce != null)
55
55
  this.erc2612Nonce = erc2612Nonce;
56
56
  }
57
+ /**
58
+ * The balance of the user for this token.
59
+ */
60
+ get balance() {
61
+ return this._balance;
62
+ }
63
+ set balance(value) {
64
+ this._balance = value;
65
+ }
57
66
  }
58
67
  exports.Holding = Holding;
@@ -108,9 +108,16 @@ export declare class Market implements IMarket {
108
108
  */
109
109
  get supplyRate(): bigint;
110
110
  /**
111
- * Returns the rate at which interest accrues for borrowers of this market,
112
- * since the last time the market was updated (scaled by WAD).
113
- * If interested in the rate at a specific timestamp, use `getAccrualBorrowRate(timestamp)` instead.
111
+ * Returns the average rate at which interest _would_ accrue from `lastUpdate`
112
+ * till now, if `accrueInterest` was called immediately onchain (scaled by WAD).
113
+ * If `accrueInterest` was just called, the average rate equals the instantaneous rate,
114
+ * so it is equivalent to `getBorrowRate(lastUpdate)`.
115
+ *
116
+ * In most cases, `accrueInterest` will not be called immediately onchain, so the
117
+ * average rate doesn't correspond to anything "real".
118
+ *
119
+ * If interested in the instantaneous rate experienced by existing market actors at a specific timestamp,
120
+ * use `getBorrowRate(timestamp)`, `getBorrowApy(timestamp)`, or `getSupplyApy(timestamp)` instead.
114
121
  */
115
122
  get borrowRate(): bigint;
116
123
  /**
@@ -136,7 +143,12 @@ export declare class Market implements IMarket {
136
143
  * at the given timestamp, if the state remains unchanged (not accrued) (scaled by WAD).
137
144
  * @param timestamp The timestamp at which to calculate the accrual borrow rate. Must be greater than or equal to `lastUpdate`. Defaults to `Time.timestamp()` (returns the current borrow rate).
138
145
  */
139
- getAccrualBorrowRate(timestamp?: BigIntish): bigint;
146
+ protected getAccrualBorrowRate(timestamp?: BigIntish): {
147
+ elapsed: bigint;
148
+ avgBorrowRate: bigint;
149
+ endBorrowRate: bigint;
150
+ endRateAtTarget?: bigint;
151
+ };
140
152
  /**
141
153
  * The market's instantaneous borrow-side Annual Percentage Yield (APY) at the given timestamp,
142
154
  * if the state remains unchanged (not accrued) (scaled by WAD).
@@ -109,12 +109,19 @@ class Market {
109
109
  return MarketUtils_js_1.MarketUtils.getSupplyRate(this.borrowRate, this);
110
110
  }
111
111
  /**
112
- * Returns the rate at which interest accrues for borrowers of this market,
113
- * since the last time the market was updated (scaled by WAD).
114
- * If interested in the rate at a specific timestamp, use `getAccrualBorrowRate(timestamp)` instead.
112
+ * Returns the average rate at which interest _would_ accrue from `lastUpdate`
113
+ * till now, if `accrueInterest` was called immediately onchain (scaled by WAD).
114
+ * If `accrueInterest` was just called, the average rate equals the instantaneous rate,
115
+ * so it is equivalent to `getBorrowRate(lastUpdate)`.
116
+ *
117
+ * In most cases, `accrueInterest` will not be called immediately onchain, so the
118
+ * average rate doesn't correspond to anything "real".
119
+ *
120
+ * If interested in the instantaneous rate experienced by existing market actors at a specific timestamp,
121
+ * use `getBorrowRate(timestamp)`, `getBorrowApy(timestamp)`, or `getSupplyApy(timestamp)` instead.
115
122
  */
116
123
  get borrowRate() {
117
- return this.getAccrualBorrowRate();
124
+ return this.getAccrualBorrowRate().avgBorrowRate;
118
125
  }
119
126
  /**
120
127
  * The market's current, instantaneous supply-side Annual Percentage Yield (APY) (scaled by WAD).
@@ -153,14 +160,20 @@ class Market {
153
160
  * @param timestamp The timestamp at which to calculate the accrual borrow rate. Must be greater than or equal to `lastUpdate`. Defaults to `Time.timestamp()` (returns the current borrow rate).
154
161
  */
155
162
  getAccrualBorrowRate(timestamp = morpho_ts_1.Time.timestamp()) {
156
- if (this.rateAtTarget == null)
157
- return 0n;
158
163
  timestamp = BigInt(timestamp);
159
164
  const elapsed = timestamp - this.lastUpdate;
160
165
  if (elapsed < 0n)
161
166
  throw new errors_js_1.BlueErrors.InvalidInterestAccrual(this.id, timestamp, this.lastUpdate);
162
- const { avgBorrowRate } = index_js_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, elapsed);
163
- return avgBorrowRate;
167
+ if (this.rateAtTarget == null)
168
+ return {
169
+ elapsed,
170
+ avgBorrowRate: 0n,
171
+ endBorrowRate: 0n,
172
+ };
173
+ return {
174
+ elapsed,
175
+ ...index_js_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, elapsed),
176
+ };
164
177
  }
165
178
  /**
166
179
  * The market's instantaneous borrow-side Annual Percentage Yield (APY) at the given timestamp,
@@ -186,26 +199,15 @@ class Market {
186
199
  */
187
200
  accrueInterest(timestamp = this.lastUpdate) {
188
201
  timestamp = BigInt(timestamp);
189
- const elapsed = timestamp - this.lastUpdate;
190
- if (elapsed < 0n)
191
- throw new errors_js_1.BlueErrors.InvalidInterestAccrual(this.id, timestamp, this.lastUpdate);
192
- if (elapsed === 0n)
193
- return new Market(this);
194
- let borrowRate = 0n;
195
- let { rateAtTarget } = this;
196
- if (rateAtTarget != null) {
197
- const { avgBorrowRate, endRateAtTarget } = index_js_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, rateAtTarget, elapsed);
198
- borrowRate = avgBorrowRate;
199
- rateAtTarget = endRateAtTarget;
200
- }
201
- const { interest, feeShares } = MarketUtils_js_1.MarketUtils.getAccruedInterest(borrowRate, this, elapsed);
202
+ const { elapsed, avgBorrowRate, endRateAtTarget } = this.getAccrualBorrowRate(timestamp);
203
+ const { interest, feeShares } = MarketUtils_js_1.MarketUtils.getAccruedInterest(avgBorrowRate, this, elapsed);
202
204
  return new Market({
203
205
  ...this,
204
206
  totalSupplyAssets: this.totalSupplyAssets + interest,
205
207
  totalBorrowAssets: this.totalBorrowAssets + interest,
206
208
  totalSupplyShares: this.totalSupplyShares + feeShares,
207
209
  lastUpdate: timestamp,
208
- rateAtTarget,
210
+ rateAtTarget: endRateAtTarget,
209
211
  });
210
212
  }
211
213
  supply(assets, shares, timestamp) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/blue-sdk",
3
3
  "description": "Framework-agnostic package that defines Morpho-related entity classes (such as `Market`, `Token`, `Vault`).",
4
- "version": "3.0.4",
4
+ "version": "3.0.6",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Rubilmax <rmilon@gmail.com>"