Docs

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",
from: connectedAddress, // optional, defaults to connected address
args: new object[] { connectedAddress, 0, 1 }
);
// transaction.SetValue("0.00000000001");
// transaction.SetGasLimit("100000");
try
{
var data = await transaction.Simulate();
Debugger.Instance.Log("[Custom Call] Simulate Successful", $"Data: {data}");
}
catch (System.Exception e)
{
Debugger.Instance.Log("[Custom Call] Simulate Error", e.Message);
return;
}
await transaction.EstimateAndSetGasLimitAsync();
var gasPrice = await transaction.GetGasPrice();
Debug.Log($"Gas Price: {gasPrice}");
var gasCosts = await transaction.EstimateGasCosts();
Debug.Log($"Gas Cost: {gasCosts.wei} WEI");
Debugger.Instance.Log("[Custom Call] Transaction Preview", transaction.ToString());
try
{
string transactionResult = await transaction.Send();
Debugger.Instance.Log("[Custom Call] Send Successful", "Tx Hash: " + transactionResult);
}
catch (System.Exception e)
{
Debugger.Instance.Log("[Custom Call] Send Error", e.ToString());
}

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.

var gasPrice = await tx.GetGasPrice();

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();

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();