How could we get the result of a previous instruction within a transaction with multiple instructions? Say we are swapping a token on an AMM and so we are uncertain of the amount of tokens we are going to receive, is there a way to get token balance changes after the swap instruction has completed and then check / log that with the memo program or something similar?
-
1There is no easy solution for composing instructions like this. You essentially have to save the data to an account and then pass that account to your downstream instruction. Because this not an ideal solution (and not always possible!), we are considering adding a key-value cache to Clockwork threads to help solve this exact problem. I'm gonna try to fit it into the v1.4 release. If you're curious to learn more and offer feedback, there's a proposal on Github here: github.com/clockwork-xyz/clockwork/issues/103– Nick GarfieldCommented Dec 5, 2022 at 15:44
-
Yeah that would be an interesting feature. You can't do anything to access what would be the inner instructions of an instruction as the tx is being processed can you? Like leveraging some of the functions that are used in the bank stage of tx processing or something along those lines?– guillermoCommented Dec 5, 2022 at 19:07
-
Not that I know of. Seems like a potential vulnerability if you could. Afaik accounts are the only memory that programs have.– Nick GarfieldCommented Dec 6, 2022 at 14:35
-
Yeah right, I know there is some capabilities with the Instruction Introspection stuff, but yeah it seems to be limited to just checking the Instruction itself, not what it's Inner Instructions actually turned out to be. Considering that the instructions are processed sequentially, I would've thought it would theoretically possible to get the result of previous instructions within the current tx, but an API would probably have to be built to allow for that.– guillermoCommented Dec 6, 2022 at 18:55
Add a comment
|
2 Answers
You could:
- Save the balance to memory before calling your CPI,
- Call your CPI,
- Call
.reload()
on the account in question, and compare balances.
-
Right it's not within an anchor program unfortunately. Just creating a tx within a rust client. Commented Dec 6, 2022 at 18:53
-
If I'm following what you mean by that, you're saying you have instruction 1 that you expect results in a token balance change of X, the value of which can't be predicted ahead of time, and then you want X to be an input to instruction 2? And you want to construct this transaction entirely client-side? If I have that right, then I believe there's currently not even an in-principle way to do that. The only way would be to roll your own program with the instructions as CPIs, and call
.reload
as described above. Commented Dec 9, 2022 at 1:30 -
Yeah that's what I was attempting. It would be nice if the Instruction Introspection methods could expand to handle that, but yeah no way to do it at the moment as you mentioned from a client side it appears. Will indeed have to just build it into a program. Commented Dec 10, 2022 at 13:07
You can fetch details by signature of your transaction. You can find logs in meta. You will need to compare pre balances and post balances either for input token or output token depending on your needs.