Skip to content

Commit

Permalink
stake-pool-js: Add new decrease instruction (#5333)
Browse files Browse the repository at this point in the history
stake-pool-js: Add new decrease instruction
  • Loading branch information
joncinque authored Sep 26, 2023
1 parent 1d5e088 commit efb0675
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion stake-pool/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -807,10 +807,11 @@ export async function decreaseValidatorStake(
);
} else {
instructions.push(
StakePoolInstruction.decreaseValidatorStake({
StakePoolInstruction.decreaseValidatorStakeWithReserve({
stakePool: stakePoolAddress,
staker: stakePool.account.data.staker,
validatorList: stakePool.account.data.validatorList,
reserveStake: stakePool.account.data.reserveStake,
transientStakeSeed: transientStakeSeed.toNumber(),
withdrawAuthority,
validatorStake,
Expand Down
54 changes: 53 additions & 1 deletion stake-pool/js/src/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type StakePoolInstructionType =
| 'WithdrawSol'
| 'IncreaseAdditionalValidatorStake'
| 'DecreaseAdditionalValidatorStake'
| 'DecreaseValidatorStakeWithReserve'
| 'Redelegate';

// 'UpdateTokenMetadata' and 'CreateTokenMetadata' have dynamic layouts
Expand Down Expand Up @@ -158,8 +159,12 @@ export const STAKE_POOL_INSTRUCTION_LAYOUTS: {
BufferLayout.ns64('ephemeralStakeSeed'),
]),
},
Redelegate: {
DecreaseValidatorStakeWithReserve: {
index: 21,
layout: MOVE_STAKE_LAYOUT,
},
Redelegate: {
index: 22,
layout: BufferLayout.struct<any>([
BufferLayout.u8('instruction'),
/// Amount of lamports to redelegate
Expand Down Expand Up @@ -225,6 +230,10 @@ export type DecreaseValidatorStakeParams = {
transientStakeSeed: number;
};

export interface DecreaseValidatorStakeWithReserveParams extends DecreaseValidatorStakeParams {
reserveStake: PublicKey;
}

export interface DecreaseAdditionalValidatorStakeParams extends DecreaseValidatorStakeParams {
reserveStake: PublicKey;
ephemeralStake: PublicKey;
Expand Down Expand Up @@ -601,6 +610,49 @@ export class StakePoolInstruction {
});
}

/**
* Creates `DecreaseValidatorStakeWithReserve` instruction (rebalance from
* validator account to transient account)
*/
static decreaseValidatorStakeWithReserve(
params: DecreaseValidatorStakeWithReserveParams,
): TransactionInstruction {
const {
stakePool,
staker,
withdrawAuthority,
validatorList,
reserveStake,
validatorStake,
transientStake,
lamports,
transientStakeSeed,
} = params;

const type = STAKE_POOL_INSTRUCTION_LAYOUTS.DecreaseValidatorStakeWithReserve;
const data = encodeData(type, { lamports, transientStakeSeed });

const keys = [
{ pubkey: stakePool, isSigner: false, isWritable: false },
{ pubkey: staker, isSigner: true, isWritable: false },
{ pubkey: withdrawAuthority, isSigner: false, isWritable: false },
{ pubkey: validatorList, isSigner: false, isWritable: true },
{ pubkey: reserveStake, isSigner: false, isWritable: true },
{ pubkey: validatorStake, isSigner: false, isWritable: true },
{ pubkey: transientStake, isSigner: false, isWritable: true },
{ pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_STAKE_HISTORY_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: StakeProgram.programId, isSigner: false, isWritable: false },
];

return new TransactionInstruction({
programId: STAKE_POOL_PROGRAM_ID,
keys,
data,
});
}

/**
* Creates `DecreaseAdditionalValidatorStake` instruction (rebalance from
* validator account to transient account)
Expand Down
2 changes: 1 addition & 1 deletion stake-pool/js/test/instructions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe('StakePoolProgram', () => {
res.instructions[0].data,
);

expect(decodedData.instruction).toBe(21);
expect(decodedData.instruction).toBe(22);
expect(decodedData.lamports).toBe(data.lamports);
expect(decodedData.sourceTransientStakeSeed).toBe(data.sourceTransientStakeSeed);
expect(decodedData.destinationTransientStakeSeed).toBe(data.destinationTransientStakeSeed);
Expand Down

0 comments on commit efb0675

Please sign in to comment.