Transactions
Build Transaction
Every change on the Blockchain requires a transaction. A transaction wraps function calls in the form of clauses. Each clause sends instructions to an address that are encoded as hex string.
To send a transaction you will need to do multiple steps:
Have Private Key at hand that will sign the transaction
Encode the function calls into data calls
Calculate how much gas the transaction will cost
Build a transaction object with all the previous information
Sign the transaction
Send the transaction to the network
Collecting Function Calls in Clauses
The instructions for executing a function on the blockchain needs to be encoded in a certain way. There are different functions to help create the right format, one is the clauseBuilder
that will in this example call increment()
on the given address:
A clause can also send VET in the same action. Check the type definition to also learn more about the internals.
Calculate Gas
While reading on the blockchain is free, writing is requires so called gas fees. Gas is paid in VTHO, the secondary token on VeChain, which is generated by holding VET too.
To calculate the right amount of gas for your transaction, you can use estimateGas
.
If you expect your contracts to have different results based on the sender, you can also pass in the sender address as optional second parameter.
Build Transaction
Once you have instructions + costs, you'll wrap them together into a transaction object with buildTransactionBody.
There are several options that can optionally be passed as third argument to enable fee delegation, dependency on other transactions, priority and an expiration. You will learn more about them in other sections.
Sign Transaction
Once a transaction is built, it needs to be signed by an entity that will execute all the code. This also makes the origin verifiable.
It is a four step process, of getting a signer first:
Get Signer
Sign Transaction
And using the signer to sign the transaction:
Build Signed Transaction Object
signTransaction
returns the fully signed transaction that can already be published using a POST request to the /transactions endpoint of a VeChain node:
For submission by SDK, the raw hex string needs to be restored into a transaction object:
Send Transaction
The signed transaction can be published to the network using sendTransaction
, which will post the data to the connected node:
Wait for Results
sendTransaction
returns a transaction id that can be used to track the status of the newly published transaction. waitForTransaction
will resolve with the full receipt as soon as the result is available:
Example Project
Last updated