Skip to content

Commit f2855a9

Browse files
committed
Fix Roslyn warnings in Core scripts + claimer proof 1155 + claim condition currency
1 parent 458a19b commit f2855a9

30 files changed

+302
-342
lines changed

Assets/Thirdweb/Core/Scripts/AccountAbstraction/Core/BundlerClient.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,31 @@ public static async Task<PMSponsorOperationResponse> PMSponsorUserOperation(stri
4040

4141
private static async Task<RpcResponseMessage> BundlerRequest(string url, string apiKey, object requestId, string method, params object[] args)
4242
{
43-
using (HttpClient client = new HttpClient())
44-
{
45-
// UnityEngine.Debug.Log($"Bundler Request: {method}({string.Join(", ", args)})");
46-
RpcRequestMessage requestMessage = new RpcRequestMessage(requestId, method, args);
47-
string requestMessageJson = JsonConvert.SerializeObject(requestMessage);
43+
using HttpClient client = new HttpClient();
44+
// UnityEngine.Debug.Log($"Bundler Request: {method}({string.Join(", ", args)})");
45+
var requestMessage = new RpcRequestMessage(requestId, method, args);
46+
string requestMessageJson = JsonConvert.SerializeObject(requestMessage);
4847

49-
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url);
50-
httpRequestMessage.Content = new StringContent(requestMessageJson, System.Text.Encoding.UTF8, "application/json");
51-
if (new Uri(url).Host.EndsWith(".thirdweb.com"))
52-
{
53-
httpRequestMessage.Headers.Add("x-client-id", ThirdwebManager.Instance.SDK.session.Options.clientId);
54-
if (!Utils.IsWebGLBuild())
55-
httpRequestMessage.Headers.Add("x-bundle-id", ThirdwebManager.Instance.SDK.session.BundleId);
56-
}
48+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StringContent(requestMessageJson, System.Text.Encoding.UTF8, "application/json") };
49+
if (new Uri(url).Host.EndsWith(".thirdweb.com"))
50+
{
51+
httpRequestMessage.Headers.Add("x-client-id", ThirdwebManager.Instance.SDK.session.Options.clientId);
52+
if (!Utils.IsWebGLBuild())
53+
httpRequestMessage.Headers.Add("x-bundle-id", ThirdwebManager.Instance.SDK.session.BundleId);
54+
}
5755

58-
var httpResponse = await client.SendAsync(httpRequestMessage);
56+
var httpResponse = await client.SendAsync(httpRequestMessage);
5957

60-
if (!httpResponse.IsSuccessStatusCode)
61-
throw new Exception($"Bundler Request Failed. Error: {httpResponse.StatusCode} - {httpResponse.ReasonPhrase} - {await httpResponse.Content.ReadAsStringAsync()}");
58+
if (!httpResponse.IsSuccessStatusCode)
59+
throw new Exception($"Bundler Request Failed. Error: {httpResponse.StatusCode} - {httpResponse.ReasonPhrase} - {await httpResponse.Content.ReadAsStringAsync()}");
6260

63-
var httpResponseJson = await httpResponse.Content.ReadAsStringAsync();
64-
// UnityEngine.Debug.Log($"Bundler Response: {httpResponseJson}");
61+
var httpResponseJson = await httpResponse.Content.ReadAsStringAsync();
62+
// UnityEngine.Debug.Log($"Bundler Response: {httpResponseJson}");
6563

66-
var response = JsonConvert.DeserializeObject<RpcResponseMessage>(httpResponseJson);
67-
if (response.Error != null)
68-
throw new Exception($"Bundler Request Failed. Error: {response.Error.Code} - {response.Error.Message} - {response.Error.Data}");
69-
return response;
70-
}
64+
var response = JsonConvert.DeserializeObject<RpcResponseMessage>(httpResponseJson);
65+
if (response.Error != null)
66+
throw new Exception($"Bundler Request Failed. Error: {response.Error.Code} - {response.Error.Message} - {response.Error.Data}");
67+
return response;
7168
}
7269
}
7370
}

Assets/Thirdweb/Core/Scripts/AccountAbstraction/Core/SmartWallet.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage req
170170
dummySig[i] = 0x01;
171171

172172
await UpdateDeploymentStatus();
173-
var initData = await GetInitCode();
173+
var (initCode, gas) = await GetInitCode();
174174

175-
var executeFn = new AccountContract.ExecuteFunction()
175+
var executeFn = new AccountContract.ExecuteFunction
176176
{
177177
Target = transactionInput.To,
178178
Value = transactionInput.Value.Value,
179179
Calldata = transactionInput.Data.HexStringToByteArray(),
180+
FromAddress = Accounts[0]
180181
};
181-
executeFn.FromAddress = Accounts[0];
182182
var executeInput = executeFn.CreateTransactionInput(Accounts[0]);
183183

184184
// Create the user operation and its safe (hexified) version
@@ -187,10 +187,10 @@ private async Task<RpcResponseMessage> CreateUserOpAndSend(RpcRequestMessage req
187187
{
188188
Sender = Accounts[0],
189189
Nonce = await GetNonce(),
190-
InitCode = initData.initCode,
190+
InitCode = initCode,
191191
CallData = executeInput.Data.HexStringToByteArray(),
192192
CallGasLimit = transactionInput.Gas != null ? (transactionInput.Gas.Value < 21000 ? 100000 : transactionInput.Gas.Value) : 100000,
193-
VerificationGasLimit = 100000 + initData.gas,
193+
VerificationGasLimit = 100000 + gas,
194194
PreVerificationGas = 21000,
195195
MaxFeePerGas = latestBlock.BaseFeePerGas.Value * 2 + BigInteger.Parse("1500000000"),
196196
MaxPriorityFeePerGas = BigInteger.Parse("1500000000"),

Assets/Thirdweb/Core/Scripts/AccountAbstraction/Nethereum/SmartWalletClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace Thirdweb.AccountAbstraction
88
{
99
public class SmartWalletClient : ClientBase
1010
{
11-
private SmartWallet _smartWallet;
11+
private readonly SmartWallet _smartWallet;
1212

1313
public SmartWalletClient(SmartWallet smartWallet)
1414
{
1515
this._smartWallet = smartWallet;
1616
}
1717

18-
private static readonly Random rng = new Random();
19-
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
18+
private static readonly Random rng = new();
19+
private static readonly DateTime UnixEpoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
2020

2121
public static long GenerateRpcId()
2222
{

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public GenericAction(Type t, Delegate d)
3838
}
3939
}
4040

41-
private static Dictionary<string, TaskCompletionSource<string>> taskMap = new Dictionary<string, TaskCompletionSource<string>>();
42-
private static Dictionary<string, GenericAction> taskActionMap = new Dictionary<string, GenericAction>();
41+
private static readonly Dictionary<string, TaskCompletionSource<string>> taskMap = new();
42+
private static readonly Dictionary<string, GenericAction> taskActionMap = new();
4343

4444
[AOT.MonoPInvokeCallback(typeof(Action<string, string, string>))]
4545
private static void jsCallback(string taskId, string result, string error)
@@ -93,11 +93,11 @@ public static async Task<string> Connect(WalletConnection walletConnection)
9393
#if UNITY_WEBGL
9494
ThirdwebConnect(
9595
taskId,
96-
walletConnection.provider.ToString().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1),
96+
walletConnection.provider.ToString()[..1].ToLower() + walletConnection.provider.ToString()[1..],
9797
walletConnection.chainId.ToString(),
9898
string.IsNullOrEmpty(walletConnection.password) ? Utils.GetDeviceIdentifier() : walletConnection.password,
9999
walletConnection.email,
100-
walletConnection.personalWallet.ToString().Substring(0,1).ToLower() + walletConnection.personalWallet.ToString().Substring(1),
100+
walletConnection.personalWallet.ToString()[..1].ToLower() + walletConnection.personalWallet.ToString()[1..],
101101
jsCallback
102102
);
103103
#endif
@@ -142,7 +142,7 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
142142
if (!Utils.IsWebGLBuild())
143143
{
144144
Debug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
145-
return default(T);
145+
return default;
146146
}
147147
var msg = Utils.ToJson(new RequestMessageBody(body));
148148
string taskId = Guid.NewGuid().ToString();

Assets/Thirdweb/Core/Scripts/Contract.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Contract(string chain, string address, string abi = null)
6969
this.ERC721 = new ERC721(baseRoute, address);
7070
this.ERC1155 = new ERC1155(baseRoute, address);
7171
this.marketplace = new Marketplace(baseRoute, address);
72-
this.pack = new Pack(chain, address);
72+
this.pack = new Pack(address);
7373
this.events = new Events(baseRoute);
7474
}
7575

@@ -86,9 +86,7 @@ public async Task<CurrencyValue> GetBalance()
8686
else
8787
{
8888
BigInteger balance = await ThirdwebManager.Instance.SDK.session.Web3.Eth.GetBalance.SendRequestAsync(address);
89-
CurrencyValue cv = new CurrencyValue();
90-
cv.value = balance.ToString();
91-
cv.displayValue = balance.ToString().ToEth();
89+
var cv = new CurrencyValue { value = balance.ToString(), displayValue = balance.ToString().ToEth() };
9290
return cv;
9391
}
9492
}
@@ -185,7 +183,7 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
185183
{
186184
if (Utils.IsWebGLBuild())
187185
{
188-
args = args ?? new object[0];
186+
args ??= new object[0];
189187
return await Bridge.InvokeRoute<TransactionResult>(getRoute("call"), Utils.ToJsonStringArray(functionName, args, transactionOverrides));
190188
}
191189
else

Assets/Thirdweb/Core/Scripts/ERC1155.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ERC1155 : Routable
2424
/// </summary>
2525
public ERC1155ClaimConditions claimConditions;
2626

27-
private string contractAddress;
27+
private readonly string contractAddress;
2828

2929
/// <summary>
3030
/// Interact with any ERC1155 compatible contract.
@@ -57,12 +57,14 @@ public async Task<NFT> Get(string tokenId)
5757

5858
tokenURI.ReturnValue1 = tokenURI.ReturnValue1.Contains("0x{id}") ? tokenURI.ReturnValue1.Replace("0x{id}", tokenId) : tokenURI.ReturnValue1;
5959

60-
NFT nft = new NFT();
61-
nft.owner = "";
62-
nft.type = "ERC1155";
63-
nft.supply = await TotalSupply(tokenId);
64-
nft.quantityOwned = 404;
65-
nft.metadata = await ThirdwebManager.Instance.SDK.storage.DownloadText<NFTMetadata>(tokenURI.ReturnValue1);
60+
var nft = new NFT
61+
{
62+
owner = "",
63+
type = "ERC1155",
64+
supply = await TotalSupply(tokenId),
65+
quantityOwned = 404,
66+
metadata = await ThirdwebManager.Instance.SDK.storage.DownloadText<NFTMetadata>(tokenURI.ReturnValue1)
67+
};
6668
nft.metadata.image = nft.metadata.image.ReplaceIPFS();
6769
nft.metadata.id = tokenId;
6870
nft.metadata.uri = tokenURI.ReturnValue1.ReplaceIPFS();
@@ -94,7 +96,7 @@ public async Task<List<NFT>> GetAll(QueryAllParams queryParams = null)
9496
start = 0;
9597
end = totalCount - 1;
9698
}
97-
List<NFT> allNfts = new List<NFT>();
99+
var allNfts = new List<NFT>();
98100
for (int i = start; i <= end; i++)
99101
allNfts.Add(await Get(i.ToString()));
100102
return allNfts;
@@ -113,9 +115,9 @@ public async Task<List<NFT>> GetOwned(string address = null)
113115
}
114116
else
115117
{
116-
string owner = address == null ? await ThirdwebManager.Instance.SDK.wallet.GetAddress() : address;
118+
string owner = address ?? await ThirdwebManager.Instance.SDK.wallet.GetAddress();
117119
int totalCount = await TotalCount();
118-
List<NFT> ownedNfts = new List<NFT>();
120+
var ownedNfts = new List<NFT>();
119121
for (int i = 0; i < totalCount; i++)
120122
{
121123
BigInteger ownedBalance = BigInteger.Parse(await BalanceOf(owner, i.ToString()));
@@ -372,7 +374,7 @@ public async Task<TransactionResult> MintTo(string address, NFTMetadataWithSuppl
372374
{
373375
To = address,
374376
TokenId = Utils.GetMaxUint256(),
375-
Uri = uri.IpfsHash.cidToIpfsUrl(),
377+
Uri = uri.IpfsHash.CidToIpfsUrl(),
376378
Amount = nft.supply
377379
}
378380
);
@@ -429,7 +431,7 @@ public async Task<TransactionResult> MintAdditionalSupplyTo(string address, stri
429431
/// </summary>
430432
public class ERC1155ClaimConditions : Routable
431433
{
432-
private string contractAddress;
434+
private readonly string contractAddress;
433435

434436
public ERC1155ClaimConditions(string parentRoute, string contractAddress)
435437
: base(Routable.append(parentRoute, "claimConditions"))
@@ -458,10 +460,10 @@ public async Task<ClaimConditions> GetActive(string tokenId)
458460
new DropERC1155Contract.GetClaimConditionByIdFunction() { TokenId = BigInteger.Parse(tokenId), ConditionId = conditionId.ReturnValue1 }
459461
);
460462

461-
Currency currency = new Currency();
463+
var currency = new Currency();
462464
try
463465
{
464-
await ThirdwebManager.Instance.SDK.GetContract(data.Condition.Currency).ERC20.Get();
466+
currency = await ThirdwebManager.Instance.SDK.GetContract(data.Condition.Currency).ERC20.Get();
465467
}
466468
catch
467469
{
@@ -517,7 +519,7 @@ public async Task<bool> GetClaimerProofs(string tokenId, string claimerAddress)
517519
{
518520
if (Utils.IsWebGLBuild())
519521
{
520-
return await Bridge.InvokeRoute<bool>(getRoute("getClaimerProofs"), Utils.ToJsonStringArray(claimerAddress));
522+
return await Bridge.InvokeRoute<bool>(getRoute("getClaimerProofs"), Utils.ToJsonStringArray(tokenId, claimerAddress));
521523
}
522524
else
523525
{
@@ -619,7 +621,7 @@ public struct ERC1155SignedPayload
619621
/// </summary>
620622
public class ERC1155Signature : Routable
621623
{
622-
private string contractAddress;
624+
private readonly string contractAddress;
623625

624626
/// <summary>
625627
/// Generate, verify and mint signed mintable payloads
@@ -708,7 +710,7 @@ await ThirdwebManager.Instance.SDK.wallet.GetChainId(),
708710
RoyaltyBps = royalty.ReturnValue2,
709711
PrimarySaleRecipient = primarySaleRecipient.ReturnValue1,
710712
TokenId = Utils.GetMaxUint256(),
711-
Uri = uri.IpfsHash.cidToIpfsUrl(),
713+
Uri = uri.IpfsHash.CidToIpfsUrl(),
712714
Quantity = payloadToSign.quantity,
713715
PricePerToken = BigInteger.Parse(payloadToSign.price.ToWei()),
714716
Currency = payloadToSign.currencyAddress,

Assets/Thirdweb/Core/Scripts/ERC20.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ERC20 : Routable
2323
/// </summary>
2424
public ERC20ClaimConditions claimConditions;
2525

26-
private string contractAddress;
26+
private readonly string contractAddress;
2727

2828
/// <summary>
2929
/// Interact with any ERC20 compatible contract.
@@ -58,8 +58,7 @@ public async Task<Currency> Get()
5858

5959
var symbol = await TransactionManager.ThirdwebRead<TokenERC20Contract.SymbolFunction, TokenERC20Contract.SymbolOutputDTO>(contractAddress, new TokenERC20Contract.SymbolFunction());
6060

61-
Currency c = new Currency(name.ReturnValue1, symbol.ReturnValue1, decimals.ReturnValue1.ToString());
62-
return c;
61+
return new Currency(name.ReturnValue1, symbol.ReturnValue1, decimals.ReturnValue1.ToString());
6362
}
6463
}
6564

@@ -169,7 +168,7 @@ public async Task<TransactionResult> SetAllowance(string spender, string amount)
169168
CurrencyValue currentAllowance = await Allowance(spender);
170169
BigInteger diff = BigInteger.Parse(amount.ToWei()) - BigInteger.Parse(currentAllowance.value);
171170

172-
TransactionResult result = new TransactionResult();
171+
var result = new TransactionResult();
173172
if (diff == 0)
174173
{
175174
Debug.LogWarning($"Allowance is already of amount {amount} - Skipping request...");
@@ -353,7 +352,7 @@ public struct ERC20SignedPayload
353352
#nullable enable
354353
public class ERC20ClaimConditions : Routable
355354
{
356-
private string contractAddress;
355+
private readonly string contractAddress;
357356

358357
public ERC20ClaimConditions(string parentRoute, string contractAddress)
359358
: base(Routable.append(parentRoute, "claimConditions"))
@@ -382,10 +381,10 @@ public async Task<ClaimConditions> GetActive()
382381
new DropERC20Contract.GetClaimConditionByIdFunction() { ConditionId = id.ReturnValue1 }
383382
);
384383

385-
Currency currency = new Currency();
384+
var currency = new Currency();
386385
try
387386
{
388-
await ThirdwebManager.Instance.SDK.GetContract(data.Condition.Currency).ERC20.Get();
387+
currency = await ThirdwebManager.Instance.SDK.GetContract(data.Condition.Currency).ERC20.Get();
389388
}
390389
catch
391390
{
@@ -455,7 +454,7 @@ public async Task<bool> GetClaimerProofs(string claimerAddress)
455454
/// </summary>
456455
public class ERC20Signature : Routable
457456
{
458-
private string contractAddress;
457+
private readonly string contractAddress;
459458

460459
/// <summary>
461460
/// Generate, verify and mint signed mintable payloads

0 commit comments

Comments
 (0)