Developer Fundamentals

-- Write Transactions II

Learn how to encode function calls into clauses, estimate gas costs using VTHO, and build complete transaction objects for VeChainThor.

Clauses, Gas Estimation, Transaction Objects

Clauses encode the function calls into data calls. The instructions for executing a function on the blockchain must be encoded in a specific way. There are various functions available to help create the correct format. One is the clauseBuilder that will, in this example, call increment() on the given address:

const clauses = [
  clauseBuilder.functionInteraction(
    '0x8384738c995d49c5b692560ae688fc8b51af1059',
    'increment()'
  ),
];

A clause can also send VET in the same action.

Estimate Gas Costs

While reading on the blockchain is free, writing requires so-called gas fees. Gas is paid in VTHO, the secondary token on VeChain, which is generated by holding VET.

To calculate the right amount of gas for your transaction, you can use estimateGas.

const gasResult = await thor.gas.estimateGas(clauses);

If you expect your contracts to have different results based on the sender, you can also pass in the sender address as an optional second parameter.

Build Transaction Object

Once you have instructions + costs, you'll wrap them together into a transaction object with buildTransactionBody.

const txBody = await thor.transactions.buildTransactionBody(
  clauses,
  gasResult.totalGas
);

There are several options that can optionally be passed as a third argument to enable fee delegation, dependency on other transactions, priority, and expiration. You will learn more about them in the Fee Delegation section.