From c5eb3f13946f59a0d3936810360515174b954c62 Mon Sep 17 00:00:00 2001 From: Alex Harley Date: Wed, 4 Aug 2021 10:00:58 +0200 Subject: [PATCH] feat: exposing merge command Co-authored-by: Tyera Eulberg --- web3.js/src/stake-program.ts | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/web3.js/src/stake-program.ts b/web3.js/src/stake-program.ts index 36de995151..44ca1a592d 100644 --- a/web3.js/src/stake-program.ts +++ b/web3.js/src/stake-program.ts @@ -166,6 +166,14 @@ export type DeactivateStakeParams = { authorizedPubkey: PublicKey; }; +/** + * Deactivate stake instruction params + */ +export type MergeStakeParams = { + stakePubkey: PublicKey; + sourceStakePubKey: PublicKey; + authorizedPubkey: PublicKey; +}; /** * Stake Instruction class */ @@ -399,7 +407,8 @@ export type StakeInstructionType = | 'Delegate' | 'Initialize' | 'Split' - | 'Withdraw'; + | 'Withdraw' + | 'Merge'; /** * An enumeration of valid stake InstructionType's @@ -446,6 +455,15 @@ export const STAKE_INSTRUCTION_LAYOUTS: { index: 5, layout: BufferLayout.struct([BufferLayout.u32('instruction')]), }, + Merge: { + index: 7, + layout: BufferLayout.struct([ + BufferLayout.u32('instruction'), + Layout.publicKey('stakePubKey'), + Layout.publicKey('sourceStakePubKey'), + Layout.publicKey('authorityOwner'), + ]), + }, AuthorizeWithSeed: { index: 8, layout: BufferLayout.struct([ @@ -706,6 +724,31 @@ export class StakeProgram { }); } + /** + * Generate a Transaction that merges Stake accounts. + */ + static merge(params: MergeStakeParams): Transaction { + const {stakePubkey, authorizedPubkey, sourceStakePubKey} = params; + const type = STAKE_INSTRUCTION_LAYOUTS.Merge; + const data = encodeData(type); + + return new Transaction().add({ + keys: [ + {pubkey: stakePubkey, isSigner: false, isWritable: true}, + {pubkey: sourceStakePubKey, isSigner: false, isWritable: true}, + {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false}, + { + pubkey: SYSVAR_STAKE_HISTORY_PUBKEY, + isSigner: false, + isWritable: false, + }, + {pubkey: authorizedPubkey, isSigner: true, isWritable: false}, + ], + programId: this.programId, + data, + }); + } + /** * Generate a Transaction that withdraws deactivated Stake tokens. */