@@ -27,7 +27,12 @@ import {
2727import { NotSupported , TransactionType } from '@bitgo/sdk-core' ;
2828import { coins , SolCoin } from '@bitgo/statics' ;
2929import assert from 'assert' ;
30- import { InstructionBuilderTypes , ValidInstructionTypesEnum , walletInitInstructionIndexes } from './constants' ;
30+ import {
31+ InstructionBuilderTypes ,
32+ JITO_STAKE_POOL_ADDRESS ,
33+ ValidInstructionTypesEnum ,
34+ walletInitInstructionIndexes ,
35+ } from './constants' ;
3136import {
3237 AtaClose ,
3338 AtaInit ,
@@ -47,6 +52,8 @@ import {
4752 SetPriorityFee ,
4853} from './iface' ;
4954import { getInstructionType } from './utils' ;
55+ import { DepositSolParams } from '@solana/spl-stake-pool' ;
56+ import { decodeDepositSol } from './jitoStakePoolOperations' ;
5057
5158/**
5259 * Construct instructions params from Solana instructions
@@ -303,6 +310,14 @@ function parseSendInstructions(
303310 return instructionData ;
304311}
305312
313+ function stakingInstructionsIsMarinade ( si : StakingInstructions ) : boolean {
314+ return ! ! ( si . delegate === undefined && si . depositSol === undefined ) ;
315+ }
316+
317+ function stakingInstructionsIsJito ( si : StakingInstructions ) : boolean {
318+ return ! ! ( si . delegate === undefined && si . depositSol ?. stakePool . toString ( ) === JITO_STAKE_POOL_ADDRESS ) ;
319+ }
320+
306321/**
307322 * Parses Solana instructions to create staking tx and delegate tx instructions params
308323 * Only supports Nonce, StakingActivate and Memo Solana instructions
@@ -312,8 +327,8 @@ function parseSendInstructions(
312327 */
313328function parseStakingActivateInstructions (
314329 instructions : TransactionInstruction [ ]
315- ) : Array < Nonce | StakingActivate | Memo > {
316- const instructionData : Array < Nonce | StakingActivate | Memo > = [ ] ;
330+ ) : Array < Nonce | StakingActivate | Memo | AtaInit > {
331+ const instructionData : Array < Nonce | StakingActivate | Memo | AtaInit > = [ ] ;
317332 const stakingInstructions = { } as StakingInstructions ;
318333 for ( const instruction of instructions ) {
319334 const type = getInstructionType ( instruction ) ;
@@ -346,21 +361,48 @@ function parseStakingActivateInstructions(
346361 case ValidInstructionTypesEnum . StakingDelegate :
347362 stakingInstructions . delegate = StakeInstruction . decodeDelegate ( instruction ) ;
348363 break ;
364+
365+ case ValidInstructionTypesEnum . DepositSol :
366+ stakingInstructions . depositSol = decodeDepositSol ( instruction ) ;
367+ break ;
368+
369+ case ValidInstructionTypesEnum . InitializeAssociatedTokenAccount :
370+ instructionData . push ( {
371+ type : InstructionBuilderTypes . CreateAssociatedTokenAccount ,
372+ params : {
373+ mintAddress : instruction . keys [ ataInitInstructionKeysIndexes . MintAddress ] . pubkey . toString ( ) ,
374+ ataAddress : instruction . keys [ ataInitInstructionKeysIndexes . ATAAddress ] . pubkey . toString ( ) ,
375+ ownerAddress : instruction . keys [ ataInitInstructionKeysIndexes . OwnerAddress ] . pubkey . toString ( ) ,
376+ payerAddress : instruction . keys [ ataInitInstructionKeysIndexes . PayerAddress ] . pubkey . toString ( ) ,
377+ tokenName : findTokenName ( instruction . keys [ ataInitInstructionKeysIndexes . MintAddress ] . pubkey . toString ( ) ) ,
378+ } ,
379+ } ) ;
380+ break ;
349381 }
350382 }
351383
352384 validateStakingInstructions ( stakingInstructions ) ;
385+
353386 const stakingActivate : StakingActivate = {
354387 type : InstructionBuilderTypes . StakingActivate ,
355388 params : {
356- fromAddress : stakingInstructions . create ?. fromPubkey . toString ( ) || '' ,
357- stakingAddress : stakingInstructions . initialize ?. stakePubkey . toString ( ) || '' ,
358- amount : stakingInstructions . create ?. lamports . toString ( ) || '' ,
389+ fromAddress :
390+ stakingInstructions . create ?. fromPubkey . toString ( ) ||
391+ stakingInstructions . depositSol ?. fundingAccount . toString ( ) ||
392+ '' ,
393+ stakingAddress :
394+ stakingInstructions . initialize ?. stakePubkey . toString ( ) ||
395+ stakingInstructions . depositSol ?. stakePool . toString ( ) ||
396+ '' ,
397+ amount :
398+ stakingInstructions . create ?. lamports . toString ( ) || stakingInstructions . depositSol ?. lamports . toString ( ) || '' ,
359399 validator :
360400 stakingInstructions . delegate ?. votePubkey . toString ( ) ||
361401 stakingInstructions . initialize ?. authorized . staker . toString ( ) ||
402+ stakingInstructions . depositSol ?. stakePool . toString ( ) ||
362403 '' ,
363- isMarinade : stakingInstructions . delegate === undefined ,
404+ isMarinade : stakingInstructionsIsMarinade ( stakingInstructions ) ,
405+ isJito : stakingInstructionsIsJito ( stakingInstructions ) ,
364406 } ,
365407 } ;
366408 instructionData . push ( stakingActivate ) ;
@@ -413,22 +455,23 @@ interface StakingInstructions {
413455 initialize ?: InitializeStakeParams ;
414456 delegate ?: DelegateStakeParams ;
415457 authorize ?: AuthorizeStakeParams [ ] ;
458+ depositSol ?: DepositSolParams ;
416459}
417460
418461function validateStakingInstructions ( stakingInstructions : StakingInstructions ) {
419- if ( ! stakingInstructions . create ) {
420- throw new NotSupported ( 'Invalid staking activate transaction, missing create stake account instruction' ) ;
421- }
422-
423- if ( ! stakingInstructions . initialize && stakingInstructions . delegate ) {
424- return ;
425- } else if ( ! stakingInstructions . delegate && stakingInstructions . initialize ) {
426- return ;
427- } else if ( ! stakingInstructions . delegate && ! stakingInstructions . initialize ) {
428- // If both are missing something is wrong
429- throw new NotSupported (
430- 'Invalid staking activate transaction, missing initialize stake account/delegate instruction'
431- ) ;
462+ if ( stakingInstructionsIsJito ( stakingInstructions ) ) {
463+ if ( ! stakingInstructions . depositSol ) {
464+ throw new NotSupported ( 'Invalid staking activate transaction, missing deposit sol instruction' ) ;
465+ }
466+ } else {
467+ if ( ! stakingInstructions . create ) {
468+ throw new NotSupported ( 'Invalid staking activate transaction, missing create stake account instruction' ) ;
469+ }
470+ if ( ! stakingInstructions . delegate && ! stakingInstructions . initialize ) {
471+ throw new NotSupported (
472+ 'Invalid staking activate transaction, missing initialize stake account/delegate instruction'
473+ ) ;
474+ }
432475 }
433476}
434477
@@ -776,6 +819,9 @@ function parseAtaInitInstructions(
776819 } ;
777820 instructionData . push ( ataInit ) ;
778821 break ;
822+ case ValidInstructionTypesEnum . DepositSol :
823+ // AtaInit is a part of spl-stake-pool's depositSol process
824+ break ;
779825 default :
780826 throw new NotSupported (
781827 'Invalid transaction, instruction type not supported: ' + getInstructionType ( instruction )
0 commit comments