fix: add TypeScript buffer type to instruction.ts
This commit is contained in:
parent
023fc028bc
commit
b516a25132
|
@ -3,21 +3,28 @@ import * as BufferLayout from '@solana/buffer-layout';
|
||||||
|
|
||||||
import * as Layout from './layout';
|
import * as Layout from './layout';
|
||||||
|
|
||||||
|
export interface IInstructionInputData {
|
||||||
|
readonly instruction: number;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export type InstructionType = {
|
export type InstructionType<TInputData extends IInstructionInputData> = {
|
||||||
/** The Instruction index (from solana upstream program) */
|
/** The Instruction index (from solana upstream program) */
|
||||||
index: number;
|
index: number;
|
||||||
/** The BufferLayout to use to build data */
|
/** The BufferLayout to use to build data */
|
||||||
layout: BufferLayout.Layout;
|
layout: BufferLayout.Layout<TInputData>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populate a buffer of instruction data using an InstructionType
|
* Populate a buffer of instruction data using an InstructionType
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export function encodeData(type: InstructionType, fields?: any): Buffer {
|
export function encodeData<TInputData extends IInstructionInputData>(
|
||||||
|
type: InstructionType<TInputData>,
|
||||||
|
fields?: any,
|
||||||
|
): Buffer {
|
||||||
const allocLength =
|
const allocLength =
|
||||||
type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
|
type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
|
||||||
const data = Buffer.alloc(allocLength);
|
const data = Buffer.alloc(allocLength);
|
||||||
|
@ -30,8 +37,11 @@ export function encodeData(type: InstructionType, fields?: any): Buffer {
|
||||||
* Decode instruction data buffer using an InstructionType
|
* Decode instruction data buffer using an InstructionType
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export function decodeData(type: InstructionType, buffer: Buffer): any {
|
export function decodeData<TInputData extends IInstructionInputData>(
|
||||||
let data;
|
type: InstructionType<TInputData>,
|
||||||
|
buffer: Buffer,
|
||||||
|
): TInputData {
|
||||||
|
let data: TInputData;
|
||||||
try {
|
try {
|
||||||
data = type.layout.decode(buffer);
|
data = type.layout.decode(buffer);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Reference in New Issue