From 224adb7645409e9e676abad61f418ae870e345b3 Mon Sep 17 00:00:00 2001 From: Alex Harley Date: Thu, 5 Aug 2021 12:46:38 +0200 Subject: [PATCH] fix: adding Merge decode function and making unit tests pass --- web3.js/src/stake-program.ts | 20 ++++++++++++++++++-- web3.js/test/stake-program.test.ts | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/web3.js/src/stake-program.ts b/web3.js/src/stake-program.ts index 7a178a57b..736923136 100644 --- a/web3.js/src/stake-program.ts +++ b/web3.js/src/stake-program.ts @@ -167,13 +167,14 @@ export type DeactivateStakeParams = { }; /** - * Deactivate stake instruction params + * Merge stake instruction params */ export type MergeStakeParams = { stakePubkey: PublicKey; sourceStakePubKey: PublicKey; authorizedPubkey: PublicKey; }; + /** * Stake Instruction class */ @@ -335,6 +336,21 @@ export class StakeInstruction { }; } + /** + * Decode a merge stake instruction and retrieve the instruction params. + */ + static decodeMerge(instruction: TransactionInstruction): MergeStakeParams { + this.checkProgramId(instruction.programId); + this.checkKeyLength(instruction.keys, 3); + decodeData(STAKE_INSTRUCTION_LAYOUTS.Merge, instruction.data); + + return { + stakePubkey: instruction.keys[0].pubkey, + sourceStakePubKey: instruction.keys[1].pubkey, + authorizedPubkey: instruction.keys[4].pubkey, + }; + } + /** * Decode a withdraw stake instruction and retrieve the instruction params. */ @@ -723,7 +739,7 @@ export class StakeProgram { * Generate a Transaction that merges Stake accounts. */ static merge(params: MergeStakeParams): Transaction { - const {stakePubkey, authorizedPubkey, sourceStakePubKey} = params; + const {stakePubkey, sourceStakePubKey, authorizedPubkey} = params; const type = STAKE_INSTRUCTION_LAYOUTS.Merge; const data = encodeData(type); diff --git a/web3.js/test/stake-program.test.ts b/web3.js/test/stake-program.test.ts index d9ba74071..be7bdffd2 100644 --- a/web3.js/test/stake-program.test.ts +++ b/web3.js/test/stake-program.test.ts @@ -220,6 +220,21 @@ describe('StakeProgram', () => { expect(params).to.eql(StakeInstruction.decodeSplit(stakeInstruction)); }); + it('merge', () => { + const stakePubkey = Keypair.generate().publicKey; + const sourceStakePubKey = Keypair.generate().publicKey; + const authorizedPubkey = Keypair.generate().publicKey; + const params = { + stakePubkey, + sourceStakePubKey, + authorizedPubkey, + }; + const transaction = StakeProgram.merge(params); + expect(transaction.instructions).to.have.length(1); + const [stakeInstruction] = transaction.instructions; + expect(params).to.eql(StakeInstruction.decodeMerge(stakeInstruction)); + }); + it('withdraw', () => { const stakePubkey = Keypair.generate().publicKey; const authorizedPubkey = Keypair.generate().publicKey;