@drift-labs/sdk 2.60.0-beta.1 → 2.60.0-beta.11

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.
@@ -1,86 +0,0 @@
1
- import { AnchorProvider } from '@coral-xyz/anchor';
2
- import { UserMap, Wallet } from '..';
3
- import { Connection, Keypair, PublicKey } from '@solana/web3.js';
4
- import {
5
- DriftClient,
6
- initialize,
7
- BulkAccountLoader,
8
- getMarketsAndOraclesForSubscription,
9
- } from '..';
10
-
11
- const env = 'mainnet-beta';
12
-
13
- const main = async () => {
14
- // Initialize Drift SDK
15
- const sdkConfig = initialize({ env });
16
-
17
- // Set up the Wallet and Provider
18
- const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
19
- const keypair = Keypair.fromSecretKey(
20
- Uint8Array.from(JSON.parse(privateKey))
21
- );
22
- const wallet = new Wallet(keypair);
23
-
24
- // Set up the Connection
25
- const rpcAddress = process.env.RPC_ADDRESS; // can use: https://api.devnet.solana.com for devnet; https://api.mainnet-beta.solana.com for mainnet;
26
- const connection = new Connection(rpcAddress);
27
-
28
- // Set up the Provider
29
- const provider = new AnchorProvider(
30
- connection,
31
- // @ts-ignore
32
- wallet,
33
- AnchorProvider.defaultOptions()
34
- );
35
-
36
- // Set up the Drift Clearing House
37
- const driftPublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
38
- const bulkAccountLoader = new BulkAccountLoader(
39
- connection,
40
- 'confirmed',
41
- 1000
42
- );
43
- const driftClient = new DriftClient({
44
- connection,
45
- wallet: provider.wallet,
46
- programID: driftPublicKey,
47
- ...getMarketsAndOraclesForSubscription(env),
48
- accountSubscription: {
49
- type: 'polling',
50
- accountLoader: bulkAccountLoader,
51
- },
52
- });
53
-
54
- console.log('Subscribing drift client...');
55
- await driftClient.subscribe();
56
-
57
- console.log('Loading user map...');
58
- const userMap = new UserMap({
59
- driftClient,
60
- subscriptionConfig: {
61
- type: 'websocket',
62
- commitment: 'processed',
63
- },
64
- skipInitialLoad: false,
65
- includeIdle: false,
66
- });
67
-
68
- // fetches all users and subscribes for updates
69
- await userMap.subscribe();
70
-
71
- console.log('Loading dlob from user map...');
72
- const slot = await driftClient.connection.getSlot();
73
- const dlob = await userMap.getDLOB(slot);
74
-
75
- console.log('number of orders', dlob.getDLOBOrders().length);
76
-
77
- dlob.clear();
78
-
79
- console.log('Unsubscribing users...');
80
- await userMap.unsubscribe();
81
-
82
- console.log('Unsubscribing drift client...');
83
- await driftClient.unsubscribe();
84
- };
85
-
86
- main();
@@ -1,162 +0,0 @@
1
- import { AnchorProvider, BN } from '@coral-xyz/anchor';
2
- import {
3
- BASE_PRECISION,
4
- calculateBidAskPrice,
5
- getMarketOrderParams,
6
- Wallet,
7
- } from '..';
8
- import { getAssociatedTokenAddress } from '@solana/spl-token';
9
- import { Connection, Keypair, PublicKey } from '@solana/web3.js';
10
- import {
11
- DriftClient,
12
- User,
13
- initialize,
14
- PositionDirection,
15
- convertToNumber,
16
- calculateTradeSlippage,
17
- BulkAccountLoader,
18
- getMarketsAndOraclesForSubscription,
19
- PRICE_PRECISION,
20
- QUOTE_PRECISION,
21
- } from '..';
22
- import { SpotMarkets } from '../constants/spotMarkets';
23
-
24
- export const getTokenAddress = (
25
- mintAddress: string,
26
- userPubKey: string
27
- ): Promise<PublicKey> => {
28
- return getAssociatedTokenAddress(
29
- new PublicKey(mintAddress),
30
- new PublicKey(userPubKey)
31
- );
32
- };
33
-
34
- const env = 'devnet';
35
-
36
- const main = async () => {
37
- // Initialize Drift SDK
38
- const sdkConfig = initialize({ env });
39
-
40
- // Set up the Wallet and Provider
41
- const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
42
- const keypair = Keypair.fromSecretKey(
43
- Uint8Array.from(JSON.parse(privateKey))
44
- );
45
- const wallet = new Wallet(keypair);
46
-
47
- // Set up the Connection
48
- const rpcAddress = process.env.RPC_ADDRESS; // can use: https://api.devnet.solana.com for devnet; https://api.mainnet-beta.solana.com for mainnet;
49
- const connection = new Connection(rpcAddress);
50
-
51
- // Set up the Provider
52
- const provider = new AnchorProvider(
53
- connection,
54
- // @ts-ignore
55
- wallet,
56
- AnchorProvider.defaultOptions()
57
- );
58
-
59
- // Check SOL Balance
60
- const lamportsBalance = await connection.getBalance(wallet.publicKey);
61
- console.log('SOL balance:', lamportsBalance / 10 ** 9);
62
-
63
- // Misc. other things to set up
64
- const usdcTokenAddress = await getTokenAddress(
65
- sdkConfig.USDC_MINT_ADDRESS,
66
- wallet.publicKey.toString()
67
- );
68
-
69
- // Set up the Drift Clearing House
70
- const driftPublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
71
- const bulkAccountLoader = new BulkAccountLoader(
72
- connection,
73
- 'confirmed',
74
- 1000
75
- );
76
- const driftClient = new DriftClient({
77
- connection,
78
- wallet: provider.wallet,
79
- programID: driftPublicKey,
80
- ...getMarketsAndOraclesForSubscription(env),
81
- accountSubscription: {
82
- type: 'polling',
83
- accountLoader: bulkAccountLoader,
84
- },
85
- });
86
- await driftClient.subscribe();
87
-
88
- // Set up user client
89
- const user = new User({
90
- driftClient: driftClient,
91
- userAccountPublicKey: await driftClient.getUserAccountPublicKey(),
92
- accountSubscription: {
93
- type: 'polling',
94
- accountLoader: bulkAccountLoader,
95
- },
96
- });
97
-
98
- //// Check if user account exists for the current wallet
99
- const userAccountExists = await user.exists();
100
-
101
- if (!userAccountExists) {
102
- //// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
103
- const depositAmount = new BN(10000).mul(QUOTE_PRECISION);
104
- await driftClient.initializeUserAccountAndDepositCollateral(
105
- depositAmount,
106
- await getTokenAddress(
107
- usdcTokenAddress.toString(),
108
- wallet.publicKey.toString()
109
- ),
110
- SpotMarkets['devnet'][0].marketIndex
111
- );
112
- }
113
-
114
- await user.subscribe();
115
-
116
- // Get current price
117
- const solMarketInfo = sdkConfig.PERP_MARKETS.find(
118
- (market) => market.baseAssetSymbol === 'SOL'
119
- );
120
-
121
- const marketIndex = solMarketInfo.marketIndex;
122
- const [bid, ask] = calculateBidAskPrice(
123
- driftClient.getPerpMarketAccount(marketIndex).amm,
124
- driftClient.getOracleDataForPerpMarket(marketIndex)
125
- );
126
-
127
- const formattedBidPrice = convertToNumber(bid, PRICE_PRECISION);
128
- const formattedAskPrice = convertToNumber(ask, PRICE_PRECISION);
129
-
130
- console.log(
131
- `Current amm bid and ask price are $${formattedBidPrice} and $${formattedAskPrice}`
132
- );
133
-
134
- // Estimate the slippage for a $5000 LONG trade
135
- const solMarketAccount = driftClient.getPerpMarketAccount(
136
- solMarketInfo.marketIndex
137
- );
138
-
139
- const slippage = convertToNumber(
140
- calculateTradeSlippage(
141
- PositionDirection.LONG,
142
- new BN(1).mul(BASE_PRECISION),
143
- solMarketAccount,
144
- 'base',
145
- driftClient.getOracleDataForPerpMarket(solMarketInfo.marketIndex)
146
- )[0],
147
- PRICE_PRECISION
148
- );
149
-
150
- console.log(`Slippage for a 1 SOL-PERP would be $${slippage}`);
151
-
152
- await driftClient.placePerpOrder(
153
- getMarketOrderParams({
154
- baseAssetAmount: new BN(1).mul(BASE_PRECISION),
155
- direction: PositionDirection.LONG,
156
- marketIndex: solMarketAccount.marketIndex,
157
- })
158
- );
159
- console.log(`Placed a 1 SOL-PERP LONG order`);
160
- };
161
-
162
- main();