@morpho-org/blue-sdk 1.5.10 → 1.6.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/lib/errors.d.ts CHANGED
@@ -35,6 +35,16 @@ export declare namespace BlueErrors {
35
35
  readonly marketId: MarketId;
36
36
  constructor(marketId: MarketId);
37
37
  }
38
+ class InsufficientPosition extends Error {
39
+ readonly user: Address;
40
+ readonly marketId: MarketId;
41
+ constructor(user: Address, marketId: MarketId);
42
+ }
43
+ class InsufficientCollateral extends Error {
44
+ readonly user: Address;
45
+ readonly marketId: MarketId;
46
+ constructor(user: Address, marketId: MarketId);
47
+ }
38
48
  }
39
49
  export declare class InvalidSignatureError extends Error {
40
50
  readonly hash: string;
package/lib/errors.js CHANGED
@@ -73,6 +73,26 @@ var BlueErrors;
73
73
  }
74
74
  }
75
75
  BlueErrors.InsufficientLiquidity = InsufficientLiquidity;
76
+ class InsufficientPosition extends Error {
77
+ user;
78
+ marketId;
79
+ constructor(user, marketId) {
80
+ super(`insufficient position for user ${user} on market ${marketId}`);
81
+ this.user = user;
82
+ this.marketId = marketId;
83
+ }
84
+ }
85
+ BlueErrors.InsufficientPosition = InsufficientPosition;
86
+ class InsufficientCollateral extends Error {
87
+ user;
88
+ marketId;
89
+ constructor(user, marketId) {
90
+ super(`insufficient collateral for user ${user} on market ${marketId}`);
91
+ this.user = user;
92
+ this.marketId = marketId;
93
+ }
94
+ }
95
+ BlueErrors.InsufficientCollateral = InsufficientCollateral;
76
96
  })(BlueErrors || (exports.BlueErrors = BlueErrors = {}));
77
97
  class InvalidSignatureError extends Error {
78
98
  hash;
@@ -92,6 +92,28 @@ export declare class AccrualPosition extends Position implements InputAccrualPos
92
92
  get withdrawCapacityLimit(): import("../market").CapacityLimit;
93
93
  get withdrawCollateralCapacityLimit(): import("../market").CapacityLimit;
94
94
  accrueInterest(timestamp: bigint): AccrualPosition;
95
+ supply(assets: bigint, shares: bigint, timestamp?: bigint): {
96
+ position: AccrualPosition;
97
+ assets: bigint;
98
+ shares: bigint;
99
+ };
100
+ withdraw(assets: bigint, shares: bigint, timestamp?: bigint): {
101
+ position: AccrualPosition;
102
+ assets: bigint;
103
+ shares: bigint;
104
+ };
105
+ supplyCollateral(assets: bigint, timestamp?: bigint): AccrualPosition;
106
+ withdrawCollateral(assets: bigint, timestamp?: bigint): AccrualPosition;
107
+ borrow(assets: bigint, shares: bigint, timestamp?: bigint): {
108
+ position: AccrualPosition;
109
+ assets: bigint;
110
+ shares: bigint;
111
+ };
112
+ repay(assets: bigint, shares: bigint, timestamp?: bigint): {
113
+ position: AccrualPosition;
114
+ assets: bigint;
115
+ shares: bigint;
116
+ };
95
117
  getRepayCapacityLimit(loanTokenBalance: bigint): import("../market").CapacityLimit;
96
118
  getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint): import("../market").MaxPositionCapacities;
97
119
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AccrualPosition = exports.Position = void 0;
4
+ const errors_1 = require("../errors");
4
5
  class Position {
5
6
  /**
6
7
  * The user holding this position.
@@ -128,6 +129,66 @@ class AccrualPosition extends Position {
128
129
  accrueInterest(timestamp) {
129
130
  return new AccrualPosition(this, this.market.accrueInterest(timestamp));
130
131
  }
132
+ supply(assets, shares, timestamp) {
133
+ let { market } = this;
134
+ ({ market, assets, shares } = market.supply(assets, shares, timestamp));
135
+ this.supplyShares += shares;
136
+ return {
137
+ position: new AccrualPosition(this, market),
138
+ assets,
139
+ shares,
140
+ };
141
+ }
142
+ withdraw(assets, shares, timestamp) {
143
+ let { market } = this;
144
+ ({ market, assets, shares } = market.withdraw(assets, shares, timestamp));
145
+ this.supplyShares -= shares;
146
+ if (this.supplyShares < 0n)
147
+ throw new errors_1.BlueErrors.InsufficientPosition(this.user, this.marketId);
148
+ return {
149
+ position: new AccrualPosition(this, market),
150
+ assets,
151
+ shares,
152
+ };
153
+ }
154
+ supplyCollateral(assets, timestamp = this.market.lastUpdate) {
155
+ const market = this.market.accrueInterest(timestamp);
156
+ this.collateral += assets;
157
+ return new AccrualPosition(this, market);
158
+ }
159
+ withdrawCollateral(assets, timestamp = this.market.lastUpdate) {
160
+ const market = this.market.accrueInterest(timestamp);
161
+ this.collateral -= assets;
162
+ if (this.collateral < 0n)
163
+ throw new errors_1.BlueErrors.InsufficientPosition(this.user, this.marketId);
164
+ if (!market.isHealthy(this))
165
+ throw new errors_1.BlueErrors.InsufficientCollateral(this.user, this.marketId);
166
+ return new AccrualPosition(this, market);
167
+ }
168
+ borrow(assets, shares, timestamp) {
169
+ let { market } = this;
170
+ ({ market, assets, shares } = market.borrow(assets, shares, timestamp));
171
+ this.borrowShares += shares;
172
+ if (!market.isHealthy(this))
173
+ throw new errors_1.BlueErrors.InsufficientCollateral(this.user, this.marketId);
174
+ return {
175
+ position: new AccrualPosition(this, market),
176
+ assets,
177
+ shares,
178
+ };
179
+ }
180
+ repay(assets, shares, timestamp) {
181
+ let { market } = this;
182
+ ({ market, assets, shares } = market.repay(assets, shares, timestamp));
183
+ this.borrowShares -= shares;
184
+ if (this.borrowShares < 0n)
185
+ throw new errors_1.BlueErrors.InsufficientPosition(this.user, this.marketId);
186
+ return {
187
+ position: new AccrualPosition(this, market),
188
+ assets,
189
+ shares,
190
+ };
191
+ }
131
192
  getRepayCapacityLimit(loanTokenBalance) {
132
193
  return this.market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
133
194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@morpho-org/blue-sdk",
3
- "version": "1.5.10",
3
+ "version": "1.6.0",
4
4
  "author": "Morpho Association <contact@morpho.org>",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -16,8 +16,8 @@
16
16
  "keccak256": "^1.0.6"
17
17
  },
18
18
  "devDependencies": {
19
- "@morpho-org/morpho-test": "^1.5.10",
20
- "@morpho-org/morpho-ts": "^1.5.10",
19
+ "@morpho-org/morpho-test": "^1.6.0",
20
+ "@morpho-org/morpho-ts": "^1.6.0",
21
21
  "@types/jest": "^29.5.12",
22
22
  "@types/node": "^22.1.0",
23
23
  "jest": "^29.7.0",
@@ -25,7 +25,7 @@
25
25
  "typescript": "^5.4.5"
26
26
  },
27
27
  "peerDependencies": {
28
- "@morpho-org/morpho-ts": "^1.5.10"
28
+ "@morpho-org/morpho-ts": "^1.6.0"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"
@@ -49,5 +49,5 @@
49
49
  ],
50
50
  "preset": "ts-jest"
51
51
  },
52
- "gitHead": "33eb208bc2d687120c062e3a2325af1982ad04b3"
52
+ "gitHead": "9de6e4c07df8e7937b0cfbbeefca693bf0ffc34a"
53
53
  }