@drift-labs/sdk 2.60.0-beta.1 → 2.60.0-beta.10
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/constants/perpMarkets.js +10 -0
- package/lib/constants/spotMarkets.d.ts +1 -0
- package/lib/constants/spotMarkets.js +13 -0
- package/lib/driftClient.js +13 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/spotMarket.js +2 -1
- package/lib/math/superStake.d.ts +12 -16
- package/lib/math/superStake.js +29 -38
- package/lib/math/utils.d.ts +7 -0
- package/lib/math/utils.js +26 -1
- package/lib/user.d.ts +3 -2
- package/lib/user.js +50 -6
- package/package.json +1 -1
- package/src/constants/perpMarkets.ts +10 -0
- package/src/constants/spotMarkets.ts +20 -0
- package/src/driftClient.ts +11 -4
- package/src/index.ts +1 -0
- package/src/math/spotMarket.ts +2 -1
- package/src/math/superStake.ts +51 -61
- package/src/math/utils.ts +23 -0
- package/src/user.ts +105 -8
- package/tests/amm/test.ts +6 -1
- package/tests/bn/test.ts +15 -1
- package/examples/phoenix.ts +0 -66
- package/lib/examples/loadDlob.d.ts +0 -1
- package/lib/examples/loadDlob.js +0 -59
- package/lib/examples/makeTradeExample.d.ts +0 -2
- package/lib/examples/makeTradeExample.js +0 -83
- package/src/examples/loadDlob.ts +0 -86
- package/src/examples/makeTradeExample.ts +0 -162
|
@@ -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();
|