Chaining multiple transactions together in the front-end to preserve atomicity

Hi all,

Let’s say I have an on-chain program with two instructions Ix_1 and Ix_2. Now in my front-end app, while interacting with the on-chain program (through the anchor typescript library), I want to chain the two transactions together

Ix = {Ix_1,Ix_2}

and I want to preserve atomicity. What I mean is that I want to make sure that the entire transaction Ix only goes through if and only if both Ix_1 and Ix_2 are successful, so if Ix_2 fails, I want Ix_1 to fail as well. Is there a way to make sure of that?

Any help would be much appreciated,


In a non-anchor context, with Solana you can supply multiple instructions in one transaction to accomplish what you are looking for.

You do this by creating a Transaction first, and then calling transaction.add(instruction1); transaction.add(instruction2);.

Anchor has kind of abstracted away the concept of an instruction with the auto-generated typescript bindings, so I don’t know exactly how to do it in Anchor, but hopefully that gives you a little bit of a hint!

You might be able to reach out on the Serum or Solana Discords to see if anybody else has ideas.

1 Like