@drift-labs/sdk 2.28.0-beta.3 → 2.28.0-beta.5
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/lib/config.d.ts +1 -0
- package/lib/config.js +4 -3
- package/lib/constants/perpMarkets.js +20 -0
- package/lib/dlob/DLOBSubscriber.js +1 -1
- package/lib/driftClient.d.ts +62 -7
- package/lib/driftClient.js +186 -66
- package/lib/driftClientConfig.d.ts +4 -3
- package/lib/idl/drift.json +856 -1
- package/lib/math/spotMarket.d.ts +1 -1
- package/lib/math/spotMarket.js +6 -1
- package/lib/math/utils.d.ts +9 -0
- package/lib/math/utils.js +39 -1
- package/lib/phoenix/phoenixSubscriber.js +1 -2
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +1 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +0 -1
- package/lib/types.js +0 -1
- package/lib/user.d.ts +8 -0
- package/lib/user.js +18 -0
- package/package.json +1 -1
- package/src/config.ts +4 -2
- package/src/constants/perpMarkets.ts +20 -0
- package/src/dlob/DLOBSubscriber.ts +2 -2
- package/src/driftClient.ts +273 -138
- package/src/driftClientConfig.ts +9 -3
- package/src/idl/drift.json +857 -2
- package/src/math/spotMarket.ts +6 -2
- package/src/math/utils.ts +46 -0
- package/src/phoenix/phoenixSubscriber.ts +4 -2
- package/src/tx/retryTxSender.ts +1 -9
- package/src/tx/types.ts +1 -2
- package/src/types.ts +0 -2
- package/src/user.ts +28 -0
- package/tests/insurance/test.ts +40 -0
package/src/math/spotMarket.ts
CHANGED
|
@@ -9,10 +9,14 @@ import { calculateAssetWeight, calculateLiabilityWeight } from './spotBalance';
|
|
|
9
9
|
import { MARGIN_PRECISION } from '../constants/numericConstants';
|
|
10
10
|
|
|
11
11
|
export function castNumberToSpotPrecision(
|
|
12
|
-
value: number,
|
|
12
|
+
value: number | BN,
|
|
13
13
|
spotMarket: SpotMarketAccount
|
|
14
14
|
): BN {
|
|
15
|
-
|
|
15
|
+
if (typeof value === 'number') {
|
|
16
|
+
return new BN(value * Math.pow(10, spotMarket.decimals));
|
|
17
|
+
} else {
|
|
18
|
+
return value.mul(new BN(Math.pow(10, spotMarket.decimals)));
|
|
19
|
+
}
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
export function calculateSpotMarketMarginRatio(
|
package/src/math/utils.ts
CHANGED
|
@@ -33,3 +33,49 @@ export const divCeil = (a: BN, b: BN): BN => {
|
|
|
33
33
|
return quotient;
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* calculates the time remaining until the next update based on a rounded, "on-the-hour" update schedule
|
|
39
|
+
* this schedule is used for Perpetual Funding Rate and Revenue -> Insurance Updates
|
|
40
|
+
* @param now: current blockchain unix timestamp
|
|
41
|
+
* @param lastUpdateTs: the unix timestamp of the last update
|
|
42
|
+
* @param updatePeriod: desired interval between updates (in seconds)
|
|
43
|
+
* @returns: timeRemainingUntilUpdate (in seconds)
|
|
44
|
+
*/
|
|
45
|
+
export function timeRemainingUntilUpdate(
|
|
46
|
+
now: BN,
|
|
47
|
+
lastUpdateTs: BN,
|
|
48
|
+
updatePeriod: BN
|
|
49
|
+
): BN {
|
|
50
|
+
const timeSinceLastUpdate = now.sub(lastUpdateTs);
|
|
51
|
+
|
|
52
|
+
// round next update time to be available on the hour
|
|
53
|
+
let nextUpdateWait = updatePeriod;
|
|
54
|
+
if (updatePeriod.gt(new BN(1))) {
|
|
55
|
+
const lastUpdateDelay = lastUpdateTs.umod(updatePeriod);
|
|
56
|
+
if (!lastUpdateDelay.isZero()) {
|
|
57
|
+
const maxDelayForNextPeriod = updatePeriod.div(new BN(3));
|
|
58
|
+
|
|
59
|
+
const twoFundingPeriods = updatePeriod.mul(new BN(2));
|
|
60
|
+
|
|
61
|
+
if (lastUpdateDelay.gt(maxDelayForNextPeriod)) {
|
|
62
|
+
// too late for on the hour next period, delay to following period
|
|
63
|
+
nextUpdateWait = twoFundingPeriods.sub(lastUpdateDelay);
|
|
64
|
+
} else {
|
|
65
|
+
// allow update on the hour
|
|
66
|
+
nextUpdateWait = updatePeriod.sub(lastUpdateDelay);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (nextUpdateWait.gt(twoFundingPeriods)) {
|
|
70
|
+
nextUpdateWait = nextUpdateWait.sub(updatePeriod);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const timeRemainingUntilUpdate = nextUpdateWait
|
|
75
|
+
.sub(timeSinceLastUpdate)
|
|
76
|
+
.isNeg()
|
|
77
|
+
? ZERO
|
|
78
|
+
: nextUpdateWait.sub(timeSinceLastUpdate);
|
|
79
|
+
|
|
80
|
+
return timeRemainingUntilUpdate;
|
|
81
|
+
}
|
|
@@ -163,8 +163,10 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
*getL2Levels(side: 'bids' | 'asks'): Generator<L2Level> {
|
|
166
|
-
|
|
167
|
-
|
|
166
|
+
const basePrecision = Math.pow(
|
|
167
|
+
10,
|
|
168
|
+
this.market.data.header.baseParams.decimals
|
|
169
|
+
);
|
|
168
170
|
const pricePrecision = PRICE_PRECISION.toNumber();
|
|
169
171
|
|
|
170
172
|
const ladder = getMarketUiLadder(
|
package/src/tx/retryTxSender.ts
CHANGED
|
@@ -115,18 +115,10 @@ export class RetryTxSender implements TxSender {
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
async sendVersionedTransaction(
|
|
118
|
-
|
|
119
|
-
lookupTableAccounts: AddressLookupTableAccount[],
|
|
118
|
+
tx: VersionedTransaction,
|
|
120
119
|
additionalSigners?: Array<Signer>,
|
|
121
120
|
opts?: ConfirmOptions
|
|
122
121
|
): Promise<TxSigAndSlot> {
|
|
123
|
-
const tx = await this.getVersionedTransaction(
|
|
124
|
-
ixs,
|
|
125
|
-
lookupTableAccounts,
|
|
126
|
-
additionalSigners,
|
|
127
|
-
opts
|
|
128
|
-
);
|
|
129
|
-
|
|
130
122
|
// @ts-ignore
|
|
131
123
|
tx.sign(additionalSigners.concat(this.provider.wallet.payer));
|
|
132
124
|
|
package/src/tx/types.ts
CHANGED
|
@@ -25,8 +25,7 @@ export interface TxSender {
|
|
|
25
25
|
): Promise<TxSigAndSlot>;
|
|
26
26
|
|
|
27
27
|
sendVersionedTransaction(
|
|
28
|
-
|
|
29
|
-
lookupTableAccounts: AddressLookupTableAccount[],
|
|
28
|
+
tx: VersionedTransaction,
|
|
30
29
|
additionalSigners?: Array<Signer>,
|
|
31
30
|
opts?: ConfirmOptions
|
|
32
31
|
): Promise<TxSigAndSlot>;
|
package/src/types.ts
CHANGED
|
@@ -857,7 +857,6 @@ export type OrderParams = {
|
|
|
857
857
|
immediateOrCancel: boolean;
|
|
858
858
|
triggerPrice: BN | null;
|
|
859
859
|
triggerCondition: OrderTriggerCondition;
|
|
860
|
-
positionLimit: BN;
|
|
861
860
|
oraclePriceOffset: number | null;
|
|
862
861
|
auctionDuration: number | null;
|
|
863
862
|
maxTs: BN | null;
|
|
@@ -899,7 +898,6 @@ export const DefaultOrderParams: OrderParams = {
|
|
|
899
898
|
immediateOrCancel: false,
|
|
900
899
|
triggerPrice: null,
|
|
901
900
|
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
902
|
-
positionLimit: ZERO,
|
|
903
901
|
oraclePriceOffset: null,
|
|
904
902
|
auctionDuration: null,
|
|
905
903
|
maxTs: null,
|
package/src/user.ts
CHANGED
|
@@ -162,6 +162,28 @@ export class User {
|
|
|
162
162
|
);
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Returns the token amount for a given market. The spot market precision is based on the token mint decimals.
|
|
167
|
+
* Positive if it is a deposit, negative if it is a borrow.
|
|
168
|
+
*
|
|
169
|
+
* @param marketIndex
|
|
170
|
+
*/
|
|
171
|
+
public getTokenAmount(marketIndex: number): BN {
|
|
172
|
+
const spotPosition = this.getSpotPosition(marketIndex);
|
|
173
|
+
if (spotPosition === undefined) {
|
|
174
|
+
return ZERO;
|
|
175
|
+
}
|
|
176
|
+
const spotMarket = this.driftClient.getSpotMarketAccount(marketIndex);
|
|
177
|
+
return getSignedTokenAmount(
|
|
178
|
+
getTokenAmount(
|
|
179
|
+
spotPosition.scaledBalance,
|
|
180
|
+
spotMarket,
|
|
181
|
+
spotPosition.balanceType
|
|
182
|
+
),
|
|
183
|
+
spotPosition.balanceType
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
165
187
|
public getEmptyPosition(marketIndex: number): PerpPosition {
|
|
166
188
|
return {
|
|
167
189
|
baseAssetAmount: ZERO,
|
|
@@ -206,6 +228,12 @@ export class User {
|
|
|
206
228
|
);
|
|
207
229
|
}
|
|
208
230
|
|
|
231
|
+
public getOpenOrders(): Order[] {
|
|
232
|
+
return this.getUserAccount()?.orders.filter((order) =>
|
|
233
|
+
isVariant(order.status, 'open')
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
209
237
|
public getUserAccountPublicKey(): PublicKey {
|
|
210
238
|
return this.userAccountPublicKey;
|
|
211
239
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BN, ZERO, timeRemainingUntilUpdate, ONE } from '../../src';
|
|
2
|
+
// import { mockPerpMarkets } from '../dlob/helpers';
|
|
3
|
+
|
|
4
|
+
import { assert } from '../../src/assert/assert';
|
|
5
|
+
|
|
6
|
+
describe('Insurance Tests', () => {
|
|
7
|
+
it('time remaining updates', () => {
|
|
8
|
+
const now = new BN(1683576852);
|
|
9
|
+
const lastUpdate = new BN(1683576000);
|
|
10
|
+
const period = new BN(3600); //hourly
|
|
11
|
+
|
|
12
|
+
let tr;
|
|
13
|
+
// console.log(now.sub(lastUpdate).toString());
|
|
14
|
+
|
|
15
|
+
tr = timeRemainingUntilUpdate(now, lastUpdate, period);
|
|
16
|
+
// console.log(tr.toString());
|
|
17
|
+
assert(tr.eq(new BN('2748')));
|
|
18
|
+
|
|
19
|
+
tr = timeRemainingUntilUpdate(now, lastUpdate.sub(period), period);
|
|
20
|
+
// console.log(tr.toString());
|
|
21
|
+
assert(tr.eq(ZERO));
|
|
22
|
+
|
|
23
|
+
const tooLateUpdate = lastUpdate.sub(period.div(new BN(3)).add(ONE));
|
|
24
|
+
tr = timeRemainingUntilUpdate(
|
|
25
|
+
tooLateUpdate.add(ONE),
|
|
26
|
+
tooLateUpdate,
|
|
27
|
+
period
|
|
28
|
+
);
|
|
29
|
+
// console.log(tr.toString());
|
|
30
|
+
assert(tr.eq(new BN('4800')));
|
|
31
|
+
|
|
32
|
+
tr = timeRemainingUntilUpdate(now, lastUpdate.add(ONE), period);
|
|
33
|
+
// console.log(tr.toString());
|
|
34
|
+
assert(tr.eq(new BN('2748')));
|
|
35
|
+
|
|
36
|
+
tr = timeRemainingUntilUpdate(now, lastUpdate.sub(ONE), period);
|
|
37
|
+
// console.log(tr.toString());
|
|
38
|
+
assert(tr.eq(new BN('2748')));
|
|
39
|
+
});
|
|
40
|
+
});
|