-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathThirdwebExtensions.cs
More file actions
2960 lines (2581 loc) · 134 KB
/
ThirdwebExtensions.cs
File metadata and controls
2960 lines (2581 loc) · 134 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Numerics;
using Nethereum.Util;
using Newtonsoft.Json;
namespace Thirdweb;
public static class ThirdwebExtensions
{
#region Common
/// <summary>
/// Returns whether the contract supports the specified interface.
/// </summary>
/// <param name="contract">The contract instance.</param>
/// <param name="interfaceId">The interface ID to check.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a boolean indicating whether the contract supports the interface.</returns>
/// <exception cref="ArgumentNullException"></exception>
public static async Task<bool> SupportsInterface(this ThirdwebContract contract, string interfaceId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return await ThirdwebContract.Read<bool>(contract, "supportsInterface", interfaceId.HexToBytes());
}
/// <summary>
/// Encodes the function call for the specified method and parameters.
/// </summary>
/// <param name="contract">The contract instance.</param>
/// <param name="method">The method to call.</param>
/// <param name="parameters">The parameters for the method.</param>
/// <returns>The generated calldata.</returns>
public static string CreateCallData(this ThirdwebContract contract, string method, params object[] parameters)
{
(var data, _) = ThirdwebContract.EncodeFunctionCall(contract, method, parameters);
return data;
}
/// <summary>
/// Sends the transaction.
/// </summary>
/// <param name="transaction">The transaction.</param>
/// <returns>The transaction hash.</returns>
public static async Task<string> Send(this ThirdwebTransaction transaction)
{
return await ThirdwebTransaction.Send(transaction);
}
/// <summary>
/// Sends the transaction and waits for the transaction receipt.
/// </summary>
/// <param name="transaction">The transaction.</param>
/// <returns>The transaction receipt.</returns>
public static async Task<ThirdwebTransactionReceipt> SendAndWaitForTransactionReceipt(this ThirdwebTransaction transaction)
{
return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
}
/// <summary>
/// Reads data from the contract using the specified method.
/// </summary>
/// <typeparam name="T">The type of the return value.</typeparam>
/// <param name="contract">The contract instance.</param>
/// <param name="method">The method to call.</param>
/// <param name="parameters">The parameters for the method.</param>
/// <returns>The result of the method call.</returns>
public static async Task<T> Read<T>(this ThirdwebContract contract, string method, params object[] parameters)
{
return await ThirdwebContract.Read<T>(contract, method, parameters);
}
/// <summary>
/// Writes data to the contract using the specified method and parameters.
/// </summary>
/// <param name="contract">The contract instance.</param>
/// <param name="wallet">The wallet instance.</param>
/// <param name="method">The method to call.</param>
/// <param name="weiValue">The value in wei to send.</param>
/// <param name="parameters">The parameters for the method.</param>
/// <returns>A transaction receipt.</returns>
public static async Task<ThirdwebTransactionReceipt> Write(this ThirdwebContract contract, IThirdwebWallet wallet, string method, BigInteger weiValue, params object[] parameters)
{
return await ThirdwebContract.Write(wallet, contract, method, weiValue, parameters);
}
/// <summary>
/// Prepares a transaction for the specified method and parameters.
/// </summary>
/// <param name="contract">The contract instance.</param>
/// <param name="wallet">The wallet instance.</param>
/// <param name="method">The method to call.</param>
/// <param name="weiValue">The value in wei to send.</param>
/// <param name="parameters">The parameters for the method.</param>
/// <returns>A prepared transaction.</returns>
public static async Task<ThirdwebTransaction> Prepare(this ThirdwebContract contract, IThirdwebWallet wallet, string method, BigInteger weiValue, params object[] parameters)
{
return await ThirdwebContract.Prepare(wallet, contract, method, weiValue, parameters);
}
/// <summary>
/// Retrieves the metadata of the specified contract.
/// </summary>
/// <param name="contract">The contract to retrieve metadata for.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the contract metadata.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<ContractMetadata> GetMetadata(this ThirdwebContract contract)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
var contractUri = await ThirdwebContract.Read<string>(contract, "contractURI");
return await ThirdwebStorage.Download<ContractMetadata>(contract.Client, contractUri);
}
/// <summary>
/// Retrieves the image bytes of the specified NFT.
/// </summary>
/// <param name="nft">The NFT to retrieve the image bytes for.</param>
/// <param name="client">The client used to download the image bytes.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the image bytes.</returns>
/// <exception cref="ArgumentNullException">Thrown when the client is null.</exception>
public static async Task<byte[]> GetNFTImageBytes(this NFT nft, ThirdwebClient client)
{
return client == null ? throw new ArgumentNullException(nameof(client))
: string.IsNullOrEmpty(nft.Metadata.Image) ? Array.Empty<byte>()
: await ThirdwebStorage.Download<byte[]>(client, nft.Metadata.Image).ConfigureAwait(false);
}
/// <summary>
/// Retrieves the default royalty information of the specified contract.
/// </summary>
/// <param name="contract">The contract to retrieve the default royalty information for.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the royalty information.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<RoyaltyInfoResult> GetDefaultRoyaltyInfo(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<RoyaltyInfoResult>(contract, "getDefaultRoyaltyInfo");
}
/// <summary>
/// Retrieves the primary sale recipient address of the specified contract.
/// </summary>
/// <param name="contract">The contract to retrieve the primary sale recipient address for.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the primary sale recipient address.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<string> GetPrimarySaleRecipient(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<string>(contract, "primarySaleRecipient");
}
/// <summary>
/// Retrieves the balance of the specified address on the specified chain.
/// </summary>
/// <param name="client">The client used to retrieve the balance.</param>
/// <param name="chainId">The chain ID to retrieve the balance from.</param>
/// <param name="address">The address to retrieve the balance for.</param>
/// <param name="erc20ContractAddress">The optional ERC20 contract address to retrieve the balance from.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the balance in Wei.</returns>
/// <exception cref="ArgumentNullException">Thrown when the client is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the chain ID is less than or equal to 0.</exception>
/// <exception cref="ArgumentException">Thrown when the address is null or empty.</exception>
public static async Task<BigInteger> GetBalanceRaw(ThirdwebClient client, BigInteger chainId, string address, string erc20ContractAddress = null)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (chainId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chainId), "Chain ID must be greater than 0.");
}
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Address must be provided");
}
if (erc20ContractAddress != null)
{
var erc20Contract = await ThirdwebContract.Create(client, erc20ContractAddress, chainId).ConfigureAwait(false);
return await erc20Contract.ERC20_BalanceOf(address).ConfigureAwait(false);
}
var rpc = ThirdwebRPC.GetRpcInstance(client, chainId);
var balanceHex = await rpc.SendRequestAsync<string>("eth_getBalance", address, "latest").ConfigureAwait(false);
return balanceHex.HexToNumber();
}
/// <summary>
/// Retrieves the balance of the specified contract.
/// </summary>
/// <param name="contract">The contract to retrieve the balance for.</param>
/// <param name="erc20ContractAddress">The optional ERC20 contract address to retrieve the balance from.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the balance in Wei.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<BigInteger> GetBalance(this ThirdwebContract contract, string erc20ContractAddress = null)
{
return contract == null
? throw new ArgumentNullException(nameof(contract))
: await GetBalanceRaw(contract.Client, contract.Chain, contract.Address, erc20ContractAddress).ConfigureAwait(false);
}
/// <summary>
/// Retrieves the balance of the specified wallet on the specified chain.
/// </summary>
/// <param name="wallet">The wallet to retrieve the balance for.</param>
/// <param name="chainId">The chain ID to retrieve the balance from.</param>
/// <param name="erc20ContractAddress">The optional ERC20 contract address to retrieve the balance from.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the balance in Wei.</returns>
/// <exception cref="ArgumentNullException">Thrown when the wallet is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the chain ID is less than or equal to 0.</exception>
public static async Task<BigInteger> GetBalance(this IThirdwebWallet wallet, BigInteger chainId, string erc20ContractAddress = null)
{
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
if (chainId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chainId), "Chain ID must be greater than 0.");
}
var address = await wallet.GetAddress().ConfigureAwait(false);
return await GetBalanceRaw(wallet.Client, chainId, address, erc20ContractAddress).ConfigureAwait(false);
}
/// <summary>
/// Retrieves the transaction count (i.e. nonce) of the specified address on the specified chain.
/// </summary>
/// <param name="client">The client used to retrieve the transaction count.</param>
/// <param name="chainId">The chain ID to retrieve the transaction count from.</param>
/// <param name="address">The address to retrieve the transaction count for.</param>
/// <param name="blocktag">The block tag to retrieve the transaction count at. Defaults to "pending".</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction count.</returns>
/// <exception cref="ArgumentNullException">Thrown when the client is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the chain ID is less than or equal to 0.</exception>
/// <exception cref="ArgumentException">Thrown when the address is null or empty.</exception>
public static async Task<BigInteger> GetTransactionCountRaw(ThirdwebClient client, BigInteger chainId, string address, string blocktag = "pending")
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (chainId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chainId), "Chain ID must be greater than 0.");
}
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Address must be provided");
}
var rpc = ThirdwebRPC.GetRpcInstance(client, chainId);
var balanceHex = await rpc.SendRequestAsync<string>("eth_getTransactionCount", address, blocktag).ConfigureAwait(false);
return balanceHex.HexToNumber();
}
/// <summary>
/// Retrieves the transaction count (i.e. nonce) of the specified contract.
/// </summary>
/// <param name="contract">The contract to retrieve the transaction count for.</param>
/// <param name="blocktag">The block tag to retrieve the transaction count at. Defaults to "pending".</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction count.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<BigInteger> GetTransactionCount(this ThirdwebContract contract, string blocktag = "pending")
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await GetTransactionCountRaw(contract.Client, contract.Chain, contract.Address, blocktag).ConfigureAwait(false);
}
/// <summary>
/// Retrieves the transaction count (i.e. nonce) of the specified wallet on the specified chain.
/// </summary>
/// <param name="wallet">The wallet to retrieve the transaction count for.</param>
/// <param name="chainId">The chain ID to retrieve the transaction count from.</param>
/// <param name="blocktag">The block tag to retrieve the transaction count at. Defaults to "pending".</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction count.</returns>
/// <exception cref="ArgumentNullException">Thrown when the wallet is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the chain ID is less than or equal to 0.</exception>
public static async Task<BigInteger> GetTransactionCount(this IThirdwebWallet wallet, BigInteger chainId, string blocktag = "pending")
{
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
if (chainId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chainId), "Chain ID must be greater than 0.");
}
var address = await wallet.GetAddress().ConfigureAwait(false);
return await GetTransactionCountRaw(wallet.Client, chainId, address, blocktag).ConfigureAwait(false);
}
/// <summary>
/// Transfers the specified amount of Wei to the specified address. Passing tokenAddress will override this function to transfer ERC20 tokens.
/// </summary>
/// <param name="wallet">The wallet to transfer from.</param>
/// <param name="chainId">The chain ID to transfer on.</param>
/// <param name="toAddress">The address to transfer to.</param>
/// <param name="weiAmount">The amount of Wei to transfer.</param>
/// <param name="tokenAddress">The optional token address to transfer from. Defaults to the native token address (ETH or equivalent).</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction receipt.</returns>
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction receipt.</returns>
/// <exception cref="ArgumentNullException">Thrown when the wallet is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the chain ID is less than or equal to 0.</exception>
/// <exception cref="ArgumentException">Thrown when the recipient address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> Transfer(
this IThirdwebWallet wallet,
BigInteger chainId,
string toAddress,
BigInteger weiAmount,
string tokenAddress = Constants.NATIVE_TOKEN_ADDRESS
)
{
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
if (chainId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chainId), "Chain ID must be greater than 0.");
}
if (string.IsNullOrEmpty(toAddress))
{
throw new ArgumentException(nameof(toAddress), "Recipient address cannot be null or empty.");
}
if (weiAmount < 0)
{
throw new ArgumentOutOfRangeException(nameof(weiAmount), "Amount must be 0 or greater.");
}
if (tokenAddress != Constants.NATIVE_TOKEN_ADDRESS)
{
var erc20Contract = await ThirdwebContract.Create(wallet.Client, tokenAddress, chainId).ConfigureAwait(false);
return await erc20Contract.ERC20_Transfer(wallet, toAddress, weiAmount).ConfigureAwait(false);
}
else
{
var txInput = new ThirdwebTransactionInput(chainId: chainId, to: toAddress, value: weiAmount);
var tx = await ThirdwebTransaction.Create(wallet, txInput).ConfigureAwait(false);
return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(tx).ConfigureAwait(false);
}
}
/// <summary>
/// Authenticates the wallet.
/// </summary>
/// <param name="wallet">The wallet that will sign the SIWE payload.</param>
/// <param name="domain">The authentication domain.</param>
/// <param name="chainId">The chain ID.</param>
/// <param name="authPayloadPath">The authentication payload path.</param>
/// <param name="authLoginPath">The authentication login path.</param>
/// <param name="httpClientOverride">The HTTP client override.</param>
/// <param name="authPayloadMethod">The authentication payload method.</param>
/// <param name="authLoginMethod">The authentication login method.</param>
/// <param name="separatePayloadAndSignatureInBody">Whether to separate the payload and signature in the body.</param>
/// <returns>The authentication result.</returns>
public static async Task<T> Authenticate<T>(
this IThirdwebWallet wallet,
string domain,
BigInteger chainId,
string authPayloadPath = "/auth/payload",
string authLoginPath = "/auth/login",
string authPayloadMethod = "GET",
string authLoginMethod = "POST",
bool separatePayloadAndSignatureInBody = false,
IThirdwebHttpClient httpClientOverride = null
)
{
var payloadURL = domain + authPayloadPath;
var loginURL = domain + authLoginPath;
var payloadBodyRaw = new { address = await wallet.GetAddress(), chainId = chainId.ToString() };
var payloadBody = JsonConvert.SerializeObject(payloadBodyRaw);
var httpClient = httpClientOverride ?? wallet.Client.HttpClient;
var payloadContent = new StringContent(payloadBody, System.Text.Encoding.UTF8, "application/json");
ThirdwebHttpResponseMessage payloadResponse;
if (authPayloadMethod == "GET")
{
payloadURL += "?address=" + await wallet.GetAddress() + "&chainId=" + chainId.ToString();
payloadResponse = await httpClient.GetAsync(payloadURL);
}
else if (authPayloadMethod == "POST")
{
payloadResponse = await httpClient.PostAsync(payloadURL, payloadContent);
}
else
{
throw new Exception("Unsupported HTTP method for auth payload");
}
_ = payloadResponse.EnsureSuccessStatusCode();
var payloadString = await payloadResponse.Content.ReadAsStringAsync();
var loginBodyRaw = JsonConvert.DeserializeObject<LoginPayload>(payloadString);
if (loginBodyRaw.Payload == null)
{
var payloadDataRaw = JsonConvert.DeserializeObject<LoginPayloadData>(payloadString);
loginBodyRaw.Payload = payloadDataRaw;
}
var payloadToSign = Utils.GenerateSIWE(loginBodyRaw.Payload);
loginBodyRaw.Signature = await wallet.PersonalSign(payloadToSign);
var loginBody = JsonConvert.SerializeObject(separatePayloadAndSignatureInBody ? new { payload = loginBodyRaw.Payload, signature = loginBodyRaw.Signature } : new { payload = loginBodyRaw });
var loginContent = new StringContent(loginBody, System.Text.Encoding.UTF8, "application/json");
ThirdwebHttpResponseMessage loginResponse;
if (authLoginMethod == "GET")
{
loginURL = loginURL + "?payload=" + JsonConvert.SerializeObject(loginBodyRaw);
loginResponse = await httpClient.GetAsync(loginURL);
}
else if (authLoginMethod == "POST")
{
loginResponse = await httpClient.PostAsync(loginURL, loginContent);
}
else
{
throw new Exception("Unsupported HTTP method for auth login");
}
_ = loginResponse.EnsureSuccessStatusCode();
if (typeof(T) == typeof(byte[]))
{
return (T)(object)await loginResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
else if (typeof(T) == typeof(string))
{
return (T)(object)await loginResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else
{
var content = await loginResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<T>(System.Text.Encoding.UTF8.GetString(content));
}
}
#endregion
#region ERC20
/// <summary>
/// Check the balance of a specific address.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddress">The address of the owner whose balance is to be checked.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the balance.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner address is null or empty.</exception>
public static async Task<BigInteger> ERC20_BalanceOf(this ThirdwebContract contract, string ownerAddress)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return string.IsNullOrEmpty(ownerAddress) ? throw new ArgumentException("Owner address must be provided") : await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", ownerAddress);
}
/// <summary>
/// Get the total supply of the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the total supply.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<BigInteger> ERC20_TotalSupply(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<BigInteger>(contract, "totalSupply");
}
/// <summary>
/// Get the number of decimals used by the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with an int result containing the number of decimals.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<int> ERC20_Decimals(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<int>(contract, "decimals");
}
/// <summary>
/// Get the symbol of the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the symbol.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<string> ERC20_Symbol(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<string>(contract, "symbol");
}
/// <summary>
/// Get the name of the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the name.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<string> ERC20_Name(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<string>(contract, "name");
}
/// <summary>
/// Get the allowance of a spender for a specific owner.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddress">The address of the owner.</param>
/// <param name="spenderAddress">The address of the spender.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the allowance.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner address or spender address is null or empty.</exception>
public static async Task<BigInteger> ERC20_Allowance(this ThirdwebContract contract, string ownerAddress, string spenderAddress)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (string.IsNullOrEmpty(ownerAddress))
{
throw new ArgumentException("Owner address must be provided");
}
return string.IsNullOrEmpty(spenderAddress)
? throw new ArgumentException("Spender address must be provided")
: await ThirdwebContract.Read<BigInteger>(contract, "allowance", ownerAddress, spenderAddress);
}
/// <summary>
/// Approve a spender to spend a specific amount of tokens.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="spenderAddress">The address of the spender.</param>
/// <param name="amount">The amount of tokens to approve.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the spender address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC20_Approve(this ThirdwebContract contract, IThirdwebWallet wallet, string spenderAddress, BigInteger amount)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
return string.IsNullOrEmpty(spenderAddress)
? throw new ArgumentException("Spender address must be provided")
: await ThirdwebContract.Write(wallet, contract, "approve", 0, spenderAddress, amount);
}
/// <summary>
/// Transfer tokens to a specific address.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="toAddress">The address of the recipient.</param>
/// <param name="amount">The amount of tokens to transfer.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the recipient address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC20_Transfer(this ThirdwebContract contract, IThirdwebWallet wallet, string toAddress, BigInteger amount)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
return string.IsNullOrEmpty(toAddress) ? throw new ArgumentException("Recipient address must be provided") : await ThirdwebContract.Write(wallet, contract, "transfer", 0, toAddress, amount);
}
/// <summary>
/// Transfer tokens from one address to another.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="fromAddress">The address of the sender.</param>
/// <param name="toAddress">The address of the recipient.</param>
/// <param name="amount">The amount of tokens to transfer.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the sender address or recipient address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC20_TransferFrom(this ThirdwebContract contract, IThirdwebWallet wallet, string fromAddress, string toAddress, BigInteger amount)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
if (string.IsNullOrEmpty(fromAddress))
{
throw new ArgumentException("Sender address must be provided");
}
return string.IsNullOrEmpty(toAddress)
? throw new ArgumentException("Recipient address must be provided")
: await ThirdwebContract.Write(wallet, contract, "transferFrom", 0, fromAddress, toAddress, amount);
}
#endregion
#region ERC721A
public static async Task<List<BigInteger>> ERC721A_TokensOfOwner(this ThirdwebContract contract, string ownerAddress)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return string.IsNullOrEmpty(ownerAddress)
? throw new ArgumentException("Owner address must be provided")
: await ThirdwebContract.Read<List<BigInteger>>(contract, "tokensOfOwner", ownerAddress);
}
public static async Task<List<BigInteger>> ERC721A_TokensOfOwnerIn(this ThirdwebContract contract, string ownerAddress, BigInteger startIndex, BigInteger endIndex)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return string.IsNullOrEmpty(ownerAddress)
? throw new ArgumentException("Owner address must be provided")
: await ThirdwebContract.Read<List<BigInteger>>(contract, "tokensOfOwnerIn", ownerAddress, startIndex, endIndex);
}
#endregion
#region ERC721
/// <summary>
/// Get the total supply of the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the total supply.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<BigInteger> ERC721_TotalSupply(this ThirdwebContract contract)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
try
{
return await ThirdwebContract.Read<BigInteger>(contract, "nextTokenIdToMint");
}
catch
{
return await ThirdwebContract.Read<BigInteger>(contract, "totalSupply");
}
}
/// <summary>
/// Get the token ID of a specific owner by index.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddress">The address of the owner.</param>
/// <param name="index">The index of the token.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the token ID.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner address is null or empty.</exception>
public static async Task<BigInteger> ERC721_TokenOfOwnerByIndex(this ThirdwebContract contract, string ownerAddress, BigInteger index)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return string.IsNullOrEmpty(ownerAddress)
? throw new ArgumentException("Owner address must be provided")
: await ThirdwebContract.Read<BigInteger>(contract, "tokenOfOwnerByIndex", ownerAddress, index);
}
/// <summary>
/// Get the token ID of a specific token by index.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="index">The index of the token.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the token ID.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<BigInteger> ERC721_TokenByIndex(this ThirdwebContract contract, BigInteger index)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<BigInteger>(contract, "tokenByIndex", index);
}
/// <summary>
/// Check the balance of a specific address.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddress">The address of the owner whose balance is to be checked.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the balance.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner address is null or empty.</exception>
public static async Task<BigInteger> ERC721_BalanceOf(this ThirdwebContract contract, string ownerAddress)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return string.IsNullOrEmpty(ownerAddress) ? throw new ArgumentException("Owner address must be provided") : await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", ownerAddress);
}
/// <summary>
/// Get the owner of a specific token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the owner's address.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the token ID is less than 0.</exception>
public static async Task<string> ERC721_OwnerOf(this ThirdwebContract contract, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return tokenId < 0 ? throw new ArgumentOutOfRangeException(nameof(tokenId), "Token ID must be equal or greater than 0") : await ThirdwebContract.Read<string>(contract, "ownerOf", tokenId);
}
/// <summary>
/// Get the name of the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the name.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<string> ERC721_Name(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<string>(contract, "name");
}
/// <summary>
/// Get the symbol of the token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the symbol.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
public static async Task<string> ERC721_Symbol(this ThirdwebContract contract)
{
return contract == null ? throw new ArgumentNullException(nameof(contract)) : await ThirdwebContract.Read<string>(contract, "symbol");
}
/// <summary>
/// Get the URI of a specific token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the token URI.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the token ID is less than 0.</exception>
public static async Task<string> ERC721_TokenURI(this ThirdwebContract contract, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return tokenId < 0 ? throw new ArgumentOutOfRangeException(nameof(tokenId), "Token ID must be equal or greater than 0") : await ThirdwebContract.Read<string>(contract, "tokenURI", tokenId);
}
/// <summary>
/// Approve a specific address to transfer a specific token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="toAddress">The address of the recipient.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the recipient address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC721_Approve(this ThirdwebContract contract, IThirdwebWallet wallet, string toAddress, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
return string.IsNullOrEmpty(toAddress) ? throw new ArgumentException("Recipient address must be provided") : await ThirdwebContract.Write(wallet, contract, "approve", 0, toAddress, tokenId);
}
/// <summary>
/// Get the approved address for a specific token.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a string result containing the approved address.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the token ID is less than 0.</exception>
public static async Task<string> ERC721_GetApproved(this ThirdwebContract contract, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
return tokenId < 0 ? throw new ArgumentOutOfRangeException(nameof(tokenId), "Token ID must be equal or greater than 0") : await ThirdwebContract.Read<string>(contract, "getApproved", tokenId);
}
/// <summary>
/// Check if an address is an operator for another address.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddress">The address of the owner.</param>
/// <param name="operatorAddress">The address of the operator.</param>
/// <returns>A task representing the asynchronous operation, with a boolean result indicating if the operator is approved for the owner.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner address or operator address is null or empty.</exception>
public static async Task<bool> ERC721_IsApprovedForAll(this ThirdwebContract contract, string ownerAddress, string operatorAddress)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (string.IsNullOrEmpty(ownerAddress))
{
throw new ArgumentException("Owner address must be provided");
}
return string.IsNullOrEmpty(operatorAddress)
? throw new ArgumentException("Operator address must be provided")
: await ThirdwebContract.Read<bool>(contract, "isApprovedForAll", ownerAddress, operatorAddress);
}
/// <summary>
/// Set or unset an operator for an owner.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="operatorAddress">The address of the operator.</param>
/// <param name="approved">A boolean indicating whether to set or unset the operator.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the operator address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC721_SetApprovalForAll(this ThirdwebContract contract, IThirdwebWallet wallet, string operatorAddress, bool approved)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
return string.IsNullOrEmpty(operatorAddress)
? throw new ArgumentException("Operator address must be provided")
: await ThirdwebContract.Write(wallet, contract, "setApprovalForAll", 0, operatorAddress, approved);
}
/// <summary>
/// Transfer a specific token from one address to another.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="fromAddress">The address of the sender.</param>
/// <param name="toAddress">The address of the recipient.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the sender address or recipient address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC721_TransferFrom(this ThirdwebContract contract, IThirdwebWallet wallet, string fromAddress, string toAddress, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
if (string.IsNullOrEmpty(fromAddress))
{
throw new ArgumentException("Sender address must be provided");
}
return string.IsNullOrEmpty(toAddress)
? throw new ArgumentException("Recipient address must be provided")
: await ThirdwebContract.Write(wallet, contract, "transferFrom", 0, fromAddress, toAddress, tokenId);
}
/// <summary>
/// Safely transfer a specific token from one address to another.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="wallet">The wallet to use for the transaction.</param>
/// <param name="fromAddress">The address of the sender.</param>
/// <param name="toAddress">The address of the recipient.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a ThirdwebTransactionReceipt result.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract or wallet is null.</exception>
/// <exception cref="ArgumentException">Thrown when the sender address or recipient address is null or empty.</exception>
public static async Task<ThirdwebTransactionReceipt> ERC721_SafeTransferFrom(this ThirdwebContract contract, IThirdwebWallet wallet, string fromAddress, string toAddress, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}
if (string.IsNullOrEmpty(fromAddress))
{
throw new ArgumentException("Sender address must be provided");
}
return string.IsNullOrEmpty(toAddress)
? throw new ArgumentException("Recipient address must be provided")
: await ThirdwebContract.Write(wallet, contract, "safeTransferFrom", 0, fromAddress, toAddress, tokenId);
}
#endregion
#region ERC1155
/// <summary>
/// Check the balance of a specific token for a specific address.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddress">The address of the owner whose balance is to be checked.</param>
/// <param name="tokenId">The ID of the token.</param>
/// <returns>A task representing the asynchronous operation, with a BigInteger result containing the balance.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner address is null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the token ID is less than 0.</exception>
public static async Task<BigInteger> ERC1155_BalanceOf(this ThirdwebContract contract, string ownerAddress, BigInteger tokenId)
{
if (contract == null)
{
throw new ArgumentNullException(nameof(contract));
}
if (string.IsNullOrEmpty(ownerAddress))
{
throw new ArgumentException("Owner address must be provided");
}
return tokenId < 0
? throw new ArgumentOutOfRangeException(nameof(tokenId), "Token ID must be equal or greater than 0")
: await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", ownerAddress, tokenId);
}
/// <summary>
/// Check the balance of multiple tokens for multiple addresses.
/// </summary>
/// <param name="contract">The contract to interact with.</param>
/// <param name="ownerAddresses">The array of owner addresses.</param>
/// <param name="tokenIds">The array of token IDs.</param>
/// <returns>A task representing the asynchronous operation, with a list of BigInteger results containing the balances.</returns>
/// <exception cref="ArgumentNullException">Thrown when the contract is null.</exception>
/// <exception cref="ArgumentException">Thrown when the owner addresses or token IDs are null.</exception>
public static async Task<List<BigInteger>> ERC1155_BalanceOfBatch(this ThirdwebContract contract, string[] ownerAddresses, BigInteger[] tokenIds)
{
if (contract == null)
{