Transaction Building
Paying for Sui Transactions with Gas Coins

Paying for Sui Transactions with Gas Coins

With Programmable Transaction blocks, you can use the gas payment coin to construct coins with a set balance using splitCoin. This is useful for Sui payments, and avoids the need for up-front coin selection. You can use txb.gas to access the gas coin in a transaction block, and it is valid as input for any arguments, as long as it is used by-reference. Practically speaking, this means you can also add to the gas coin with mergeCoins and borrow it for Move functions with moveCall.

You can also transfer the gas coin using transferObjects, in the event that you want to transfer all of your coin balance to another address.

Gas configuration

The new transaction builder comes with default behavior for all gas logic, including automatically setting the gas price, budget, and selecting coins to be used as gas. This behavior can be customized.

Gas price

By default, the gas price is set to the reference gas price of the network. You can also explicitly set the gas price of the transaction block by calling setGasPrice on the transaction builder.

txb.setGasPrice(gasPrice);

Budget

By default, the gas budget is automatically derived by executing a dry-run of the transaction block beforehand. The dry run gas consumption is then used to determine a balance for the transaction. You can override this behavior by explicitly setting a gas budget for the transaction, by calling setGasBudget on the transaction builder.

Note: The gas budget is represented in Sui, and should take the gas price of the transaction block into account.

txb.setGasBudget(gasBudgetAmount);

Gas payment

By default, the gas payment is automatically determined by the SDK. The SDK selects all of the users coins that are not used as inputs in the transaction block.

The list of coins used as gas payment will be merged down into a single gas coin before executing the transaction block, and all but one of the gas objects will be deleted. The gas coin at the 0-index will be the coin that all others are merged into.

// you need to ensure that the coins do not overlap with any
// of the input objects for the transaction block
txb.setGasPayment([coin1, coin2]);