Skip to content

Commit

Permalink
fix: add instruction handling to stake-program
Browse files Browse the repository at this point in the history
  • Loading branch information
CriesofCarrots committed Jan 2, 2020
1 parent 99a4cb6 commit b46c9fb
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/stake-program.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}

/**
Expand Down

0 comments on commit b46c9fb

Please sign in to comment.