|
| 1 | +/** |
| 2 | + * @file Implementation of the depositSol instruction. On upgrade of |
| 3 | + * '@solana/spl-token', this module may no longer be necessary. |
| 4 | + */ |
| 5 | + |
| 6 | +import { StakePoolInstruction, STAKE_POOL_PROGRAM_ID, DepositSolParams } from '@solana/spl-stake-pool'; |
| 7 | +import { |
| 8 | + createAssociatedTokenAccountInstruction, |
| 9 | + getAssociatedTokenAddressSync, |
| 10 | + TOKEN_PROGRAM_ID, |
| 11 | +} from '@solana/spl-token'; |
| 12 | +import { AccountMeta, PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js'; |
| 13 | +import assert from 'assert'; |
| 14 | + |
| 15 | +export const DEPOSIT_SOL_LAYOUT_CODE = 14; |
| 16 | + |
| 17 | +export interface DepositSolInstructionsParams { |
| 18 | + stakePoolAddress: PublicKey; |
| 19 | + from: PublicKey; |
| 20 | + lamports: bigint; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Construct Solana depositSol stake pool instruction from parameters. |
| 25 | + * |
| 26 | + * @param {DepositSolInstructionsParams} params - parameters for staking to stake pool |
| 27 | + * @param poolMint - pool mint derived from getStakePoolAccount |
| 28 | + * @param reserveStake - reserve account derived from getStakePoolAccount |
| 29 | + * @param managerFeeAccount - manager fee account derived from getStakePoolAccount |
| 30 | + * @returns {TransactionInstruction} |
| 31 | + */ |
| 32 | +export function depositSolInstructions( |
| 33 | + params: DepositSolInstructionsParams, |
| 34 | + poolMint: PublicKey, |
| 35 | + reserveStake: PublicKey, |
| 36 | + managerFeeAccount: PublicKey |
| 37 | +): TransactionInstruction[] { |
| 38 | + const { stakePoolAddress, from, lamports } = params; |
| 39 | + |
| 40 | + // findWithdrawAuthorityProgramAddress |
| 41 | + const [withdrawAuthority] = PublicKey.findProgramAddressSync( |
| 42 | + [stakePoolAddress.toBuffer(), Buffer.from('withdraw')], |
| 43 | + STAKE_POOL_PROGRAM_ID |
| 44 | + ); |
| 45 | + |
| 46 | + const associatedAddress = getAssociatedTokenAddressSync(poolMint, from); |
| 47 | + |
| 48 | + return [ |
| 49 | + createAssociatedTokenAccountInstruction(from, associatedAddress, from, poolMint), |
| 50 | + StakePoolInstruction.depositSol({ |
| 51 | + stakePool: stakePoolAddress, |
| 52 | + reserveStake, |
| 53 | + fundingAccount: from, |
| 54 | + destinationPoolAccount: associatedAddress, |
| 55 | + managerFeeAccount: managerFeeAccount, |
| 56 | + referralPoolAccount: associatedAddress, |
| 57 | + poolMint: poolMint, |
| 58 | + lamports: Number(lamports), |
| 59 | + withdrawAuthority, |
| 60 | + }), |
| 61 | + ]; |
| 62 | +} |
| 63 | + |
| 64 | +function parseKey(key: AccountMeta, template: { isSigner: boolean; isWritable: boolean }): PublicKey { |
| 65 | + assert( |
| 66 | + key.isSigner === template.isSigner && key.isWritable === template.isWritable, |
| 67 | + 'Unexpected key metadata in DepositSol instruction' |
| 68 | + ); |
| 69 | + return key.pubkey; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Construct Solana depositSol stake pool parameters from instruction. |
| 74 | + * |
| 75 | + * @param {TransactionInstruction} instruction |
| 76 | + * @returns {DepositSolParams} |
| 77 | + */ |
| 78 | +export function decodeDepositSol(instruction: TransactionInstruction): DepositSolParams { |
| 79 | + const { programId, keys, data } = instruction; |
| 80 | + |
| 81 | + assert( |
| 82 | + programId.equals(STAKE_POOL_PROGRAM_ID), |
| 83 | + 'Invalid DepositSol instruction, program ID must be the Stake Pool Program' |
| 84 | + ); |
| 85 | + |
| 86 | + let i = 0; |
| 87 | + const stakePool = parseKey(keys[i++], { isSigner: false, isWritable: true }); |
| 88 | + const withdrawAuthority = parseKey(keys[i++], { isSigner: false, isWritable: false }); |
| 89 | + const reserveStake = parseKey(keys[i++], { isSigner: false, isWritable: true }); |
| 90 | + const fundingAccount = parseKey(keys[i++], { isSigner: true, isWritable: true }); |
| 91 | + const destinationPoolAccount = parseKey(keys[i++], { isSigner: false, isWritable: true }); |
| 92 | + const managerFeeAccount = parseKey(keys[i++], { isSigner: false, isWritable: true }); |
| 93 | + const referralPoolAccount = parseKey(keys[i++], { isSigner: false, isWritable: true }); |
| 94 | + const poolMint = parseKey(keys[i++], { isSigner: false, isWritable: true }); |
| 95 | + const systemProgramProgramId = parseKey(keys[i++], { isSigner: false, isWritable: false }); |
| 96 | + assert(systemProgramProgramId.equals(SystemProgram.programId), 'Unexpected pubkey in DepositSol instruction'); |
| 97 | + const tokenProgramId = parseKey(keys[i++], { isSigner: false, isWritable: false }); |
| 98 | + assert(tokenProgramId.equals(TOKEN_PROGRAM_ID), 'Unexpected pubkey in DepositSol instruction'); |
| 99 | + const depositAuthority = keys.length > 10 ? parseKey(keys[i++], { isSigner: true, isWritable: false }) : undefined; |
| 100 | + assert(keys.length <= 11, 'Too many keys in DepositSol instruction'); |
| 101 | + |
| 102 | + const layoutCode = data.readUint8(0); |
| 103 | + assert(layoutCode === DEPOSIT_SOL_LAYOUT_CODE, 'Incorrect layout code in DepositSol data'); |
| 104 | + assert(data.length === 9, 'Incorrect data size for DepositSol layout'); |
| 105 | + const lamports = data.readBigInt64LE(1); |
| 106 | + |
| 107 | + return { |
| 108 | + stakePool, |
| 109 | + depositAuthority, |
| 110 | + withdrawAuthority, |
| 111 | + reserveStake, |
| 112 | + fundingAccount, |
| 113 | + destinationPoolAccount, |
| 114 | + managerFeeAccount, |
| 115 | + referralPoolAccount, |
| 116 | + poolMint, |
| 117 | + lamports: Number(lamports), |
| 118 | + }; |
| 119 | +} |
0 commit comments