Skip to content

Commit 179904c

Browse files
authored
more utils, tests and docs (#84)
1 parent 9f895d6 commit 179904c

File tree

10 files changed

+637
-301
lines changed

10 files changed

+637
-301
lines changed

Thirdweb.Tests/Thirdweb.Contracts/Thirdweb.Contracts.Tests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public async Task ReadTest_PartialSig()
9494
Assert.Equal("Kitty DropERC20", result);
9595
}
9696

97+
[Fact(Timeout = 120000)]
98+
public async Task PrepareTest_NoSig()
99+
{
100+
var contract = await this.GetContract();
101+
var exception = await Assert.ThrowsAsync<ArgumentException>(async () => await ThirdwebContract.Prepare(null, contract, "sup", 0));
102+
Assert.Contains("Method signature not found in contract ABI.", exception.Message);
103+
}
104+
97105
private sealed class AllowlistProof
98106
{
99107
public List<byte[]> Proof { get; set; } = new List<byte[]>();
@@ -263,7 +271,7 @@ private async Task<SmartWallet> GetAccount()
263271
private async Task<ThirdwebContract> GetContract()
264272
{
265273
var client = this.Client;
266-
var contract = await ThirdwebContract.Create(client: client, address: "0xEBB8a39D865465F289fa349A67B3391d8f910da9", chain: 421614);
274+
var contract = await ThirdwebContract.Create(client: client, address: "0xEBB8a39D865465F289fa349A67B3391d8f910da9", chain: 421614); // DropERC20
267275
return contract;
268276
}
269277
}

Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ public async Task NullChecks()
105105
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => ThirdwebExtensions.Transfer(wallet, 0, validAddress, 0));
106106
_ = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebExtensions.Transfer(wallet, this._chainId, null, 0));
107107
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => ThirdwebExtensions.Transfer(wallet, this._chainId, validAddress, -1));
108+
109+
// GetTransactionCountRaw
110+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.GetTransactionCountRaw(null, this._chainId, validAddress));
111+
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => ThirdwebExtensions.GetTransactionCountRaw(client, 0, validAddress));
112+
_ = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebExtensions.GetTransactionCountRaw(client, this._chainId, null));
113+
114+
// GetTransactionCount
115+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.GetTransactionCount(null));
116+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.GetTransactionCount(null, "latest"));
117+
118+
// ERC721_TokenByIndex
119+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => ThirdwebExtensions.ERC721_TokenByIndex(null, 0));
108120
}
109121

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

284+
[Fact(Timeout = 120000)]
285+
public async Task Contract_Read()
286+
{
287+
var contract = await this.GetTokenERC20Contract();
288+
var result = await contract.Read<string>("name");
289+
Assert.NotNull(result);
290+
Assert.NotEmpty(result);
291+
}
292+
293+
[Fact(Timeout = 120000)]
294+
public async Task Contract_Write()
295+
{
296+
var contract = await this.GetTokenERC20Contract();
297+
var wallet = await this.GetSmartWallet();
298+
var receipt = await contract.Write(wallet, "approve", 0, contract.Address, BigInteger.Zero);
299+
Assert.NotNull(receipt);
300+
Assert.True(receipt.TransactionHash.Length == 66);
301+
}
302+
303+
[Fact(Timeout = 120000)]
304+
public async Task Contract_Prepare()
305+
{
306+
var contract = await this.GetTokenERC20Contract();
307+
var wallet = await this.GetSmartWallet();
308+
var transaction = await contract.Prepare(wallet, "approve", 0, contract.Address, BigInteger.Zero);
309+
Assert.NotNull(transaction);
310+
Assert.NotNull(transaction.Input.Data);
311+
Assert.NotNull(transaction.Input.To);
312+
Assert.NotNull(transaction.Input.Value);
313+
}
314+
272315
#endregion
273316

274317
#region ERC20
@@ -394,6 +437,30 @@ public async Task ERC20_Approve()
394437

395438
#endregion
396439

440+
#region ERC721A
441+
442+
[Fact(Timeout = 120000)]
443+
public async Task ERC721A_TokensOfOwner()
444+
{
445+
var contract = await this.GetERC721AContract();
446+
var ownerAddress = "0x10a798EC43A776c39BA19978EDb6e4a7706326FA";
447+
var tokens = await contract.ERC721A_TokensOfOwner(ownerAddress);
448+
Assert.NotNull(tokens);
449+
Assert.NotEmpty(tokens);
450+
}
451+
452+
[Fact(Timeout = 120000)]
453+
public async Task ERC721A_TokensOfOwnerIn()
454+
{
455+
var contract = await this.GetERC721AContract();
456+
var ownerAddress = "0x10a798EC43A776c39BA19978EDb6e4a7706326FA";
457+
var tokens = await contract.ERC721A_TokensOfOwnerIn(ownerAddress, 0, 1);
458+
Assert.NotNull(tokens);
459+
Assert.NotEmpty(tokens);
460+
}
461+
462+
#endregion
463+
397464
#region ERC721
398465

399466
[Fact(Timeout = 120000)]

Thirdweb.Tests/Thirdweb.Transactions/Thirdweb.Transactions.Tests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,16 @@ public async Task WaitForTransactionReceipt_CancellationToken()
444444
var aaReceipt2 = await ThirdwebTransaction.WaitForTransactionReceipt(client, chainId, aaTxHash, CancellationToken.None);
445445
Assert.NotNull(aaReceipt2);
446446
}
447+
448+
[Fact(Timeout = 120000)]
449+
public async Task WaitForTransactionReceipt_ToStringReturnsJson()
450+
{
451+
var client = this.Client;
452+
var chainId = 421614;
453+
var normalTxHash = "0x5a0b6cdb01ecfb25b368d3de1ac844414980ee3c330ec8c1435117b75027b5d7";
454+
455+
var normalReceipt = await ThirdwebTransaction.WaitForTransactionReceipt(client, chainId, normalTxHash);
456+
Assert.NotNull(normalReceipt);
457+
Assert.StartsWith("{", normalReceipt.ToString());
458+
}
447459
}

Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,98 @@ public async Task GetSocialProfiles_ThrowsException_InvalidAuth()
669669

670670
Assert.Contains("Failed to fetch social profiles", exception.Message);
671671
}
672+
673+
[Fact(Timeout = 120000)]
674+
public async Task IsEip155Enforced_ReturnsFalse_WhenChainIs1()
675+
{
676+
var chainId = new BigInteger(1);
677+
var isEip155Enforced = await Utils.IsEip155Enforced(this.Client, chainId);
678+
679+
Assert.False(isEip155Enforced);
680+
}
681+
682+
[Fact(Timeout = 120000)]
683+
public async Task IsEip155Enforced_ReturnsTrue_WhenChainIs842()
684+
{
685+
var chainId = new BigInteger(842);
686+
var isEip155Enforced = await Utils.IsEip155Enforced(this.Client, chainId);
687+
688+
Assert.True(isEip155Enforced);
689+
}
690+
691+
[Fact(Timeout = 120000)]
692+
public void ReconstructHttpClient_WithHeaders()
693+
{
694+
var newClient = Utils.ReconstructHttpClient(this.Client.HttpClient, this.Client.HttpClient.Headers);
695+
var newHeaders = newClient.Headers;
696+
697+
Assert.NotNull(newHeaders);
698+
Assert.Equal(this.Client.HttpClient.Headers, newHeaders);
699+
}
700+
701+
[Fact(Timeout = 120000)]
702+
public void ReconstructHttpClient_WithoutHeaders()
703+
{
704+
var newClient = Utils.ReconstructHttpClient(this.Client.HttpClient);
705+
var newHeaders = newClient.Headers;
706+
707+
Assert.NotNull(newHeaders);
708+
Assert.Empty(newHeaders);
709+
}
710+
711+
[Fact(Timeout = 120000)]
712+
public async Task FetchGasPrice_Success()
713+
{
714+
var gasPrice = await Utils.FetchGasPrice(this.Client, 1);
715+
Assert.True(gasPrice > 0);
716+
}
717+
718+
[Fact(Timeout = 120000)]
719+
public async Task FetchGasFees_1()
720+
{
721+
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, 1);
722+
Assert.True(maxFee > 0);
723+
Assert.True(maxPrio > 0);
724+
Assert.NotEqual(maxFee, maxPrio);
725+
}
726+
727+
[Fact(Timeout = 120000)]
728+
public async Task FetchGasFees_137()
729+
{
730+
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, 137);
731+
Assert.True(maxFee > 0);
732+
Assert.True(maxPrio > 0);
733+
Assert.True(maxFee > maxPrio);
734+
}
735+
736+
[Fact(Timeout = 120000)]
737+
public async Task FetchGasFees_80002()
738+
{
739+
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, 80002);
740+
Assert.True(maxFee > 0);
741+
Assert.True(maxPrio > 0);
742+
Assert.True(maxFee > maxPrio);
743+
}
744+
745+
[Fact(Timeout = 120000)]
746+
public async Task FetchGasFees_Celo()
747+
{
748+
var chainId = new BigInteger(42220);
749+
var (maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, chainId);
750+
Assert.True(maxFee > 0);
751+
Assert.True(maxPrio > 0);
752+
Assert.Equal(maxFee, maxPrio);
753+
754+
chainId = new BigInteger(44787);
755+
(maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, chainId);
756+
Assert.True(maxFee > 0);
757+
Assert.True(maxPrio > 0);
758+
Assert.Equal(maxFee, maxPrio);
759+
760+
chainId = new BigInteger(62320);
761+
(maxFee, maxPrio) = await Utils.FetchGasFees(this.Client, chainId);
762+
Assert.True(maxFee > 0);
763+
Assert.True(maxPrio > 0);
764+
Assert.Equal(maxFee, maxPrio);
765+
}
672766
}

Thirdweb.Tests/Thirdweb.Wallets/Thirdweb.PrivateKeyWallet.Tests.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,52 @@ public async Task Initialization_Success()
2121
}
2222

2323
[Fact(Timeout = 120000)]
24-
public async void Initialization_NullPrivateKey()
24+
public async void Create_NullClient()
25+
{
26+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => PrivateKeyWallet.Create(null, "0x1234567890abcdef"));
27+
}
28+
29+
[Fact(Timeout = 120000)]
30+
public async void Create_NullPrivateKey()
2531
{
2632
var client = this.Client;
2733
var ex = await Assert.ThrowsAsync<ArgumentNullException>(async () => await PrivateKeyWallet.Create(client, null));
2834
Assert.Equal("Private key cannot be null or empty. (Parameter 'privateKeyHex')", ex.Message);
2935
}
3036

37+
[Fact(Timeout = 120000)]
38+
public async void Create_EmptyPrivateKey()
39+
{
40+
var client = this.Client;
41+
var ex = await Assert.ThrowsAsync<ArgumentNullException>(async () => await PrivateKeyWallet.Create(client, string.Empty));
42+
Assert.Equal("Private key cannot be null or empty. (Parameter 'privateKeyHex')", ex.Message);
43+
}
44+
45+
[Fact(Timeout = 120000)]
46+
public async void Generate_NullClient()
47+
{
48+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => PrivateKeyWallet.Generate(null));
49+
}
50+
51+
[Fact(Timeout = 120000)]
52+
public async void LoadOrGenerate_NullClient()
53+
{
54+
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => PrivateKeyWallet.LoadOrGenerate(null));
55+
}
56+
57+
[Fact(Timeout = 120000)]
58+
public async void SaveAndDelete_CheckPath()
59+
{
60+
var wallet = await PrivateKeyWallet.Generate(this.Client);
61+
await wallet.Save();
62+
63+
var path = PrivateKeyWallet.GetSavePath();
64+
Assert.True(File.Exists(path));
65+
66+
PrivateKeyWallet.Delete();
67+
Assert.False(File.Exists(path));
68+
}
69+
3170
[Fact(Timeout = 120000)]
3271
public async Task Connect()
3372
{

Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ public static async Task<TotalCosts> EstimateTotalCosts(ThirdwebTransaction tran
215215
/// <returns>The estimated gas price.</returns>
216216
public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transaction, bool withBump = true)
217217
{
218-
var rpc = ThirdwebRPC.GetRpcInstance(transaction._wallet.Client, transaction.Input.ChainId.Value);
219-
var hex = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_gasPrice").ConfigureAwait(false));
220-
return withBump ? hex.Value * 10 / 9 : hex.Value;
218+
return await Utils.FetchGasPrice(transaction._wallet.Client, transaction.Input.ChainId.Value, withBump).ConfigureAwait(false);
221219
}
222220

223221
/// <summary>
@@ -238,38 +236,9 @@ public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transa
238236
var maxPriorityFee = fees["max_priority_fee_per_gas"].ToObject<HexBigInteger>().Value;
239237
return withBump ? (maxFee * 10 / 5, maxPriorityFee * 10 / 5) : (maxFee, maxPriorityFee);
240238
}
241-
242-
var gasPrice = await EstimateGasPrice(transaction, withBump).ConfigureAwait(false);
243-
244-
// Polygon Mainnet & Amoy
245-
if (chainId == (BigInteger)137 || chainId == (BigInteger)80002)
246-
{
247-
return (gasPrice * 3 / 2, gasPrice * 4 / 3);
248-
}
249-
250-
// Celo Mainnet, Alfajores & Baklava
251-
if (chainId == (BigInteger)42220 || chainId == (BigInteger)44787 || chainId == (BigInteger)62320)
252-
{
253-
return (gasPrice, gasPrice);
254-
}
255-
256-
try
257-
{
258-
var block = await rpc.SendRequestAsync<JObject>("eth_getBlockByNumber", "latest", true).ConfigureAwait(false);
259-
var baseBlockFee = block["baseFeePerGas"]?.ToObject<HexBigInteger>();
260-
var maxFeePerGas = baseBlockFee.Value * 2;
261-
var maxPriorityFeePerGas = ((await rpc.SendRequestAsync<HexBigInteger>("eth_maxPriorityFeePerGas").ConfigureAwait(false))?.Value) ?? maxFeePerGas / 2;
262-
263-
if (maxPriorityFeePerGas > maxFeePerGas)
264-
{
265-
maxPriorityFeePerGas = maxFeePerGas / 2;
266-
}
267-
268-
return (maxFeePerGas + (maxPriorityFeePerGas * 10 / 9), maxPriorityFeePerGas * 10 / 9);
269-
}
270-
catch
239+
else
271240
{
272-
return (gasPrice, gasPrice);
241+
return await Utils.FetchGasFees(transaction._wallet.Client, chainId, withBump).ConfigureAwait(false);
273242
}
274243
}
275244

Thirdweb/Thirdweb.Utils/Utils.Types.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ public class ThirdwebChainData
4545
[JsonProperty("icon")]
4646
public ThirdwebChainIcon Icon { get; set; }
4747

48-
[JsonProperty("faucets")]
49-
public List<object> Faucets { get; set; }
50-
51-
[JsonProperty("slip44")]
52-
public int? Slip44 { get; set; }
53-
5448
[JsonProperty("ens")]
5549
public ThirdwebChainEns Ens { get; set; }
5650

@@ -114,12 +108,6 @@ public class ThirdwebChainExplorer
114108
public ThirdwebChainIcon Icon { get; set; }
115109
}
116110

117-
public class ThirdwebChainBridge
118-
{
119-
[JsonProperty("url")]
120-
public string Url { get; set; }
121-
}
122-
123111
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
124112
public class FarcasterProfile
125113
{

0 commit comments

Comments
 (0)