From 477355df3bd6635f0cac17dfb82e6a6c6c6de9f6 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Thu, 24 Mar 2022 00:10:24 -0700 Subject: [PATCH] fix: add TypeScript buffer type to stake-program.ts --- web3.js/src/stake-program.ts | 104 ++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/web3.js/src/stake-program.ts b/web3.js/src/stake-program.ts index 1caf18224f70ce..1128819f99ec82 100644 --- a/web3.js/src/stake-program.ts +++ b/web3.js/src/stake-program.ts @@ -1,6 +1,11 @@ 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 {PublicKey} from './publickey'; import {SystemProgram} from './system-program'; @@ -40,6 +45,11 @@ export class Authorized { } } +type AuthorizedRaw = Readonly<{ + staker: Uint8Array; + withdrawer: Uint8Array; +}>; + /** * Stake account lockup info */ @@ -66,6 +76,12 @@ export class Lockup { static default: Lockup = new Lockup(0, 0, PublicKey.default); } +type LockupRaw = Readonly<{ + custodian: Uint8Array; + epoch: number; + unixTimestamp: number; +}>; + /** * Create stake account transaction params */ @@ -429,25 +445,63 @@ export class StakeInstruction { * An enumeration of valid StakeInstructionType's */ export type StakeInstructionType = - | 'AuthorizeWithSeed' + // FIXME + // It would be preferable for this type to be `keyof StakeInstructionInputData` + // but Typedoc does not transpile `keyof` expressions. + // See https://github.com/TypeStrong/typedoc/issues/1894 | 'Authorize' + | 'AuthorizeWithSeed' | 'Deactivate' | 'Delegate' | 'Initialize' + | 'Merge' | 'Split' - | 'Withdraw' - | 'Merge'; + | 'Withdraw'; + +type StakeInstructionInputData = { + Authorize: IInstructionInputData & + Readonly<{ + newAuthorized: Uint8Array; + stakeAuthorizationType: number; + }>; + AuthorizeWithSeed: IInstructionInputData & + Readonly<{ + authorityOwner: Uint8Array; + authoritySeed: string; + instruction: number; + newAuthorized: Uint8Array; + stakeAuthorizationType: number; + }>; + Deactivate: IInstructionInputData; + Delegate: IInstructionInputData; + Initialize: IInstructionInputData & + Readonly<{ + authorized: AuthorizedRaw; + lockup: LockupRaw; + }>; + Merge: IInstructionInputData; + Split: IInstructionInputData & + Readonly<{ + lamports: number; + }>; + Withdraw: IInstructionInputData & + Readonly<{ + lamports: number; + }>; +}; /** * An enumeration of valid stake InstructionType's * @internal */ -export const STAKE_INSTRUCTION_LAYOUTS: { - [type in StakeInstructionType]: InstructionType; -} = Object.freeze({ +export const STAKE_INSTRUCTION_LAYOUTS = Object.freeze<{ + [Instruction in StakeInstructionType]: InstructionType< + StakeInstructionInputData[Instruction] + >; +}>({ Initialize: { index: 0, - layout: BufferLayout.struct([ + layout: BufferLayout.struct([ BufferLayout.u32('instruction'), Layout.authorized(), Layout.lockup(), @@ -455,7 +509,7 @@ export const STAKE_INSTRUCTION_LAYOUTS: { }, Authorize: { index: 1, - layout: BufferLayout.struct([ + layout: BufferLayout.struct([ BufferLayout.u32('instruction'), Layout.publicKey('newAuthorized'), BufferLayout.u32('stakeAuthorizationType'), @@ -463,39 +517,47 @@ export const STAKE_INSTRUCTION_LAYOUTS: { }, Delegate: { index: 2, - layout: BufferLayout.struct([BufferLayout.u32('instruction')]), + layout: BufferLayout.struct([ + BufferLayout.u32('instruction'), + ]), }, Split: { index: 3, - layout: BufferLayout.struct([ + layout: BufferLayout.struct([ BufferLayout.u32('instruction'), BufferLayout.ns64('lamports'), ]), }, Withdraw: { index: 4, - layout: BufferLayout.struct([ + layout: BufferLayout.struct([ BufferLayout.u32('instruction'), BufferLayout.ns64('lamports'), ]), }, Deactivate: { index: 5, - layout: BufferLayout.struct([BufferLayout.u32('instruction')]), + layout: BufferLayout.struct([ + BufferLayout.u32('instruction'), + ]), }, Merge: { index: 7, - layout: BufferLayout.struct([BufferLayout.u32('instruction')]), + layout: BufferLayout.struct([ + BufferLayout.u32('instruction'), + ]), }, AuthorizeWithSeed: { index: 8, - layout: BufferLayout.struct([ - BufferLayout.u32('instruction'), - Layout.publicKey('newAuthorized'), - BufferLayout.u32('stakeAuthorizationType'), - Layout.rustString('authoritySeed'), - Layout.publicKey('authorityOwner'), - ]), + layout: BufferLayout.struct( + [ + BufferLayout.u32('instruction'), + Layout.publicKey('newAuthorized'), + BufferLayout.u32('stakeAuthorizationType'), + Layout.rustString('authoritySeed'), + Layout.publicKey('authorityOwner'), + ], + ), }, });