Skip to content

Commit 591cea1

Browse files
authored
utils tests (#9)
* utils tests * reason str and cancellation token * Update Thirdweb.Utils.Tests.cs
1 parent 4f8d405 commit 591cea1

File tree

2 files changed

+198
-1
lines changed

2 files changed

+198
-1
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
namespace Thirdweb.Tests;
2+
3+
public class UtilsTests : BaseTests
4+
{
5+
public UtilsTests(ITestOutputHelper output)
6+
: base(output) { }
7+
8+
[Fact]
9+
public void ComputeClientIdFromSecretKey()
10+
{
11+
Assert.True(Utils.ComputeClientIdFromSecretKey(_secretKey).Length == 32);
12+
}
13+
14+
[Fact]
15+
public void HexConcat()
16+
{
17+
var hexStrings = new string[] { "0x1234", "0x5678", "0x90AB" };
18+
Assert.Equal("0x1234567890AB", Utils.HexConcat(hexStrings));
19+
}
20+
21+
[Fact]
22+
public async Task GetTransactionReceipt()
23+
{
24+
var client = new ThirdwebClient(secretKey: _secretKey);
25+
var chainId = 421614;
26+
var normalTxHash = "0x5a0b6cdb01ecfb25b368d3de1ac844414980ee3c330ec8c1435117b75027b5d7";
27+
var failedTxHash = "0xd2840219ffe172377c8a455c13d95e4dca204d5c0dd72232093e092eef412488";
28+
var aaTxHash = "0xbf76bd85e1759cf5cf9f4c7c52e76a74d32687f0b516017ff28192d04df50782";
29+
var aaSilentRevertTxHash = "0x8ada86c63846da7a3f91b8c8332de03f134e7619886425df858ee5400a9d9958";
30+
31+
var normalReceipt = await Utils.GetTransactionReceipt(client, chainId, normalTxHash);
32+
Assert.NotNull(normalReceipt);
33+
34+
var failedReceipt = await Assert.ThrowsAsync<Exception>(async () => await Utils.GetTransactionReceipt(client, chainId, failedTxHash));
35+
Assert.Equal($"Transaction {failedTxHash} execution reverted.", failedReceipt.Message);
36+
37+
var aaReceipt = await Utils.GetTransactionReceipt(client, chainId, aaTxHash);
38+
Assert.NotNull(aaReceipt);
39+
40+
var aaFailedReceipt = await Assert.ThrowsAsync<Exception>(async () => await Utils.GetTransactionReceipt(client, chainId, aaSilentRevertTxHash));
41+
Assert.StartsWith($"Transaction {aaSilentRevertTxHash} execution silently reverted", aaFailedReceipt.Message);
42+
}
43+
44+
[Fact]
45+
public async Task GetTransactionReceipt_AAReasonString()
46+
{
47+
var client = new ThirdwebClient(secretKey: _secretKey);
48+
var chainId = 84532;
49+
var aaSilentRevertTxHashWithReason = "0x5374743bbb749df47a279ac21e6ed472c30cd471923a7bc78db6a40e1b6924de";
50+
var aaFailedReceiptWithReason = await Assert.ThrowsAsync<Exception>(async () => await Utils.GetTransactionReceipt(client, chainId, aaSilentRevertTxHashWithReason));
51+
Assert.StartsWith($"Transaction {aaSilentRevertTxHashWithReason} execution silently reverted:", aaFailedReceiptWithReason.Message);
52+
}
53+
54+
[Fact]
55+
public async Task GetTransactionReceipt_CancellationToken()
56+
{
57+
var client = new ThirdwebClient(secretKey: _secretKey);
58+
var chainId = 421614;
59+
var normalTxHash = "0x5a0b6cdb01ecfb25b368d3de1ac844414980ee3c330ec8c1435117b75027b5d7";
60+
var failedTxHash = "0xd2840219ffe172377c8a455c13d95e4dca204d5c0dd72232093e092eef412488";
61+
var aaTxHash = "0xbf76bd85e1759cf5cf9f4c7c52e76a74d32687f0b516017ff28192d04df50782";
62+
var aaSilentRevertTxHash = "0x8ada86c63846da7a3f91b8c8332de03f134e7619886425df858ee5400a9d9958";
63+
64+
var cts = new CancellationTokenSource();
65+
cts.CancelAfter(10000);
66+
var normalReceipt = await Utils.GetTransactionReceipt(client, chainId, normalTxHash, cts.Token);
67+
Assert.NotNull(normalReceipt);
68+
69+
cts = new CancellationTokenSource();
70+
cts.CancelAfter(10000);
71+
var failedReceipt = await Assert.ThrowsAsync<Exception>(async () => await Utils.GetTransactionReceipt(client, chainId, failedTxHash, cts.Token));
72+
Assert.Equal($"Transaction {failedTxHash} execution reverted.", failedReceipt.Message);
73+
74+
cts = new CancellationTokenSource();
75+
cts.CancelAfter(10000);
76+
var aaReceipt = await Utils.GetTransactionReceipt(client, chainId, aaTxHash, cts.Token);
77+
Assert.NotNull(aaReceipt);
78+
79+
cts = new CancellationTokenSource();
80+
cts.CancelAfter(10000);
81+
var aaFailedReceipt = await Assert.ThrowsAsync<Exception>(async () => await Utils.GetTransactionReceipt(client, chainId, aaSilentRevertTxHash, cts.Token));
82+
Assert.StartsWith($"Transaction {aaSilentRevertTxHash} execution silently reverted", aaFailedReceipt.Message);
83+
84+
var infiniteTxHash = "0x55181384a4b908ddf6311cf0eb55ea0aa2b1ef4d9e0cc047eab9051fec284958";
85+
cts = new CancellationTokenSource();
86+
cts.CancelAfter(1);
87+
var infiniteReceipt = await Assert.ThrowsAsync<TaskCanceledException>(async () => await Utils.GetTransactionReceipt(client, chainId, infiniteTxHash, cts.Token));
88+
Assert.Equal("A task was canceled.", infiniteReceipt.Message);
89+
90+
cts = new CancellationTokenSource();
91+
var infiniteReceipt2 = Assert.ThrowsAsync<TaskCanceledException>(() => Utils.GetTransactionReceipt(client, chainId, infiniteTxHash, cts.Token));
92+
await Task.Delay(2000);
93+
cts.Cancel();
94+
Assert.Equal("A task was canceled.", (await infiniteReceipt2).Message);
95+
96+
var aaReceipt2 = await Utils.GetTransactionReceipt(client, chainId, aaTxHash, CancellationToken.None);
97+
Assert.NotNull(aaReceipt2);
98+
}
99+
100+
[Fact]
101+
public void HashPrefixedMessage()
102+
{
103+
var messageStr = "Hello, World!";
104+
var message = System.Text.Encoding.UTF8.GetBytes(messageStr);
105+
var hashStr = Utils.HashPrefixedMessage(messageStr);
106+
var hash = Utils.HashPrefixedMessage(message);
107+
Assert.Equal(hashStr, Utils.BytesToHex(hash));
108+
Assert.Equal("0xc8ee0d506e864589b799a645ddb88b08f5d39e8049f9f702b3b61fa15e55fc73", hashStr);
109+
}
110+
111+
[Fact]
112+
public void BytesToHex()
113+
{
114+
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
115+
Assert.Equal("0x1234567890abcdef", Utils.BytesToHex(bytes));
116+
}
117+
118+
[Fact]
119+
public void HexToBytes()
120+
{
121+
var hex = "0x1234567890abcdef";
122+
var bytes = Utils.HexToBytes(hex);
123+
Assert.Equal(new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }, bytes);
124+
}
125+
126+
[Fact]
127+
public void StringToHex()
128+
{
129+
var str = "Hello, World!";
130+
var hex = Utils.StringToHex(str);
131+
Assert.Equal("0x48656c6c6f2c20576f726c6421", hex);
132+
}
133+
134+
[Fact]
135+
public void HexToString()
136+
{
137+
var hex = "0x48656c6c6f2c20576f726c6421";
138+
var str = Utils.HexToString(hex);
139+
Assert.Equal("Hello, World!", str);
140+
}
141+
142+
[Fact]
143+
public void GetUnixTimeStampNow()
144+
{
145+
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
146+
var now2 = Utils.GetUnixTimeStampNow();
147+
Assert.Equal(now, now2);
148+
}
149+
150+
[Fact]
151+
public void GetUnixTimeStampIn10Years()
152+
{
153+
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
154+
var tenYears = 60 * 60 * 24 * 365 * 10;
155+
var tenYearsFromNow = now + tenYears;
156+
var tenYearsFromNow2 = Utils.GetUnixTimeStampIn10Years();
157+
Assert.Equal(tenYearsFromNow, tenYearsFromNow2);
158+
}
159+
160+
[Fact]
161+
public void ReplaceIPFS()
162+
{
163+
var uri = "ipfs://QmXn1b6Q7";
164+
var gateway = "https://myawesomegateway.io/ipfs/";
165+
var replaced = Utils.ReplaceIPFS(uri, gateway);
166+
Assert.Equal("https://myawesomegateway.io/ipfs/QmXn1b6Q7", replaced);
167+
168+
uri = "https://myawesomegateway.io/ipfs/QmXn1b6Q7";
169+
replaced = Utils.ReplaceIPFS(uri, gateway);
170+
Assert.Equal("https://myawesomegateway.io/ipfs/QmXn1b6Q7", replaced);
171+
172+
uri = "ipfs://QmXn1b6Q7";
173+
gateway = null;
174+
replaced = Utils.ReplaceIPFS(uri, gateway);
175+
Assert.Equal("https://ipfs.io/ipfs/QmXn1b6Q7", replaced);
176+
}
177+
}

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Nethereum.Hex.HexConvertors.Extensions;
77
using Nethereum.RPC.Eth.DTOs;
88
using Nethereum.Signer;
9-
using Newtonsoft.Json;
109

1110
namespace Thirdweb
1211
{
@@ -86,6 +85,27 @@ public static string HashPrefixedMessage(this string message)
8685
return signer.HashPrefixedMessage(Encoding.UTF8.GetBytes(message)).ToHex(true);
8786
}
8887

88+
public static string BytesToHex(byte[] bytes)
89+
{
90+
return bytes.ToHex(true);
91+
}
92+
93+
public static byte[] HexToBytes(string hex)
94+
{
95+
return hex.HexToByteArray();
96+
}
97+
98+
public static string StringToHex(string str)
99+
{
100+
return "0x" + Encoding.UTF8.GetBytes(str).ToHex();
101+
}
102+
103+
public static string HexToString(string hex)
104+
{
105+
var array = HexToBytes(hex);
106+
return Encoding.UTF8.GetString(array, 0, array.Length);
107+
}
108+
89109
public static long GetUnixTimeStampNow()
90110
{
91111
return DateTimeOffset.UtcNow.ToUnixTimeSeconds();

0 commit comments

Comments
 (0)