@drift-labs/sdk 2.60.0-beta.13 → 2.60.0-beta.15
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/accounts/webSocketProgramAccountSubscriber.js +4 -0
- package/lib/auctionSubscriber/auctionSubscriber.js +1 -1
- package/lib/math/trade.d.ts +2 -1
- package/lib/math/trade.js +15 -1
- package/lib/user.d.ts +1 -1
- package/lib/user.js +2 -2
- package/package.json +1 -1
- package/src/accounts/webSocketProgramAccountSubscriber.ts +6 -0
- package/src/auctionSubscriber/auctionSubscriber.ts +1 -1
- package/src/math/trade.ts +27 -0
- package/src/user.ts +6 -4
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.60.0-beta.
|
|
1
|
+
2.60.0-beta.15
|
|
@@ -12,6 +12,9 @@ class WebSocketProgramAccountSubscriber {
|
|
|
12
12
|
this.program = program;
|
|
13
13
|
this.decodeBuffer = decodeBufferFn;
|
|
14
14
|
this.resubTimeoutMs = resubTimeoutMs;
|
|
15
|
+
if (this.resubTimeoutMs < 1000) {
|
|
16
|
+
console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
|
|
17
|
+
}
|
|
15
18
|
this.options = options;
|
|
16
19
|
this.receivingData = false;
|
|
17
20
|
}
|
|
@@ -33,6 +36,7 @@ class WebSocketProgramAccountSubscriber {
|
|
|
33
36
|
}
|
|
34
37
|
}, (_a = this.options.commitment) !== null && _a !== void 0 ? _a : this.program.provider.opts.commitment, this.options.filters);
|
|
35
38
|
if (this.resubTimeoutMs) {
|
|
39
|
+
this.receivingData = true;
|
|
36
40
|
this.setTimeout();
|
|
37
41
|
}
|
|
38
42
|
}
|
|
@@ -15,7 +15,7 @@ class AuctionSubscriber {
|
|
|
15
15
|
if (!this.subscriber) {
|
|
16
16
|
this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('AuctionSubscriber', 'User', this.driftClient.program, this.driftClient.program.account.user.coder.accounts.decode.bind(this.driftClient.program.account.user.coder.accounts), {
|
|
17
17
|
filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getUserWithAuctionFilter)()],
|
|
18
|
-
commitment: this.
|
|
18
|
+
commitment: this.opts.commitment,
|
|
19
19
|
}, this.resubTimeoutMs);
|
|
20
20
|
}
|
|
21
21
|
await this.subscriber.subscribe((accountId, data, context) => {
|
package/lib/math/trade.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import { PerpMarketAccount, PositionDirection, SpotMarketAccount } from '../types';
|
|
2
|
+
import { PerpMarketAccount, PositionDirection, SpotMarketAccount, UserStatsAccount } from '../types';
|
|
3
3
|
import { BN } from '@coral-xyz/anchor';
|
|
4
4
|
import { AssetType } from './amm';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
@@ -114,3 +114,4 @@ export declare function calculateEstimatedEntryPriceWithL2(assetType: AssetType,
|
|
|
114
114
|
baseFilled: BN;
|
|
115
115
|
quoteFilled: BN;
|
|
116
116
|
};
|
|
117
|
+
export declare function getUser30dRollingVolumeEstimate(userStatsAccount: UserStatsAccount, now?: BN): BN;
|
package/lib/math/trade.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateEstimatedEntryPriceWithL2 = exports.calculateEstimatedSpotEntryPrice = exports.calculateEstimatedPerpEntryPrice = exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
|
|
3
|
+
exports.getUser30dRollingVolumeEstimate = exports.calculateEstimatedEntryPriceWithL2 = exports.calculateEstimatedSpotEntryPrice = exports.calculateEstimatedPerpEntryPrice = exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
6
6
|
const assert_1 = require("../assert/assert");
|
|
@@ -621,3 +621,17 @@ function calculateEstimatedEntryPriceWithL2(assetType, amount, direction, basePr
|
|
|
621
621
|
};
|
|
622
622
|
}
|
|
623
623
|
exports.calculateEstimatedEntryPriceWithL2 = calculateEstimatedEntryPriceWithL2;
|
|
624
|
+
function getUser30dRollingVolumeEstimate(userStatsAccount, now) {
|
|
625
|
+
now = now || new anchor_1.BN(new Date().getTime() / 1000);
|
|
626
|
+
const sinceLastTaker = anchor_1.BN.max(now.sub(userStatsAccount.lastTakerVolume30DTs), numericConstants_1.ZERO);
|
|
627
|
+
const sinceLastMaker = anchor_1.BN.max(now.sub(userStatsAccount.lastMakerVolume30DTs), numericConstants_1.ZERO);
|
|
628
|
+
const thirtyDaysInSeconds = new anchor_1.BN(60 * 60 * 24 * 30);
|
|
629
|
+
const last30dVolume = userStatsAccount.takerVolume30D
|
|
630
|
+
.mul(anchor_1.BN.max(thirtyDaysInSeconds.sub(sinceLastTaker), numericConstants_1.ZERO))
|
|
631
|
+
.div(thirtyDaysInSeconds)
|
|
632
|
+
.add(userStatsAccount.makerVolume30D
|
|
633
|
+
.mul(anchor_1.BN.max(thirtyDaysInSeconds.sub(sinceLastMaker), numericConstants_1.ZERO))
|
|
634
|
+
.div(thirtyDaysInSeconds));
|
|
635
|
+
return last30dVolume;
|
|
636
|
+
}
|
|
637
|
+
exports.getUser30dRollingVolumeEstimate = getUser30dRollingVolumeEstimate;
|
package/lib/user.d.ts
CHANGED
|
@@ -340,7 +340,7 @@ export declare class User {
|
|
|
340
340
|
* @returns leverageRatio : Precision TEN_THOUSAND
|
|
341
341
|
*/
|
|
342
342
|
accountLeverageRatioAfterTrade(targetMarketIndex: number, targetMarketType: MarketType, tradeQuoteAmount: BN, tradeSide: PositionDirection, includeOpenOrders?: boolean): BN;
|
|
343
|
-
getUserFeeTier(marketType: MarketType): import("./types").FeeTier;
|
|
343
|
+
getUserFeeTier(marketType: MarketType, now?: BN): import("./types").FeeTier;
|
|
344
344
|
/**
|
|
345
345
|
* Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
|
|
346
346
|
* @param marketType
|
package/lib/user.js
CHANGED
|
@@ -1640,14 +1640,14 @@ class User {
|
|
|
1640
1640
|
.div(netAssetValue);
|
|
1641
1641
|
return newLeverage;
|
|
1642
1642
|
}
|
|
1643
|
-
getUserFeeTier(marketType) {
|
|
1643
|
+
getUserFeeTier(marketType, now) {
|
|
1644
1644
|
const state = this.driftClient.getStateAccount();
|
|
1645
1645
|
let feeTierIndex = 0;
|
|
1646
1646
|
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
1647
1647
|
const userStatsAccount = this.driftClient
|
|
1648
1648
|
.getUserStats()
|
|
1649
1649
|
.getAccount();
|
|
1650
|
-
const total30dVolume =
|
|
1650
|
+
const total30dVolume = (0, _1.getUser30dRollingVolumeEstimate)(userStatsAccount, now);
|
|
1651
1651
|
const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
|
|
1652
1652
|
const volumeTiers = [
|
|
1653
1653
|
new _1.BN(100000000).mul(numericConstants_1.QUOTE_PRECISION),
|
package/package.json
CHANGED
|
@@ -42,6 +42,11 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
42
42
|
this.program = program;
|
|
43
43
|
this.decodeBuffer = decodeBufferFn;
|
|
44
44
|
this.resubTimeoutMs = resubTimeoutMs;
|
|
45
|
+
if (this.resubTimeoutMs < 1000) {
|
|
46
|
+
console.log(
|
|
47
|
+
'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
|
|
48
|
+
);
|
|
49
|
+
}
|
|
45
50
|
this.options = options;
|
|
46
51
|
this.receivingData = false;
|
|
47
52
|
}
|
|
@@ -73,6 +78,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
73
78
|
);
|
|
74
79
|
|
|
75
80
|
if (this.resubTimeoutMs) {
|
|
81
|
+
this.receivingData = true;
|
|
76
82
|
this.setTimeout();
|
|
77
83
|
}
|
|
78
84
|
}
|
package/src/math/trade.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
PerpMarketAccount,
|
|
4
4
|
PositionDirection,
|
|
5
5
|
SpotMarketAccount,
|
|
6
|
+
UserStatsAccount,
|
|
6
7
|
} from '../types';
|
|
7
8
|
import { BN } from '@coral-xyz/anchor';
|
|
8
9
|
import { assert } from '../assert/assert';
|
|
@@ -964,3 +965,29 @@ export function calculateEstimatedEntryPriceWithL2(
|
|
|
964
965
|
quoteFilled: cumulativeQuoteFilled,
|
|
965
966
|
};
|
|
966
967
|
}
|
|
968
|
+
|
|
969
|
+
export function getUser30dRollingVolumeEstimate(
|
|
970
|
+
userStatsAccount: UserStatsAccount,
|
|
971
|
+
now?: BN
|
|
972
|
+
) {
|
|
973
|
+
now = now || new BN(new Date().getTime() / 1000);
|
|
974
|
+
const sinceLastTaker = BN.max(
|
|
975
|
+
now.sub(userStatsAccount.lastTakerVolume30DTs),
|
|
976
|
+
ZERO
|
|
977
|
+
);
|
|
978
|
+
const sinceLastMaker = BN.max(
|
|
979
|
+
now.sub(userStatsAccount.lastMakerVolume30DTs),
|
|
980
|
+
ZERO
|
|
981
|
+
);
|
|
982
|
+
const thirtyDaysInSeconds = new BN(60 * 60 * 24 * 30);
|
|
983
|
+
const last30dVolume = userStatsAccount.takerVolume30D
|
|
984
|
+
.mul(BN.max(thirtyDaysInSeconds.sub(sinceLastTaker), ZERO))
|
|
985
|
+
.div(thirtyDaysInSeconds)
|
|
986
|
+
.add(
|
|
987
|
+
userStatsAccount.makerVolume30D
|
|
988
|
+
.mul(BN.max(thirtyDaysInSeconds.sub(sinceLastMaker), ZERO))
|
|
989
|
+
.div(thirtyDaysInSeconds)
|
|
990
|
+
);
|
|
991
|
+
|
|
992
|
+
return last30dVolume;
|
|
993
|
+
}
|
package/src/user.ts
CHANGED
|
@@ -55,6 +55,7 @@ import {
|
|
|
55
55
|
getSignedTokenAmount,
|
|
56
56
|
getStrictTokenValue,
|
|
57
57
|
getTokenValue,
|
|
58
|
+
getUser30dRollingVolumeEstimate,
|
|
58
59
|
MarketType,
|
|
59
60
|
PositionDirection,
|
|
60
61
|
sigNum,
|
|
@@ -3059,7 +3060,7 @@ export class User {
|
|
|
3059
3060
|
return newLeverage;
|
|
3060
3061
|
}
|
|
3061
3062
|
|
|
3062
|
-
public getUserFeeTier(marketType: MarketType) {
|
|
3063
|
+
public getUserFeeTier(marketType: MarketType, now?: BN) {
|
|
3063
3064
|
const state = this.driftClient.getStateAccount();
|
|
3064
3065
|
|
|
3065
3066
|
let feeTierIndex = 0;
|
|
@@ -3068,9 +3069,10 @@ export class User {
|
|
|
3068
3069
|
.getUserStats()
|
|
3069
3070
|
.getAccount();
|
|
3070
3071
|
|
|
3071
|
-
const total30dVolume =
|
|
3072
|
-
userStatsAccount
|
|
3073
|
-
|
|
3072
|
+
const total30dVolume = getUser30dRollingVolumeEstimate(
|
|
3073
|
+
userStatsAccount,
|
|
3074
|
+
now
|
|
3075
|
+
);
|
|
3074
3076
|
|
|
3075
3077
|
const stakedQuoteAssetAmount = userStatsAccount.ifStakedQuoteAssetAmount;
|
|
3076
3078
|
const volumeTiers = [
|