fix: adding Merge decode function and making unit tests pass
This commit is contained in:
parent
4a6f63c750
commit
224adb7645
|
@ -167,13 +167,14 @@ export type DeactivateStakeParams = {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deactivate stake instruction params
|
* Merge stake instruction params
|
||||||
*/
|
*/
|
||||||
export type MergeStakeParams = {
|
export type MergeStakeParams = {
|
||||||
stakePubkey: PublicKey;
|
stakePubkey: PublicKey;
|
||||||
sourceStakePubKey: PublicKey;
|
sourceStakePubKey: PublicKey;
|
||||||
authorizedPubkey: PublicKey;
|
authorizedPubkey: PublicKey;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stake Instruction class
|
* 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.
|
* Decode a withdraw stake instruction and retrieve the instruction params.
|
||||||
*/
|
*/
|
||||||
|
@ -723,7 +739,7 @@ export class StakeProgram {
|
||||||
* Generate a Transaction that merges Stake accounts.
|
* Generate a Transaction that merges Stake accounts.
|
||||||
*/
|
*/
|
||||||
static merge(params: MergeStakeParams): Transaction {
|
static merge(params: MergeStakeParams): Transaction {
|
||||||
const {stakePubkey, authorizedPubkey, sourceStakePubKey} = params;
|
const {stakePubkey, sourceStakePubKey, authorizedPubkey} = params;
|
||||||
const type = STAKE_INSTRUCTION_LAYOUTS.Merge;
|
const type = STAKE_INSTRUCTION_LAYOUTS.Merge;
|
||||||
const data = encodeData(type);
|
const data = encodeData(type);
|
||||||
|
|
||||||
|
|
|
@ -220,6 +220,21 @@ describe('StakeProgram', () => {
|
||||||
expect(params).to.eql(StakeInstruction.decodeSplit(stakeInstruction));
|
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', () => {
|
it('withdraw', () => {
|
||||||
const stakePubkey = Keypair.generate().publicKey;
|
const stakePubkey = Keypair.generate().publicKey;
|
||||||
const authorizedPubkey = Keypair.generate().publicKey;
|
const authorizedPubkey = Keypair.generate().publicKey;
|
||||||
|
|
Loading…
Reference in New Issue