Skip to content

Add postInstructions #1007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ incremented for features.
### Features

* lang: Add `SystemAccount<'info>` account type for generic wallet addresses or accounts owned by the system program ([#954](https://github.com/project-serum/anchor/pull/954))
* ts: Add inputs `postInstructions` and `preInstructions` as a replacement for (the now deprecated) `instructions`

### Fixes

Expand Down
13 changes: 13 additions & 0 deletions ts/src/program/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@ export type Context<A extends Accounts = Accounts> = {
signers?: Array<Signer>;

/**
* @deprecated use preInstructions instead.
* Instructions to run *before* a given method. Often this is used, for
* example to create accounts prior to executing a method.
*/
instructions?: TransactionInstruction[];

/**
* Instructions to run *before* a given method. Often this is used, for
* example to create accounts prior to executing a method.
*/
preInstructions?: TransactionInstruction[];

/**
* Instructions to run *after* a given method. Often this is used, for
* example to close accounts after executing a method.
*/
postInstructions?: TransactionInstruction[];

/**
* Commitment parameters to use for a transaction.
*/
Expand Down
5 changes: 5 additions & 0 deletions ts/src/program/namespace/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ export default class TransactionFactory {
const txFn: TransactionFn<IDL, I> = (...args): Transaction => {
const [, ctx] = splitArgsAndCtx(idlIx, [...args]);
const tx = new Transaction();
if (ctx.preInstructions && ctx.instructions) {
throw new Error("instructions is deprecated, use preInstructions");
}
ctx.preInstructions?.forEach((ix) => tx.add(ix));
ctx.instructions?.forEach((ix) => tx.add(ix));
tx.add(ixFn(...args));
ctx.postInstructions?.forEach((ix) => tx.add(ix));
return tx;
};

Expand Down
71 changes: 71 additions & 0 deletions ts/tests/transaction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import TransactionFactory from "../src/program/namespace/transaction";
import InstructionFactory from "../src/program/namespace/instruction";
import { Coder } from "../src";
import { PublicKey, TransactionInstruction } from "@solana/web3.js";

describe("Transaction", () => {
const preIx = new TransactionInstruction({
keys: [],
programId: PublicKey.default,
data: Buffer.from("pre"),
});
const postIx = new TransactionInstruction({
keys: [],
programId: PublicKey.default,
data: Buffer.from("post"),
});
const idl = {
version: "0.0.0",
name: "basic_0",
instructions: [
{
name: "initialize",
accounts: [],
args: [],
},
],
};

it("should add pre instructions before method ix", async () => {
const coder = new Coder(idl);
const programId = PublicKey.default;
const ixItem = InstructionFactory.build(
idl.instructions[0],
(ixName, ix) => coder.instruction.encode(ixName, ix),
programId
);
const txItem = TransactionFactory.build(idl.instructions[0], ixItem);
const tx = txItem({ accounts: {}, preInstructions: [preIx] });
expect(tx.instructions.length).toBe(2);
expect(tx.instructions[0]).toMatchObject(preIx);
});

it("should add post instructions after method ix", async () => {
const coder = new Coder(idl);
const programId = PublicKey.default;
const ixItem = InstructionFactory.build(
idl.instructions[0],
(ixName, ix) => coder.instruction.encode(ixName, ix),
programId
);
const txItem = TransactionFactory.build(idl.instructions[0], ixItem);
const tx = txItem({ accounts: {}, postInstructions: [postIx] });
expect(tx.instructions.length).toBe(2);
expect(tx.instructions[1]).toMatchObject(postIx);
});

it("should throw error if both preInstructions and instructions are used", async () => {
const coder = new Coder(idl);
const programId = PublicKey.default;
const ixItem = InstructionFactory.build(
idl.instructions[0],
(ixName, ix) => coder.instruction.encode(ixName, ix),
programId
);
const txItem = TransactionFactory.build(idl.instructions[0], ixItem);

expect(() =>
txItem({ accounts: {}, preInstructions: [preIx], instructions: [preIx] })
).toThrow(new Error("instructions is deprecated, use preInstructions"));
});
});