Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Thirdweb.Tests/Thirdweb.Contracts/Thirdweb.Contracts.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public async Task ReadTest_PartialSig()
Assert.Equal("Kitty DropERC20", result);
}

[Fact(Timeout = 120000)]
public async Task PrepareTest_NoSig()
{
var contract = await this.GetContract();
var exception = await Assert.ThrowsAsync<ArgumentException>(async () => await ThirdwebContract.Prepare(null, contract, "sup", 0));
Assert.Contains("Method signature not found in contract ABI.", exception.Message);
}

private sealed class AllowlistProof
{
public List<byte[]> Proof { get; set; } = new List<byte[]>();
Expand Down Expand Up @@ -263,7 +271,7 @@ private async Task<SmartWallet> GetAccount()
private async Task<ThirdwebContract> GetContract()
{
var client = this.Client;
var contract = await ThirdwebContract.Create(client: client, address: "0xEBB8a39D865465F289fa349A67B3391d8f910da9", chain: 421614);
var contract = await ThirdwebContract.Create(client: client, address: "0xEBB8a39D865465F289fa349A67B3391d8f910da9", chain: 421614); // DropERC20
return contract;
}
}
67 changes: 67 additions & 0 deletions Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ public async Task NullChecks()
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => ThirdwebExtensions.Transfer(wallet, 0, validAddress, 0));
_ = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebExtensions.Transfer(wallet, this._chainId, null, 0));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => ThirdwebExtensions.Transfer(wallet, this._chainId, validAddress, -1));

// GetTransactionCountRaw
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.GetTransactionCountRaw(null, this._chainId, validAddress));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => ThirdwebExtensions.GetTransactionCountRaw(client, 0, validAddress));
_ = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebExtensions.GetTransactionCountRaw(client, this._chainId, null));

// GetTransactionCount
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.GetTransactionCount(null));
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.GetTransactionCount(null, "latest"));

// ERC721_TokenByIndex
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.ERC721_TokenByIndex(null, 0));
}

[Fact(Timeout = 120000)]
Expand Down Expand Up @@ -269,6 +281,37 @@ public async Task Transfer()
Assert.True(receipt.TransactionHash.Length == 66);
}

[Fact(Timeout = 120000)]
public async Task Contract_Read()
{
var contract = await this.GetTokenERC20Contract();
var result = await contract.Read<string>("name");
Assert.NotNull(result);
Assert.NotEmpty(result);
}

[Fact(Timeout = 120000)]
public async Task Contract_Write()
{
var contract = await this.GetTokenERC20Contract();
var wallet = await this.GetSmartWallet();
var receipt = await contract.Write(wallet, "approve", 0, contract.Address, BigInteger.Zero);
Assert.NotNull(receipt);
Assert.True(receipt.TransactionHash.Length == 66);
}

[Fact(Timeout = 120000)]
public async Task Contract_Prepare()
{
var contract = await this.GetTokenERC20Contract();
var wallet = await this.GetSmartWallet();
var transaction = await contract.Prepare(wallet, "approve", 0, contract.Address, BigInteger.Zero);
Assert.NotNull(transaction);
Assert.NotNull(transaction.Input.Data);
Assert.NotNull(transaction.Input.To);
Assert.NotNull(transaction.Input.Value);
}

#endregion

#region ERC20
Expand Down Expand Up @@ -394,6 +437,30 @@ public async Task ERC20_Approve()

#endregion

#region ERC721A

[Fact(Timeout = 120000)]
public async Task ERC721A_TokensOfOwner()
{
var contract = await this.GetERC721AContract();
var ownerAddress = "0x10a798EC43A776c39BA19978EDb6e4a7706326FA";
var tokens = await contract.ERC721A_TokensOfOwner(ownerAddress);
Assert.NotNull(tokens);
Assert.NotEmpty(tokens);
}

[Fact(Timeout = 120000)]
public async Task ERC721A_TokensOfOwnerIn()
{
var contract = await this.GetERC721AContract();
var ownerAddress = "0x10a798EC43A776c39BA19978EDb6e4a7706326FA";
var tokens = await contract.ERC721A_TokensOfOwnerIn(ownerAddress, 0, 1);
Assert.NotNull(tokens);
Assert.NotEmpty(tokens);
}

#endregion

#region ERC721

[Fact(Timeout = 120000)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,16 @@ public async Task WaitForTransactionReceipt_CancellationToken()
var aaReceipt2 = await ThirdwebTransaction.WaitForTransactionReceipt(client, chainId, aaTxHash, CancellationToken.None);
Assert.NotNull(aaReceipt2);
}

[Fact(Timeout = 120000)]
public async Task WaitForTransactionReceipt_ToStringReturnsJson()
{
var client = this.Client;
var chainId = 421614;
var normalTxHash = "0x5a0b6cdb01ecfb25b368d3de1ac844414980ee3c330ec8c1435117b75027b5d7";

var normalReceipt = await ThirdwebTransaction.WaitForTransactionReceipt(client, chainId, normalTxHash);
Assert.NotNull(normalReceipt);
Assert.StartsWith("{", normalReceipt.ToString());
}
}
94 changes: 94 additions & 0 deletions Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,4 +669,98 @@ public async Task GetSocialProfiles_ThrowsException_InvalidAuth()

Assert.Contains("Failed to fetch social profiles", exception.Message);
}

[Fact(Timeout = 120000)]
public async Task IsEip155Enforced_ReturnsFalse_WhenChainIs1()
{
var chainId = new BigInteger(1);
var isEip155Enforced = await Utils.IsEip155Enforced(this.Client, chainId);

Assert.False(isEip155Enforced);
}

[Fact(Timeout = 120000)]
public async Task IsEip155Enforced_ReturnsTrue_WhenChainIs842()
{
var chainId = new BigInteger(842);
var isEip155Enforced = await Utils.IsEip155Enforced(this.Client, chainId);

Assert.True(isEip155Enforced);
}

[Fact(Timeout = 120000)]
public void ReconstructHttpClient_WithHeaders()
{
var newClient = Utils.ReconstructHttpClient(this.Client.HttpClient, this.Client.HttpClient.Headers);
var newHeaders = newClient.Headers;

Assert.NotNull(newHeaders);
Assert.Equal(this.Client.HttpClient.Headers, newHeaders);
}

[Fact(Timeout = 120000)]
public void ReconstructHttpClient_WithoutHeaders()
{
var newClient = Utils.ReconstructHttpClient(this.Client.HttpClient);
var newHeaders = newClient.Headers;

Assert.NotNull(newHeaders);
Assert.Empty(newHeaders);
}

[Fact(Timeout = 120000)]
public async Task FetchGasPrice_Success()
{
var gasPrice = await Utils.FetchGasPrice(this.Client, 1);
Assert.True(gasPrice > 0);
}

[Fact(Timeout = 120000)]
public async Task FetchGasFees_1()
{
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, 1);
Assert.True(maxFee > 0);
Assert.True(maxPrio > 0);
Assert.NotEqual(maxFee, maxPrio);
}

[Fact(Timeout = 120000)]
public async Task FetchGasFees_137()
{
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, 137);
Assert.True(maxFee > 0);
Assert.True(maxPrio > 0);
Assert.True(maxFee > maxPrio);
}

[Fact(Timeout = 120000)]
public async Task FetchGasFees_80002()
{
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, 80002);
Assert.True(maxFee > 0);
Assert.True(maxPrio > 0);
Assert.True(maxFee > maxPrio);
}

[Fact(Timeout = 120000)]
public async Task FetchGasFees_Celo()
{
var chainId = new BigInteger(42220);
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, chainId);
Assert.True(maxFee > 0);
Assert.True(maxPrio > 0);
Assert.Equal(maxFee, maxPrio);

chainId = new BigInteger(44787);
(maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, chainId);
Assert.True(maxFee > 0);
Assert.True(maxPrio > 0);
Assert.Equal(maxFee, maxPrio);

chainId = new BigInteger(62320);
(maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, chainId);
Assert.True(maxFee > 0);
Assert.True(maxPrio > 0);
Assert.Equal(maxFee, maxPrio);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,52 @@ public async Task Initialization_Success()
}

[Fact(Timeout = 120000)]
public async void Initialization_NullPrivateKey()
public async void Create_NullClient()
{
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => PrivateKeyWallet.Create(null, "0x1234567890abcdef"));
}

[Fact(Timeout = 120000)]
public async void Create_NullPrivateKey()
{
var client = this.Client;
var ex = await Assert.ThrowsAsync<ArgumentNullException>(async () => await PrivateKeyWallet.Create(client, null));
Assert.Equal("Private key cannot be null or empty. (Parameter 'privateKeyHex')", ex.Message);
}

[Fact(Timeout = 120000)]
public async void Create_EmptyPrivateKey()
{
var client = this.Client;
var ex = await Assert.ThrowsAsync<ArgumentNullException>(async () => await PrivateKeyWallet.Create(client, string.Empty));
Assert.Equal("Private key cannot be null or empty. (Parameter 'privateKeyHex')", ex.Message);
}

[Fact(Timeout = 120000)]
public async void Generate_NullClient()
{
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => PrivateKeyWallet.Generate(null));
}

[Fact(Timeout = 120000)]
public async void LoadOrGenerate_NullClient()
{
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => PrivateKeyWallet.LoadOrGenerate(null));
}

[Fact(Timeout = 120000)]
public async void SaveAndDelete_CheckPath()
{
var wallet = await PrivateKeyWallet.Generate(this.Client);
await wallet.Save();

var path = PrivateKeyWallet.GetSavePath();
Assert.True(File.Exists(path));

PrivateKeyWallet.Delete();
Assert.False(File.Exists(path));
}

[Fact(Timeout = 120000)]
public async Task Connect()
{
Expand Down
37 changes: 3 additions & 34 deletions Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ public static async Task<TotalCosts> EstimateTotalCosts(ThirdwebTransaction tran
/// <returns>The estimated gas price.</returns>
public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transaction, bool withBump = true)
{
var rpc = ThirdwebRPC.GetRpcInstance(transaction._wallet.Client, transaction.Input.ChainId.Value);
var hex = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_gasPrice").ConfigureAwait(false));
return withBump ? hex.Value * 10 / 9 : hex.Value;
return await Utils.FetchGasPrice(transaction._wallet.Client, transaction.Input.ChainId.Value, withBump).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -238,38 +236,9 @@ public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transa
var maxPriorityFee = fees["max_priority_fee_per_gas"].ToObject<HexBigInteger>().Value;
return withBump ? (maxFee * 10 / 5, maxPriorityFee * 10 / 5) : (maxFee, maxPriorityFee);
}

var gasPrice = await EstimateGasPrice(transaction, withBump).ConfigureAwait(false);

// Polygon Mainnet & Amoy
if (chainId == (BigInteger)137 || chainId == (BigInteger)80002)
{
return (gasPrice * 3 / 2, gasPrice * 4 / 3);
}

// Celo Mainnet, Alfajores & Baklava
if (chainId == (BigInteger)42220 || chainId == (BigInteger)44787 || chainId == (BigInteger)62320)
{
return (gasPrice, gasPrice);
}

try
{
var block = await rpc.SendRequestAsync<JObject>("eth_getBlockByNumber", "latest", true).ConfigureAwait(false);
var baseBlockFee = block["baseFeePerGas"]?.ToObject<HexBigInteger>();
var maxFeePerGas = baseBlockFee.Value * 2;
var maxPriorityFeePerGas = ((await rpc.SendRequestAsync<HexBigInteger>("eth_maxPriorityFeePerGas").ConfigureAwait(false))?.Value) ?? maxFeePerGas / 2;

if (maxPriorityFeePerGas > maxFeePerGas)
{
maxPriorityFeePerGas = maxFeePerGas / 2;
}

return (maxFeePerGas + (maxPriorityFeePerGas * 10 / 9), maxPriorityFeePerGas * 10 / 9);
}
catch
else
{
return (gasPrice, gasPrice);
return await Utils.FetchGasFees(transaction._wallet.Client, chainId, withBump).ConfigureAwait(false);
}
}

Expand Down
12 changes: 0 additions & 12 deletions Thirdweb/Thirdweb.Utils/Utils.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ public class ThirdwebChainData
[JsonProperty("icon")]
public ThirdwebChainIcon Icon { get; set; }

[JsonProperty("faucets")]
public List<object> Faucets { get; set; }

[JsonProperty("slip44")]
public int? Slip44 { get; set; }

[JsonProperty("ens")]
public ThirdwebChainEns Ens { get; set; }

Expand Down Expand Up @@ -114,12 +108,6 @@ public class ThirdwebChainExplorer
public ThirdwebChainIcon Icon { get; set; }
}

public class ThirdwebChainBridge
{
[JsonProperty("url")]
public string Url { get; set; }
}

[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
public class FarcasterProfile
{
Expand Down
Loading
Loading