fix: adding Merge decode function and making unit tests pass

This commit is contained in:
Alex Harley 2021-08-05 12:46:38 +02:00 committed by Tyera Eulberg
parent 4a6f63c750
commit 224adb7645
2 changed files with 33 additions and 2 deletions

View File

@ -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);

View File

@ -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;