fix: add instruction handling to stake-program

This commit is contained in:
Tyera Eulberg 2019-12-23 15:53:50 -07:00 committed by Michael Vines
parent 532b28e96e
commit 6ebd47fbac
1 changed files with 40 additions and 0 deletions

View File

@ -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,
);
}
}
/**