@drift-labs/sdk 2.31.1-beta.16 → 2.31.1-beta.18
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 +1 -1
- package/lib/idl/drift.json +1 -1
- package/lib/math/spotBalance.d.ts +2 -2
- package/lib/math/spotBalance.js +11 -5
- package/lib/math/superStake.d.ts +6 -0
- package/lib/math/superStake.js +54 -1
- package/package.json +1 -1
- package/src/idl/drift.json +1 -1
- package/src/math/spotBalance.ts +17 -6
- package/src/math/superStake.ts +67 -0
- package/tests/spot/test.ts +150 -96
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.31.1-beta.
|
|
1
|
+
2.31.1-beta.18
|
package/lib/idl/drift.json
CHANGED
|
@@ -49,7 +49,7 @@ export declare function getStrictTokenValue(tokenAmount: BN, spotDecimals: numbe
|
|
|
49
49
|
export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: OraclePriceData): BN;
|
|
50
50
|
export declare function calculateAssetWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
51
51
|
export declare function calculateLiabilityWeight(size: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
52
|
-
export declare function calculateUtilization(bank: SpotMarketAccount): BN;
|
|
52
|
+
export declare function calculateUtilization(bank: SpotMarketAccount, delta?: any): BN;
|
|
53
53
|
/**
|
|
54
54
|
* calculates max borrow amount where rate would stay below targetBorrowRate
|
|
55
55
|
* @param spotMarketAccount
|
|
@@ -57,7 +57,7 @@ export declare function calculateUtilization(bank: SpotMarketAccount): BN;
|
|
|
57
57
|
* @returns : Precision: TOKEN DECIMALS
|
|
58
58
|
*/
|
|
59
59
|
export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: SpotMarketAccount, targetBorrowRate: BN): BN;
|
|
60
|
-
export declare function calculateInterestRate(bank: SpotMarketAccount): BN;
|
|
60
|
+
export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: any): BN;
|
|
61
61
|
export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
|
|
62
62
|
export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
|
|
63
63
|
export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -156,9 +156,15 @@ function calculateLiabilityWeight(size, spotMarket, marginCategory) {
|
|
|
156
156
|
return liabilityWeight;
|
|
157
157
|
}
|
|
158
158
|
exports.calculateLiabilityWeight = calculateLiabilityWeight;
|
|
159
|
-
function calculateUtilization(bank) {
|
|
160
|
-
|
|
161
|
-
|
|
159
|
+
function calculateUtilization(bank, delta = numericConstants_1.ZERO) {
|
|
160
|
+
let tokenDepositAmount = getTokenAmount(bank.depositBalance, bank, types_1.SpotBalanceType.DEPOSIT);
|
|
161
|
+
let tokenBorrowAmount = getTokenAmount(bank.borrowBalance, bank, types_1.SpotBalanceType.BORROW);
|
|
162
|
+
if (delta.gt(numericConstants_1.ZERO)) {
|
|
163
|
+
tokenDepositAmount = tokenDepositAmount.add(delta);
|
|
164
|
+
}
|
|
165
|
+
else if (delta.lt(numericConstants_1.ZERO)) {
|
|
166
|
+
tokenBorrowAmount = tokenBorrowAmount.add(delta.abs());
|
|
167
|
+
}
|
|
162
168
|
let utilization;
|
|
163
169
|
if (tokenBorrowAmount.eq(numericConstants_1.ZERO) && tokenDepositAmount.eq(numericConstants_1.ZERO)) {
|
|
164
170
|
utilization = numericConstants_1.ZERO;
|
|
@@ -216,8 +222,8 @@ function calculateSpotMarketBorrowCapacity(spotMarketAccount, targetBorrowRate)
|
|
|
216
222
|
}
|
|
217
223
|
}
|
|
218
224
|
exports.calculateSpotMarketBorrowCapacity = calculateSpotMarketBorrowCapacity;
|
|
219
|
-
function calculateInterestRate(bank) {
|
|
220
|
-
const utilization = calculateUtilization(bank);
|
|
225
|
+
function calculateInterestRate(bank, delta = numericConstants_1.ZERO) {
|
|
226
|
+
const utilization = calculateUtilization(bank, delta);
|
|
221
227
|
let interestRate;
|
|
222
228
|
if (utilization.gt(new anchor_1.BN(bank.optimalUtilization))) {
|
|
223
229
|
const surplusUtilization = utilization.sub(new anchor_1.BN(bank.optimalUtilization));
|
package/lib/math/superStake.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@s
|
|
|
2
2
|
import { JupiterClient } from '../jupiter/jupiterClient';
|
|
3
3
|
import { DriftClient } from '../driftClient';
|
|
4
4
|
import { BN } from '@coral-xyz/anchor';
|
|
5
|
+
import { User } from '../user';
|
|
6
|
+
import { DepositRecord } from '../types';
|
|
5
7
|
export declare function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, }: {
|
|
6
8
|
amount: BN;
|
|
7
9
|
jupiterClient: JupiterClient;
|
|
@@ -13,3 +15,7 @@ export declare function findBestSuperStakeIxs({ amount, jupiterClient, driftClie
|
|
|
13
15
|
method: 'jupiter' | 'marinade';
|
|
14
16
|
price: number;
|
|
15
17
|
}>;
|
|
18
|
+
export declare function calculateSolEarned({ user, depositRecords, }: {
|
|
19
|
+
user: User;
|
|
20
|
+
depositRecords: DepositRecord[];
|
|
21
|
+
}): Promise<BN>;
|
package/lib/math/superStake.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findBestSuperStakeIxs = void 0;
|
|
3
|
+
exports.calculateSolEarned = exports.findBestSuperStakeIxs = void 0;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
4
5
|
const marinade_1 = require("../marinade");
|
|
6
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
7
|
+
const types_1 = require("../types");
|
|
8
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
9
|
async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, }) {
|
|
6
10
|
const marinadeProgram = (0, marinade_1.getMarinadeFinanceProgram)(driftClient.provider);
|
|
7
11
|
const marinadePrice = await (0, marinade_1.getMarinadeMSolPrice)(marinadeProgram);
|
|
@@ -41,3 +45,52 @@ async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userA
|
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
47
|
exports.findBestSuperStakeIxs = findBestSuperStakeIxs;
|
|
48
|
+
async function calculateSolEarned({ user, depositRecords, }) {
|
|
49
|
+
const now = Date.now() / 1000;
|
|
50
|
+
const timestamps = [
|
|
51
|
+
now,
|
|
52
|
+
...depositRecords.map((r) => r.ts.toNumber()),
|
|
53
|
+
];
|
|
54
|
+
const msolRatios = new Map();
|
|
55
|
+
const getPrice = async (timestamp) => {
|
|
56
|
+
const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
|
|
57
|
+
const swaggerApiDateTime = date.toISOString(); // Format date as swagger API date-time
|
|
58
|
+
const url = `https://api.marinade.finance/msol/price_sol?time=${swaggerApiDateTime}`;
|
|
59
|
+
const response = await fetch(url);
|
|
60
|
+
if (response.status === 200) {
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
msolRatios.set(timestamp, data);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
await Promise.all(timestamps.map(getPrice));
|
|
66
|
+
let solEarned = numericConstants_1.ZERO;
|
|
67
|
+
for (const record of depositRecords) {
|
|
68
|
+
if (record.marketIndex === 1) {
|
|
69
|
+
if ((0, types_1.isVariant)(record.explanation, 'deposit')) {
|
|
70
|
+
solEarned = solEarned.sub(record.amount);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
solEarned = solEarned.add(record.amount);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (record.marketIndex === 2) {
|
|
77
|
+
const msolRatio = msolRatios.get(record.ts.toNumber());
|
|
78
|
+
const msolRatioBN = new anchor_1.BN(msolRatio * web3_js_1.LAMPORTS_PER_SOL);
|
|
79
|
+
const solAmount = record.amount.mul(msolRatioBN).div(numericConstants_1.LAMPORTS_PRECISION);
|
|
80
|
+
if ((0, types_1.isVariant)(record.explanation, 'deposit')) {
|
|
81
|
+
solEarned = solEarned.sub(solAmount);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
solEarned = solEarned.add(solAmount);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const currentMSOLTokenAmount = await user.getTokenAmount(2);
|
|
89
|
+
const currentSOLTokenAmount = await user.getTokenAmount(1);
|
|
90
|
+
const currentMSOLRatio = msolRatios.get(now);
|
|
91
|
+
const currentMSOLRatioBN = new anchor_1.BN(currentMSOLRatio * web3_js_1.LAMPORTS_PER_SOL);
|
|
92
|
+
solEarned = solEarned.add(currentMSOLTokenAmount.mul(currentMSOLRatioBN).div(numericConstants_1.LAMPORTS_PRECISION));
|
|
93
|
+
solEarned = solEarned.add(currentSOLTokenAmount);
|
|
94
|
+
return solEarned;
|
|
95
|
+
}
|
|
96
|
+
exports.calculateSolEarned = calculateSolEarned;
|
package/package.json
CHANGED
package/src/idl/drift.json
CHANGED
package/src/math/spotBalance.ts
CHANGED
|
@@ -236,18 +236,27 @@ export function calculateLiabilityWeight(
|
|
|
236
236
|
return liabilityWeight;
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
-
export function calculateUtilization(
|
|
240
|
-
|
|
239
|
+
export function calculateUtilization(
|
|
240
|
+
bank: SpotMarketAccount,
|
|
241
|
+
delta = ZERO
|
|
242
|
+
): BN {
|
|
243
|
+
let tokenDepositAmount = getTokenAmount(
|
|
241
244
|
bank.depositBalance,
|
|
242
245
|
bank,
|
|
243
246
|
SpotBalanceType.DEPOSIT
|
|
244
247
|
);
|
|
245
|
-
|
|
248
|
+
let tokenBorrowAmount = getTokenAmount(
|
|
246
249
|
bank.borrowBalance,
|
|
247
250
|
bank,
|
|
248
251
|
SpotBalanceType.BORROW
|
|
249
252
|
);
|
|
250
253
|
|
|
254
|
+
if (delta.gt(ZERO)) {
|
|
255
|
+
tokenDepositAmount = tokenDepositAmount.add(delta);
|
|
256
|
+
} else if (delta.lt(ZERO)) {
|
|
257
|
+
tokenBorrowAmount = tokenBorrowAmount.add(delta.abs());
|
|
258
|
+
}
|
|
259
|
+
|
|
251
260
|
let utilization: BN;
|
|
252
261
|
if (tokenBorrowAmount.eq(ZERO) && tokenDepositAmount.eq(ZERO)) {
|
|
253
262
|
utilization = ZERO;
|
|
@@ -329,9 +338,11 @@ export function calculateSpotMarketBorrowCapacity(
|
|
|
329
338
|
}
|
|
330
339
|
}
|
|
331
340
|
|
|
332
|
-
export function calculateInterestRate(
|
|
333
|
-
|
|
334
|
-
|
|
341
|
+
export function calculateInterestRate(
|
|
342
|
+
bank: SpotMarketAccount,
|
|
343
|
+
delta = ZERO
|
|
344
|
+
): BN {
|
|
345
|
+
const utilization = calculateUtilization(bank, delta);
|
|
335
346
|
let interestRate: BN;
|
|
336
347
|
if (utilization.gt(new BN(bank.optimalUtilization))) {
|
|
337
348
|
const surplusUtilization = utilization.sub(new BN(bank.optimalUtilization));
|
package/src/math/superStake.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AddressLookupTableAccount,
|
|
3
|
+
LAMPORTS_PER_SOL,
|
|
3
4
|
PublicKey,
|
|
4
5
|
TransactionInstruction,
|
|
5
6
|
} from '@solana/web3.js';
|
|
@@ -7,6 +8,9 @@ import { JupiterClient } from '../jupiter/jupiterClient';
|
|
|
7
8
|
import { DriftClient } from '../driftClient';
|
|
8
9
|
import { getMarinadeFinanceProgram, getMarinadeMSolPrice } from '../marinade';
|
|
9
10
|
import { BN } from '@coral-xyz/anchor';
|
|
11
|
+
import { User } from '../user';
|
|
12
|
+
import { DepositRecord, isVariant } from '../types';
|
|
13
|
+
import { LAMPORTS_PRECISION, ZERO } from '../constants/numericConstants';
|
|
10
14
|
|
|
11
15
|
export async function findBestSuperStakeIxs({
|
|
12
16
|
amount,
|
|
@@ -63,3 +67,66 @@ export async function findBestSuperStakeIxs({
|
|
|
63
67
|
};
|
|
64
68
|
}
|
|
65
69
|
}
|
|
70
|
+
|
|
71
|
+
export async function calculateSolEarned({
|
|
72
|
+
user,
|
|
73
|
+
depositRecords,
|
|
74
|
+
}: {
|
|
75
|
+
user: User;
|
|
76
|
+
depositRecords: DepositRecord[];
|
|
77
|
+
}): Promise<BN> {
|
|
78
|
+
const now = Date.now() / 1000;
|
|
79
|
+
const timestamps: number[] = [
|
|
80
|
+
now,
|
|
81
|
+
...depositRecords.map((r) => r.ts.toNumber()),
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
const msolRatios = new Map<number, number>();
|
|
85
|
+
|
|
86
|
+
const getPrice = async (timestamp) => {
|
|
87
|
+
const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
|
|
88
|
+
const swaggerApiDateTime = date.toISOString(); // Format date as swagger API date-time
|
|
89
|
+
const url = `https://api.marinade.finance/msol/price_sol?time=${swaggerApiDateTime}`;
|
|
90
|
+
const response = await fetch(url);
|
|
91
|
+
if (response.status === 200) {
|
|
92
|
+
const data = await response.json();
|
|
93
|
+
msolRatios.set(timestamp, data);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
await Promise.all(timestamps.map(getPrice));
|
|
98
|
+
|
|
99
|
+
let solEarned = ZERO;
|
|
100
|
+
for (const record of depositRecords) {
|
|
101
|
+
if (record.marketIndex === 1) {
|
|
102
|
+
if (isVariant(record.explanation, 'deposit')) {
|
|
103
|
+
solEarned = solEarned.sub(record.amount);
|
|
104
|
+
} else {
|
|
105
|
+
solEarned = solEarned.add(record.amount);
|
|
106
|
+
}
|
|
107
|
+
} else if (record.marketIndex === 2) {
|
|
108
|
+
const msolRatio = msolRatios.get(record.ts.toNumber());
|
|
109
|
+
const msolRatioBN = new BN(msolRatio * LAMPORTS_PER_SOL);
|
|
110
|
+
|
|
111
|
+
const solAmount = record.amount.mul(msolRatioBN).div(LAMPORTS_PRECISION);
|
|
112
|
+
if (isVariant(record.explanation, 'deposit')) {
|
|
113
|
+
solEarned = solEarned.sub(solAmount);
|
|
114
|
+
} else {
|
|
115
|
+
solEarned = solEarned.add(solAmount);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const currentMSOLTokenAmount = await user.getTokenAmount(2);
|
|
121
|
+
const currentSOLTokenAmount = await user.getTokenAmount(1);
|
|
122
|
+
|
|
123
|
+
const currentMSOLRatio = msolRatios.get(now);
|
|
124
|
+
const currentMSOLRatioBN = new BN(currentMSOLRatio * LAMPORTS_PER_SOL);
|
|
125
|
+
|
|
126
|
+
solEarned = solEarned.add(
|
|
127
|
+
currentMSOLTokenAmount.mul(currentMSOLRatioBN).div(LAMPORTS_PRECISION)
|
|
128
|
+
);
|
|
129
|
+
solEarned = solEarned.add(currentSOLTokenAmount);
|
|
130
|
+
|
|
131
|
+
return solEarned;
|
|
132
|
+
}
|
package/tests/spot/test.ts
CHANGED
|
@@ -1,102 +1,156 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
BN,
|
|
3
|
+
ZERO,
|
|
4
|
+
calculateSpotMarketBorrowCapacity,
|
|
5
|
+
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION,
|
|
6
|
+
} from '../../src';
|
|
2
7
|
import { mockSpotMarkets } from '../dlob/helpers';
|
|
3
8
|
|
|
4
9
|
import { assert } from '../../src/assert/assert';
|
|
5
|
-
import { SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION } from '@drift-labs/sdk';
|
|
6
10
|
|
|
7
11
|
describe('Spot Tests', () => {
|
|
8
12
|
it('base borrow capacity', () => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
13
|
+
const mockSpot = mockSpotMarkets[0];
|
|
14
|
+
mockSpot.maxBorrowRate = 1000000;
|
|
15
|
+
mockSpot.optimalBorrowRate = 100000;
|
|
16
|
+
mockSpot.optimalUtilization = 700000;
|
|
17
|
+
|
|
18
|
+
mockSpot.decimals = 9;
|
|
19
|
+
mockSpot.cumulativeDepositInterest =
|
|
20
|
+
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION;
|
|
21
|
+
mockSpot.cumulativeBorrowInterest =
|
|
22
|
+
SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION;
|
|
23
|
+
|
|
24
|
+
const tokenAmount = 100000;
|
|
25
|
+
// no borrows
|
|
26
|
+
mockSpot.depositBalance = new BN(tokenAmount * 1e9);
|
|
27
|
+
mockSpot.borrowBalance = ZERO;
|
|
28
|
+
|
|
29
|
+
// todo, should incorp all other spot market constraints?
|
|
30
|
+
const aboveMaxAmount = calculateSpotMarketBorrowCapacity(
|
|
31
|
+
mockSpot,
|
|
32
|
+
new BN(2000000)
|
|
33
|
+
);
|
|
34
|
+
assert(aboveMaxAmount.gt(mockSpot.depositBalance));
|
|
35
|
+
|
|
36
|
+
const maxAmount = calculateSpotMarketBorrowCapacity(
|
|
37
|
+
mockSpot,
|
|
38
|
+
new BN(1000000)
|
|
39
|
+
);
|
|
40
|
+
assert(maxAmount.eq(mockSpot.depositBalance));
|
|
41
|
+
|
|
42
|
+
const optAmount = calculateSpotMarketBorrowCapacity(
|
|
43
|
+
mockSpot,
|
|
44
|
+
new BN(100000)
|
|
45
|
+
);
|
|
46
|
+
const ans = new BN((mockSpot.depositBalance.toNumber() * 7) / 10);
|
|
47
|
+
// console.log('optAmount:', optAmount.toNumber(), ans.toNumber());
|
|
48
|
+
assert(optAmount.eq(ans));
|
|
49
|
+
|
|
50
|
+
const betweenOptMaxAmount = calculateSpotMarketBorrowCapacity(
|
|
51
|
+
mockSpot,
|
|
52
|
+
new BN(810000)
|
|
53
|
+
);
|
|
54
|
+
// console.log('betweenOptMaxAmount:', betweenOptMaxAmount.toNumber());
|
|
55
|
+
assert(betweenOptMaxAmount.lt(mockSpot.depositBalance));
|
|
56
|
+
assert(betweenOptMaxAmount.gt(ans));
|
|
57
|
+
assert(betweenOptMaxAmount.eq(new BN(93666600000000)));
|
|
58
|
+
|
|
59
|
+
const belowOptAmount = calculateSpotMarketBorrowCapacity(
|
|
60
|
+
mockSpot,
|
|
61
|
+
new BN(50000)
|
|
62
|
+
);
|
|
63
|
+
// console.log('belowOptAmount:', belowOptAmount.toNumber());
|
|
64
|
+
assert(belowOptAmount.eq(ans.div(new BN(2))));
|
|
65
|
+
|
|
66
|
+
const belowOptAmount2 = calculateSpotMarketBorrowCapacity(
|
|
67
|
+
mockSpot,
|
|
68
|
+
new BN(24900)
|
|
69
|
+
);
|
|
70
|
+
// console.log('belowOptAmount2:', belowOptAmount2.toNumber());
|
|
71
|
+
assert(belowOptAmount2.lt(ans.div(new BN(4))));
|
|
72
|
+
assert(belowOptAmount2.eq(new BN('17430000000000')));
|
|
73
|
+
|
|
74
|
+
const belowOptAmount3 = calculateSpotMarketBorrowCapacity(
|
|
75
|
+
mockSpot,
|
|
76
|
+
new BN(1)
|
|
77
|
+
);
|
|
78
|
+
// console.log('belowOptAmount3:', belowOptAmount3.toNumber());
|
|
79
|
+
assert(belowOptAmount3.eq(new BN('700000000'))); //0.7
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('complex borrow capacity', () => {
|
|
83
|
+
const mockSpot = mockSpotMarkets[0];
|
|
84
|
+
mockSpot.maxBorrowRate = 1000000;
|
|
85
|
+
mockSpot.optimalBorrowRate = 70000;
|
|
86
|
+
mockSpot.optimalUtilization = 700000;
|
|
87
|
+
|
|
88
|
+
mockSpot.decimals = 9;
|
|
89
|
+
mockSpot.cumulativeDepositInterest = new BN(
|
|
90
|
+
1.0154217042 * SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION.toNumber()
|
|
91
|
+
);
|
|
92
|
+
mockSpot.cumulativeBorrowInterest = new BN(
|
|
93
|
+
1.0417153549 * SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION.toNumber()
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
mockSpot.depositBalance = new BN(88522.734106451 * 1e9);
|
|
97
|
+
mockSpot.borrowBalance = new BN(7089.91675884 * 1e9);
|
|
98
|
+
|
|
99
|
+
// todo, should incorp all other spot market constraints?
|
|
100
|
+
const aboveMaxAmount = calculateSpotMarketBorrowCapacity(
|
|
101
|
+
mockSpot,
|
|
102
|
+
new BN(2000000)
|
|
103
|
+
);
|
|
104
|
+
assert(aboveMaxAmount.eq(new BN('111498270939007')));
|
|
105
|
+
|
|
106
|
+
const maxAmount = calculateSpotMarketBorrowCapacity(
|
|
107
|
+
mockSpot,
|
|
108
|
+
new BN(1000000)
|
|
109
|
+
);
|
|
110
|
+
assert(maxAmount.eq(new BN('82502230374168')));
|
|
111
|
+
// console.log('aboveMaxAmount:', aboveMaxAmount.toNumber(), 'maxAmount:', maxAmount.toNumber());
|
|
112
|
+
const optAmount = calculateSpotMarketBorrowCapacity(
|
|
113
|
+
mockSpot,
|
|
114
|
+
new BN(70000)
|
|
115
|
+
);
|
|
116
|
+
// console.log('optAmount:', optAmount.toNumber());
|
|
117
|
+
assert(optAmount.eq(new BN('55535858716123'))); // ~ 55535
|
|
118
|
+
|
|
119
|
+
const betweenOptMaxAmount = calculateSpotMarketBorrowCapacity(
|
|
120
|
+
mockSpot,
|
|
121
|
+
new BN(810000)
|
|
122
|
+
);
|
|
123
|
+
// console.log('betweenOptMaxAmount:', betweenOptMaxAmount.toNumber());
|
|
124
|
+
assert(betweenOptMaxAmount.lt(maxAmount));
|
|
125
|
+
assert(betweenOptMaxAmount.eq(new BN(76992910756523)));
|
|
126
|
+
assert(betweenOptMaxAmount.gt(optAmount));
|
|
127
|
+
|
|
128
|
+
const belowOptAmount = calculateSpotMarketBorrowCapacity(
|
|
129
|
+
mockSpot,
|
|
130
|
+
new BN(50000)
|
|
131
|
+
);
|
|
132
|
+
// console.log('belowOptAmount:', belowOptAmount.toNumber());
|
|
133
|
+
assert(belowOptAmount.eq(new BN('37558277610760')));
|
|
134
|
+
|
|
135
|
+
const belowOptAmount2 = calculateSpotMarketBorrowCapacity(
|
|
136
|
+
mockSpot,
|
|
137
|
+
new BN(24900)
|
|
138
|
+
);
|
|
139
|
+
// console.log('belowOptAmount2:', belowOptAmount2.toNumber());
|
|
140
|
+
assert(belowOptAmount2.eq(new BN('14996413323529')));
|
|
141
|
+
|
|
142
|
+
const belowOptAmount3 = calculateSpotMarketBorrowCapacity(
|
|
143
|
+
mockSpot,
|
|
144
|
+
new BN(4900)
|
|
145
|
+
);
|
|
146
|
+
// console.log('belowOptAmount2:', belowOptAmount3.toNumber());
|
|
147
|
+
assert(belowOptAmount3.eq(new BN('0')));
|
|
148
|
+
|
|
149
|
+
const belowOptAmount4 = calculateSpotMarketBorrowCapacity(
|
|
150
|
+
mockSpot,
|
|
151
|
+
new BN(1)
|
|
152
|
+
);
|
|
153
|
+
// console.log('belowOptAmount3:', belowOptAmount4.toNumber());
|
|
154
|
+
assert(belowOptAmount4.eq(new BN('0')));
|
|
155
|
+
});
|
|
156
|
+
});
|