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

This commit is contained in:
steveluscher
2022-03-23 22:25:57 -07:00
committed by Steven Luscher
parent 807f88e547
commit 607a5c05de

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 {NONCE_ACCOUNT_LENGTH} from './nonce-account'; import {NONCE_ACCOUNT_LENGTH} from './nonce-account';
import {PublicKey} from './publickey'; import {PublicKey} from './publickey';
@ -517,6 +522,10 @@ export class SystemInstruction {
* An enumeration of valid SystemInstructionType's * An enumeration of valid SystemInstructionType's
*/ */
export type SystemInstructionType = export type SystemInstructionType =
// FIXME
// It would be preferable for this type to be `keyof SystemInstructionInputData`
// but Typedoc does not transpile `keyof` expressions.
// See https://github.com/TypeStrong/typedoc/issues/1894
| 'AdvanceNonceAccount' | 'AdvanceNonceAccount'
| 'Allocate' | 'Allocate'
| 'AllocateWithSeed' | 'AllocateWithSeed'
@ -530,16 +539,68 @@ export type SystemInstructionType =
| 'TransferWithSeed' | 'TransferWithSeed'
| 'WithdrawNonceAccount'; | 'WithdrawNonceAccount';
type SystemInstructionInputData = {
AdvanceNonceAccount: IInstructionInputData;
Allocate: IInstructionInputData & {
space: number;
};
AllocateWithSeed: IInstructionInputData & {
base: Uint8Array;
programId: Uint8Array;
seed: string;
space: number;
};
Assign: IInstructionInputData & {
programId: Uint8Array;
};
AssignWithSeed: IInstructionInputData & {
base: Uint8Array;
seed: string;
programId: Uint8Array;
};
AuthorizeNonceAccount: IInstructionInputData & {
authorized: Uint8Array;
};
Create: IInstructionInputData & {
lamports: number;
programId: Uint8Array;
space: number;
};
CreateWithSeed: IInstructionInputData & {
base: Uint8Array;
lamports: number;
programId: Uint8Array;
seed: string;
space: number;
};
InitializeNonceAccount: IInstructionInputData & {
authorized: Uint8Array;
};
Transfer: IInstructionInputData & {
lamports: number;
};
TransferWithSeed: IInstructionInputData & {
lamports: number;
programId: Uint8Array;
seed: string;
};
WithdrawNonceAccount: IInstructionInputData & {
lamports: number;
};
};
/** /**
* An enumeration of valid system InstructionType's * An enumeration of valid system InstructionType's
* @internal * @internal
*/ */
export const SYSTEM_INSTRUCTION_LAYOUTS: { export const SYSTEM_INSTRUCTION_LAYOUTS = Object.freeze<{
[type in SystemInstructionType]: InstructionType; [Instruction in SystemInstructionType]: InstructionType<
} = Object.freeze({ SystemInstructionInputData[Instruction]
>;
}>({
Create: { Create: {
index: 0, index: 0,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['Create']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('lamports'), BufferLayout.ns64('lamports'),
BufferLayout.ns64('space'), BufferLayout.ns64('space'),
@ -548,21 +609,21 @@ export const SYSTEM_INSTRUCTION_LAYOUTS: {
}, },
Assign: { Assign: {
index: 1, index: 1,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['Assign']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.publicKey('programId'), Layout.publicKey('programId'),
]), ]),
}, },
Transfer: { Transfer: {
index: 2, index: 2,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['Transfer']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('lamports'), BufferLayout.ns64('lamports'),
]), ]),
}, },
CreateWithSeed: { CreateWithSeed: {
index: 3, index: 3,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['CreateWithSeed']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.publicKey('base'), Layout.publicKey('base'),
Layout.rustString('seed'), Layout.rustString('seed'),
@ -573,49 +634,50 @@ export const SYSTEM_INSTRUCTION_LAYOUTS: {
}, },
AdvanceNonceAccount: { AdvanceNonceAccount: {
index: 4, index: 4,
layout: BufferLayout.struct([BufferLayout.u32('instruction')]), layout: BufferLayout.struct<
SystemInstructionInputData['AdvanceNonceAccount']
>([BufferLayout.u32('instruction')]),
}, },
WithdrawNonceAccount: { WithdrawNonceAccount: {
index: 5, index: 5,
layout: BufferLayout.struct([ layout: BufferLayout.struct<
BufferLayout.u32('instruction'), SystemInstructionInputData['WithdrawNonceAccount']
BufferLayout.ns64('lamports'), >([BufferLayout.u32('instruction'), BufferLayout.ns64('lamports')]),
]),
}, },
InitializeNonceAccount: { InitializeNonceAccount: {
index: 6, index: 6,
layout: BufferLayout.struct([ layout: BufferLayout.struct<
BufferLayout.u32('instruction'), SystemInstructionInputData['InitializeNonceAccount']
Layout.publicKey('authorized'), >([BufferLayout.u32('instruction'), Layout.publicKey('authorized')]),
]),
}, },
AuthorizeNonceAccount: { AuthorizeNonceAccount: {
index: 7, index: 7,
layout: BufferLayout.struct([ layout: BufferLayout.struct<
BufferLayout.u32('instruction'), SystemInstructionInputData['AuthorizeNonceAccount']
Layout.publicKey('authorized'), >([BufferLayout.u32('instruction'), Layout.publicKey('authorized')]),
]),
}, },
Allocate: { Allocate: {
index: 8, index: 8,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['Allocate']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('space'), BufferLayout.ns64('space'),
]), ]),
}, },
AllocateWithSeed: { AllocateWithSeed: {
index: 9, index: 9,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['AllocateWithSeed']>(
[
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.publicKey('base'), Layout.publicKey('base'),
Layout.rustString('seed'), Layout.rustString('seed'),
BufferLayout.ns64('space'), BufferLayout.ns64('space'),
Layout.publicKey('programId'), Layout.publicKey('programId'),
]), ],
),
}, },
AssignWithSeed: { AssignWithSeed: {
index: 10, index: 10,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['AssignWithSeed']>([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.publicKey('base'), Layout.publicKey('base'),
Layout.rustString('seed'), Layout.rustString('seed'),
@ -624,12 +686,14 @@ export const SYSTEM_INSTRUCTION_LAYOUTS: {
}, },
TransferWithSeed: { TransferWithSeed: {
index: 11, index: 11,
layout: BufferLayout.struct([ layout: BufferLayout.struct<SystemInstructionInputData['TransferWithSeed']>(
[
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('lamports'), BufferLayout.ns64('lamports'),
Layout.rustString('seed'), Layout.rustString('seed'),
Layout.publicKey('programId'), Layout.publicKey('programId'),
]), ],
),
}, },
}); });