fix: add TypeScript buffer type to vote-program.ts

This commit is contained in:
steveluscher
2022-03-23 22:42:41 -07:00
committed by Steven Luscher
parent 607a5c05de
commit c227b8ca4d

View File

@ -1,6 +1,11 @@
import * as BufferLayout from '@solana/buffer-layout'; import * as BufferLayout from '@solana/buffer-layout';
import {encodeData, decodeData, InstructionType} from './instruction'; import {
encodeData,
decodeData,
InstructionType,
IInstructionInputData,
} from './instruction';
import * as Layout from './layout'; import * as Layout from './layout';
import {PublicKey} from './publickey'; import {PublicKey} from './publickey';
import {SystemProgram} from './system-program'; import {SystemProgram} from './system-program';
@ -202,23 +207,45 @@ export class VoteInstruction {
* An enumeration of valid VoteInstructionType's * An enumeration of valid VoteInstructionType's
*/ */
export type VoteInstructionType = export type VoteInstructionType =
| 'Authorize' // FIXME
| 'InitializeAccount' // It would be preferable for this type to be `keyof VoteInstructionInputData`
| 'Withdraw'; // but Typedoc does not transpile `keyof` expressions.
// See https://github.com/TypeStrong/typedoc/issues/1894
'Authorize' | 'InitializeAccount' | 'Withdraw';
const VOTE_INSTRUCTION_LAYOUTS: { type VoteInstructionInputData = {
[type in VoteInstructionType]: InstructionType; Authorize: IInstructionInputData & {
} = Object.freeze({ newAuthorized: Uint8Array;
voteAuthorizationType: number;
};
InitializeAccount: IInstructionInputData & {
voteInit: Readonly<{
authorizedVoter: Uint8Array;
authorizedWithdrawer: Uint8Array;
commission: number;
nodePubkey: Uint8Array;
}>;
};
Withdraw: IInstructionInputData & {
lamports: number;
};
};
const VOTE_INSTRUCTION_LAYOUTS = Object.freeze<{
[Instruction in VoteInstructionType]: InstructionType<
VoteInstructionInputData[Instruction]
>;
}>({
InitializeAccount: { InitializeAccount: {
index: 0, index: 0,
layout: BufferLayout.struct([ layout: BufferLayout.struct<VoteInstructionInputData['InitializeAccount']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.voteInit(), Layout.voteInit(),
]), ]),
}, },
Authorize: { Authorize: {
index: 1, index: 1,
layout: BufferLayout.struct([ layout: BufferLayout.struct<VoteInstructionInputData['Authorize']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.publicKey('newAuthorized'), Layout.publicKey('newAuthorized'),
BufferLayout.u32('voteAuthorizationType'), BufferLayout.u32('voteAuthorizationType'),
@ -226,7 +253,7 @@ const VOTE_INSTRUCTION_LAYOUTS: {
}, },
Withdraw: { Withdraw: {
index: 3, index: 3,
layout: BufferLayout.struct([ layout: BufferLayout.struct<VoteInstructionInputData['Withdraw']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('lamports'), BufferLayout.ns64('lamports'),
]), ]),