Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed May 31, 2024
1 parent 7503447 commit 58c1de9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
41 changes: 28 additions & 13 deletions Thirdweb.Tests/Thirdweb.Transactions.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private async Task<ThirdwebTransaction> 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;
}

Expand All @@ -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<ArgumentException>(() => 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<ArgumentException>(() => ThirdwebTransaction.Create(client, wallet, txInput, BigInteger.Zero));
Assert.Contains("Transaction sender (from) must match wallet address", ex.Message);
}
Expand Down Expand Up @@ -150,7 +160,7 @@ public async Task Send_ThrowsIfToAddressNotProvided()
var transaction = await CreateSampleTransaction();
_ = transaction.SetTo(null);

_ = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebTransaction.Send(transaction));
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => ThirdwebTransaction.Send(transaction));
}

[Fact]
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public static async Task<string> Sign(ThirdwebTransaction transaction)

public static async Task<string> 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");
Expand Down

0 comments on commit 58c1de9

Please sign in to comment.