@drift-labs/sdk 2.31.1-beta.16 → 2.31.1-beta.17
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/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/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.17
|
package/lib/idl/drift.json
CHANGED
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/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
|
+
});
|