Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit befea2c

Browse files
authored
token-js: Allow (Signer | PublicKey)[] instead of Signer[] when possible (#3967)
* Allow PublicKey[] | Signer[] instead of Signer[] when possible * Change Signer[] | PublicKey[] to (Signer | PublicKey)[]
1 parent 6793256 commit befea2c

File tree

21 files changed

+76
-71
lines changed

21 files changed

+76
-71
lines changed

token/js/src/extensions/cpiGuard/instructions.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PublicKey, Signer } from '@solana/web3.js';
33
import { TransactionInstruction } from '@solana/web3.js';
44
import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js';
55
import { TokenUnsupportedInstructionError } from '../../errors.js';
6+
import { addSigners } from '../../instructions/internal.js';
67
import { TokenInstruction } from '../../instructions/types.js';
78

89
export enum CpiGuardInstruction {
@@ -32,7 +33,7 @@ export const cpiGuardInstructionData = struct<CpiGuardInstructionData>([u8('inst
3233
export function createEnableCpiGuardInstruction(
3334
account: PublicKey,
3435
authority: PublicKey,
35-
multiSigners: Signer[] = [],
36+
multiSigners: (Signer | PublicKey)[] = [],
3637
programId = TOKEN_2022_PROGRAM_ID
3738
): TransactionInstruction {
3839
return createCpiGuardInstruction(CpiGuardInstruction.Enable, account, authority, multiSigners, programId);
@@ -51,7 +52,7 @@ export function createEnableCpiGuardInstruction(
5152
export function createDisableCpiGuardInstruction(
5253
account: PublicKey,
5354
authority: PublicKey,
54-
multiSigners: Signer[] = [],
55+
multiSigners: (Signer | PublicKey)[] = [],
5556
programId = TOKEN_2022_PROGRAM_ID
5657
): TransactionInstruction {
5758
return createCpiGuardInstruction(CpiGuardInstruction.Disable, account, authority, multiSigners, programId);
@@ -61,17 +62,13 @@ function createCpiGuardInstruction(
6162
cpiGuardInstruction: CpiGuardInstruction,
6263
account: PublicKey,
6364
authority: PublicKey,
64-
multiSigners: Signer[],
65+
multiSigners: (Signer | PublicKey)[],
6566
programId: PublicKey
6667
): TransactionInstruction {
6768
if (!programSupportsExtensions(programId)) {
6869
throw new TokenUnsupportedInstructionError();
6970
}
70-
const keys = [{ pubkey: account, isSigner: false, isWritable: true }];
71-
keys.push({ pubkey: authority, isSigner: !multiSigners.length, isWritable: false });
72-
for (const signer of multiSigners) {
73-
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false });
74-
}
71+
const keys = addSigners([{ pubkey: account, isSigner: false, isWritable: true }], authority, multiSigners);
7572

7673
const data = Buffer.alloc(cpiGuardInstructionData.span);
7774
cpiGuardInstructionData.encode(

token/js/src/extensions/defaultAccountState/instructions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PublicKey, Signer } from '@solana/web3.js';
33
import { TransactionInstruction } from '@solana/web3.js';
44
import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js';
55
import { TokenUnsupportedInstructionError } from '../../errors.js';
6+
import { addSigners } from '../../instructions/internal.js';
67
import { TokenInstruction } from '../../instructions/types.js';
78
import type { AccountState } from '../../state/account.js';
89

@@ -71,18 +72,14 @@ export function createUpdateDefaultAccountStateInstruction(
7172
mint: PublicKey,
7273
accountState: AccountState,
7374
freezeAuthority: PublicKey,
74-
multiSigners: Signer[] = [],
75+
multiSigners: (Signer | PublicKey)[] = [],
7576
programId = TOKEN_2022_PROGRAM_ID
7677
): TransactionInstruction {
7778
if (!programSupportsExtensions(programId)) {
7879
throw new TokenUnsupportedInstructionError();
7980
}
80-
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
81-
keys.push({ pubkey: freezeAuthority, isSigner: !multiSigners.length, isWritable: false });
82-
for (const signer of multiSigners) {
83-
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false });
84-
}
8581

82+
const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], freezeAuthority, multiSigners);
8683
const data = Buffer.alloc(defaultAccountStateInstructionData.span);
8784
defaultAccountStateInstructionData.encode(
8885
{

token/js/src/extensions/interestBearingMint/instructions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function createUpdateRateInterestBearingMintInstruction(
8383
mint: PublicKey,
8484
rateAuthority: PublicKey,
8585
rate: number,
86-
multiSigners: Signer[] = [],
86+
multiSigners: (Signer | PublicKey)[] = [],
8787
programId = TOKEN_2022_PROGRAM_ID
8888
) {
8989
const keys = addSigners(

token/js/src/extensions/memoTransfer/instructions.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PublicKey, Signer } from '@solana/web3.js';
33
import { TransactionInstruction } from '@solana/web3.js';
44
import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js';
55
import { TokenUnsupportedInstructionError } from '../../errors.js';
6+
import { addSigners } from '../../instructions/internal.js';
67
import { TokenInstruction } from '../../instructions/types.js';
78

89
export enum MemoTransferInstruction {
@@ -35,7 +36,7 @@ export const memoTransferInstructionData = struct<MemoTransferInstructionData>([
3536
export function createEnableRequiredMemoTransfersInstruction(
3637
account: PublicKey,
3738
authority: PublicKey,
38-
multiSigners: Signer[] = [],
39+
multiSigners: (Signer | PublicKey)[] = [],
3940
programId = TOKEN_2022_PROGRAM_ID
4041
): TransactionInstruction {
4142
return createMemoTransferInstruction(MemoTransferInstruction.Enable, account, authority, multiSigners, programId);
@@ -54,7 +55,7 @@ export function createEnableRequiredMemoTransfersInstruction(
5455
export function createDisableRequiredMemoTransfersInstruction(
5556
account: PublicKey,
5657
authority: PublicKey,
57-
multiSigners: Signer[] = [],
58+
multiSigners: (Signer | PublicKey)[] = [],
5859
programId = TOKEN_2022_PROGRAM_ID
5960
): TransactionInstruction {
6061
return createMemoTransferInstruction(MemoTransferInstruction.Disable, account, authority, multiSigners, programId);
@@ -64,18 +65,14 @@ function createMemoTransferInstruction(
6465
memoTransferInstruction: MemoTransferInstruction,
6566
account: PublicKey,
6667
authority: PublicKey,
67-
multiSigners: Signer[],
68+
multiSigners: (Signer | PublicKey)[],
6869
programId: PublicKey
6970
): TransactionInstruction {
7071
if (!programSupportsExtensions(programId)) {
7172
throw new TokenUnsupportedInstructionError();
7273
}
73-
const keys = [{ pubkey: account, isSigner: false, isWritable: true }];
74-
keys.push({ pubkey: authority, isSigner: !multiSigners.length, isWritable: false });
75-
for (const signer of multiSigners) {
76-
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false });
77-
}
7874

75+
const keys = addSigners([{ pubkey: account, isSigner: false, isWritable: true }], authority, multiSigners);
7976
const data = Buffer.alloc(memoTransferInstructionData.span);
8077
memoTransferInstructionData.encode(
8178
{

token/js/src/extensions/transferFee/instructions.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
TokenInvalidInstructionTypeError,
1111
TokenUnsupportedInstructionError,
1212
} from '../../errors.js';
13+
import { addSigners } from '../../instructions/internal.js';
1314
import { TokenInstruction } from '../../instructions/types.js';
1415

1516
export enum TransferFeeInstruction {
@@ -237,7 +238,7 @@ export function createTransferCheckedWithFeeInstruction(
237238
amount: bigint,
238239
decimals: number,
239240
fee: bigint,
240-
multiSigners: Signer[] = [],
241+
multiSigners: (Signer | PublicKey)[] = [],
241242
programId = TOKEN_2022_PROGRAM_ID
242243
): TransactionInstruction {
243244
if (!programSupportsExtensions(programId)) {
@@ -254,14 +255,15 @@ export function createTransferCheckedWithFeeInstruction(
254255
},
255256
data
256257
);
257-
const keys: AccountMeta[] = [];
258-
keys.push({ pubkey: source, isSigner: false, isWritable: true });
259-
keys.push({ pubkey: mint, isSigner: false, isWritable: false });
260-
keys.push({ pubkey: destination, isSigner: false, isWritable: true });
261-
keys.push({ pubkey: authority, isSigner: !multiSigners.length, isWritable: false });
262-
for (const signer of multiSigners) {
263-
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false });
264-
}
258+
const keys = addSigners(
259+
[
260+
{ pubkey: source, isSigner: false, isWritable: true },
261+
{ pubkey: mint, isSigner: false, isWritable: false },
262+
{ pubkey: destination, isSigner: false, isWritable: true },
263+
],
264+
authority,
265+
multiSigners
266+
);
265267
return new TransactionInstruction({ keys, programId, data });
266268
}
267269

@@ -403,7 +405,7 @@ export function createWithdrawWithheldTokensFromMintInstruction(
403405
mint: PublicKey,
404406
destination: PublicKey,
405407
authority: PublicKey,
406-
signers: Signer[] = [],
408+
signers: (Signer | PublicKey)[] = [],
407409
programId = TOKEN_2022_PROGRAM_ID
408410
): TransactionInstruction {
409411
if (!programSupportsExtensions(programId)) {
@@ -417,15 +419,14 @@ export function createWithdrawWithheldTokensFromMintInstruction(
417419
},
418420
data
419421
);
420-
const keys: AccountMeta[] = [];
421-
keys.push(
422-
{ pubkey: mint, isSigner: false, isWritable: true },
423-
{ pubkey: destination, isSigner: false, isWritable: true },
424-
{ pubkey: authority, isSigner: !signers.length, isWritable: false }
422+
const keys = addSigners(
423+
[
424+
{ pubkey: mint, isSigner: false, isWritable: true },
425+
{ pubkey: destination, isSigner: false, isWritable: true },
426+
],
427+
authority,
428+
signers
425429
);
426-
for (const signer of signers) {
427-
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false });
428-
}
429430
return new TransactionInstruction({ keys, programId, data });
430431
}
431432

@@ -557,7 +558,7 @@ export function createWithdrawWithheldTokensFromAccountsInstruction(
557558
mint: PublicKey,
558559
destination: PublicKey,
559560
authority: PublicKey,
560-
signers: Signer[],
561+
signers: (Signer | PublicKey)[],
561562
sources: PublicKey[],
562563
programId = TOKEN_2022_PROGRAM_ID
563564
): TransactionInstruction {
@@ -573,15 +574,14 @@ export function createWithdrawWithheldTokensFromAccountsInstruction(
573574
},
574575
data
575576
);
576-
const keys: AccountMeta[] = [];
577-
keys.push(
578-
{ pubkey: mint, isSigner: false, isWritable: true },
579-
{ pubkey: destination, isSigner: false, isWritable: true },
580-
{ pubkey: authority, isSigner: !signers.length, isWritable: false }
577+
const keys = addSigners(
578+
[
579+
{ pubkey: mint, isSigner: false, isWritable: true },
580+
{ pubkey: destination, isSigner: false, isWritable: true },
581+
],
582+
authority,
583+
signers
581584
);
582-
for (const signer of signers) {
583-
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false });
584-
}
585585
for (const source of sources) {
586586
keys.push({ pubkey: source, isSigner: false, isWritable: true });
587587
}

token/js/src/instructions/approve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function createApproveInstruction(
3838
delegate: PublicKey,
3939
owner: PublicKey,
4040
amount: number | bigint,
41-
multiSigners: Signer[] = [],
41+
multiSigners: (Signer | PublicKey)[] = [],
4242
programId = TOKEN_PROGRAM_ID
4343
): TransactionInstruction {
4444
const keys = addSigners(

token/js/src/instructions/approveChecked.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function createApproveCheckedInstruction(
4747
owner: PublicKey,
4848
amount: number | bigint,
4949
decimals: number,
50-
multiSigners: Signer[] = [],
50+
multiSigners: (Signer | PublicKey)[] = [],
5151
programId = TOKEN_PROGRAM_ID
5252
): TransactionInstruction {
5353
const keys = addSigners(

token/js/src/instructions/burn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function createBurnInstruction(
3838
mint: PublicKey,
3939
owner: PublicKey,
4040
amount: number | bigint,
41-
multiSigners: Signer[] = [],
41+
multiSigners: (Signer | PublicKey)[] = [],
4242
programId = TOKEN_PROGRAM_ID
4343
): TransactionInstruction {
4444
const keys = addSigners(

token/js/src/instructions/burnChecked.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function createBurnCheckedInstruction(
4545
owner: PublicKey,
4646
amount: number | bigint,
4747
decimals: number,
48-
multiSigners: Signer[] = [],
48+
multiSigners: (Signer | PublicKey)[] = [],
4949
programId = TOKEN_PROGRAM_ID
5050
): TransactionInstruction {
5151
const keys = addSigners(

token/js/src/instructions/closeAccount.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function createCloseAccountInstruction(
3434
account: PublicKey,
3535
destination: PublicKey,
3636
authority: PublicKey,
37-
multiSigners: Signer[] = [],
37+
multiSigners: (Signer | PublicKey)[] = [],
3838
programId = TOKEN_PROGRAM_ID
3939
): TransactionInstruction {
4040
const keys = addSigners(

0 commit comments

Comments
 (0)