Skip to content

Commit d4de5fd

Browse files
committed
Remove default rpc id, allow disabling initialize on awake
1 parent 7d37284 commit d4de5fd

File tree

5 files changed

+65
-71
lines changed

5 files changed

+65
-71
lines changed

Assets/Thirdweb/Core/Scripts/ThirdwebManager.cs

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ChainData(string identifier, string chainId, string rpcOverride)
2121
public class ThirdwebManager : MonoBehaviour
2222
{
2323
[Tooltip("The chain to initialize the SDK with")]
24-
public string chain = "goerli";
24+
public string activeChain = "goerli";
2525

2626
[Tooltip("Support any chain by adding it to this list from the inspector")]
2727
public List<ChainData> supportedChains =
@@ -46,6 +46,9 @@ public class ThirdwebManager : MonoBehaviour
4646
[Tooltip("Thirdweb Client ID (https://thirdweb.com/create-api-key/). Used for default thirdweb services such as Storage and Account Abstraction.")]
4747
public string clientId;
4848

49+
[Tooltip("Whether the SDK should initialize on awake or not")]
50+
public bool initializeOnAwake = true;
51+
4952
[Tooltip("The name of your app")]
5053
public string appName = null;
5154

@@ -112,8 +115,6 @@ public class ThirdwebManager : MonoBehaviour
112115

113116
private void Awake()
114117
{
115-
// Single persistent instance at all times.
116-
117118
if (Instance == null)
118119
{
119120
Instance = this;
@@ -126,42 +127,59 @@ private void Awake()
126127
return;
127128
}
128129

129-
// Inspector chain data dictionary.
130+
if (initializeOnAwake)
131+
Initialize(activeChain);
132+
}
130133

131-
ChainData currentChain = supportedChains.Find(x => x.identifier == chain);
134+
public void Initialize(string chainIdentifier)
135+
{
136+
// Pass supported chains with replaced RPCs
132137

133-
// Chain ID must be provided on native platforms.
138+
var options = new ThirdwebSDK.Options();
134139

135-
BigInteger chainId = -1;
140+
activeChain = chainIdentifier;
141+
string activeChainId = null;
142+
string activeChainRpc = null;
136143

137-
if (string.IsNullOrEmpty(currentChain.chainId))
138-
throw new UnityException("You must provide a Chain ID on native platforms!");
144+
var supportedChainData = new List<ThirdwebChainData>();
145+
foreach (var chainData in this.supportedChains)
146+
{
147+
if (string.IsNullOrEmpty(chainData.identifier))
148+
throw new UnityException($"You must provide a valid chain identifier! See https://thirdweb.com/dashboard/rpc for a list of supported chains.");
139149

140-
if (!BigInteger.TryParse(currentChain.chainId, out chainId))
141-
throw new UnityException("The Chain ID must be a non-negative integer!");
150+
if (string.IsNullOrEmpty(chainData.chainId) || !BigInteger.TryParse(chainData.chainId, out _))
151+
throw new UnityException($"Could not add {chainData.identifier} to supported chains, you must provide a valid chain ID!");
142152

143-
// Must provide a proper chain identifier (https://thirdweb.com/dashboard/rpc) or RPC override.
153+
if (!string.IsNullOrEmpty(chainData.rpcOverride) && !chainData.rpcOverride.StartsWith("https://"))
154+
throw new UnityException($"Could not add {chainData.identifier} to supported chains, RPC overrides must start with https:// or be left empty to use thirdweb RPCs!");
144155

145-
string chainOrRPC = null;
156+
string rpc = string.IsNullOrEmpty(chainData.rpcOverride)
157+
? (string.IsNullOrEmpty(clientId) ? $"https://{chainData.identifier}.rpc.thirdweb.com/" : $"https://{chainData.identifier}.rpc.thirdweb.com/{clientId}")
158+
: chainData.rpcOverride;
146159

147-
if (!string.IsNullOrEmpty(currentChain.rpcOverride))
148-
{
149-
if (!currentChain.rpcOverride.StartsWith("https://"))
150-
throw new UnityException("RPC overrides must start with https:// !");
151-
else
152-
chainOrRPC = currentChain.rpcOverride;
153-
}
154-
else
155-
{
156-
if (string.IsNullOrEmpty(currentChain.identifier))
157-
throw new UnityException("When not providing an RPC, you must provide a chain identifier!");
158-
else
159-
chainOrRPC = currentChain.identifier;
160+
if (new System.Uri(rpc).Host.EndsWith(".thirdweb.com"))
161+
rpc = rpc.AppendBundleIdQueryParam();
162+
163+
if (chainData.identifier == activeChain)
164+
{
165+
activeChainId = chainData.chainId;
166+
activeChainRpc = rpc;
167+
}
168+
169+
try
170+
{
171+
supportedChainData.Add(ThirdwebSession.FetchChainData(BigInteger.Parse(chainData.chainId), rpc));
172+
}
173+
catch (System.Exception e)
174+
{
175+
Debug.LogWarning($"Failed to fetch chain data for {chainData.identifier} ({chainData.chainId}) - {e}, skipping...");
176+
continue;
177+
}
160178
}
161179

162-
// Set up storage and gasless options (if any)
180+
options.supportedChains = supportedChainData.ToArray();
163181

164-
var options = new ThirdwebSDK.Options();
182+
// Set up storage and gasless options (if any)
165183

166184
if (!string.IsNullOrEmpty(storageIpfsGatewayUrl))
167185
{
@@ -200,43 +218,17 @@ private void Awake()
200218
{
201219
factoryAddress = factoryAddress,
202220
gasless = gasless,
203-
bundlerUrl = string.IsNullOrEmpty(bundlerUrl) ? $"https://{currentChain.identifier}.bundler.thirdweb.com" : bundlerUrl,
204-
paymasterUrl = string.IsNullOrEmpty(paymasterUrl) ? $"https://{currentChain.identifier}.bundler.thirdweb.com" : paymasterUrl,
221+
bundlerUrl = string.IsNullOrEmpty(bundlerUrl) ? $"https://{activeChain}.bundler.thirdweb.com" : bundlerUrl,
222+
paymasterUrl = string.IsNullOrEmpty(paymasterUrl) ? $"https://{activeChain}.bundler.thirdweb.com" : paymasterUrl,
205223
entryPointAddress = string.IsNullOrEmpty(entryPointAddress) ? Thirdweb.AccountAbstraction.Constants.DEFAULT_ENTRYPOINT_ADDRESS : entryPointAddress,
206224
};
207225

208226
// Set up Client ID
209227

210228
options.clientId = string.IsNullOrEmpty(clientId) ? null : clientId;
211229

212-
// Pass supported chains with replaced RPCs
213-
214-
var supportedChainData = new List<ThirdwebChainData>();
215-
foreach (var chain in this.supportedChains)
216-
{
217-
string rpc = string.IsNullOrEmpty(chain.rpcOverride)
218-
? (
219-
string.IsNullOrEmpty(clientId)
220-
? $"https://{chain.identifier}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3"
221-
: $"https://{chain.identifier}.rpc.thirdweb.com/{clientId}"
222-
)
223-
: chain.rpcOverride;
224-
225-
if (new System.Uri(rpc).Host.EndsWith(".thirdweb.com"))
226-
rpc = rpc.AppendBundleIdQueryParam();
227-
try
228-
{
229-
supportedChainData.Add(ThirdwebSession.FetchChainData(BigInteger.Parse(chain.chainId), rpc));
230-
}
231-
catch (System.Exception e)
232-
{
233-
Debug.LogWarning($"Failed to fetch chain data for {chain.identifier} ({chain.chainId}) - {e}, skipping...");
234-
continue;
235-
}
236-
}
237-
238-
options.supportedChains = supportedChainData.ToArray();
230+
// Pass active chain rpc and chainId
239231

240-
SDK = new ThirdwebSDK(chainOrRPC, chainId, options);
232+
SDK = new ThirdwebSDK(activeChainRpc, BigInteger.Parse(activeChainId), options);
241233
}
242234
}

Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Numerics;
3+
using Newtonsoft.Json;
34
using UnityEngine;
45

56
namespace Thirdweb
@@ -241,14 +242,10 @@ public struct BiconomyOptions
241242
this.storage = new Storage(options.storage, options.clientId);
242243

243244
string rpc = !chainOrRPC.StartsWith("https://")
244-
? (
245-
string.IsNullOrEmpty(options.clientId)
246-
? $"https://{chainOrRPC}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3"
247-
: $"https://{chainOrRPC}.rpc.thirdweb.com/{options.clientId}"
248-
)
245+
? (string.IsNullOrEmpty(options.clientId) ? $"https://{chainOrRPC}.rpc.thirdweb.com/" : $"https://{chainOrRPC}.rpc.thirdweb.com/{options.clientId}")
249246
: chainOrRPC;
250247

251-
if (new System.Uri(rpc).Host.EndsWith(".thirdweb.com"))
248+
if (new System.Uri(rpc).Host.EndsWith(".thirdweb.com") && !rpc.Contains("bundleId="))
252249
rpc = rpc.AppendBundleIdQueryParam();
253250

254251
if (Utils.IsWebGLBuild())
@@ -266,6 +263,8 @@ public struct BiconomyOptions
266263
Debug.LogWarning(
267264
"No Client ID provided. You will have limited access to thirdweb services for storage, RPC, and Account Abstraction. You can get a Client ID from https://thirdweb.com/create-api-key/"
268265
);
266+
267+
Debug.Log($"Thirdweb SDK Initialized.\nRPC: {rpc}\nChain ID: {chainId}\nOptions: {JsonConvert.SerializeObject(options, Formatting.Indented)}");
269268
}
270269

271270
/// <summary>

Assets/Thirdweb/Core/Scripts/ThirdwebSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public static ThirdwebChainData FetchChainData(BigInteger chainId, string rpcOve
216216
{
217217
chainId = BigInteger.Parse(currentNetwork.chainId).ToHex(false, true) ?? BigInteger.Parse(chainId.ToString()).ToHex(false, true),
218218
blockExplorerUrls = explorerUrls.ToArray(),
219-
chainName = currentNetwork.name ?? ThirdwebManager.Instance.chain,
219+
chainName = currentNetwork.name ?? ThirdwebManager.Instance.activeChain,
220220
iconUrls = new string[] { currentNetwork.icon },
221221
nativeCurrency = new ThirdwebNativeCurrency()
222222
{

Assets/Thirdweb/Editor/ThirdwebManagerEditor.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
[CustomEditor(typeof(ThirdwebManager))]
66
public class ThirdwebManagerEditor : Editor
77
{
8-
private SerializedProperty chainProperty;
8+
private SerializedProperty activeChainProperty;
99
private SerializedProperty supportedChainsProperty;
10+
private SerializedProperty clientIdProperty;
11+
private SerializedProperty initializeOnAwakeProperty;
1012
private SerializedProperty appNameProperty;
1113
private SerializedProperty appDescriptionProperty;
1214
private SerializedProperty appIconsProperty;
@@ -20,7 +22,6 @@ public class ThirdwebManagerEditor : Editor
2022
private SerializedProperty walletConnectProjectIdProperty;
2123
private SerializedProperty paperClientIdProperty;
2224
private SerializedProperty factoryAddressProperty;
23-
private SerializedProperty clientIdProperty;
2425
private SerializedProperty gaslessProperty;
2526
private SerializedProperty bundlerUrlProperty;
2627
private SerializedProperty paymasterUrlProperty;
@@ -42,8 +43,10 @@ public class ThirdwebManagerEditor : Editor
4243

4344
private void OnEnable()
4445
{
45-
chainProperty = serializedObject.FindProperty("chain");
46+
activeChainProperty = serializedObject.FindProperty("activeChain");
4647
supportedChainsProperty = serializedObject.FindProperty("supportedChains");
48+
clientIdProperty = serializedObject.FindProperty("clientId");
49+
initializeOnAwakeProperty = serializedObject.FindProperty("initializeOnAwake");
4750
appNameProperty = serializedObject.FindProperty("appName");
4851
appDescriptionProperty = serializedObject.FindProperty("appDescription");
4952
appIconsProperty = serializedObject.FindProperty("appIcons");
@@ -57,7 +60,6 @@ private void OnEnable()
5760
walletConnectProjectIdProperty = serializedObject.FindProperty("walletConnectProjectId");
5861
paperClientIdProperty = serializedObject.FindProperty("paperClientId");
5962
factoryAddressProperty = serializedObject.FindProperty("factoryAddress");
60-
clientIdProperty = serializedObject.FindProperty("clientId");
6163
gaslessProperty = serializedObject.FindProperty("gasless");
6264
bundlerUrlProperty = serializedObject.FindProperty("bundlerUrl");
6365
paymasterUrlProperty = serializedObject.FindProperty("paymasterUrl");
@@ -156,10 +158,11 @@ public override void OnInspectorGUI()
156158
() =>
157159
{
158160
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
159-
EditorGUILayout.PropertyField(chainProperty);
161+
EditorGUILayout.PropertyField(activeChainProperty);
160162
EditorGUILayout.Space(10);
161163
supportedChainsList.DoLayoutList();
162164
EditorGUILayout.PropertyField(clientIdProperty);
165+
EditorGUILayout.PropertyField(initializeOnAwakeProperty);
163166
EditorGUILayout.EndVertical();
164167
}
165168
);

Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_ConnectWallet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private void Start()
135135

136136
OrGameObject.SetActive(usingEmailWallet && usingNormalWallet);
137137

138-
_currentChainData = ThirdwebManager.Instance.supportedChains.Find(x => x.identifier == ThirdwebManager.Instance.chain);
138+
_currentChainData = ThirdwebManager.Instance.supportedChains.Find(x => x.identifier == ThirdwebManager.Instance.activeChain);
139139
_address = null;
140140
_email = null;
141141
_password = null;

0 commit comments

Comments
 (0)