feat: require programId and keys for TransactionInstruction

This commit is contained in:
Justin Starry 2021-03-15 10:16:45 +08:00 committed by Justin Starry
parent 806bfdd67b
commit 98ea058ebe
3 changed files with 25 additions and 15 deletions

6
web3.js/module.d.ts vendored
View File

@ -662,8 +662,8 @@ declare module '@solana/web3.js' {
}; };
export type TransactionInstructionCtorFields = { export type TransactionInstructionCtorFields = {
keys?: Array<AccountMeta>; keys: Array<AccountMeta>;
programId?: PublicKey; programId: PublicKey;
data?: Buffer; data?: Buffer;
}; };
@ -672,7 +672,7 @@ declare module '@solana/web3.js' {
programId: PublicKey; programId: PublicKey;
data: Buffer; data: Buffer;
constructor(opts?: TransactionInstructionCtorFields); constructor(opts: TransactionInstructionCtorFields);
} }
export type SignaturePubkeyPair = { export type SignaturePubkeyPair = {

View File

@ -663,8 +663,8 @@ declare module '@solana/web3.js' {
}; };
declare type TransactionInstructionCtorFields = {| declare type TransactionInstructionCtorFields = {|
keys: ?Array<AccountMeta>, keys: Array<AccountMeta>,
programId?: PublicKey, programId: PublicKey,
data?: Buffer, data?: Buffer,
|}; |};
@ -673,9 +673,7 @@ declare module '@solana/web3.js' {
programId: PublicKey; programId: PublicKey;
data: Buffer; data: Buffer;
constructor( constructor(opts: TransactionInstructionCtorFields): TransactionInstruction;
opts?: TransactionInstructionCtorFields,
): TransactionInstruction;
} }
declare type SignaturePubkeyPair = {| declare type SignaturePubkeyPair = {|

View File

@ -53,13 +53,13 @@ export type AccountMeta = {
* List of TransactionInstruction object fields that may be initialized at construction * List of TransactionInstruction object fields that may be initialized at construction
* *
* @typedef {Object} TransactionInstructionCtorFields * @typedef {Object} TransactionInstructionCtorFields
* @property {?Array<PublicKey>} keys * @property {Array<PublicKey>} keys
* @property {?PublicKey} programId * @property {PublicKey} programId
* @property {?Buffer} data * @property {?Buffer} data
*/ */
export type TransactionInstructionCtorFields = {| export type TransactionInstructionCtorFields = {|
keys?: Array<AccountMeta>, keys: Array<AccountMeta>,
programId?: PublicKey, programId: PublicKey,
data?: Buffer, data?: Buffer,
|}; |};
@ -83,7 +83,7 @@ export class TransactionInstruction {
* Public keys to include in this transaction * Public keys to include in this transaction
* Boolean represents whether this pubkey needs to sign the transaction * Boolean represents whether this pubkey needs to sign the transaction
*/ */
keys: Array<AccountMeta> = []; keys: Array<AccountMeta>;
/** /**
* Program Id to execute * Program Id to execute
@ -95,8 +95,12 @@ export class TransactionInstruction {
*/ */
data: Buffer = Buffer.alloc(0); data: Buffer = Buffer.alloc(0);
constructor(opts?: TransactionInstructionCtorFields) { constructor(opts: TransactionInstructionCtorFields) {
opts && Object.assign(this, opts); this.programId = opts.programId;
this.keys = opts.keys;
if (opts.data) {
this.data = opts.data;
}
} }
} }
@ -236,6 +240,14 @@ export class Transaction {
throw new Error('Transaction fee payer required'); throw new Error('Transaction fee payer required');
} }
for (let i = 0; i < this.instructions.length; i++) {
if (this.instructions[i].programId === undefined) {
throw new Error(
`Transaction instruction index ${i} has undefined program id`,
);
}
}
const programIds: string[] = []; const programIds: string[] = [];
const accountMetas: AccountMeta[] = []; const accountMetas: AccountMeta[] = [];
this.instructions.forEach(instruction => { this.instructions.forEach(instruction => {