diff --git a/Thirdweb.Tests/Thirdweb.Transactions.Tests.cs b/Thirdweb.Tests/Thirdweb.Transactions.Tests.cs index 8edb1c2..de3e146 100644 --- a/Thirdweb.Tests/Thirdweb.Transactions.Tests.cs +++ b/Thirdweb.Tests/Thirdweb.Transactions.Tests.cs @@ -15,7 +15,7 @@ private async Task CreateSampleTransaction() var wallet = await PrivateKeyWallet.Create(client, _testPrivateKey); var chainId = new BigInteger(421614); - var transaction = await ThirdwebTransaction.Create(client, wallet, new ThirdwebTransactionInput(), chainId); + var transaction = await ThirdwebTransaction.Create(client, wallet, new ThirdwebTransactionInput() { From = await wallet.GetAddress(), To = await wallet.GetAddress(), }, chainId); return transaction; } @@ -24,18 +24,28 @@ public async Task Create_ValidatesInputParameters() { var client = ThirdwebClient.Create(secretKey: _secretKey); var wallet = await PrivateKeyWallet.Create(client, _testPrivateKey); - var txInput = new ThirdwebTransactionInput() { From = await wallet.GetAddress() }; + var txInput = new ThirdwebTransactionInput() { From = await wallet.GetAddress(), To = Constants.ADDRESS_ZERO }; var chainId = new BigInteger(421614); var transaction = await ThirdwebTransaction.Create(client, wallet, txInput, chainId); Assert.NotNull(transaction); } + [Fact] + public async Task Create_ThrowsOnNoTo() + { + var client = ThirdwebClient.Create(secretKey: _secretKey); + var wallet = await PrivateKeyWallet.Create(client, _testPrivateKey); + var txInput = new ThirdwebTransactionInput() { From = await wallet.GetAddress() }; + var ex = await Assert.ThrowsAsync(() => ThirdwebTransaction.Create(client, wallet, txInput, BigInteger.Zero)); + Assert.Contains("Transaction recipient (to) must be provided", ex.Message); + } + [Fact] public async Task Create_ThrowsOnInvalidAddress() { var client = ThirdwebClient.Create(secretKey: _secretKey); var wallet = await PrivateKeyWallet.Create(client, _testPrivateKey); - var txInput = new ThirdwebTransactionInput() { From = "0x123" }; + var txInput = new ThirdwebTransactionInput() { From = "0x123", To = Constants.ADDRESS_ZERO }; var ex = await Assert.ThrowsAsync(() => ThirdwebTransaction.Create(client, wallet, txInput, BigInteger.Zero)); Assert.Contains("Transaction sender (from) must match wallet address", ex.Message); } @@ -150,7 +160,7 @@ public async Task Send_ThrowsIfToAddressNotProvided() var transaction = await CreateSampleTransaction(); _ = transaction.SetTo(null); - _ = await Assert.ThrowsAsync(() => ThirdwebTransaction.Send(transaction)); + _ = await Assert.ThrowsAsync(() => ThirdwebTransaction.Send(transaction)); } [Fact] @@ -268,15 +278,11 @@ public async Task EstimateGasCosts_SmartWalletHigherThanPrivateKeyWallet() var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey); var smartAccount = await SmartWallet.Create(client, personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614); - var transaction = await ThirdwebTransaction.Create(client, smartAccount, new ThirdwebTransactionInput(), 421614); - _ = transaction.SetTo(Constants.ADDRESS_ZERO); - _ = transaction.SetValue(new BigInteger(1000)); + var transaction = await ThirdwebTransaction.Create(client, smartAccount, new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO, Value = new HexBigInteger(1000), }, 421614); var smartCosts = await ThirdwebTransaction.EstimateGasCosts(transaction); - transaction = await ThirdwebTransaction.Create(client, privateKeyAccount, new ThirdwebTransactionInput(), 421614); - _ = transaction.SetTo(Constants.ADDRESS_ZERO); - _ = transaction.SetValue(new BigInteger(1000)); + transaction = await ThirdwebTransaction.Create(client, privateKeyAccount, new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO, Value = new HexBigInteger(1000), }, 421614); var privateCosts = await ThirdwebTransaction.EstimateGasCosts(transaction); @@ -339,9 +345,18 @@ public async Task Simulate_ReturnsData() var client = ThirdwebClient.Create(secretKey: _secretKey); var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey); var smartAccount = await SmartWallet.Create(client, personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614); - var transaction = await ThirdwebTransaction.Create(client, smartAccount, new ThirdwebTransactionInput(), 421614); - _ = transaction.SetValue(new BigInteger(0)); - _ = transaction.SetGasLimit(250000); + var transaction = await ThirdwebTransaction.Create( + client, + smartAccount, + new ThirdwebTransactionInput() + { + To = Constants.ADDRESS_ZERO, + Value = new HexBigInteger(0), + Data = "0x", + Gas = new HexBigInteger(250000), + }, + 421614 + ); var data = await ThirdwebTransaction.Simulate(transaction); Assert.NotNull(data); diff --git a/Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs b/Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs index 470b751..5f50db4 100644 --- a/Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs +++ b/Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs @@ -242,6 +242,11 @@ public static async Task Sign(ThirdwebTransaction transaction) public static async Task Send(ThirdwebTransaction transaction) { + if (transaction.Input.To == null) + { + throw new InvalidOperationException("Transaction recipient (to) must be provided"); + } + if (transaction.Input.GasPrice != null && (transaction.Input.MaxFeePerGas != null || transaction.Input.MaxPriorityFeePerGas != null)) { throw new InvalidOperationException("Transaction GasPrice and MaxFeePerGas/MaxPriorityFeePerGas cannot be set at the same time");