@drift-labs/sdk 2.31.1-beta.15 → 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 CHANGED
@@ -1 +1 @@
1
- 2.31.1-beta.15
1
+ 2.31.1-beta.17
@@ -205,7 +205,11 @@ export declare class DriftClient {
205
205
  deposit(amount: BN, marketIndex: number, associatedTokenAccount: PublicKey, subAccountId?: number, reduceOnly?: boolean): Promise<TransactionSignature>;
206
206
  getDepositInstruction(amount: BN, marketIndex: number, userTokenAccount: PublicKey, subAccountId?: number, reduceOnly?: boolean, userInitialized?: boolean): Promise<TransactionInstruction>;
207
207
  private checkIfAccountExists;
208
- private getWrappedSolAccountCreationIxs;
208
+ getWrappedSolAccountCreationIxs(amount: BN, includeRent?: boolean): Promise<{
209
+ ixs: anchor.web3.TransactionInstruction[];
210
+ signers: Signer[];
211
+ pubkey: PublicKey;
212
+ }>;
209
213
  getAssociatedTokenAccountCreationIx(tokenMintAddress: PublicKey, associatedTokenAddress: PublicKey): anchor.web3.TransactionInstruction;
210
214
  /**
211
215
  * Creates the User account for a user, and deposits some initial collateral
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.14",
2
+ "version": "2.31.1-beta.16",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -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>;
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.31.1-beta.15",
3
+ "version": "2.31.1-beta.17",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1672,7 +1672,7 @@ export class DriftClient {
1672
1672
  }
1673
1673
  }
1674
1674
 
1675
- private async getWrappedSolAccountCreationIxs(
1675
+ public async getWrappedSolAccountCreationIxs(
1676
1676
  amount: BN,
1677
1677
  includeRent?: boolean
1678
1678
  ): Promise<{
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.15",
2
+ "version": "2.31.1-beta.17",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -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
+ }
@@ -1,102 +1,156 @@
1
- import { BN, ZERO, calculateSpotMarketBorrowCapacity } from '../../src';
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
- const mockSpot = mockSpotMarkets[0];
10
- mockSpot.maxBorrowRate = 1000000;
11
- mockSpot.optimalBorrowRate = 100000;
12
- mockSpot.optimalUtilization = 700000;
13
-
14
- mockSpot.decimals = 9;
15
- mockSpot.cumulativeDepositInterest = SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION;
16
- mockSpot.cumulativeBorrowInterest = SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION;
17
-
18
- const tokenAmount = 100000;
19
- // no borrows
20
- mockSpot.depositBalance = new BN(tokenAmount * 1e9);
21
- mockSpot.borrowBalance = ZERO;
22
-
23
- // todo, should incorp all other spot market constraints?
24
- const aboveMaxAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(2000000));
25
- assert(aboveMaxAmount.gt(mockSpot.depositBalance));
26
-
27
- const maxAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(1000000));
28
- assert(maxAmount.eq(mockSpot.depositBalance));
29
-
30
- const optAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(100000));
31
- const ans = new BN(mockSpot.depositBalance.toNumber() * 7 / 10);
32
- // console.log('optAmount:', optAmount.toNumber(), ans.toNumber());
33
- assert(optAmount.eq(ans));
34
-
35
- const betweenOptMaxAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(810000));
36
- // console.log('betweenOptMaxAmount:', betweenOptMaxAmount.toNumber());
37
- assert(betweenOptMaxAmount.lt(mockSpot.depositBalance));
38
- assert(betweenOptMaxAmount.gt(ans));
39
- assert(betweenOptMaxAmount.eq(new BN(93666600000000)));
40
-
41
- const belowOptAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(50000));
42
- // console.log('belowOptAmount:', belowOptAmount.toNumber());
43
- assert(belowOptAmount.eq(ans.div(new BN(2))));
44
-
45
- const belowOptAmount2 = calculateSpotMarketBorrowCapacity(mockSpot, new BN(24900));
46
- // console.log('belowOptAmount2:', belowOptAmount2.toNumber());
47
- assert(belowOptAmount2.lt(ans.div(new BN(4))));
48
- assert(belowOptAmount2.eq(new BN('17430000000000')));
49
-
50
- const belowOptAmount3 = calculateSpotMarketBorrowCapacity(mockSpot, new BN(1));
51
- // console.log('belowOptAmount3:', belowOptAmount3.toNumber());
52
- assert(belowOptAmount3.eq(new BN('700000000'))); //0.7
53
- });
54
-
55
-
56
- it('complex borrow capacity', () => {
57
- const mockSpot = mockSpotMarkets[0];
58
- mockSpot.maxBorrowRate = 1000000;
59
- mockSpot.optimalBorrowRate = 70000;
60
- mockSpot.optimalUtilization = 700000;
61
-
62
- mockSpot.decimals = 9;
63
- mockSpot.cumulativeDepositInterest = new BN(1.0154217042 * SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION.toNumber());
64
- mockSpot.cumulativeBorrowInterest = new BN(1.0417153549 * SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION.toNumber());
65
-
66
- mockSpot.depositBalance = new BN(88522.734106451 * 1e9);
67
- mockSpot.borrowBalance = new BN(7089.91675884 * 1e9);
68
-
69
- // todo, should incorp all other spot market constraints?
70
- const aboveMaxAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(2000000));
71
- assert(aboveMaxAmount.eq(new BN('111498270939007')));
72
-
73
- const maxAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(1000000));
74
- assert(maxAmount.eq(new BN('82502230374168')));
75
- // console.log('aboveMaxAmount:', aboveMaxAmount.toNumber(), 'maxAmount:', maxAmount.toNumber());
76
- const optAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(70000));
77
- // console.log('optAmount:', optAmount.toNumber());
78
- assert(optAmount.eq(new BN('55535858716123'))); // ~ 55535
79
-
80
- const betweenOptMaxAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(810000));
81
- // console.log('betweenOptMaxAmount:', betweenOptMaxAmount.toNumber());
82
- assert(betweenOptMaxAmount.lt(maxAmount));
83
- assert(betweenOptMaxAmount.eq(new BN(76992910756523)));
84
- assert(betweenOptMaxAmount.gt(optAmount));
85
-
86
- const belowOptAmount = calculateSpotMarketBorrowCapacity(mockSpot, new BN(50000));
87
- // console.log('belowOptAmount:', belowOptAmount.toNumber());
88
- assert(belowOptAmount.eq(new BN('37558277610760')));
89
-
90
- const belowOptAmount2 = calculateSpotMarketBorrowCapacity(mockSpot, new BN(24900));
91
- // console.log('belowOptAmount2:', belowOptAmount2.toNumber());
92
- assert(belowOptAmount2.eq(new BN('14996413323529')));
93
-
94
- const belowOptAmount3 = calculateSpotMarketBorrowCapacity(mockSpot, new BN(4900));
95
- // console.log('belowOptAmount2:', belowOptAmount3.toNumber());
96
- assert(belowOptAmount3.eq(new BN('0')));
97
-
98
- const belowOptAmount4 = calculateSpotMarketBorrowCapacity(mockSpot, new BN(1));
99
- // console.log('belowOptAmount3:', belowOptAmount4.toNumber());
100
- assert(belowOptAmount4.eq(new BN('0')));
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
+ });