Transaction Builder

By default, all transactions initiated using the SDK perform every step of the process for a transaction; From preparing and building the transaction all the way to waiting until it has been mined, and the data is available to be read from the blockchain.

To gain more granular control over the transaction process, all Contract objects come with a Prepare function that returns a Transaction object, which can be used to build, fine-tune, and execute the transaction.

Usage

string connectedAddress = await ThirdwebManager.Instance.SDK.Wallet.GetAddress();
Transaction transaction = await contract.Prepare(
functionName: "claim",
args: new object[] { connectedAddress, 0, 1 }
);
// This step is optional if you don't care about visualizing the input, tx.Send will do it for you
_ = await tx.Popuate();
Debug.Log($"Transaction input after preparing: {tx}");
// You may update the input if you want
var increasedGasLimit = tx.Input.Gas.Value * 10 / 9;
_ = tx.SetGasLimit(increasedGasLimit.ToString());
// Send without waiting for the transaction to be mined
var hash = await tx.Send();
Debug.Log($"Transaction hash: {hash}");
// or you can wait for the transaction to be mined if you don't care about the hash
// var receipt = await tx.SendAndWaitForTransactionResult();
// Debug.Log($"Transaction receipt: {receipt}");

Please be advised that in most cases you do not need to use these functions, as the SDK will handle the transaction process for you. However, if you need more granular control over the transaction process, these functions can be used to fine-tune the transaction.

Static Methods

WaitForTransactionResult

Waits for a transaction to be mined, returning a transaction receipt.

var transactionReceipt = await Transaction.WaitForTransactionResult(transactionHash);

Transaction Methods

SetMaxPriorityFeePerGas

Set the maximum priority fee per gas for the transaction (EIP-1559)

await tx.SetMaxPriorityFeePerGas("1000000000");

SetMaxFeePerGas

Set the maximum fee per gas for the transaction (EIP-1559)

await tx.SetMaxFeePerGas("1000000000");

SetData

Override the data for the transaction.

await tx.SetData("0x1234567890");

SetValue

Set the value for the transaction.

await tx.SetValue("0.00000000001");

SetFrom

Override the sender address for the transaction.

await tx.SetFrom("0x1234567890");

SetGasLimit

Set the gas limit for the transaction. Good if you want to avoid pre-estimation/simulation.

await tx.SetGasLimit("100000");

SetTo

Override the to address for the transaction.

await tx.SetTo("0x1234567890");

SetType

Override the transaction type for the transaction. This will most likely be inferred automatically.

await tx.SetType(TransactionType.Legacy);

SetGasPrice

Set the gas price for the transaction (Legacy Transaction Type).

await tx.SetGasPrice("1000000000");

SetChainId

Override the chain id for the transaction. This will most likely be inferred automatically.

await tx.SetChainId("137");

SetNonce

Override the nonce for the transaction. This will most likely be inferred automatically.

await tx.SetNonce("123");

SetArgs

Override the arguments for the transaction. This may affect calldata.

await tx.SetArgs(new object[] { "0x1234567890", 0, 1 });

GetGasPrice

Get the gas price (for legacy transactions).

var gasPrice = await tx.GetGasPrice();

GetGasFees

Get the max fee per gas and max priority fee per gas (for EIP-1559 transactions).

var gasFees = await tx.GetGasFees();

EstimateGasLimit

Estimate the gas limit for the transaction.

var gasLimit = await tx.EstimateGasLimit();

EstimateGasCosts

Roughly estimates the gas costs for this transaction.

var gasCosts = await tx.EstimateGasCosts();

EstimateAndSetGasLimitAsync

Estimate the gas limit for the transaction and set it.

await tx.EstimateAndSetGasLimitAsync();

Simulate

Attempts to simulate the transaction to find out if it will succeed in its current state.

var data = await tx.Simulate();

Sign

Signs the transaction asynchronously, if the wallet supports it. Useful for smart wallet user op delayed broadcasting through thirdweb Engine. Otherwise not recommended.

await tx.Sign();

Populate

Populates the transaction with the necessary data to be sent. This is called automatically by the SDK when sending a transaction, but can be called manually if needed.

There is no guarantee the gas and nonce values will be preserved when using Account Abstraction.

await tx.Populate();

Send

Sends the transaction without waiting for it to be mined. Returns a transaction hash instead of the typical transaction receipt object.

var transactionHash = await tx.Send();

SendAndWaitForTransactionResult

Sends the transaction and waits for it to be mined, returning a transaction receipt.

var transactionReceipt = await tx.SendAndWaitForTransactionResult();