@drift-labs/sdk 2.32.1-beta.2 → 2.32.1-beta.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.32.1-beta.2
1
+ 2.32.1-beta.3
@@ -56,7 +56,10 @@ export declare function calculateUtilization(bank: SpotMarketAccount, delta?: an
56
56
  * @param targetBorrowRate
57
57
  * @returns : Precision: TOKEN DECIMALS
58
58
  */
59
- export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: SpotMarketAccount, targetBorrowRate: BN): BN;
59
+ export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: SpotMarketAccount, targetBorrowRate: BN): {
60
+ totalCapacity: BN;
61
+ remainingCapacity: BN;
62
+ };
60
63
  export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: any): BN;
61
64
  export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
62
65
  export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
@@ -188,38 +188,39 @@ exports.calculateUtilization = calculateUtilization;
188
188
  */
189
189
  function calculateSpotMarketBorrowCapacity(spotMarketAccount, targetBorrowRate) {
190
190
  const currentBorrowRate = calculateBorrowRate(spotMarketAccount);
191
+ const tokenDepositAmount = getTokenAmount(spotMarketAccount.depositBalance, spotMarketAccount, types_1.SpotBalanceType.DEPOSIT);
192
+ const tokenBorrowAmount = getTokenAmount(spotMarketAccount.borrowBalance, spotMarketAccount, types_1.SpotBalanceType.BORROW);
193
+ let targetUtilization;
194
+ // target utilization past mid point
195
+ if (targetBorrowRate.gte(new anchor_1.BN(spotMarketAccount.optimalBorrowRate))) {
196
+ const borrowRateSlope = new anchor_1.BN(spotMarketAccount.maxBorrowRate - spotMarketAccount.optimalBorrowRate)
197
+ .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
198
+ .div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(new anchor_1.BN(spotMarketAccount.optimalUtilization)));
199
+ const surplusTargetUtilization = targetBorrowRate
200
+ .sub(new anchor_1.BN(spotMarketAccount.optimalBorrowRate))
201
+ .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
202
+ .div(borrowRateSlope);
203
+ targetUtilization = surplusTargetUtilization.add(new anchor_1.BN(spotMarketAccount.optimalUtilization));
204
+ }
205
+ else {
206
+ const borrowRateSlope = new anchor_1.BN(spotMarketAccount.optimalBorrowRate)
207
+ .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
208
+ .div(new anchor_1.BN(spotMarketAccount.optimalUtilization));
209
+ targetUtilization = targetBorrowRate
210
+ .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
211
+ .div(borrowRateSlope);
212
+ }
213
+ const totalCapacity = tokenDepositAmount
214
+ .mul(targetUtilization)
215
+ .div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
216
+ let remainingCapacity;
191
217
  if (currentBorrowRate.gte(targetBorrowRate)) {
192
- return numericConstants_1.ZERO;
218
+ remainingCapacity = numericConstants_1.ZERO;
193
219
  }
194
220
  else {
195
- const tokenDepositAmount = getTokenAmount(spotMarketAccount.depositBalance, spotMarketAccount, types_1.SpotBalanceType.DEPOSIT);
196
- const tokenBorrowAmount = getTokenAmount(spotMarketAccount.borrowBalance, spotMarketAccount, types_1.SpotBalanceType.BORROW);
197
- let targetUtilization;
198
- // target utilization past mid point
199
- if (targetBorrowRate.gte(new anchor_1.BN(spotMarketAccount.optimalBorrowRate))) {
200
- const borrowRateSlope = new anchor_1.BN(spotMarketAccount.maxBorrowRate - spotMarketAccount.optimalBorrowRate)
201
- .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
202
- .div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(new anchor_1.BN(spotMarketAccount.optimalUtilization)));
203
- const surplusTargetUtilization = targetBorrowRate
204
- .sub(new anchor_1.BN(spotMarketAccount.optimalBorrowRate))
205
- .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
206
- .div(borrowRateSlope);
207
- targetUtilization = surplusTargetUtilization.add(new anchor_1.BN(spotMarketAccount.optimalUtilization));
208
- }
209
- else {
210
- const borrowRateSlope = new anchor_1.BN(spotMarketAccount.optimalBorrowRate)
211
- .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
212
- .div(new anchor_1.BN(spotMarketAccount.optimalUtilization));
213
- targetUtilization = targetBorrowRate
214
- .mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
215
- .div(borrowRateSlope);
216
- }
217
- const targetBorrowAmount = tokenDepositAmount
218
- .mul(targetUtilization)
219
- .div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
220
- const capacity = anchor_1.BN.max(numericConstants_1.ZERO, targetBorrowAmount.sub(tokenBorrowAmount));
221
- return capacity;
221
+ remainingCapacity = anchor_1.BN.max(numericConstants_1.ZERO, totalCapacity.sub(tokenBorrowAmount));
222
222
  }
223
+ return { totalCapacity, remainingCapacity };
223
224
  }
224
225
  exports.calculateSpotMarketBorrowCapacity = calculateSpotMarketBorrowCapacity;
225
226
  function calculateInterestRate(bank, delta = numericConstants_1.ZERO) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.32.1-beta.2",
3
+ "version": "2.32.1-beta.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -280,62 +280,64 @@ export function calculateUtilization(
280
280
  export function calculateSpotMarketBorrowCapacity(
281
281
  spotMarketAccount: SpotMarketAccount,
282
282
  targetBorrowRate: BN
283
- ): BN {
283
+ ): { totalCapacity: BN; remainingCapacity: BN } {
284
284
  const currentBorrowRate = calculateBorrowRate(spotMarketAccount);
285
285
 
286
- if (currentBorrowRate.gte(targetBorrowRate)) {
287
- return ZERO;
288
- } else {
289
- const tokenDepositAmount = getTokenAmount(
290
- spotMarketAccount.depositBalance,
291
- spotMarketAccount,
292
- SpotBalanceType.DEPOSIT
293
- );
294
- const tokenBorrowAmount = getTokenAmount(
295
- spotMarketAccount.borrowBalance,
296
- spotMarketAccount,
297
- SpotBalanceType.BORROW
298
- );
286
+ const tokenDepositAmount = getTokenAmount(
287
+ spotMarketAccount.depositBalance,
288
+ spotMarketAccount,
289
+ SpotBalanceType.DEPOSIT
290
+ );
299
291
 
300
- let targetUtilization;
301
-
302
- // target utilization past mid point
303
- if (targetBorrowRate.gte(new BN(spotMarketAccount.optimalBorrowRate))) {
304
- const borrowRateSlope = new BN(
305
- spotMarketAccount.maxBorrowRate - spotMarketAccount.optimalBorrowRate
306
- )
307
- .mul(SPOT_MARKET_UTILIZATION_PRECISION)
308
- .div(
309
- SPOT_MARKET_UTILIZATION_PRECISION.sub(
310
- new BN(spotMarketAccount.optimalUtilization)
311
- )
312
- );
313
-
314
- const surplusTargetUtilization = targetBorrowRate
315
- .sub(new BN(spotMarketAccount.optimalBorrowRate))
316
- .mul(SPOT_MARKET_UTILIZATION_PRECISION)
317
- .div(borrowRateSlope);
318
-
319
- targetUtilization = surplusTargetUtilization.add(
320
- new BN(spotMarketAccount.optimalUtilization)
292
+ const tokenBorrowAmount = getTokenAmount(
293
+ spotMarketAccount.borrowBalance,
294
+ spotMarketAccount,
295
+ SpotBalanceType.BORROW
296
+ );
297
+
298
+ let targetUtilization;
299
+ // target utilization past mid point
300
+ if (targetBorrowRate.gte(new BN(spotMarketAccount.optimalBorrowRate))) {
301
+ const borrowRateSlope = new BN(
302
+ spotMarketAccount.maxBorrowRate - spotMarketAccount.optimalBorrowRate
303
+ )
304
+ .mul(SPOT_MARKET_UTILIZATION_PRECISION)
305
+ .div(
306
+ SPOT_MARKET_UTILIZATION_PRECISION.sub(
307
+ new BN(spotMarketAccount.optimalUtilization)
308
+ )
321
309
  );
322
- } else {
323
- const borrowRateSlope = new BN(spotMarketAccount.optimalBorrowRate)
324
- .mul(SPOT_MARKET_UTILIZATION_PRECISION)
325
- .div(new BN(spotMarketAccount.optimalUtilization));
326
-
327
- targetUtilization = targetBorrowRate
328
- .mul(SPOT_MARKET_UTILIZATION_PRECISION)
329
- .div(borrowRateSlope);
330
- }
331
-
332
- const targetBorrowAmount = tokenDepositAmount
333
- .mul(targetUtilization)
334
- .div(SPOT_MARKET_UTILIZATION_PRECISION);
335
- const capacity = BN.max(ZERO, targetBorrowAmount.sub(tokenBorrowAmount));
336
310
 
337
- return capacity;
311
+ const surplusTargetUtilization = targetBorrowRate
312
+ .sub(new BN(spotMarketAccount.optimalBorrowRate))
313
+ .mul(SPOT_MARKET_UTILIZATION_PRECISION)
314
+ .div(borrowRateSlope);
315
+
316
+ targetUtilization = surplusTargetUtilization.add(
317
+ new BN(spotMarketAccount.optimalUtilization)
318
+ );
319
+ } else {
320
+ const borrowRateSlope = new BN(spotMarketAccount.optimalBorrowRate)
321
+ .mul(SPOT_MARKET_UTILIZATION_PRECISION)
322
+ .div(new BN(spotMarketAccount.optimalUtilization));
323
+
324
+ targetUtilization = targetBorrowRate
325
+ .mul(SPOT_MARKET_UTILIZATION_PRECISION)
326
+ .div(borrowRateSlope);
338
327
  }
328
+
329
+ const totalCapacity = tokenDepositAmount
330
+ .mul(targetUtilization)
331
+ .div(SPOT_MARKET_UTILIZATION_PRECISION);
332
+
333
+ let remainingCapacity;
334
+ if (currentBorrowRate.gte(targetBorrowRate)) {
335
+ remainingCapacity = ZERO;
336
+ } else {
337
+ remainingCapacity = BN.max(ZERO, totalCapacity.sub(tokenBorrowAmount));
338
+ }
339
+
340
+ return { totalCapacity, remainingCapacity };
339
341
  }
340
342
 
341
343
  export function calculateInterestRate(
@@ -27,19 +27,17 @@ describe('Spot Tests', () => {
27
27
  mockSpot.borrowBalance = ZERO;
28
28
 
29
29
  // todo, should incorp all other spot market constraints?
30
- const aboveMaxAmount = calculateSpotMarketBorrowCapacity(
31
- mockSpot,
32
- new BN(2000000)
33
- );
30
+ const { remainingCapacity: aboveMaxAmount } =
31
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(2000000));
34
32
  assert(aboveMaxAmount.gt(mockSpot.depositBalance));
35
33
 
36
- const maxAmount = calculateSpotMarketBorrowCapacity(
34
+ const { remainingCapacity: maxAmount } = calculateSpotMarketBorrowCapacity(
37
35
  mockSpot,
38
36
  new BN(1000000)
39
37
  );
40
38
  assert(maxAmount.eq(mockSpot.depositBalance));
41
39
 
42
- const optAmount = calculateSpotMarketBorrowCapacity(
40
+ const { remainingCapacity: optAmount } = calculateSpotMarketBorrowCapacity(
43
41
  mockSpot,
44
42
  new BN(100000)
45
43
  );
@@ -47,34 +45,26 @@ describe('Spot Tests', () => {
47
45
  // console.log('optAmount:', optAmount.toNumber(), ans.toNumber());
48
46
  assert(optAmount.eq(ans));
49
47
 
50
- const betweenOptMaxAmount = calculateSpotMarketBorrowCapacity(
51
- mockSpot,
52
- new BN(810000)
53
- );
48
+ const { remainingCapacity: betweenOptMaxAmount } =
49
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(810000));
54
50
  // console.log('betweenOptMaxAmount:', betweenOptMaxAmount.toNumber());
55
51
  assert(betweenOptMaxAmount.lt(mockSpot.depositBalance));
56
52
  assert(betweenOptMaxAmount.gt(ans));
57
53
  assert(betweenOptMaxAmount.eq(new BN(93666600000000)));
58
54
 
59
- const belowOptAmount = calculateSpotMarketBorrowCapacity(
60
- mockSpot,
61
- new BN(50000)
62
- );
55
+ const { remainingCapacity: belowOptAmount } =
56
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(50000));
63
57
  // console.log('belowOptAmount:', belowOptAmount.toNumber());
64
58
  assert(belowOptAmount.eq(ans.div(new BN(2))));
65
59
 
66
- const belowOptAmount2 = calculateSpotMarketBorrowCapacity(
67
- mockSpot,
68
- new BN(24900)
69
- );
60
+ const { remainingCapacity: belowOptAmount2 } =
61
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(24900));
70
62
  // console.log('belowOptAmount2:', belowOptAmount2.toNumber());
71
63
  assert(belowOptAmount2.lt(ans.div(new BN(4))));
72
64
  assert(belowOptAmount2.eq(new BN('17430000000000')));
73
65
 
74
- const belowOptAmount3 = calculateSpotMarketBorrowCapacity(
75
- mockSpot,
76
- new BN(1)
77
- );
66
+ const { remainingCapacity: belowOptAmount3 } =
67
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(1));
78
68
  // console.log('belowOptAmount3:', belowOptAmount3.toNumber());
79
69
  assert(belowOptAmount3.eq(new BN('700000000'))); //0.7
80
70
  });
@@ -97,59 +87,47 @@ describe('Spot Tests', () => {
97
87
  mockSpot.borrowBalance = new BN(7089.91675884 * 1e9);
98
88
 
99
89
  // todo, should incorp all other spot market constraints?
100
- const aboveMaxAmount = calculateSpotMarketBorrowCapacity(
101
- mockSpot,
102
- new BN(2000000)
103
- );
90
+ const { remainingCapacity: aboveMaxAmount } =
91
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(2000000));
104
92
  assert(aboveMaxAmount.eq(new BN('111498270939007')));
105
93
 
106
- const maxAmount = calculateSpotMarketBorrowCapacity(
94
+ const { remainingCapacity: maxAmount } = calculateSpotMarketBorrowCapacity(
107
95
  mockSpot,
108
96
  new BN(1000000)
109
97
  );
110
98
  assert(maxAmount.eq(new BN('82502230374168')));
111
99
  // console.log('aboveMaxAmount:', aboveMaxAmount.toNumber(), 'maxAmount:', maxAmount.toNumber());
112
- const optAmount = calculateSpotMarketBorrowCapacity(
100
+ const { remainingCapacity: optAmount } = calculateSpotMarketBorrowCapacity(
113
101
  mockSpot,
114
102
  new BN(70000)
115
103
  );
116
104
  // console.log('optAmount:', optAmount.toNumber());
117
105
  assert(optAmount.eq(new BN('55535858716123'))); // ~ 55535
118
106
 
119
- const betweenOptMaxAmount = calculateSpotMarketBorrowCapacity(
120
- mockSpot,
121
- new BN(810000)
122
- );
107
+ const { remainingCapacity: betweenOptMaxAmount } =
108
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(810000));
123
109
  // console.log('betweenOptMaxAmount:', betweenOptMaxAmount.toNumber());
124
110
  assert(betweenOptMaxAmount.lt(maxAmount));
125
111
  assert(betweenOptMaxAmount.eq(new BN(76992910756523)));
126
112
  assert(betweenOptMaxAmount.gt(optAmount));
127
113
 
128
- const belowOptAmount = calculateSpotMarketBorrowCapacity(
129
- mockSpot,
130
- new BN(50000)
131
- );
114
+ const { remainingCapacity: belowOptAmount } =
115
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(50000));
132
116
  // console.log('belowOptAmount:', belowOptAmount.toNumber());
133
117
  assert(belowOptAmount.eq(new BN('37558277610760')));
134
118
 
135
- const belowOptAmount2 = calculateSpotMarketBorrowCapacity(
136
- mockSpot,
137
- new BN(24900)
138
- );
119
+ const { remainingCapacity: belowOptAmount2 } =
120
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(24900));
139
121
  // console.log('belowOptAmount2:', belowOptAmount2.toNumber());
140
122
  assert(belowOptAmount2.eq(new BN('14996413323529')));
141
123
 
142
- const belowOptAmount3 = calculateSpotMarketBorrowCapacity(
143
- mockSpot,
144
- new BN(4900)
145
- );
124
+ const { remainingCapacity: belowOptAmount3 } =
125
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(4900));
146
126
  // console.log('belowOptAmount2:', belowOptAmount3.toNumber());
147
127
  assert(belowOptAmount3.eq(new BN('0')));
148
128
 
149
- const belowOptAmount4 = calculateSpotMarketBorrowCapacity(
150
- mockSpot,
151
- new BN(1)
152
- );
129
+ const { remainingCapacity: belowOptAmount4 } =
130
+ calculateSpotMarketBorrowCapacity(mockSpot, new BN(1));
153
131
  // console.log('belowOptAmount3:', belowOptAmount4.toNumber());
154
132
  assert(belowOptAmount4.eq(new BN('0')));
155
133
  });