@drift-labs/sdk 2.114.0-beta.5 → 2.114.0-beta.7
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/browser/driftClient.d.ts +1 -0
- package/lib/browser/driftClient.js +31 -5
- package/lib/browser/math/funding.d.ts +5 -5
- package/lib/browser/math/funding.js +7 -7
- package/lib/browser/math/superStake.d.ts +2 -3
- package/lib/node/driftClient.d.ts +1 -0
- package/lib/node/driftClient.js +31 -5
- package/lib/node/math/funding.d.ts +5 -5
- package/lib/node/math/funding.js +7 -7
- package/lib/node/math/superStake.d.ts +2 -3
- package/package.json +2 -2
- package/src/driftClient.ts +41 -6
- package/src/math/funding.ts +17 -23
- package/tests/amm/test.ts +11 -11
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.114.0-beta.
|
|
1
|
+
2.114.0-beta.7
|
|
@@ -912,6 +912,7 @@ export declare class DriftClient {
|
|
|
912
912
|
}, feedId: string, encodedVaaAddress: PublicKey): Promise<TransactionInstruction>;
|
|
913
913
|
postPythLazerOracleUpdate(feedIds: number[], pythMessageHex: string): Promise<string>;
|
|
914
914
|
getPostPythLazerOracleUpdateIxs(feedIds: number[], pythMessageHex: string, precedingIxs?: TransactionInstruction[], overrideCustomIxIndex?: number): Promise<TransactionInstruction[]>;
|
|
915
|
+
getPostManySwitchboardOnDemandUpdatesAtomicIxs(feeds: PublicKey[], recentSlothash?: Slothash, numSignatures?: number): Promise<TransactionInstruction[] | undefined>;
|
|
915
916
|
getPostSwitchboardOnDemandUpdateAtomicIx(feed: PublicKey, recentSlothash?: Slothash, numSignatures?: number): Promise<TransactionInstruction | undefined>;
|
|
916
917
|
postSwitchboardOnDemandUpdate(feed: PublicKey, recentSlothash?: Slothash, numSignatures?: number): Promise<TransactionSignature>;
|
|
917
918
|
private getBuildEncodedVaaIxs;
|
|
@@ -4848,6 +4848,30 @@ class DriftClient {
|
|
|
4848
4848
|
});
|
|
4849
4849
|
return [verifyIx, ix];
|
|
4850
4850
|
}
|
|
4851
|
+
async getPostManySwitchboardOnDemandUpdatesAtomicIxs(feeds, recentSlothash, numSignatures = 3) {
|
|
4852
|
+
const program = await this.getSwitchboardOnDemandProgram();
|
|
4853
|
+
for (const feed of feeds) {
|
|
4854
|
+
const feedAccount = new on_demand_1.PullFeed(program, feed);
|
|
4855
|
+
if (!this.sbProgramFeedConfigs) {
|
|
4856
|
+
this.sbProgramFeedConfigs = new Map();
|
|
4857
|
+
}
|
|
4858
|
+
if (!this.sbProgramFeedConfigs.has(feedAccount.pubkey.toString())) {
|
|
4859
|
+
const feedConfig = await feedAccount.loadConfigs();
|
|
4860
|
+
this.sbProgramFeedConfigs.set(feed.toString(), feedConfig);
|
|
4861
|
+
}
|
|
4862
|
+
}
|
|
4863
|
+
const [pullIxs, _responses, success] = await on_demand_1.PullFeed.fetchUpdateManyIx(program, {
|
|
4864
|
+
feeds,
|
|
4865
|
+
numSignatures,
|
|
4866
|
+
recentSlothashes: recentSlothash
|
|
4867
|
+
? [[new anchor_1.BN(recentSlothash.slot), recentSlothash.hash]]
|
|
4868
|
+
: undefined,
|
|
4869
|
+
});
|
|
4870
|
+
if (!success) {
|
|
4871
|
+
return undefined;
|
|
4872
|
+
}
|
|
4873
|
+
return pullIxs;
|
|
4874
|
+
}
|
|
4851
4875
|
async getPostSwitchboardOnDemandUpdateAtomicIx(feed, recentSlothash, numSignatures = 3) {
|
|
4852
4876
|
const program = await this.getSwitchboardOnDemandProgram();
|
|
4853
4877
|
const feedAccount = new on_demand_1.PullFeed(program, feed);
|
|
@@ -4858,15 +4882,17 @@ class DriftClient {
|
|
|
4858
4882
|
const feedConfig = await feedAccount.loadConfigs();
|
|
4859
4883
|
this.sbProgramFeedConfigs.set(feed.toString(), feedConfig);
|
|
4860
4884
|
}
|
|
4861
|
-
const [pullIx, _responses, success] = await
|
|
4885
|
+
const [pullIx, _responses, success] = await on_demand_1.PullFeed.fetchUpdateManyIx(program, {
|
|
4886
|
+
feeds: [feed],
|
|
4862
4887
|
numSignatures,
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4888
|
+
recentSlothashes: recentSlothash
|
|
4889
|
+
? [[new anchor_1.BN(recentSlothash.slot), recentSlothash.hash]]
|
|
4890
|
+
: undefined,
|
|
4891
|
+
});
|
|
4866
4892
|
if (!success) {
|
|
4867
4893
|
return undefined;
|
|
4868
4894
|
}
|
|
4869
|
-
return pullIx;
|
|
4895
|
+
return pullIx[0];
|
|
4870
4896
|
}
|
|
4871
4897
|
async postSwitchboardOnDemandUpdate(feed, recentSlothash, numSignatures = 3) {
|
|
4872
4898
|
const pullIx = await this.getPostSwitchboardOnDemandUpdateAtomicIx(feed, recentSlothash, numSignatures);
|
|
@@ -9,17 +9,17 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
9
9
|
* @param periodAdjustment
|
|
10
10
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
11
11
|
*/
|
|
12
|
-
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN):
|
|
12
|
+
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN, BN];
|
|
13
13
|
/**
|
|
14
14
|
* Calculate funding rates in human-readable form. Values will have some lost precision and shouldn't be used in strict accounting.
|
|
15
15
|
* @param period : 'hour' | 'year' :: Use 'hour' for the hourly payment as a percentage, 'year' for the payment as an estimated APR.
|
|
16
16
|
*/
|
|
17
|
-
export declare function calculateFormattedLiveFundingRate(market: PerpMarketAccount, oraclePriceData: OraclePriceData, period: 'hour' | 'year'):
|
|
17
|
+
export declare function calculateFormattedLiveFundingRate(market: PerpMarketAccount, oraclePriceData: OraclePriceData, period: 'hour' | 'year'): {
|
|
18
18
|
longRate: number;
|
|
19
19
|
shortRate: number;
|
|
20
20
|
fundingRateUnit: string;
|
|
21
21
|
formattedFundingRateSummary: string;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
/**
|
|
24
24
|
*
|
|
25
25
|
* @param market
|
|
@@ -27,7 +27,7 @@ export declare function calculateFormattedLiveFundingRate(market: PerpMarketAcco
|
|
|
27
27
|
* @param periodAdjustment
|
|
28
28
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
29
29
|
*/
|
|
30
|
-
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN):
|
|
30
|
+
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN];
|
|
31
31
|
/**
|
|
32
32
|
*
|
|
33
33
|
* @param market
|
|
@@ -35,7 +35,7 @@ export declare function calculateLongShortFundingRate(market: PerpMarketAccount,
|
|
|
35
35
|
* @param periodAdjustment
|
|
36
36
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
37
37
|
*/
|
|
38
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN):
|
|
38
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN];
|
|
39
39
|
/**
|
|
40
40
|
*
|
|
41
41
|
* @param market
|
|
@@ -58,7 +58,7 @@ function shrinkStaleTwaps(market, markTwapWithMantissa, oracleTwapWithMantissa,
|
|
|
58
58
|
* @param periodAdjustment
|
|
59
59
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
function calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now) {
|
|
62
62
|
if ((0, types_1.isVariant)(market.status, 'uninitialized')) {
|
|
63
63
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
64
64
|
}
|
|
@@ -152,9 +152,9 @@ const getFundingRatePct = (rawFundingRate) => {
|
|
|
152
152
|
* Calculate funding rates in human-readable form. Values will have some lost precision and shouldn't be used in strict accounting.
|
|
153
153
|
* @param period : 'hour' | 'year' :: Use 'hour' for the hourly payment as a percentage, 'year' for the payment as an estimated APR.
|
|
154
154
|
*/
|
|
155
|
-
|
|
155
|
+
function calculateFormattedLiveFundingRate(market, oraclePriceData, period) {
|
|
156
156
|
const nowBN = new anchor_1.BN(Date.now() / 1000);
|
|
157
|
-
const [_markTwapLive, _oracleTwapLive, longFundingRate, shortFundingRate] =
|
|
157
|
+
const [_markTwapLive, _oracleTwapLive, longFundingRate, shortFundingRate] = calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, undefined, nowBN);
|
|
158
158
|
let longFundingRateNum = getFundingRatePct(longFundingRate);
|
|
159
159
|
let shortFundingRateNum = getFundingRatePct(shortFundingRate);
|
|
160
160
|
if (period == 'year') {
|
|
@@ -205,8 +205,8 @@ function getMaxPriceDivergenceForFundingRate(market, oracleTwap) {
|
|
|
205
205
|
* @param periodAdjustment
|
|
206
206
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
207
207
|
*/
|
|
208
|
-
|
|
209
|
-
const [_1, _2, _, cappedAltEst, interpEst] =
|
|
208
|
+
function calculateLongShortFundingRate(market, oraclePriceData, markPrice, now) {
|
|
209
|
+
const [_1, _2, _, cappedAltEst, interpEst] = calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now);
|
|
210
210
|
if (market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort)) {
|
|
211
211
|
return [cappedAltEst, interpEst];
|
|
212
212
|
}
|
|
@@ -225,8 +225,8 @@ exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
|
225
225
|
* @param periodAdjustment
|
|
226
226
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
227
227
|
*/
|
|
228
|
-
|
|
229
|
-
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] =
|
|
228
|
+
function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, markPrice, now) {
|
|
229
|
+
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now);
|
|
230
230
|
if (market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort.abs())) {
|
|
231
231
|
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
232
232
|
}
|
|
@@ -5,7 +5,6 @@ import { DriftClient } from '../driftClient';
|
|
|
5
5
|
import { BN } from '@coral-xyz/anchor';
|
|
6
6
|
import { User } from '../user';
|
|
7
7
|
import { DepositRecord } from '../types';
|
|
8
|
-
import fetch from 'node-fetch';
|
|
9
8
|
export type BSOL_STATS_API_RESPONSE = {
|
|
10
9
|
success: boolean;
|
|
11
10
|
stats?: {
|
|
@@ -28,8 +27,8 @@ export type BSOL_EMISSIONS_API_RESPONSE = {
|
|
|
28
27
|
lend: number;
|
|
29
28
|
};
|
|
30
29
|
};
|
|
31
|
-
export declare function fetchBSolMetrics(): Promise<
|
|
32
|
-
export declare function fetchBSolDriftEmissions(): Promise<
|
|
30
|
+
export declare function fetchBSolMetrics(): Promise<any>;
|
|
31
|
+
export declare function fetchBSolDriftEmissions(): Promise<any>;
|
|
33
32
|
export declare function findBestSuperStakeIxs({ marketIndex, amount, jupiterClient, driftClient, userAccountPublicKey, price, forceMarinade, onlyDirectRoutes, jupiterQuote, }: {
|
|
34
33
|
marketIndex: number;
|
|
35
34
|
amount: BN;
|
|
@@ -912,6 +912,7 @@ export declare class DriftClient {
|
|
|
912
912
|
}, feedId: string, encodedVaaAddress: PublicKey): Promise<TransactionInstruction>;
|
|
913
913
|
postPythLazerOracleUpdate(feedIds: number[], pythMessageHex: string): Promise<string>;
|
|
914
914
|
getPostPythLazerOracleUpdateIxs(feedIds: number[], pythMessageHex: string, precedingIxs?: TransactionInstruction[], overrideCustomIxIndex?: number): Promise<TransactionInstruction[]>;
|
|
915
|
+
getPostManySwitchboardOnDemandUpdatesAtomicIxs(feeds: PublicKey[], recentSlothash?: Slothash, numSignatures?: number): Promise<TransactionInstruction[] | undefined>;
|
|
915
916
|
getPostSwitchboardOnDemandUpdateAtomicIx(feed: PublicKey, recentSlothash?: Slothash, numSignatures?: number): Promise<TransactionInstruction | undefined>;
|
|
916
917
|
postSwitchboardOnDemandUpdate(feed: PublicKey, recentSlothash?: Slothash, numSignatures?: number): Promise<TransactionSignature>;
|
|
917
918
|
private getBuildEncodedVaaIxs;
|
package/lib/node/driftClient.js
CHANGED
|
@@ -4848,6 +4848,30 @@ class DriftClient {
|
|
|
4848
4848
|
});
|
|
4849
4849
|
return [verifyIx, ix];
|
|
4850
4850
|
}
|
|
4851
|
+
async getPostManySwitchboardOnDemandUpdatesAtomicIxs(feeds, recentSlothash, numSignatures = 3) {
|
|
4852
|
+
const program = await this.getSwitchboardOnDemandProgram();
|
|
4853
|
+
for (const feed of feeds) {
|
|
4854
|
+
const feedAccount = new on_demand_1.PullFeed(program, feed);
|
|
4855
|
+
if (!this.sbProgramFeedConfigs) {
|
|
4856
|
+
this.sbProgramFeedConfigs = new Map();
|
|
4857
|
+
}
|
|
4858
|
+
if (!this.sbProgramFeedConfigs.has(feedAccount.pubkey.toString())) {
|
|
4859
|
+
const feedConfig = await feedAccount.loadConfigs();
|
|
4860
|
+
this.sbProgramFeedConfigs.set(feed.toString(), feedConfig);
|
|
4861
|
+
}
|
|
4862
|
+
}
|
|
4863
|
+
const [pullIxs, _responses, success] = await on_demand_1.PullFeed.fetchUpdateManyIx(program, {
|
|
4864
|
+
feeds,
|
|
4865
|
+
numSignatures,
|
|
4866
|
+
recentSlothashes: recentSlothash
|
|
4867
|
+
? [[new anchor_1.BN(recentSlothash.slot), recentSlothash.hash]]
|
|
4868
|
+
: undefined,
|
|
4869
|
+
});
|
|
4870
|
+
if (!success) {
|
|
4871
|
+
return undefined;
|
|
4872
|
+
}
|
|
4873
|
+
return pullIxs;
|
|
4874
|
+
}
|
|
4851
4875
|
async getPostSwitchboardOnDemandUpdateAtomicIx(feed, recentSlothash, numSignatures = 3) {
|
|
4852
4876
|
const program = await this.getSwitchboardOnDemandProgram();
|
|
4853
4877
|
const feedAccount = new on_demand_1.PullFeed(program, feed);
|
|
@@ -4858,15 +4882,17 @@ class DriftClient {
|
|
|
4858
4882
|
const feedConfig = await feedAccount.loadConfigs();
|
|
4859
4883
|
this.sbProgramFeedConfigs.set(feed.toString(), feedConfig);
|
|
4860
4884
|
}
|
|
4861
|
-
const [pullIx, _responses, success] = await
|
|
4885
|
+
const [pullIx, _responses, success] = await on_demand_1.PullFeed.fetchUpdateManyIx(program, {
|
|
4886
|
+
feeds: [feed],
|
|
4862
4887
|
numSignatures,
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4888
|
+
recentSlothashes: recentSlothash
|
|
4889
|
+
? [[new anchor_1.BN(recentSlothash.slot), recentSlothash.hash]]
|
|
4890
|
+
: undefined,
|
|
4891
|
+
});
|
|
4866
4892
|
if (!success) {
|
|
4867
4893
|
return undefined;
|
|
4868
4894
|
}
|
|
4869
|
-
return pullIx;
|
|
4895
|
+
return pullIx[0];
|
|
4870
4896
|
}
|
|
4871
4897
|
async postSwitchboardOnDemandUpdate(feed, recentSlothash, numSignatures = 3) {
|
|
4872
4898
|
const pullIx = await this.getPostSwitchboardOnDemandUpdateAtomicIx(feed, recentSlothash, numSignatures);
|
|
@@ -9,17 +9,17 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
9
9
|
* @param periodAdjustment
|
|
10
10
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
11
11
|
*/
|
|
12
|
-
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN):
|
|
12
|
+
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN, BN];
|
|
13
13
|
/**
|
|
14
14
|
* Calculate funding rates in human-readable form. Values will have some lost precision and shouldn't be used in strict accounting.
|
|
15
15
|
* @param period : 'hour' | 'year' :: Use 'hour' for the hourly payment as a percentage, 'year' for the payment as an estimated APR.
|
|
16
16
|
*/
|
|
17
|
-
export declare function calculateFormattedLiveFundingRate(market: PerpMarketAccount, oraclePriceData: OraclePriceData, period: 'hour' | 'year'):
|
|
17
|
+
export declare function calculateFormattedLiveFundingRate(market: PerpMarketAccount, oraclePriceData: OraclePriceData, period: 'hour' | 'year'): {
|
|
18
18
|
longRate: number;
|
|
19
19
|
shortRate: number;
|
|
20
20
|
fundingRateUnit: string;
|
|
21
21
|
formattedFundingRateSummary: string;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
/**
|
|
24
24
|
*
|
|
25
25
|
* @param market
|
|
@@ -27,7 +27,7 @@ export declare function calculateFormattedLiveFundingRate(market: PerpMarketAcco
|
|
|
27
27
|
* @param periodAdjustment
|
|
28
28
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
29
29
|
*/
|
|
30
|
-
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN):
|
|
30
|
+
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN];
|
|
31
31
|
/**
|
|
32
32
|
*
|
|
33
33
|
* @param market
|
|
@@ -35,7 +35,7 @@ export declare function calculateLongShortFundingRate(market: PerpMarketAccount,
|
|
|
35
35
|
* @param periodAdjustment
|
|
36
36
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
37
37
|
*/
|
|
38
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN):
|
|
38
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN];
|
|
39
39
|
/**
|
|
40
40
|
*
|
|
41
41
|
* @param market
|
package/lib/node/math/funding.js
CHANGED
|
@@ -58,7 +58,7 @@ function shrinkStaleTwaps(market, markTwapWithMantissa, oracleTwapWithMantissa,
|
|
|
58
58
|
* @param periodAdjustment
|
|
59
59
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
function calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now) {
|
|
62
62
|
if ((0, types_1.isVariant)(market.status, 'uninitialized')) {
|
|
63
63
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
64
64
|
}
|
|
@@ -152,9 +152,9 @@ const getFundingRatePct = (rawFundingRate) => {
|
|
|
152
152
|
* Calculate funding rates in human-readable form. Values will have some lost precision and shouldn't be used in strict accounting.
|
|
153
153
|
* @param period : 'hour' | 'year' :: Use 'hour' for the hourly payment as a percentage, 'year' for the payment as an estimated APR.
|
|
154
154
|
*/
|
|
155
|
-
|
|
155
|
+
function calculateFormattedLiveFundingRate(market, oraclePriceData, period) {
|
|
156
156
|
const nowBN = new anchor_1.BN(Date.now() / 1000);
|
|
157
|
-
const [_markTwapLive, _oracleTwapLive, longFundingRate, shortFundingRate] =
|
|
157
|
+
const [_markTwapLive, _oracleTwapLive, longFundingRate, shortFundingRate] = calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, undefined, nowBN);
|
|
158
158
|
let longFundingRateNum = getFundingRatePct(longFundingRate);
|
|
159
159
|
let shortFundingRateNum = getFundingRatePct(shortFundingRate);
|
|
160
160
|
if (period == 'year') {
|
|
@@ -205,8 +205,8 @@ function getMaxPriceDivergenceForFundingRate(market, oracleTwap) {
|
|
|
205
205
|
* @param periodAdjustment
|
|
206
206
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
207
207
|
*/
|
|
208
|
-
|
|
209
|
-
const [_1, _2, _, cappedAltEst, interpEst] =
|
|
208
|
+
function calculateLongShortFundingRate(market, oraclePriceData, markPrice, now) {
|
|
209
|
+
const [_1, _2, _, cappedAltEst, interpEst] = calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now);
|
|
210
210
|
if (market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort)) {
|
|
211
211
|
return [cappedAltEst, interpEst];
|
|
212
212
|
}
|
|
@@ -225,8 +225,8 @@ exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
|
225
225
|
* @param periodAdjustment
|
|
226
226
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
227
227
|
*/
|
|
228
|
-
|
|
229
|
-
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] =
|
|
228
|
+
function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, markPrice, now) {
|
|
229
|
+
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now);
|
|
230
230
|
if (market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort.abs())) {
|
|
231
231
|
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
232
232
|
}
|
|
@@ -5,7 +5,6 @@ import { DriftClient } from '../driftClient';
|
|
|
5
5
|
import { BN } from '@coral-xyz/anchor';
|
|
6
6
|
import { User } from '../user';
|
|
7
7
|
import { DepositRecord } from '../types';
|
|
8
|
-
import fetch from 'node-fetch';
|
|
9
8
|
export type BSOL_STATS_API_RESPONSE = {
|
|
10
9
|
success: boolean;
|
|
11
10
|
stats?: {
|
|
@@ -28,8 +27,8 @@ export type BSOL_EMISSIONS_API_RESPONSE = {
|
|
|
28
27
|
lend: number;
|
|
29
28
|
};
|
|
30
29
|
};
|
|
31
|
-
export declare function fetchBSolMetrics(): Promise<
|
|
32
|
-
export declare function fetchBSolDriftEmissions(): Promise<
|
|
30
|
+
export declare function fetchBSolMetrics(): Promise<any>;
|
|
31
|
+
export declare function fetchBSolDriftEmissions(): Promise<any>;
|
|
33
32
|
export declare function findBestSuperStakeIxs({ marketIndex, amount, jupiterClient, driftClient, userAccountPublicKey, price, forceMarinade, onlyDirectRoutes, jupiterQuote, }: {
|
|
34
33
|
marketIndex: number;
|
|
35
34
|
amount: BN;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "2.114.0-beta.
|
|
3
|
+
"version": "2.114.0-beta.7",
|
|
4
4
|
"main": "lib/node/index.js",
|
|
5
5
|
"types": "lib/node/index.d.ts",
|
|
6
6
|
"browser": "./lib/browser/index.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@pythnetwork/pyth-solana-receiver": "0.7.0",
|
|
48
48
|
"@solana/spl-token": "0.3.7",
|
|
49
49
|
"@solana/web3.js": "1.92.3",
|
|
50
|
-
"@switchboard-xyz/on-demand": "
|
|
50
|
+
"@switchboard-xyz/on-demand": "2.2.0",
|
|
51
51
|
"@triton-one/yellowstone-grpc": "1.3.0",
|
|
52
52
|
"anchor-bankrun": "0.3.0",
|
|
53
53
|
"nanoid": "3.3.4",
|
package/src/driftClient.ts
CHANGED
|
@@ -9289,6 +9289,39 @@ export class DriftClient {
|
|
|
9289
9289
|
return [verifyIx, ix];
|
|
9290
9290
|
}
|
|
9291
9291
|
|
|
9292
|
+
public async getPostManySwitchboardOnDemandUpdatesAtomicIxs(
|
|
9293
|
+
feeds: PublicKey[],
|
|
9294
|
+
recentSlothash?: Slothash,
|
|
9295
|
+
numSignatures = 3
|
|
9296
|
+
): Promise<TransactionInstruction[] | undefined> {
|
|
9297
|
+
const program = await this.getSwitchboardOnDemandProgram();
|
|
9298
|
+
for (const feed of feeds) {
|
|
9299
|
+
const feedAccount = new PullFeed(program, feed);
|
|
9300
|
+
if (!this.sbProgramFeedConfigs) {
|
|
9301
|
+
this.sbProgramFeedConfigs = new Map();
|
|
9302
|
+
}
|
|
9303
|
+
if (!this.sbProgramFeedConfigs.has(feedAccount.pubkey.toString())) {
|
|
9304
|
+
const feedConfig = await feedAccount.loadConfigs();
|
|
9305
|
+
this.sbProgramFeedConfigs.set(feed.toString(), feedConfig);
|
|
9306
|
+
}
|
|
9307
|
+
}
|
|
9308
|
+
|
|
9309
|
+
const [pullIxs, _responses, success] = await PullFeed.fetchUpdateManyIx(
|
|
9310
|
+
program,
|
|
9311
|
+
{
|
|
9312
|
+
feeds,
|
|
9313
|
+
numSignatures,
|
|
9314
|
+
recentSlothashes: recentSlothash
|
|
9315
|
+
? [[new BN(recentSlothash.slot), recentSlothash.hash]]
|
|
9316
|
+
: undefined,
|
|
9317
|
+
}
|
|
9318
|
+
);
|
|
9319
|
+
if (!success) {
|
|
9320
|
+
return undefined;
|
|
9321
|
+
}
|
|
9322
|
+
return pullIxs;
|
|
9323
|
+
}
|
|
9324
|
+
|
|
9292
9325
|
public async getPostSwitchboardOnDemandUpdateAtomicIx(
|
|
9293
9326
|
feed: PublicKey,
|
|
9294
9327
|
recentSlothash?: Slothash,
|
|
@@ -9303,18 +9336,20 @@ export class DriftClient {
|
|
|
9303
9336
|
const feedConfig = await feedAccount.loadConfigs();
|
|
9304
9337
|
this.sbProgramFeedConfigs.set(feed.toString(), feedConfig);
|
|
9305
9338
|
}
|
|
9306
|
-
const [pullIx, _responses, success] = await
|
|
9339
|
+
const [pullIx, _responses, success] = await PullFeed.fetchUpdateManyIx(
|
|
9340
|
+
program,
|
|
9307
9341
|
{
|
|
9342
|
+
feeds: [feed],
|
|
9308
9343
|
numSignatures,
|
|
9309
|
-
|
|
9310
|
-
|
|
9311
|
-
|
|
9312
|
-
|
|
9344
|
+
recentSlothashes: recentSlothash
|
|
9345
|
+
? [[new BN(recentSlothash.slot), recentSlothash.hash]]
|
|
9346
|
+
: undefined,
|
|
9347
|
+
}
|
|
9313
9348
|
);
|
|
9314
9349
|
if (!success) {
|
|
9315
9350
|
return undefined;
|
|
9316
9351
|
}
|
|
9317
|
-
return pullIx;
|
|
9352
|
+
return pullIx[0];
|
|
9318
9353
|
}
|
|
9319
9354
|
|
|
9320
9355
|
public async postSwitchboardOnDemandUpdate(
|
package/src/math/funding.ts
CHANGED
|
@@ -120,12 +120,12 @@ function shrinkStaleTwaps(
|
|
|
120
120
|
* @param periodAdjustment
|
|
121
121
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
122
122
|
*/
|
|
123
|
-
export
|
|
123
|
+
export function calculateAllEstimatedFundingRate(
|
|
124
124
|
market: PerpMarketAccount,
|
|
125
125
|
oraclePriceData?: OraclePriceData,
|
|
126
126
|
markPrice?: BN,
|
|
127
127
|
now?: BN
|
|
128
|
-
):
|
|
128
|
+
): [BN, BN, BN, BN, BN] {
|
|
129
129
|
if (isVariant(market.status, 'uninitialized')) {
|
|
130
130
|
return [ZERO, ZERO, ZERO, ZERO, ZERO];
|
|
131
131
|
}
|
|
@@ -263,20 +263,20 @@ const getFundingRatePct = (rawFundingRate: BN) => {
|
|
|
263
263
|
* Calculate funding rates in human-readable form. Values will have some lost precision and shouldn't be used in strict accounting.
|
|
264
264
|
* @param period : 'hour' | 'year' :: Use 'hour' for the hourly payment as a percentage, 'year' for the payment as an estimated APR.
|
|
265
265
|
*/
|
|
266
|
-
export
|
|
266
|
+
export function calculateFormattedLiveFundingRate(
|
|
267
267
|
market: PerpMarketAccount,
|
|
268
268
|
oraclePriceData: OraclePriceData,
|
|
269
269
|
period: 'hour' | 'year'
|
|
270
|
-
):
|
|
270
|
+
): {
|
|
271
271
|
longRate: number;
|
|
272
272
|
shortRate: number;
|
|
273
273
|
fundingRateUnit: string;
|
|
274
274
|
formattedFundingRateSummary: string;
|
|
275
|
-
}
|
|
275
|
+
} {
|
|
276
276
|
const nowBN = new BN(Date.now() / 1000);
|
|
277
277
|
|
|
278
278
|
const [_markTwapLive, _oracleTwapLive, longFundingRate, shortFundingRate] =
|
|
279
|
-
|
|
279
|
+
calculateLongShortFundingRateAndLiveTwaps(
|
|
280
280
|
market,
|
|
281
281
|
oraclePriceData,
|
|
282
282
|
undefined,
|
|
@@ -347,19 +347,18 @@ function getMaxPriceDivergenceForFundingRate(
|
|
|
347
347
|
* @param periodAdjustment
|
|
348
348
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
349
349
|
*/
|
|
350
|
-
export
|
|
350
|
+
export function calculateLongShortFundingRate(
|
|
351
351
|
market: PerpMarketAccount,
|
|
352
352
|
oraclePriceData?: OraclePriceData,
|
|
353
353
|
markPrice?: BN,
|
|
354
354
|
now?: BN
|
|
355
|
-
):
|
|
356
|
-
const [_1, _2, _, cappedAltEst, interpEst] =
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
);
|
|
355
|
+
): [BN, BN] {
|
|
356
|
+
const [_1, _2, _, cappedAltEst, interpEst] = calculateAllEstimatedFundingRate(
|
|
357
|
+
market,
|
|
358
|
+
oraclePriceData,
|
|
359
|
+
markPrice,
|
|
360
|
+
now
|
|
361
|
+
);
|
|
363
362
|
|
|
364
363
|
if (market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort)) {
|
|
365
364
|
return [cappedAltEst, interpEst];
|
|
@@ -379,19 +378,14 @@ export async function calculateLongShortFundingRate(
|
|
|
379
378
|
* @param periodAdjustment
|
|
380
379
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
381
380
|
*/
|
|
382
|
-
export
|
|
381
|
+
export function calculateLongShortFundingRateAndLiveTwaps(
|
|
383
382
|
market: PerpMarketAccount,
|
|
384
383
|
oraclePriceData?: OraclePriceData,
|
|
385
384
|
markPrice?: BN,
|
|
386
385
|
now?: BN
|
|
387
|
-
):
|
|
386
|
+
): [BN, BN, BN, BN] {
|
|
388
387
|
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] =
|
|
389
|
-
|
|
390
|
-
market,
|
|
391
|
-
oraclePriceData,
|
|
392
|
-
markPrice,
|
|
393
|
-
now
|
|
394
|
-
);
|
|
388
|
+
calculateAllEstimatedFundingRate(market, oraclePriceData, markPrice, now);
|
|
395
389
|
|
|
396
390
|
if (
|
|
397
391
|
market.amm.baseAssetAmountLong.gt(market.amm.baseAssetAmountShort.abs())
|
package/tests/amm/test.ts
CHANGED
|
@@ -1303,7 +1303,7 @@ describe('AMM Tests', () => {
|
|
|
1303
1303
|
);
|
|
1304
1304
|
});
|
|
1305
1305
|
|
|
1306
|
-
it('predicted funding rate mock1',
|
|
1306
|
+
it('predicted funding rate mock1', () => {
|
|
1307
1307
|
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
|
|
1308
1308
|
const mockMarket1 = myMockPerpMarkets[0];
|
|
1309
1309
|
|
|
@@ -1350,7 +1350,7 @@ describe('AMM Tests', () => {
|
|
|
1350
1350
|
_lowerboundEst,
|
|
1351
1351
|
_cappedAltEst,
|
|
1352
1352
|
_interpEst,
|
|
1353
|
-
] =
|
|
1353
|
+
] = calculateAllEstimatedFundingRate(
|
|
1354
1354
|
mockMarket1,
|
|
1355
1355
|
oraclePriceData,
|
|
1356
1356
|
currentMarkPrice,
|
|
@@ -1358,7 +1358,7 @@ describe('AMM Tests', () => {
|
|
|
1358
1358
|
);
|
|
1359
1359
|
|
|
1360
1360
|
const [markTwapLive, oracleTwapLive, est1, est2] =
|
|
1361
|
-
|
|
1361
|
+
calculateLongShortFundingRateAndLiveTwaps(
|
|
1362
1362
|
mockMarket1,
|
|
1363
1363
|
oraclePriceData,
|
|
1364
1364
|
currentMarkPrice,
|
|
@@ -1376,7 +1376,7 @@ describe('AMM Tests', () => {
|
|
|
1376
1376
|
assert(est2.eq(new BN('16525')));
|
|
1377
1377
|
});
|
|
1378
1378
|
|
|
1379
|
-
it('predicted funding rate mock2',
|
|
1379
|
+
it('predicted funding rate mock2', () => {
|
|
1380
1380
|
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
|
|
1381
1381
|
const mockMarket1 = myMockPerpMarkets[0];
|
|
1382
1382
|
|
|
@@ -1423,7 +1423,7 @@ describe('AMM Tests', () => {
|
|
|
1423
1423
|
_lowerboundEst,
|
|
1424
1424
|
_cappedAltEst,
|
|
1425
1425
|
_interpEst,
|
|
1426
|
-
] =
|
|
1426
|
+
] = calculateAllEstimatedFundingRate(
|
|
1427
1427
|
mockMarket1,
|
|
1428
1428
|
oraclePriceData,
|
|
1429
1429
|
currentMarkPrice,
|
|
@@ -1438,7 +1438,7 @@ describe('AMM Tests', () => {
|
|
|
1438
1438
|
// console.log('-----');
|
|
1439
1439
|
|
|
1440
1440
|
const [markTwapLive, oracleTwapLive, est1, est2] =
|
|
1441
|
-
|
|
1441
|
+
calculateLongShortFundingRateAndLiveTwaps(
|
|
1442
1442
|
mockMarket1,
|
|
1443
1443
|
oraclePriceData,
|
|
1444
1444
|
currentMarkPrice,
|
|
@@ -1465,7 +1465,7 @@ describe('AMM Tests', () => {
|
|
|
1465
1465
|
assert(est2.eq(new BN('-719')));
|
|
1466
1466
|
});
|
|
1467
1467
|
|
|
1468
|
-
it('predicted funding rate mock clamp',
|
|
1468
|
+
it('predicted funding rate mock clamp', () => {
|
|
1469
1469
|
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
|
|
1470
1470
|
const mockMarket1 = myMockPerpMarkets[0];
|
|
1471
1471
|
|
|
@@ -1513,7 +1513,7 @@ describe('AMM Tests', () => {
|
|
|
1513
1513
|
_lowerboundEst,
|
|
1514
1514
|
_cappedAltEst,
|
|
1515
1515
|
_interpEst,
|
|
1516
|
-
] =
|
|
1516
|
+
] = calculateAllEstimatedFundingRate(
|
|
1517
1517
|
mockMarket1,
|
|
1518
1518
|
oraclePriceData,
|
|
1519
1519
|
currentMarkPrice,
|
|
@@ -1528,7 +1528,7 @@ describe('AMM Tests', () => {
|
|
|
1528
1528
|
// console.log('-----');
|
|
1529
1529
|
|
|
1530
1530
|
let [markTwapLive, oracleTwapLive, est1, est2] =
|
|
1531
|
-
|
|
1531
|
+
calculateLongShortFundingRateAndLiveTwaps(
|
|
1532
1532
|
mockMarket1,
|
|
1533
1533
|
oraclePriceData,
|
|
1534
1534
|
currentMarkPrice,
|
|
@@ -1557,7 +1557,7 @@ describe('AMM Tests', () => {
|
|
|
1557
1557
|
mockMarket1.contractTier = ContractTier.C;
|
|
1558
1558
|
|
|
1559
1559
|
[markTwapLive, oracleTwapLive, est1, est2] =
|
|
1560
|
-
|
|
1560
|
+
calculateLongShortFundingRateAndLiveTwaps(
|
|
1561
1561
|
mockMarket1,
|
|
1562
1562
|
oraclePriceData,
|
|
1563
1563
|
currentMarkPrice,
|
|
@@ -1586,7 +1586,7 @@ describe('AMM Tests', () => {
|
|
|
1586
1586
|
mockMarket1.contractTier = ContractTier.SPECULATIVE;
|
|
1587
1587
|
|
|
1588
1588
|
[markTwapLive, oracleTwapLive, est1, est2] =
|
|
1589
|
-
|
|
1589
|
+
calculateLongShortFundingRateAndLiveTwaps(
|
|
1590
1590
|
mockMarket1,
|
|
1591
1591
|
oraclePriceData,
|
|
1592
1592
|
currentMarkPrice,
|