@exodus/solana-lib 1.2.9-build → 1.2.9

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.
Files changed (60) hide show
  1. package/package.json +3 -3
  2. package/src/constants.js +12 -0
  3. package/src/encode.js +57 -0
  4. package/src/fee-data/index.js +1 -0
  5. package/src/fee-data/solana.js +9 -0
  6. package/src/helpers/spl-token.js +108 -0
  7. package/src/helpers/tokenTransfer.js +72 -0
  8. package/src/index.js +8 -0
  9. package/src/keypair.js +32 -0
  10. package/src/tokens.js +19 -0
  11. package/src/transaction.js +240 -0
  12. package/src/tx/create-and-sign-tx.js +8 -0
  13. package/{lib → src}/tx/create-unsigned-tx.js +11 -18
  14. package/src/tx/index.js +4 -0
  15. package/src/tx/parse-unsigned-tx.js +41 -0
  16. package/src/tx/sign-unsigned-tx.js +78 -0
  17. package/src/vendor/account.js +38 -0
  18. package/src/vendor/index.js +9 -0
  19. package/src/vendor/instruction.js +46 -0
  20. package/src/vendor/message.js +216 -0
  21. package/src/vendor/nonce-account.js +46 -0
  22. package/src/vendor/publickey.js +212 -0
  23. package/src/vendor/stake-program.js +527 -0
  24. package/src/vendor/system-program.js +782 -0
  25. package/src/vendor/sysvar.js +16 -0
  26. package/src/vendor/transaction.js +594 -0
  27. package/src/vendor/utils/blockhash.js +6 -0
  28. package/src/vendor/utils/fee-calculator.js +17 -0
  29. package/src/vendor/utils/layout.js +80 -0
  30. package/src/vendor/utils/shortvec-encoding.js +30 -0
  31. package/src/vendor/utils/to-buffer.js +9 -0
  32. package/lib/constants.js +0 -19
  33. package/lib/encode.js +0 -67
  34. package/lib/fee-data/index.js +0 -15
  35. package/lib/fee-data/solana.js +0 -14
  36. package/lib/helpers/spl-token.js +0 -122
  37. package/lib/helpers/tokenTransfer.js +0 -78
  38. package/lib/index.js +0 -79
  39. package/lib/keypair.js +0 -38
  40. package/lib/tokens.js +0 -21
  41. package/lib/transaction.js +0 -279
  42. package/lib/tx/create-and-sign-tx.js +0 -15
  43. package/lib/tx/index.js +0 -53
  44. package/lib/tx/parse-unsigned-tx.js +0 -55
  45. package/lib/tx/sign-unsigned-tx.js +0 -90
  46. package/lib/vendor/account.js +0 -48
  47. package/lib/vendor/index.js +0 -113
  48. package/lib/vendor/instruction.js +0 -48
  49. package/lib/vendor/message.js +0 -167
  50. package/lib/vendor/nonce-account.js +0 -56
  51. package/lib/vendor/publickey.js +0 -211
  52. package/lib/vendor/stake-program.js +0 -476
  53. package/lib/vendor/system-program.js +0 -640
  54. package/lib/vendor/sysvar.js +0 -19
  55. package/lib/vendor/transaction.js +0 -594
  56. package/lib/vendor/utils/blockhash.js +0 -1
  57. package/lib/vendor/utils/fee-calculator.js +0 -25
  58. package/lib/vendor/utils/layout.js +0 -97
  59. package/lib/vendor/utils/shortvec-encoding.js +0 -41
  60. package/lib/vendor/utils/to-buffer.js +0 -18
@@ -0,0 +1,527 @@
1
+ // @flow
2
+
3
+ import * as BufferLayout from '@exodus/buffer-layout'
4
+
5
+ import { encodeData, decodeData } from './instruction'
6
+ import * as Layout from './utils/layout'
7
+ import { PublicKey } from './publickey'
8
+ import { SystemProgram } from './system-program'
9
+ import { SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY } from './sysvar'
10
+ import { Transaction, TransactionInstruction } from './transaction'
11
+
12
+ export const STAKE_CONFIG_ID = new PublicKey('StakeConfig11111111111111111111111111111111')
13
+
14
+ export class Authorized {
15
+ staker: PublicKey
16
+ withdrawer: PublicKey
17
+
18
+ /**
19
+ * Create a new Authorized object
20
+ */
21
+ constructor(staker: PublicKey, withdrawer: PublicKey) {
22
+ this.staker = staker
23
+ this.withdrawer = withdrawer
24
+ }
25
+ }
26
+
27
+ export class Lockup {
28
+ unixTimestamp: number
29
+ epoch: number
30
+ custodian: PublicKey
31
+
32
+ /**
33
+ * Create a new Lockup object
34
+ */
35
+ constructor(unixTimestamp: number, epoch: number, custodian: PublicKey) {
36
+ this.unixTimestamp = unixTimestamp
37
+ this.epoch = epoch
38
+ this.custodian = custodian
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Create stake account transaction params
44
+ * @typedef {Object} CreateStakeAccountParams
45
+ * @property {PublicKey} fromPubkey
46
+ * @property {PublicKey} stakePubkey
47
+ * @property {Authorized} authorized
48
+ * @property {Lockup} lockup
49
+ * @property {number} lamports
50
+ */
51
+ export type CreateStakeAccountParams = {|
52
+ fromPubkey: PublicKey,
53
+ stakePubkey: PublicKey,
54
+ authorized: Authorized,
55
+ lockup: Lockup,
56
+ lamports: number,
57
+ |}
58
+
59
+ /**
60
+ * Create stake account with seed transaction params
61
+ * @typedef {Object} CreateStakeAccountWithSeedParams
62
+ * @property {PublicKey} fromPubkey
63
+ * @property {PublicKey} stakePubkey
64
+ * @property {PublicKey} basePubkey
65
+ * @property {string} seed
66
+ * @property {Authorized} authorized
67
+ * @property {Lockup} lockup
68
+ * @property {number} lamports
69
+ */
70
+ export type CreateStakeAccountWithSeedParams = {|
71
+ fromPubkey: PublicKey,
72
+ stakePubkey: PublicKey,
73
+ basePubkey: PublicKey,
74
+ seed: string,
75
+ authorized: Authorized,
76
+ lockup: Lockup,
77
+ lamports: number,
78
+ |}
79
+
80
+ /**
81
+ * Initialize stake instruction params
82
+ * @typedef {Object} InitializeStakeParams
83
+ * @property {PublicKey} stakePubkey
84
+ * @property {Authorized} authorized
85
+ * @property {Lockup} lockup
86
+ */
87
+ export type InitializeStakeParams = {|
88
+ stakePubkey: PublicKey,
89
+ authorized: Authorized,
90
+ lockup: Lockup,
91
+ |}
92
+
93
+ /**
94
+ * Delegate stake instruction params
95
+ * @typedef {Object} DelegateStakeParams
96
+ * @property {PublicKey} stakePubkey
97
+ * @property {PublicKey} authorizedPubkey
98
+ * @property {PublicKey} votePubkey
99
+ */
100
+ export type DelegateStakeParams = {|
101
+ stakePubkey: PublicKey,
102
+ authorizedPubkey: PublicKey,
103
+ votePubkey: PublicKey,
104
+ |}
105
+
106
+ /**
107
+ * @typedef {Object} StakeAuthorizationType
108
+ * @property (index} The Stake Authorization index (from solana-stake-program)
109
+ */
110
+ export type StakeAuthorizationType = {|
111
+ index: number,
112
+ |}
113
+
114
+ /**
115
+ * Authorize stake instruction params
116
+ * @typedef {Object} AuthorizeStakeParams
117
+ * @property {PublicKey} stakePubkey
118
+ * @property {PublicKey} authorizedPubkey
119
+ * @property {PublicKey} newAuthorizedPubkey
120
+ * @property {StakeAuthorizationType} stakeAuthorizationType
121
+ */
122
+ export type AuthorizeStakeParams = {|
123
+ stakePubkey: PublicKey,
124
+ authorizedPubkey: PublicKey,
125
+ newAuthorizedPubkey: PublicKey,
126
+ stakeAuthorizationType: StakeAuthorizationType,
127
+ |}
128
+
129
+ /**
130
+ * Authorize stake instruction params using a derived key
131
+ * @typedef {Object} AuthorizeWithSeedStakeParams
132
+ * @property {PublicKey} stakePubkey
133
+ * @property {PublicKey} authorityBase
134
+ * @property {string} authoritySeed
135
+ * @property {PublicKey} authorityOwner
136
+ * @property {PublicKey} newAuthorizedPubkey
137
+ * @property {StakeAuthorizationType} stakeAuthorizationType
138
+ */
139
+ export type AuthorizeWithSeedStakeParams = {|
140
+ stakePubkey: PublicKey,
141
+ authorityBase: PublicKey,
142
+ authoritySeed: string,
143
+ authorityOwner: PublicKey,
144
+ newAuthorizedPubkey: PublicKey,
145
+ stakeAuthorizationType: StakeAuthorizationType,
146
+ |}
147
+
148
+ /**
149
+ * Split stake instruction params
150
+ * @typedef {Object} SplitStakeParams
151
+ * @property {PublicKey} stakePubkey
152
+ * @property {PublicKey} authorizedPubkey
153
+ * @property {PublicKey} splitStakePubkey
154
+ * @property {number} lamports
155
+ */
156
+ export type SplitStakeParams = {|
157
+ stakePubkey: PublicKey,
158
+ authorizedPubkey: PublicKey,
159
+ splitStakePubkey: PublicKey,
160
+ lamports: number,
161
+ |}
162
+
163
+ /**
164
+ * Withdraw stake instruction params
165
+ * @typedef {Object} WithdrawStakeParams
166
+ * @property {PublicKey} stakePubkey
167
+ * @property {PublicKey} authorizedPubkey
168
+ * @property {PublicKey} toPubkey
169
+ * @property {number} lamports
170
+ */
171
+ export type WithdrawStakeParams = {|
172
+ stakePubkey: PublicKey,
173
+ authorizedPubkey: PublicKey,
174
+ toPubkey: PublicKey,
175
+ lamports: number,
176
+ |}
177
+
178
+ /**
179
+ * Deactivate stake instruction params
180
+ * @typedef {Object} DeactivateStakeParams
181
+ * @property {PublicKey} stakePubkey
182
+ * @property {PublicKey} authorizedPubkey
183
+ */
184
+ export type DeactivateStakeParams = {|
185
+ stakePubkey: PublicKey,
186
+ authorizedPubkey: PublicKey,
187
+ |}
188
+
189
+ /**
190
+ * An enumeration of valid stake InstructionType's
191
+ */
192
+ export const STAKE_INSTRUCTION_LAYOUTS = Object.freeze({
193
+ Initialize: {
194
+ index: 0,
195
+ layout: BufferLayout.struct([
196
+ BufferLayout.u32('instruction'),
197
+ Layout.authorized(),
198
+ Layout.lockup(),
199
+ ]),
200
+ },
201
+ Authorize: {
202
+ index: 1,
203
+ layout: BufferLayout.struct([
204
+ BufferLayout.u32('instruction'),
205
+ Layout.publicKey('newAuthorized'),
206
+ BufferLayout.u32('stakeAuthorizationType'),
207
+ ]),
208
+ },
209
+ Delegate: {
210
+ index: 2,
211
+ layout: BufferLayout.struct([BufferLayout.u32('instruction')]),
212
+ },
213
+ Split: {
214
+ index: 3,
215
+ layout: BufferLayout.struct([BufferLayout.u32('instruction'), BufferLayout.ns64('lamports')]),
216
+ },
217
+ Withdraw: {
218
+ index: 4,
219
+ layout: BufferLayout.struct([BufferLayout.u32('instruction'), BufferLayout.ns64('lamports')]),
220
+ },
221
+ Deactivate: {
222
+ index: 5,
223
+ layout: BufferLayout.struct([BufferLayout.u32('instruction')]),
224
+ },
225
+ AuthorizeWithSeed: {
226
+ index: 8,
227
+ layout: BufferLayout.struct([
228
+ BufferLayout.u32('instruction'),
229
+ Layout.publicKey('newAuthorized'),
230
+ BufferLayout.u32('stakeAuthorizationType'),
231
+ Layout.rustString('authoritySeed'),
232
+ Layout.publicKey('authorityOwner'),
233
+ ]),
234
+ },
235
+ })
236
+
237
+ /**
238
+ * Stake Instruction class
239
+ */
240
+ export class StakeInstruction {
241
+ /**
242
+ * Decode a stake instruction and retrieve the instruction type.
243
+ */
244
+ static decodeInstructionType(instruction: TransactionInstruction) {
245
+ this.checkProgramId(instruction.programId)
246
+
247
+ const instructionTypeLayout = BufferLayout.u32('instruction')
248
+ const typeIndex = instructionTypeLayout.decode(instruction.data)
249
+
250
+ let type
251
+ for (const t of Object.keys(STAKE_INSTRUCTION_LAYOUTS)) {
252
+ if (STAKE_INSTRUCTION_LAYOUTS[t].index === typeIndex) {
253
+ type = t
254
+ }
255
+ }
256
+
257
+ if (!type) {
258
+ throw new Error('Instruction type incorrect; not a StakeInstruction')
259
+ }
260
+
261
+ return type
262
+ }
263
+
264
+ /**
265
+ * Decode a initialize stake instruction and retrieve the instruction params.
266
+ */
267
+ static decodeInitialize(instruction: TransactionInstruction): InitializeStakeParams {
268
+ this.checkProgramId(instruction.programId)
269
+ this.checkKeyLength(instruction.keys, 2)
270
+
271
+ const { authorized, lockup } = decodeData(
272
+ STAKE_INSTRUCTION_LAYOUTS.Initialize,
273
+ instruction.data
274
+ )
275
+
276
+ return {
277
+ stakePubkey: instruction.keys[0].pubkey,
278
+ authorized: new Authorized(
279
+ new PublicKey(authorized.staker),
280
+ new PublicKey(authorized.withdrawer)
281
+ ),
282
+ lockup: new Lockup(lockup.unixTimestamp, lockup.epoch, new PublicKey(lockup.custodian)),
283
+ }
284
+ }
285
+
286
+ /**
287
+ * Decode a delegate stake instruction and retrieve the instruction params.
288
+ */
289
+ static decodeDelegate(instruction: TransactionInstruction): DelegateStakeParams {
290
+ this.checkProgramId(instruction.programId)
291
+ this.checkKeyLength(instruction.keys, 6)
292
+ decodeData(STAKE_INSTRUCTION_LAYOUTS.Delegate, instruction.data)
293
+
294
+ return {
295
+ stakePubkey: instruction.keys[0].pubkey,
296
+ votePubkey: instruction.keys[1].pubkey,
297
+ authorizedPubkey: instruction.keys[5].pubkey,
298
+ }
299
+ }
300
+
301
+ /**
302
+ * Decode a withdraw stake instruction and retrieve the instruction params.
303
+ */
304
+ static decodeWithdraw(instruction: TransactionInstruction): WithdrawStakeParams {
305
+ this.checkProgramId(instruction.programId)
306
+ this.checkKeyLength(instruction.keys, 5)
307
+ const { lamports } = decodeData(STAKE_INSTRUCTION_LAYOUTS.Withdraw, instruction.data)
308
+
309
+ return {
310
+ stakePubkey: instruction.keys[0].pubkey,
311
+ toPubkey: instruction.keys[1].pubkey,
312
+ authorizedPubkey: instruction.keys[4].pubkey,
313
+ lamports,
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Decode a deactivate stake instruction and retrieve the instruction params.
319
+ */
320
+ static decodeDeactivate(instruction: TransactionInstruction): DeactivateStakeParams {
321
+ this.checkProgramId(instruction.programId)
322
+ this.checkKeyLength(instruction.keys, 3)
323
+ decodeData(STAKE_INSTRUCTION_LAYOUTS.Deactivate, instruction.data)
324
+
325
+ return {
326
+ stakePubkey: instruction.keys[0].pubkey,
327
+ authorizedPubkey: instruction.keys[2].pubkey,
328
+ }
329
+ }
330
+
331
+ /**
332
+ * @private
333
+ */
334
+ static checkProgramId(programId: PublicKey) {
335
+ if (!programId.equals(StakeProgram.programId)) {
336
+ throw new Error('invalid instruction; programId is not StakeProgram')
337
+ }
338
+ }
339
+
340
+ /**
341
+ * @private
342
+ */
343
+ static checkKeyLength(keys: Array<any>, expectedLength: number) {
344
+ if (keys.length < expectedLength) {
345
+ throw new Error(
346
+ `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`
347
+ )
348
+ }
349
+ }
350
+ }
351
+
352
+ /**
353
+ * An enumeration of valid StakeAuthorizationLayout's
354
+ */
355
+ export const StakeAuthorizationLayout = Object.freeze({
356
+ Staker: {
357
+ index: 0,
358
+ },
359
+ Withdrawer: {
360
+ index: 1,
361
+ },
362
+ })
363
+
364
+ /**
365
+ * Factory class for transactions to interact with the Stake program
366
+ */
367
+ export class StakeProgram {
368
+ /**
369
+ * Public key that identifies the Stake program
370
+ */
371
+ static get programId(): PublicKey {
372
+ return new PublicKey('Stake11111111111111111111111111111111111111')
373
+ }
374
+
375
+ /**
376
+ * Max space of a Stake account
377
+ *
378
+ * This is generated from the solana-stake-program StakeState struct as
379
+ * `std::mem::size_of::<StakeState>()`:
380
+ * https://docs.rs/solana-stake-program/1.4.4/solana_stake_program/stake_state/enum.StakeState.html
381
+ */
382
+ static get space(): number {
383
+ return 200
384
+ }
385
+
386
+ /**
387
+ * Generate an Initialize instruction to add to a Stake Create transaction
388
+ */
389
+ static initialize(params: InitializeStakeParams): TransactionInstruction {
390
+ const { stakePubkey, authorized, lockup } = params
391
+ const type = STAKE_INSTRUCTION_LAYOUTS.Initialize
392
+ const data = encodeData(type, {
393
+ authorized: {
394
+ staker: authorized.staker.toBuffer(),
395
+ withdrawer: authorized.withdrawer.toBuffer(),
396
+ },
397
+ lockup: {
398
+ unixTimestamp: lockup.unixTimestamp,
399
+ epoch: lockup.epoch,
400
+ custodian: lockup.custodian.toBuffer(),
401
+ },
402
+ })
403
+ const instructionData = {
404
+ keys: [
405
+ { pubkey: stakePubkey, isSigner: false, isWritable: true },
406
+ { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
407
+ ],
408
+ programId: this.programId,
409
+ data,
410
+ }
411
+ return new TransactionInstruction(instructionData)
412
+ }
413
+
414
+ /**
415
+ * Generate a Transaction that creates a new Stake account at
416
+ * an address generated with `from`, a seed, and the Stake programId
417
+ */
418
+ static createAccountWithSeed(params: CreateStakeAccountWithSeedParams): Transaction {
419
+ const transaction = new Transaction()
420
+ transaction.add(
421
+ SystemProgram.createAccountWithSeed({
422
+ fromPubkey: params.fromPubkey,
423
+ newAccountPubkey: params.stakePubkey,
424
+ basePubkey: params.basePubkey,
425
+ seed: params.seed,
426
+ lamports: params.lamports,
427
+ space: this.space,
428
+ programId: this.programId,
429
+ })
430
+ )
431
+
432
+ const { stakePubkey, authorized, lockup } = params
433
+ return transaction.add(this.initialize({ stakePubkey, authorized, lockup }))
434
+ }
435
+
436
+ /**
437
+ * Generate a Transaction that creates a new Stake account
438
+ */
439
+ static createAccount(params: CreateStakeAccountParams): Transaction {
440
+ const transaction = new Transaction()
441
+ transaction.add(
442
+ SystemProgram.createAccount({
443
+ fromPubkey: params.fromPubkey,
444
+ newAccountPubkey: params.stakePubkey,
445
+ lamports: params.lamports,
446
+ space: this.space,
447
+ programId: this.programId,
448
+ })
449
+ )
450
+
451
+ const { stakePubkey, authorized, lockup } = params
452
+ return transaction.add(this.initialize({ stakePubkey, authorized, lockup }))
453
+ }
454
+
455
+ /**
456
+ * Generate a Transaction that delegates Stake tokens to a validator
457
+ * Vote PublicKey. This transaction can also be used to redelegate Stake
458
+ * to a new validator Vote PublicKey.
459
+ */
460
+ static delegate(params: DelegateStakeParams): Transaction {
461
+ const { stakePubkey, authorizedPubkey, votePubkey } = params
462
+
463
+ const type = STAKE_INSTRUCTION_LAYOUTS.Delegate
464
+ const data = encodeData(type)
465
+
466
+ return new Transaction().add({
467
+ keys: [
468
+ { pubkey: stakePubkey, isSigner: false, isWritable: true },
469
+ { pubkey: votePubkey, isSigner: false, isWritable: false },
470
+ { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
471
+ {
472
+ pubkey: SYSVAR_STAKE_HISTORY_PUBKEY,
473
+ isSigner: false,
474
+ isWritable: false,
475
+ },
476
+ { pubkey: STAKE_CONFIG_ID, isSigner: false, isWritable: false },
477
+ { pubkey: authorizedPubkey, isSigner: true, isWritable: false },
478
+ ],
479
+ programId: this.programId,
480
+ data,
481
+ })
482
+ }
483
+
484
+ /**
485
+ * Generate a Transaction that withdraws deactivated Stake tokens.
486
+ */
487
+ static withdraw(params: WithdrawStakeParams): Transaction {
488
+ const { stakePubkey, authorizedPubkey, toPubkey, lamports } = params
489
+ const type = STAKE_INSTRUCTION_LAYOUTS.Withdraw
490
+ const data = encodeData(type, { lamports })
491
+
492
+ return new Transaction().add({
493
+ keys: [
494
+ { pubkey: stakePubkey, isSigner: false, isWritable: true },
495
+ { pubkey: toPubkey, isSigner: false, isWritable: true },
496
+ { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
497
+ {
498
+ pubkey: SYSVAR_STAKE_HISTORY_PUBKEY,
499
+ isSigner: false,
500
+ isWritable: false,
501
+ },
502
+ { pubkey: authorizedPubkey, isSigner: true, isWritable: false },
503
+ ],
504
+ programId: this.programId,
505
+ data,
506
+ })
507
+ }
508
+
509
+ /**
510
+ * Generate a Transaction that deactivates Stake tokens.
511
+ */
512
+ static deactivate(params: DeactivateStakeParams): Transaction {
513
+ const { stakePubkey, authorizedPubkey } = params
514
+ const type = STAKE_INSTRUCTION_LAYOUTS.Deactivate
515
+ const data = encodeData(type)
516
+
517
+ return new Transaction().add({
518
+ keys: [
519
+ { pubkey: stakePubkey, isSigner: false, isWritable: true },
520
+ { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
521
+ { pubkey: authorizedPubkey, isSigner: true, isWritable: false },
522
+ ],
523
+ programId: this.programId,
524
+ data,
525
+ })
526
+ }
527
+ }