Is There Any Way to Merge Two PTBs?

Hi Sui community,

Out of some necessity, I need to merge two separate transactions into one. But I’ve looked through a lot of documentation and haven’t found an official solution to merge the PTBs.

So I tried to use the tx.getData() method to retrieve the commands and inputs from both transactions. Then, I attempted to combine these into a new transaction object by manually adding commands using tx.add().

However, I encountered difficulties in properly handling the inputs. Specifically, I’m unsure how to correctly pass the inputs into the new transaction as easily as possible, like merging them without having to reconstruct each input manually. It feels like there should be a more straightforward way to integrate the inputs from both transactions, but I’m struggling to find the best approach to do this.

Has anyone successfully merged transactions in this way? Or is there a better method or utility within the SDK to help handle this scenario?

Any advice would be greatly appreciated!

Thank you!

1 Like

It would be very helpful if you could provide more context on the types of transactions you’re trying to “merge” and the format you’re working with. Specifically, are you trying to combine simple transfers, move calls, or something else?

For now, I can provide a general answer:

If you’re working with the Sui SDK and have the flexibility to adjust how the transactions are executed, you can try combining both operations within the same Transaction block. This allows you to execute multiple commands in sequence as part of a single transaction.

However, please note that each transaction, particularly Programmable Transaction Blocks (PTBs), involves objects or returns from Move calls that are tied to the specific context of that transaction. This means that you cannot use the return of a moveCall from PTB A in PTB B. You will need to retrieve the valid address of the object from the network first and use that in PTB B.

Without specific details about what you are trying to do, it’s challenging to give a more targeted solution. Feel free to share more information and remember you can also attend the Sui Engineering Office Hours and get more technical support

2 Likes

The fundemantals:
The ‘inputs’’ are a enumerated list of pure and object entries that you specifiy in the transaction command.
And then there are “results” of the command itself (not all commands or move_calls return a result) that can be used in commands that follow that.

If you are merging two transactions you need to take care when adding to the inputs. If your base transaction has 2 inputs [0,1] and you are adding two from the transaction you want to merge, not only would you need to append the new, you have to make sure that you append the commands and those new command input references are adjusted to you input references 2 and 3.

The same pretty much holds true for commands results enumerations
(there are variances of course).

2 Likes