@drift-labs/sdk 0.2.0-master.13 → 0.2.0-master.16
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/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -2
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +2 -0
- package/lib/admin.js +18 -0
- package/lib/clearingHouse.d.ts +17 -1
- package/lib/clearingHouse.js +195 -14
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +5 -0
- package/lib/clearingHouseUser.js +97 -3
- package/lib/clearingHouseUserStats.d.ts +17 -0
- package/lib/clearingHouseUserStats.js +36 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/{src/clearingHouseConfig.js → lib/clearingHouseUserStatsConfig.js} +0 -0
- package/lib/config.js +1 -1
- package/lib/factory/bigNum.js +7 -9
- package/lib/idl/clearing_house.json +614 -64
- package/lib/math/bankBalance.d.ts +4 -0
- package/lib/math/bankBalance.js +24 -2
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/position.js +2 -1
- package/lib/math/trade.js +2 -2
- package/lib/types.d.ts +59 -9
- package/lib/types.js +6 -0
- package/package.json +1 -1
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +29 -1
- package/src/clearingHouse.ts +318 -15
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +113 -10
- package/src/clearingHouseUserStats.ts +53 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/factory/bigNum.ts +6 -7
- package/src/idl/clearing_house.json +614 -64
- package/src/math/bankBalance.ts +50 -1
- package/src/math/oracles.ts +42 -5
- package/src/math/position.ts +2 -1
- package/src/math/trade.ts +2 -2
- package/src/types.ts +63 -9
- package/tests/bn/test.ts +8 -0
- package/src/accounts/bulkAccountLoader.js +0 -197
- package/src/accounts/bulkUserSubscription.js +0 -33
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
- package/src/accounts/pollingOracleSubscriber.js +0 -93
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
- package/src/accounts/pollingUserAccountSubscriber.js +0 -132
- package/src/accounts/types.js +0 -10
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -93
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
- package/src/addresses/marketAddresses.js +0 -26
- package/src/assert/assert.js +0 -9
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/constants/numericConstants.js +0 -41
- package/src/events/eventList.js +0 -77
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/examples/makeTradeExample.js +0 -80
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/index.js +0 -69
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -280
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/orderParams.js +0 -20
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -189
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/types.js +0 -125
- package/src/userName.js +0 -20
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
- package/src/wallet.js +0 -35
package/src/clearingHouseUser.ts
CHANGED
|
@@ -53,7 +53,6 @@ import { OraclePriceData } from './oracles/types';
|
|
|
53
53
|
import { ClearingHouseUserConfig } from './clearingHouseUserConfig';
|
|
54
54
|
import { PollingUserAccountSubscriber } from './accounts/pollingUserAccountSubscriber';
|
|
55
55
|
import { WebSocketUserAccountSubscriber } from './accounts/webSocketUserAccountSubscriber';
|
|
56
|
-
|
|
57
56
|
export class ClearingHouseUser {
|
|
58
57
|
clearingHouse: ClearingHouse;
|
|
59
58
|
userAccountPublicKey: PublicKey;
|
|
@@ -137,6 +136,11 @@ export class ClearingHouseUser {
|
|
|
137
136
|
openOrders: ZERO,
|
|
138
137
|
openBids: ZERO,
|
|
139
138
|
openAsks: ZERO,
|
|
139
|
+
realizedPnl: ZERO,
|
|
140
|
+
lpShares: ZERO,
|
|
141
|
+
lastFeePerLp: ZERO,
|
|
142
|
+
lastNetBaseAssetAmountPerLp: ZERO,
|
|
143
|
+
lastNetQuoteAssetAmountPerLp: ZERO,
|
|
140
144
|
};
|
|
141
145
|
}
|
|
142
146
|
|
|
@@ -172,6 +176,98 @@ export class ClearingHouseUser {
|
|
|
172
176
|
return userAccountRPCResponse.value !== null;
|
|
173
177
|
}
|
|
174
178
|
|
|
179
|
+
/**
|
|
180
|
+
* calculates the market position if the lp position was settled
|
|
181
|
+
* @returns : userPosition
|
|
182
|
+
*/
|
|
183
|
+
public getSettledLPPosition(marketIndex: BN): [UserPosition, BN] {
|
|
184
|
+
const position = this.getUserPosition(marketIndex);
|
|
185
|
+
const market = this.clearingHouse.getMarketAccount(position.marketIndex);
|
|
186
|
+
const nShares = position.lpShares;
|
|
187
|
+
|
|
188
|
+
const deltaBaa = market.amm.marketPositionPerLp.baseAssetAmount
|
|
189
|
+
.sub(position.lastNetBaseAssetAmountPerLp)
|
|
190
|
+
.mul(nShares)
|
|
191
|
+
.div(AMM_RESERVE_PRECISION);
|
|
192
|
+
const deltaQaa = market.amm.marketPositionPerLp.quoteAssetAmount
|
|
193
|
+
.sub(position.lastNetQuoteAssetAmountPerLp)
|
|
194
|
+
.mul(nShares)
|
|
195
|
+
.div(AMM_RESERVE_PRECISION);
|
|
196
|
+
|
|
197
|
+
function sign(v: BN) {
|
|
198
|
+
const sign = { true: new BN(1), false: new BN(-1) }[
|
|
199
|
+
v.gte(ZERO).toString()
|
|
200
|
+
];
|
|
201
|
+
return sign;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const remainder = deltaBaa
|
|
205
|
+
.abs()
|
|
206
|
+
.mod(market.amm.baseAssetAmountStepSize)
|
|
207
|
+
.mul(sign(deltaBaa));
|
|
208
|
+
const _standardizedBaa = deltaBaa.sub(remainder);
|
|
209
|
+
|
|
210
|
+
let remainderBaa;
|
|
211
|
+
if (_standardizedBaa.abs().gte(market.amm.baseAssetAmountStepSize)) {
|
|
212
|
+
remainderBaa = remainder;
|
|
213
|
+
} else {
|
|
214
|
+
remainderBaa = deltaBaa;
|
|
215
|
+
}
|
|
216
|
+
const standardizedBaa = deltaBaa.sub(remainderBaa);
|
|
217
|
+
|
|
218
|
+
const reaminderPerLP = remainderBaa.mul(AMM_RESERVE_PRECISION).div(nShares);
|
|
219
|
+
|
|
220
|
+
position.baseAssetAmount = position.baseAssetAmount.add(standardizedBaa);
|
|
221
|
+
position.quoteAssetAmount = position.quoteAssetAmount.add(deltaQaa);
|
|
222
|
+
|
|
223
|
+
position.lastNetBaseAssetAmountPerLp =
|
|
224
|
+
market.amm.marketPositionPerLp.baseAssetAmount.sub(reaminderPerLP);
|
|
225
|
+
|
|
226
|
+
let updateType;
|
|
227
|
+
if (position.baseAssetAmount.eq(ZERO)) {
|
|
228
|
+
updateType = 'open';
|
|
229
|
+
} else if (sign(position.baseAssetAmount).eq(sign(deltaBaa))) {
|
|
230
|
+
updateType = 'increase';
|
|
231
|
+
} else if (position.baseAssetAmount.abs().gt(deltaBaa.abs())) {
|
|
232
|
+
updateType = 'reduce';
|
|
233
|
+
} else if (position.baseAssetAmount.abs().eq(deltaBaa.abs())) {
|
|
234
|
+
updateType = 'close';
|
|
235
|
+
} else {
|
|
236
|
+
updateType = 'flip';
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let newQuoteEntry;
|
|
240
|
+
let pnl;
|
|
241
|
+
if (updateType == 'open' || updateType == 'increase') {
|
|
242
|
+
newQuoteEntry = position.quoteEntryAmount.add(deltaQaa);
|
|
243
|
+
pnl = 0;
|
|
244
|
+
} else if (updateType == 'reduce' || updateType == 'close') {
|
|
245
|
+
newQuoteEntry = position.quoteEntryAmount.sub(
|
|
246
|
+
position.quoteEntryAmount
|
|
247
|
+
.mul(deltaBaa.abs())
|
|
248
|
+
.div(position.baseAssetAmount.abs())
|
|
249
|
+
);
|
|
250
|
+
pnl = position.quoteEntryAmount.sub(newQuoteEntry);
|
|
251
|
+
} else {
|
|
252
|
+
newQuoteEntry = deltaQaa.sub(
|
|
253
|
+
deltaQaa.mul(position.baseAssetAmount.abs()).div(deltaBaa.abs())
|
|
254
|
+
);
|
|
255
|
+
pnl = position.quoteEntryAmount.add(deltaQaa.sub(newQuoteEntry));
|
|
256
|
+
}
|
|
257
|
+
position.quoteEntryAmount = newQuoteEntry;
|
|
258
|
+
|
|
259
|
+
if (position.baseAssetAmount.gt(ZERO)) {
|
|
260
|
+
position.lastCumulativeFundingRate = market.amm.cumulativeFundingRateLong;
|
|
261
|
+
} else if (position.baseAssetAmount.lt(ZERO)) {
|
|
262
|
+
position.lastCumulativeFundingRate =
|
|
263
|
+
market.amm.cumulativeFundingRateShort;
|
|
264
|
+
} else {
|
|
265
|
+
position.lastCumulativeFundingRate = ZERO;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return [position, pnl];
|
|
269
|
+
}
|
|
270
|
+
|
|
175
271
|
/**
|
|
176
272
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
177
273
|
* @returns : Precision QUOTE_PRECISION
|
|
@@ -716,13 +812,17 @@ export class ClearingHouseUser {
|
|
|
716
812
|
const proposedMarketPosition: UserPosition = {
|
|
717
813
|
marketIndex: marketPosition.marketIndex,
|
|
718
814
|
baseAssetAmount: proposedBaseAssetAmount,
|
|
719
|
-
lastCumulativeFundingRate:
|
|
720
|
-
currentMarketPosition.lastCumulativeFundingRate,
|
|
721
815
|
quoteAssetAmount: new BN(0),
|
|
816
|
+
lastCumulativeFundingRate: ZERO,
|
|
722
817
|
quoteEntryAmount: new BN(0),
|
|
723
818
|
openOrders: new BN(0),
|
|
724
819
|
openBids: new BN(0),
|
|
725
820
|
openAsks: new BN(0),
|
|
821
|
+
realizedPnl: ZERO,
|
|
822
|
+
lpShares: ZERO,
|
|
823
|
+
lastFeePerLp: ZERO,
|
|
824
|
+
lastNetBaseAssetAmountPerLp: ZERO,
|
|
825
|
+
lastNetQuoteAssetAmountPerLp: ZERO,
|
|
726
826
|
};
|
|
727
827
|
|
|
728
828
|
if (proposedBaseAssetAmount.eq(ZERO)) return new BN(-1);
|
|
@@ -753,14 +853,17 @@ export class ClearingHouseUser {
|
|
|
753
853
|
position,
|
|
754
854
|
this.getOracleDataForMarket(market.marketIndex)
|
|
755
855
|
);
|
|
756
|
-
const marketMarginRequirement = positionValue
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
856
|
+
const marketMarginRequirement = positionValue
|
|
857
|
+
.mul(
|
|
858
|
+
new BN(
|
|
859
|
+
calculateMarketMarginRatio(
|
|
860
|
+
market,
|
|
861
|
+
position.baseAssetAmount.abs(),
|
|
862
|
+
'Maintenance'
|
|
863
|
+
)
|
|
864
|
+
)
|
|
762
865
|
)
|
|
763
|
-
|
|
866
|
+
.div(MARGIN_PRECISION);
|
|
764
867
|
totalMarginRequirement = totalMarginRequirement.add(
|
|
765
868
|
marketMarginRequirement
|
|
766
869
|
);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ClearingHouse } from './clearingHouse';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { DataAndSlot, UserStatsAccountSubscriber } from './accounts/types';
|
|
4
|
+
import { ClearingHouseUserStatsConfig } from './clearingHouseUserStatsConfig';
|
|
5
|
+
import { PollingUserStatsAccountSubscriber } from './accounts/pollingUserStatsAccountSubscriber';
|
|
6
|
+
import { WebSocketUserStatsAccountSubscriber } from './accounts/webSocketUserStatsAccountSubsriber';
|
|
7
|
+
import { UserStatsAccount } from './types';
|
|
8
|
+
|
|
9
|
+
export class ClearingHouseUserStats {
|
|
10
|
+
clearingHouse: ClearingHouse;
|
|
11
|
+
userStatsAccountPublicKey: PublicKey;
|
|
12
|
+
accountSubscriber: UserStatsAccountSubscriber;
|
|
13
|
+
isSubscribed: boolean;
|
|
14
|
+
|
|
15
|
+
public constructor(config: ClearingHouseUserStatsConfig) {
|
|
16
|
+
this.clearingHouse = config.clearingHouse;
|
|
17
|
+
this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
|
|
18
|
+
if (config.accountSubscription?.type === 'polling') {
|
|
19
|
+
this.accountSubscriber = new PollingUserStatsAccountSubscriber(
|
|
20
|
+
config.clearingHouse.program,
|
|
21
|
+
config.userStatsAccountPublicKey,
|
|
22
|
+
config.accountSubscription.accountLoader
|
|
23
|
+
);
|
|
24
|
+
} else {
|
|
25
|
+
this.accountSubscriber = new WebSocketUserStatsAccountSubscriber(
|
|
26
|
+
config.clearingHouse.program,
|
|
27
|
+
config.userStatsAccountPublicKey
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async subscribe(): Promise<boolean> {
|
|
33
|
+
this.isSubscribed = await this.accountSubscriber.subscribe();
|
|
34
|
+
return this.isSubscribed;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async fetchAccounts(): Promise<void> {
|
|
38
|
+
await this.accountSubscriber.fetch();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async unsubscribe(): Promise<void> {
|
|
42
|
+
await this.accountSubscriber.unsubscribe();
|
|
43
|
+
this.isSubscribed = false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public getAccountAndSlot(): DataAndSlot<UserStatsAccount> {
|
|
47
|
+
return this.accountSubscriber.getUserStatsAccountAndSlot();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getAccount(): UserStatsAccount {
|
|
51
|
+
return this.accountSubscriber.getUserStatsAccountAndSlot().data;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ClearingHouse } from './clearingHouse';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
+
|
|
5
|
+
export type ClearingHouseUserStatsConfig = {
|
|
6
|
+
accountSubscription?: ClearingHouseUserStatsAccountSubscriptionConfig;
|
|
7
|
+
clearingHouse: ClearingHouse;
|
|
8
|
+
userStatsAccountPublicKey: PublicKey;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ClearingHouseUserStatsAccountSubscriptionConfig =
|
|
12
|
+
| {
|
|
13
|
+
type: 'websocket';
|
|
14
|
+
}
|
|
15
|
+
| {
|
|
16
|
+
type: 'polling';
|
|
17
|
+
accountLoader: BulkAccountLoader;
|
|
18
|
+
};
|
package/src/config.ts
CHANGED
|
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
|
|
|
28
28
|
devnet: {
|
|
29
29
|
ENV: 'devnet',
|
|
30
30
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
31
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
31
|
+
CLEARING_HOUSE_PROGRAM_ID: 'D9bW92Maa1yDigJqvabRgr5S5VybPNDB5xxSpQD6mkkV',
|
|
32
32
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
33
33
|
MARKETS: DevnetMarkets,
|
|
34
34
|
BANKS: DevnetBanks,
|
package/src/factory/bigNum.ts
CHANGED
|
@@ -211,7 +211,8 @@ export class BigNum {
|
|
|
211
211
|
'Tried to print a BN with precision lower than zero'
|
|
212
212
|
);
|
|
213
213
|
|
|
214
|
-
const
|
|
214
|
+
const isNeg = this.isNeg();
|
|
215
|
+
const plainString = this.abs().toString();
|
|
215
216
|
const precisionNum = this.precision.toNumber();
|
|
216
217
|
|
|
217
218
|
// make a string with at least the precisionNum number of zeroes
|
|
@@ -230,12 +231,10 @@ export class BigNum {
|
|
|
230
231
|
printString = printString.replace(/^0+/, '');
|
|
231
232
|
|
|
232
233
|
// add zero if leading delim
|
|
233
|
-
if (
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (printString[0] === BigNum.delim) printString = `0${printString}`;
|
|
238
|
-
}
|
|
234
|
+
if (printString[0] === BigNum.delim) printString = `0${printString}`;
|
|
235
|
+
|
|
236
|
+
// Add minus if negative
|
|
237
|
+
if (isNeg) printString = `-${printString}`;
|
|
239
238
|
|
|
240
239
|
// remove trailing delim
|
|
241
240
|
if (printString[printString.length - 1] === BigNum.delim)
|