From 6ebd47fbac317952a6e1fb126a37b3334e2aa87c Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 23 Dec 2019 15:53:50 -0700 Subject: [PATCH] fix: add instruction handling to stake-program --- web3.js/src/stake-program.js | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/web3.js/src/stake-program.js b/web3.js/src/stake-program.js index d02c90ba35..bb3be876e2 100644 --- a/web3.js/src/stake-program.js +++ b/web3.js/src/stake-program.js @@ -52,6 +52,46 @@ export class StakeInstruction extends TransactionInstruction { * Type of StakeInstruction */ type: InstructionType; + + constructor(opts?: TransactionInstructionCtorFields, type?: InstructionType) { + if ( + opts && + opts.programId && + !opts.programId.equals(StakeProgram.programId) + ) { + throw new Error('programId incorrect; not a StakeInstruction'); + } + super(opts); + if (type) { + this.type = type; + } + } + + static from(instruction: TransactionInstruction): StakeInstruction { + if (!instruction.programId.equals(StakeProgram.programId)) { + throw new Error('programId incorrect; not StakeProgram'); + } + + const instructionTypeLayout = BufferLayout.u32('instruction'); + const typeIndex = instructionTypeLayout.decode(instruction.data); + let type; + for (const t in StakeInstructionLayout) { + if (StakeInstructionLayout[t].index == typeIndex) { + type = StakeInstructionLayout[t]; + } + } + if (!type) { + throw new Error('Instruction type incorrect; not a StakeInstruction'); + } + return new StakeInstruction( + { + keys: instruction.keys, + programId: instruction.programId, + data: instruction.data, + }, + type, + ); + } } /**