|
| 1 | +import { |
| 2 | + Connection, |
| 3 | + Keypair, |
| 4 | + PublicKey, |
| 5 | + Signer, |
| 6 | + StakeAuthorizationLayout, |
| 7 | + StakeProgram, |
| 8 | + SystemProgram, |
| 9 | + SYSVAR_CLOCK_PUBKEY, |
| 10 | + SYSVAR_STAKE_HISTORY_PUBKEY, |
| 11 | + TransactionInstruction, |
| 12 | +} from "@solana/web3.js"; |
| 13 | +import { |
| 14 | + getStakePoolAccount, |
| 15 | + STAKE_POOL_PROGRAM_ID, |
| 16 | +} from "@solana/spl-stake-pool"; |
| 17 | +import { |
| 18 | + createDepositStakeInstruction, |
| 19 | + DepositStakeInstructionAccounts, |
| 20 | + DepositStakeInstructionArgs, |
| 21 | + PROGRAM_ID, |
| 22 | + StakePoolDepositStakeAuthority, |
| 23 | +} from "./generated"; |
| 24 | +import { |
| 25 | + createAssociatedTokenAccountIdempotentInstruction, |
| 26 | + getAssociatedTokenAddressSync, |
| 27 | + TOKEN_PROGRAM_ID, |
| 28 | +} from "@solana/spl-token"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Creates instructions required to deposit stake to stake pool via |
| 32 | + * Stake Deposit Interceptor. |
| 33 | + * |
| 34 | + * @param connection |
| 35 | + * @param payer - [NEW] pays rent for DepositReceipt |
| 36 | + * @param stakePoolAddress |
| 37 | + * @param authorizedPubkey |
| 38 | + * @param validatorVote |
| 39 | + * @param depositStake |
| 40 | + * @param poolTokenReceiverAccount |
| 41 | + */ |
| 42 | +export const depositStake = async ( |
| 43 | + connection: Connection, |
| 44 | + payer: PublicKey, |
| 45 | + stakePoolAddress: PublicKey, |
| 46 | + authorizedPubkey: PublicKey, |
| 47 | + validatorVote: PublicKey, |
| 48 | + depositStake: PublicKey, |
| 49 | + poolTokenReceiverAccount?: PublicKey |
| 50 | +) => { |
| 51 | + const stakePool = await getStakePoolAccount(connection, stakePoolAddress); |
| 52 | + const stakePoolDepositAuthority = |
| 53 | + await StakePoolDepositStakeAuthority.fromAccountAddress( |
| 54 | + connection, |
| 55 | + stakePool.account.data.stakeDepositAuthority |
| 56 | + ); |
| 57 | + const withdrawAuthority = await findWithdrawAuthorityProgramAddress( |
| 58 | + STAKE_POOL_PROGRAM_ID, |
| 59 | + stakePoolAddress |
| 60 | + ); |
| 61 | + const validatorStake = await findStakeProgramAddress( |
| 62 | + STAKE_POOL_PROGRAM_ID, |
| 63 | + validatorVote, |
| 64 | + stakePoolAddress |
| 65 | + ); |
| 66 | + |
| 67 | + const instructions: TransactionInstruction[] = []; |
| 68 | + const signers: Signer[] = []; |
| 69 | + |
| 70 | + const base = Keypair.generate(); |
| 71 | + const poolMint = stakePool.account.data.poolMint; |
| 72 | + |
| 73 | + signers.push(base); |
| 74 | + |
| 75 | + // Create token account if not specified |
| 76 | + if (!poolTokenReceiverAccount) { |
| 77 | + const associatedAddress = getAssociatedTokenAddressSync( |
| 78 | + poolMint, |
| 79 | + authorizedPubkey |
| 80 | + ); |
| 81 | + instructions.push( |
| 82 | + createAssociatedTokenAccountIdempotentInstruction( |
| 83 | + authorizedPubkey, |
| 84 | + associatedAddress, |
| 85 | + authorizedPubkey, |
| 86 | + poolMint |
| 87 | + ) |
| 88 | + ); |
| 89 | + poolTokenReceiverAccount = associatedAddress; |
| 90 | + } |
| 91 | + |
| 92 | + instructions.push( |
| 93 | + ...StakeProgram.authorize({ |
| 94 | + stakePubkey: depositStake, |
| 95 | + authorizedPubkey, |
| 96 | + newAuthorizedPubkey: stakePool.account.data.stakeDepositAuthority, |
| 97 | + stakeAuthorizationType: StakeAuthorizationLayout.Staker, |
| 98 | + }).instructions |
| 99 | + ); |
| 100 | + instructions.push( |
| 101 | + ...StakeProgram.authorize({ |
| 102 | + stakePubkey: depositStake, |
| 103 | + authorizedPubkey, |
| 104 | + newAuthorizedPubkey: stakePool.account.data.stakeDepositAuthority, |
| 105 | + stakeAuthorizationType: StakeAuthorizationLayout.Withdrawer, |
| 106 | + }).instructions |
| 107 | + ); |
| 108 | + |
| 109 | + // Derive DepositReceipt Address |
| 110 | + const [depositReceiptAddress] = PublicKey.findProgramAddressSync( |
| 111 | + [ |
| 112 | + Buffer.from("deposit_receipt"), |
| 113 | + stakePoolAddress.toBuffer(), |
| 114 | + base.publicKey.toBuffer(), |
| 115 | + ], |
| 116 | + PROGRAM_ID |
| 117 | + ); |
| 118 | + |
| 119 | + const depositStakeIxArgs: DepositStakeInstructionArgs = { |
| 120 | + depositStakeArgs: { |
| 121 | + owner: authorizedPubkey, |
| 122 | + }, |
| 123 | + }; |
| 124 | + const depositStakeIxAccounts: DepositStakeInstructionAccounts = { |
| 125 | + payer, |
| 126 | + stakePoolProgram: STAKE_POOL_PROGRAM_ID, |
| 127 | + depositReceipt: depositReceiptAddress, |
| 128 | + stakePool: stakePoolAddress, |
| 129 | + validatorStakeList: stakePool.account.data.validatorList, |
| 130 | + depositStakeAuthority: stakePool.account.data.stakeDepositAuthority, |
| 131 | + base: base.publicKey, |
| 132 | + stakePoolWithdrawAuthority: withdrawAuthority, |
| 133 | + stake: depositStake, |
| 134 | + validatorStakeAccount: validatorStake, |
| 135 | + reserveStakeAccount: stakePool.account.data.reserveStake, |
| 136 | + vault: stakePoolDepositAuthority.vault, |
| 137 | + managerFeeAccount: stakePool.account.data.managerFeeAccount, |
| 138 | + referrerPoolTokensAccount: poolTokenReceiverAccount, |
| 139 | + poolMint, |
| 140 | + clock: SYSVAR_CLOCK_PUBKEY, |
| 141 | + stakeHistory: SYSVAR_STAKE_HISTORY_PUBKEY, |
| 142 | + tokenProgram: TOKEN_PROGRAM_ID, |
| 143 | + stakeProgram: StakeProgram.programId, |
| 144 | + systemProgram: SystemProgram.programId, |
| 145 | + }; |
| 146 | + const depositStakeIx = createDepositStakeInstruction( |
| 147 | + depositStakeIxAccounts, |
| 148 | + depositStakeIxArgs |
| 149 | + ); |
| 150 | + instructions.push(depositStakeIx); |
| 151 | + |
| 152 | + return { |
| 153 | + instructions, |
| 154 | + signers, |
| 155 | + }; |
| 156 | +}; |
| 157 | + |
| 158 | +/** |
| 159 | + * Generates the withdraw authority program address for the stake pool |
| 160 | + */ |
| 161 | +const findWithdrawAuthorityProgramAddress = ( |
| 162 | + programId: PublicKey, |
| 163 | + stakePoolAddress: PublicKey |
| 164 | +) => { |
| 165 | + const [publicKey] = PublicKey.findProgramAddressSync( |
| 166 | + [stakePoolAddress.toBuffer(), Buffer.from("withdraw")], |
| 167 | + programId |
| 168 | + ); |
| 169 | + return publicKey; |
| 170 | +}; |
| 171 | + |
| 172 | +/** |
| 173 | + * Generates the stake program address for a validator's vote account |
| 174 | + */ |
| 175 | +const findStakeProgramAddress = ( |
| 176 | + programId: PublicKey, |
| 177 | + voteAccountAddress: PublicKey, |
| 178 | + stakePoolAddress: PublicKey |
| 179 | +) => { |
| 180 | + const [publicKey] = PublicKey.findProgramAddressSync( |
| 181 | + [ |
| 182 | + voteAccountAddress.toBuffer(), |
| 183 | + stakePoolAddress.toBuffer(), |
| 184 | + Buffer.alloc(0), |
| 185 | + ], |
| 186 | + programId |
| 187 | + ); |
| 188 | + return publicKey; |
| 189 | +}; |
0 commit comments