Skip to content

Commit

Permalink
make addExtraAccountMetasForExecute modify the instruction in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jan 11, 2024
1 parent f847d4f commit 07b2e6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
67 changes: 35 additions & 32 deletions token/js/src/extensions/transferHook/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ export function createExecuteInstruction(
}

/**
* Adds all the extra accounts needed for a transfer hook to an instruction
* Adds all the extra accounts needed for a transfer hook to an instruction.
*
* Note this will modify the instruction passed in.
*
* @param connection Connection to use
* @param instruction The instruction to add accounts to
Expand All @@ -239,7 +241,6 @@ export function createExecuteInstruction(
* @param owner Owner of the source account
* @param amount The amount of tokens to transfer
* @param commitment Commitment to use
* @returns A new instruction with the extra account metas added
*/
export async function addExtraAccountMetasForExecute(
connection: Connection,
Expand Down Expand Up @@ -295,8 +296,6 @@ export async function addExtraAccountMetasForExecute(
// Add the transfer hook program ID and the validation state account
instruction.keys.push({ pubkey: programId, isSigner: false, isWritable: false });
instruction.keys.push({ pubkey: validateStatePubkey, isSigner: false, isWritable: false });

return instruction;
}

/**
Expand Down Expand Up @@ -327,7 +326,7 @@ export async function createTransferCheckedWithTransferHookInstruction(
commitment?: Commitment,
programId = TOKEN_PROGRAM_ID
) {
const rawInstruction = createTransferCheckedInstruction(
const instruction = createTransferCheckedInstruction(
source,
mint,
destination,
Expand All @@ -341,19 +340,21 @@ export async function createTransferCheckedWithTransferHookInstruction(
const mintInfo = await getMint(connection, mint, commitment, programId);
const transferHook = getTransferHook(mintInfo);

return transferHook
? addExtraAccountMetasForExecute(
connection,
rawInstruction,
transferHook.programId,
source,
mint,
destination,
owner,
amount,
commitment
)
: rawInstruction;
if (transferHook) {
await addExtraAccountMetasForExecute(
connection,
instruction,
transferHook.programId,
source,
mint,
destination,
owner,
amount,
commitment
);
}

return instruction;
}

/**
Expand Down Expand Up @@ -386,7 +387,7 @@ export async function createTransferCheckedWithFeeAndTransferHookInstruction(
commitment?: Commitment,
programId = TOKEN_PROGRAM_ID
) {
const rawInstruction = createTransferCheckedWithFeeInstruction(
const instruction = createTransferCheckedWithFeeInstruction(
source,
mint,
destination,
Expand All @@ -401,17 +402,19 @@ export async function createTransferCheckedWithFeeAndTransferHookInstruction(
const mintInfo = await getMint(connection, mint, commitment, programId);
const transferHook = getTransferHook(mintInfo);

return transferHook
? addExtraAccountMetasForExecute(
connection,
rawInstruction,
transferHook.programId,
source,
mint,
destination,
owner,
amount,
commitment
)
: rawInstruction;
if (transferHook) {
await addExtraAccountMetasForExecute(
connection,
instruction,
transferHook.programId,
source,
mint,
destination,
owner,
amount,
commitment
);
}

return instruction;
}
8 changes: 4 additions & 4 deletions token/js/test/unit/transferHook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ describe('transferHook', () => {
)
).to.be.rejectedWith('Missing required account in instruction');

const rawInstruction = new TransactionInstruction({
const instruction = new TransactionInstruction({
keys: [
{ pubkey: sourcePubkey, isSigner: false, isWritable: true },
{ pubkey: mintPubkey, isSigner: false, isWritable: false },
Expand All @@ -407,9 +407,9 @@ describe('transferHook', () => {
programId: transferHookProgramId,
});

const hydratedInstruction = await addExtraAccountMetasForExecute(
await addExtraAccountMetasForExecute(
connection,
rawInstruction,
instruction,
transferHookProgramId,
sourcePubkey,
mintPubkey,
Expand All @@ -433,7 +433,7 @@ describe('transferHook', () => {
{ pubkey: validateStatePubkey, isSigner: false, isWritable: false },
];

expect(hydratedInstruction.keys).to.eql(checkMetas);
expect(instruction.keys).to.eql(checkMetas);
});

it('can create a transfer instruction with extra metas', async () => {
Expand Down

0 comments on commit 07b2e6b

Please sign in to comment.