Docs

ThirdwebContract.Write

The ThirdwebContract.Write method allows you to execute transactions that alter the state of the blockchain via a smart contract. This is typically used for functions that create, modify, or delete data on the blockchain.

Usage

var transactionReceipt = await ThirdwebContract.Write(wallet, contract, "methodName", weiValue, parameters);

Example

Let's say you have a smart contract with a transfer method that transfers ERC20 tokens from the caller's address to another address. Here's how you could execute a transaction to transfer tokens using ThirdwebContract.Write:

BigInteger chainId = 1; // Ethereum mainnet
string contractAddress = "0x..."; // Your contract address
var client = ThirdwebClient.Create(secretKey: "yourSecretKey");
var contract = await ThirdwebContract.Create(client, contractAddress, chainId);
// The wallet that signs and sends the transaction
var wallet = await PrivateKeyWallet.Create(client, "yourPrivateKeyHex");
// Assuming transfer takes an address and an amount as parameters
string toAddress = "0x...";
BigInteger amount = new BigInteger(1000); // The amount to transfer
// No ether is being sent in this non-payable transaction, so weiValue is 0
BigInteger weiValue = BigInteger.Zero;
// Executing the transfer
var receipt = await ThirdwebContract.Write(wallet, contract, "transfer", weiValue, toAddress, amount);
Console.WriteLine($"Transaction receipt: {receipt}");

This method is essential for interacting with smart contracts when you need to make changes to the blockchain, such as updating state or transferring assets.