@meteora-ag/dynamic-bonding-curve-sdk 1.0.0

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/src/derive.ts ADDED
@@ -0,0 +1,488 @@
1
+ import { PublicKey } from '@solana/web3.js'
2
+ import {
3
+ BASE_ADDRESS,
4
+ DAMM_V1_PROGRAM_ID,
5
+ DAMM_V2_PROGRAM_ID,
6
+ LOCKER_PROGRAM_ID,
7
+ METAPLEX_PROGRAM_ID,
8
+ VAULT_PROGRAM_ID,
9
+ DYNAMIC_BONDING_CURVE_PROGRAM_ID,
10
+ } from './constants'
11
+ import { getFirstKey, getSecondKey } from './utils'
12
+
13
+ const SEED = Object.freeze({
14
+ POOL_AUTHORITY: 'pool_authority',
15
+ EVENT_AUTHORITY: '__event_authority',
16
+ POOL: 'pool',
17
+ TOKEN_VAULT: 'token_vault',
18
+ METADATA: 'metadata',
19
+ PARTNER_METADATA: 'partner_metadata',
20
+ CLAIM_FEE_OPERATOR: 'cf_operator',
21
+ DAMM_V1_MIGRATION_METADATA: 'meteora',
22
+ DAMM_V2_MIGRATION_METADATA: 'damm_v2',
23
+ LP_MINT: 'lp_mint',
24
+ FEE: 'fee',
25
+ POSITION: 'position',
26
+ POSITION_NFT_ACCOUNT: 'position_nft_account',
27
+ LOCK_ESCROW: 'lock_escrow',
28
+ VIRTUAL_POOL_METADATA: 'virtual_pool_metadata',
29
+ ESCROW: 'escrow',
30
+ BASE_LOCKER: 'base_locker',
31
+ VAULT: 'vault',
32
+ })
33
+
34
+ /////////////////////
35
+ // EVENT AUTHORITY //
36
+ /////////////////////
37
+
38
+ /**
39
+ * Derive the dynamic bonding curve event authority
40
+ * @returns The event authority
41
+ */
42
+ export function deriveEventAuthority(): PublicKey {
43
+ const [eventAuthority] = PublicKey.findProgramAddressSync(
44
+ [Buffer.from(SEED.EVENT_AUTHORITY)],
45
+ DYNAMIC_BONDING_CURVE_PROGRAM_ID
46
+ )
47
+ return eventAuthority
48
+ }
49
+
50
+ /**
51
+ * Derive the DAMM V2 event authority
52
+ * @returns The event authority
53
+ */
54
+ export function deriveDammV2EventAuthority(): PublicKey {
55
+ const [eventAuthority] = PublicKey.findProgramAddressSync(
56
+ [Buffer.from(SEED.EVENT_AUTHORITY)],
57
+ DAMM_V2_PROGRAM_ID
58
+ )
59
+ return eventAuthority
60
+ }
61
+
62
+ /**
63
+ * Derive the locker event authority
64
+ * @returns The event authority
65
+ */
66
+ export function deriveLockerEventAuthority(): PublicKey {
67
+ const [eventAuthority] = PublicKey.findProgramAddressSync(
68
+ [Buffer.from(SEED.EVENT_AUTHORITY)],
69
+ LOCKER_PROGRAM_ID
70
+ )
71
+ return eventAuthority
72
+ }
73
+
74
+ ////////////////////
75
+ // POOL AUTHORITY //
76
+ ////////////////////
77
+
78
+ /**
79
+ * Derive the pool authority
80
+ * @param programId - The program ID
81
+ * @returns The pool authority
82
+ */
83
+ export function derivePoolAuthority(programId: PublicKey): PublicKey {
84
+ const [poolAuthority] = PublicKey.findProgramAddressSync(
85
+ [Buffer.from(SEED.POOL_AUTHORITY)],
86
+ programId
87
+ )
88
+
89
+ return poolAuthority
90
+ }
91
+
92
+ ////////////////////
93
+ // POOL ADDRESSES //
94
+ ////////////////////
95
+
96
+ /**
97
+ * Derive the pool address
98
+ * @param quoteMint - The quote mint
99
+ * @param baseMint - The base mint
100
+ * @param config - The config
101
+ * @param programId - The program ID
102
+ * @returns The pool
103
+ */
104
+ export function derivePool(
105
+ quoteMint: PublicKey,
106
+ baseMint: PublicKey,
107
+ config: PublicKey,
108
+ programId: PublicKey
109
+ ): PublicKey {
110
+ const isQuoteMintBiggerThanBaseMint =
111
+ new PublicKey(quoteMint)
112
+ .toBuffer()
113
+ .compare(new Uint8Array(new PublicKey(baseMint).toBuffer())) > 0
114
+
115
+ const [pool] = PublicKey.findProgramAddressSync(
116
+ [
117
+ Buffer.from(SEED.POOL),
118
+ new PublicKey(config).toBuffer(),
119
+ isQuoteMintBiggerThanBaseMint
120
+ ? new PublicKey(quoteMint).toBuffer()
121
+ : new PublicKey(baseMint).toBuffer(),
122
+ isQuoteMintBiggerThanBaseMint
123
+ ? new PublicKey(baseMint).toBuffer()
124
+ : new PublicKey(quoteMint).toBuffer(),
125
+ ],
126
+ programId
127
+ )
128
+
129
+ return pool
130
+ }
131
+
132
+ /**
133
+ * Derive the DAMM pool address
134
+ * @param config - The config
135
+ * @param tokenAMint - The token A mint
136
+ * @param tokenBMint - The token B mint
137
+ * @returns The DAMM pool address
138
+ */
139
+ export function deriveDammPoolAddress(
140
+ config: PublicKey,
141
+ tokenAMint: PublicKey,
142
+ tokenBMint: PublicKey
143
+ ): PublicKey {
144
+ return PublicKey.findProgramAddressSync(
145
+ [
146
+ getFirstKey(tokenAMint, tokenBMint),
147
+ getSecondKey(tokenAMint, tokenBMint),
148
+ config.toBuffer(),
149
+ ],
150
+ DAMM_V1_PROGRAM_ID
151
+ )[0]
152
+ }
153
+
154
+ /**
155
+ * Derive the DAMM V2 pool address
156
+ * @param config - The config
157
+ * @param tokenAMint - The token A mint
158
+ * @param tokenBMint - The token B mint
159
+ * @returns The DAMM V2 pool address
160
+ */
161
+ export function deriveDammV2PoolAddress(
162
+ config: PublicKey,
163
+ tokenAMint: PublicKey,
164
+ tokenBMint: PublicKey
165
+ ): PublicKey {
166
+ return PublicKey.findProgramAddressSync(
167
+ [
168
+ Buffer.from(SEED.POOL),
169
+ config.toBuffer(),
170
+ getFirstKey(tokenAMint, tokenBMint),
171
+ getSecondKey(tokenAMint, tokenBMint),
172
+ ],
173
+ DAMM_V2_PROGRAM_ID
174
+ )[0]
175
+ }
176
+
177
+ ////////////////////////
178
+ // METADATA ADDRESSES //
179
+ ////////////////////////
180
+
181
+ /**
182
+ * Derive the metadata address
183
+ * @param mint - The mint
184
+ * @returns The metadata address
185
+ */
186
+ export function deriveMetadata(mint: PublicKey): PublicKey {
187
+ const [metadata] = PublicKey.findProgramAddressSync(
188
+ [
189
+ Buffer.from(SEED.METADATA),
190
+ METAPLEX_PROGRAM_ID.toBuffer(),
191
+ mint.toBuffer(),
192
+ ],
193
+ METAPLEX_PROGRAM_ID
194
+ )
195
+
196
+ return metadata
197
+ }
198
+
199
+ /**
200
+ * Derive the partner metadata
201
+ * @param feeClaimer - The fee claimer
202
+ * @param programId - The program ID
203
+ * @returns The partner metadata
204
+ */
205
+ export function derivePartnerMetadata(
206
+ feeClaimer: PublicKey,
207
+ programId: PublicKey
208
+ ): PublicKey {
209
+ const [partnerMetadata] = PublicKey.findProgramAddressSync(
210
+ [Buffer.from(SEED.PARTNER_METADATA), feeClaimer.toBuffer()],
211
+ programId
212
+ )
213
+ return partnerMetadata
214
+ }
215
+
216
+ /**
217
+ * Derive the virtual pool metadata
218
+ * @param pool - The pool
219
+ * @returns The virtual pool metadata
220
+ */
221
+ export function deriveVirtualPoolMetadata(pool: PublicKey): PublicKey {
222
+ return PublicKey.findProgramAddressSync(
223
+ [Buffer.from(SEED.VIRTUAL_POOL_METADATA), pool.toBuffer()],
224
+ DYNAMIC_BONDING_CURVE_PROGRAM_ID
225
+ )[0]
226
+ }
227
+
228
+ /**
229
+ * Derive the DAMM migration metadata address
230
+ * @param virtual_pool - The virtual pool
231
+ * @param programId - The program ID
232
+ * @param migrateToDammV2 - Whether to migrate to DAMM V2
233
+ * @returns The DAMM migration metadata address
234
+ */
235
+ export function deriveDammMigrationMetadataAddress(
236
+ virtual_pool: PublicKey,
237
+ programId: PublicKey,
238
+ migrateToDammV2: boolean
239
+ ): PublicKey {
240
+ if (migrateToDammV2) {
241
+ return PublicKey.findProgramAddressSync(
242
+ [
243
+ Buffer.from(SEED.DAMM_V2_MIGRATION_METADATA),
244
+ virtual_pool.toBuffer(),
245
+ ],
246
+ programId
247
+ )[0]
248
+ } else {
249
+ return PublicKey.findProgramAddressSync(
250
+ [
251
+ Buffer.from(SEED.DAMM_V1_MIGRATION_METADATA),
252
+ virtual_pool.toBuffer(),
253
+ ],
254
+ programId
255
+ )[0]
256
+ }
257
+ }
258
+
259
+ /////////////////////
260
+ // VAULT ADDRESSES //
261
+ /////////////////////
262
+
263
+ /**
264
+ * Derive the token vault address
265
+ * @param pool - The pool
266
+ * @param mint - The mint
267
+ * @param programId - The program ID
268
+ * @returns The token vault
269
+ */
270
+ export function deriveTokenVaultAddress(
271
+ pool: PublicKey,
272
+ mint: PublicKey,
273
+ programId: PublicKey
274
+ ): PublicKey {
275
+ const [tokenVault] = PublicKey.findProgramAddressSync(
276
+ [Buffer.from(SEED.TOKEN_VAULT), mint.toBuffer(), pool.toBuffer()],
277
+ programId
278
+ )
279
+
280
+ return tokenVault
281
+ }
282
+
283
+ /**
284
+ * Derive the vault LP address
285
+ * @param vault - The vault
286
+ * @param pool - The pool
287
+ * @param programId - The program ID
288
+ * @returns The vault LP address
289
+ */
290
+ export function deriveVaultLPAddress(
291
+ vault: PublicKey,
292
+ pool: PublicKey,
293
+ programId: PublicKey
294
+ ): PublicKey {
295
+ return PublicKey.findProgramAddressSync(
296
+ [vault.toBuffer(), pool.toBuffer()],
297
+ programId
298
+ )[0]
299
+ }
300
+
301
+ /**
302
+ * Derive the vault address
303
+ * @param mint - The mint
304
+ * @param payer - The payer
305
+ * @returns The vault address
306
+ */
307
+ export function deriveVaultAddress(
308
+ mint: PublicKey,
309
+ payer: PublicKey
310
+ ): PublicKey {
311
+ return PublicKey.findProgramAddressSync(
312
+ [Buffer.from(SEED.VAULT), mint.toBuffer(), payer.toBuffer()],
313
+ VAULT_PROGRAM_ID
314
+ )[0]
315
+ }
316
+
317
+ /**
318
+ * Derive the vault PDAs
319
+ * @param tokenMint - The token mint
320
+ * @param programId - The program ID
321
+ * @param seedBaseKey - The seed base key
322
+ * @returns The vault PDAs
323
+ */
324
+ export const deriveVaultPdas = (
325
+ tokenMint: PublicKey,
326
+ programId: PublicKey,
327
+ seedBaseKey?: PublicKey
328
+ ) => {
329
+ const [vault] = PublicKey.findProgramAddressSync(
330
+ [
331
+ Buffer.from(SEED.VAULT),
332
+ tokenMint.toBuffer(),
333
+ (seedBaseKey ?? BASE_ADDRESS).toBuffer(),
334
+ ],
335
+ programId
336
+ )
337
+
338
+ const [tokenVault] = PublicKey.findProgramAddressSync(
339
+ [Buffer.from(SEED.TOKEN_VAULT), vault.toBuffer()],
340
+ programId
341
+ )
342
+ const [lpMint] = PublicKey.findProgramAddressSync(
343
+ [Buffer.from(SEED.LP_MINT), vault.toBuffer()],
344
+ programId
345
+ )
346
+
347
+ return {
348
+ vaultPda: vault,
349
+ tokenVaultPda: tokenVault,
350
+ lpMintPda: lpMint,
351
+ }
352
+ }
353
+
354
+ /**
355
+ * Derive the token vault key
356
+ * @param vaultKey - The vault key
357
+ * @returns The token vault key
358
+ */
359
+ export function deriveTokenVaultKey(vaultKey: PublicKey): PublicKey {
360
+ return PublicKey.findProgramAddressSync(
361
+ [Buffer.from(SEED.TOKEN_VAULT), vaultKey.toBuffer()],
362
+ VAULT_PROGRAM_ID
363
+ )[0]
364
+ }
365
+
366
+ //////////////////
367
+ // LP ADDRESSES //
368
+ //////////////////
369
+
370
+ /**
371
+ * Derive the LP mint address
372
+ * @param pool - The pool
373
+ * @param programId - The program ID
374
+ * @returns The LP mint address
375
+ */
376
+ export function deriveLpMintAddress(pool: PublicKey, programId: PublicKey) {
377
+ return PublicKey.findProgramAddressSync(
378
+ [Buffer.from(SEED.LP_MINT), pool.toBuffer()],
379
+ programId
380
+ )[0]
381
+ }
382
+
383
+ ////////////////////////
384
+ // POSITION ADDRESSES //
385
+ ////////////////////////
386
+
387
+ /**
388
+ * Derive the position address
389
+ * @param positionNft - The position NFT
390
+ * @returns The position address
391
+ */
392
+ export function derivePositionAddress(positionNft: PublicKey): PublicKey {
393
+ return PublicKey.findProgramAddressSync(
394
+ [Buffer.from(SEED.POSITION), positionNft.toBuffer()],
395
+ DAMM_V2_PROGRAM_ID
396
+ )[0]
397
+ }
398
+
399
+ /**
400
+ * Derive the position NFT account
401
+ * @param positionNftMint - The position NFT mint
402
+ * @returns The position NFT account
403
+ */
404
+ export function derivePositionNftAccount(
405
+ positionNftMint: PublicKey
406
+ ): PublicKey {
407
+ return PublicKey.findProgramAddressSync(
408
+ [Buffer.from(SEED.POSITION_NFT_ACCOUNT), positionNftMint.toBuffer()],
409
+ DAMM_V2_PROGRAM_ID
410
+ )[0]
411
+ }
412
+
413
+ //////////////////////
414
+ // ESCROW ADDRESSES //
415
+ //////////////////////
416
+
417
+ /**
418
+ * Derive the lock escrow address
419
+ * @param dammPool - The DAMM pool
420
+ * @param creator - The creator of the virtual pool
421
+ * @param programId - The program ID
422
+ * @returns The lock escrow address
423
+ */
424
+ export function deriveLockEscrowAddress(
425
+ dammPool: PublicKey,
426
+ creator: PublicKey,
427
+ programId: PublicKey
428
+ ): PublicKey {
429
+ return PublicKey.findProgramAddressSync(
430
+ [
431
+ Buffer.from(SEED.LOCK_ESCROW),
432
+ dammPool.toBuffer(),
433
+ creator.toBuffer(),
434
+ ],
435
+ programId
436
+ )[0]
437
+ }
438
+
439
+ /**
440
+ * Derive the escrow address
441
+ * @param base - The base mint
442
+ * @returns The escrow address
443
+ */
444
+ export function deriveEscrow(base: PublicKey): PublicKey {
445
+ const [escrow] = PublicKey.findProgramAddressSync(
446
+ [Buffer.from(SEED.ESCROW), base.toBuffer()],
447
+ LOCKER_PROGRAM_ID
448
+ )
449
+ return escrow
450
+ }
451
+
452
+ ///////////////////
453
+ // FEE ADDRESSES //
454
+ ///////////////////
455
+
456
+ /**
457
+ * Derive the protocol fee address
458
+ * @param mint - The mint
459
+ * @param pool - The pool
460
+ * @param programId - The program ID
461
+ * @returns The protocol fee address
462
+ */
463
+ export function deriveProtocolFeeAddress(
464
+ mint: PublicKey,
465
+ pool: PublicKey,
466
+ programId: PublicKey
467
+ ) {
468
+ return PublicKey.findProgramAddressSync(
469
+ [Buffer.from(SEED.FEE), mint.toBuffer(), pool.toBuffer()],
470
+ programId
471
+ )[0]
472
+ }
473
+
474
+ ////////////////////
475
+ // LOCKER ADDRESS //
476
+ ////////////////////
477
+
478
+ /**
479
+ * Derive the base key for the locker
480
+ * @param virtualPool - The virtual pool
481
+ * @returns The base key for the locker
482
+ */
483
+ export function deriveBaseKeyForLocker(virtualPool: PublicKey): PublicKey {
484
+ return PublicKey.findProgramAddressSync(
485
+ [Buffer.from(SEED.BASE_LOCKER), virtualPool.toBuffer()],
486
+ DYNAMIC_BONDING_CURVE_PROGRAM_ID
487
+ )[0]
488
+ }