Skip to content

Commit e5a7b1f

Browse files
committed
initial commit
0 parents  commit e5a7b1f

13 files changed

+883
-0
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PRIVATE_KEY=/path/to/keypair.json
2+
RPC_URL=mainnet.rpc.com

.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# drift-examples
2+
3+
Examples here are written using `bun` 1.1.9. If you using `node`, recommended version is v20.18.0. And use [`@drift-labs/sdk`](https://www.npmjs.com/package/@drift-labs/sdk).
4+
If you are using the drift sdk in browser environment, it's recommended to use [`@drift-labs/sdk-browser`](https://www.npmjs.com/package/@drift-labs/sdk-browser) instead.
5+
6+
## Install dependencies:
7+
8+
```bash
9+
bun install
10+
```
11+
12+
All examples require the following environment variables:
13+
14+
- `PRIVATE_KEY`: The private key of the account to use, can be:
15+
- numbers array (from `solana-keygen new`)
16+
- base58 encoded private key (phantom exported private key)
17+
- a file to any of the above
18+
- `RPC_URL`: The RPC URL of the network to use for the example, you need a proper one
19+
- https://helius.xyz/
20+
- https://www.triton.one/
21+
22+
23+
## Run Examples
24+
25+
Each file under `examples/` is an example, start with:
26+
27+
```bash
28+
bun run example/1_init_user.ts
29+
```
30+

bun.lockb

96.6 KB
Binary file not shown.

examples/1_init_user.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* This example demonstrates how to initialize a new user account. This step is not required
3+
* if you have already created a drift account on the drift UI.
4+
*/
5+
import {
6+
DriftClient,
7+
Wallet,
8+
loadKeypair,
9+
} from "@drift-labs/sdk";
10+
import { Connection } from "@solana/web3.js";
11+
12+
const privateKey = process.env.PRIVATE_KEY;
13+
if (!privateKey) {
14+
throw new Error("PRIVATE_KEY environment variable is not set");
15+
}
16+
17+
const rpcUrl = process.env.RPC_URL;
18+
if (!rpcUrl) {
19+
throw new Error("RPC_URL environment variable is not set");
20+
}
21+
22+
const keypair = loadKeypair(privateKey);
23+
console.log(`Using keypair for user: ${keypair.publicKey.toBase58()}`);
24+
25+
const connection = new Connection(rpcUrl);
26+
const driftClient = new DriftClient({
27+
connection,
28+
wallet: new Wallet(keypair),
29+
env: "mainnet-beta",
30+
});
31+
await driftClient.subscribe();
32+
33+
// subaccount 0 is default
34+
try {
35+
const user = driftClient.getUser(0);
36+
console.log(`User 0 already exists: ${user.userAccountPublicKey.toBase58()}`);
37+
} catch (e) {
38+
console.log(`User does not exist, initializing...`);
39+
const [tx, userAccountPublicKey] = await driftClient.initializeUserAccount(0);
40+
console.log(`User initialized subsaccount 0: ${userAccountPublicKey.toBase58()}`);
41+
console.log(`https://solscan.io/tx/${tx}`);
42+
}
43+
44+
process.exit(0);

examples/2_deposit.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* This example demonstrates how to deposit spot assets into a user account.
3+
*
4+
* You can get spot market indexes from:
5+
* * `View Details` from the spot market on https://app.drift.trade
6+
* * Streamlit: https://analytics.drift.trade/?tab=Overview-Markets
7+
* * spot markets constants file: https://github.com/drift-labs/protocol-v2/blob/master/sdk/src/constants/spotMarkets.ts
8+
*/
9+
import {
10+
DriftClient,
11+
BN,
12+
QUOTE_PRECISION,
13+
Wallet,
14+
loadKeypair,
15+
} from "@drift-labs/sdk";
16+
import { Connection } from "@solana/web3.js";
17+
18+
const privateKey = process.env.PRIVATE_KEY;
19+
if (!privateKey) {
20+
throw new Error("PRIVATE_KEY environment variable is not set");
21+
}
22+
23+
const rpcUrl = process.env.RPC_URL;
24+
if (!rpcUrl) {
25+
throw new Error("RPC_URL environment variable is not set");
26+
}
27+
28+
const keypair = loadKeypair(privateKey);
29+
console.log(`Using keypair for user: ${keypair.publicKey.toBase58()}`);
30+
31+
const connection = new Connection(rpcUrl);
32+
const driftClient = new DriftClient({
33+
connection,
34+
wallet: new Wallet(keypair),
35+
env: "mainnet-beta",
36+
});
37+
await driftClient.subscribe();
38+
39+
const spotMarketIndex = 0; // USDC
40+
const depositAmount = new BN(100).mul(QUOTE_PRECISION);
41+
42+
const tx = await driftClient.deposit(
43+
depositAmount,
44+
spotMarketIndex,
45+
// helper function to get ATA for spot market mint
46+
await driftClient.getAssociatedTokenAccount(0),
47+
0, // default will deposit to subaccount 0
48+
undefined,
49+
{
50+
computeUnitsPrice: 10_000,
51+
},
52+
)
53+
console.log(`Deposit tx: https://solscan.io/tx/${tx}`);
54+
55+
process.exit(0);

0 commit comments

Comments
 (0)