@@ -21,7 +21,7 @@ public ChainData(string identifier, string chainId, string rpcOverride)
21
21
public class ThirdwebManager : MonoBehaviour
22
22
{
23
23
[ Tooltip ( "The chain to initialize the SDK with" ) ]
24
- public string chain = "goerli" ;
24
+ public string activeChain = "goerli" ;
25
25
26
26
[ Tooltip ( "Support any chain by adding it to this list from the inspector" ) ]
27
27
public List < ChainData > supportedChains =
@@ -46,6 +46,9 @@ public class ThirdwebManager : MonoBehaviour
46
46
[ Tooltip ( "Thirdweb Client ID (https://thirdweb.com/create-api-key/). Used for default thirdweb services such as Storage and Account Abstraction." ) ]
47
47
public string clientId ;
48
48
49
+ [ Tooltip ( "Whether the SDK should initialize on awake or not" ) ]
50
+ public bool initializeOnAwake = true ;
51
+
49
52
[ Tooltip ( "The name of your app" ) ]
50
53
public string appName = null ;
51
54
@@ -112,8 +115,6 @@ public class ThirdwebManager : MonoBehaviour
112
115
113
116
private void Awake ( )
114
117
{
115
- // Single persistent instance at all times.
116
-
117
118
if ( Instance == null )
118
119
{
119
120
Instance = this ;
@@ -126,42 +127,59 @@ private void Awake()
126
127
return ;
127
128
}
128
129
129
- // Inspector chain data dictionary.
130
+ if ( initializeOnAwake )
131
+ Initialize ( activeChain ) ;
132
+ }
130
133
131
- ChainData currentChain = supportedChains . Find ( x => x . identifier == chain ) ;
134
+ public void Initialize ( string chainIdentifier )
135
+ {
136
+ // Pass supported chains with replaced RPCs
132
137
133
- // Chain ID must be provided on native platforms.
138
+ var options = new ThirdwebSDK . Options ( ) ;
134
139
135
- BigInteger chainId = - 1 ;
140
+ activeChain = chainIdentifier ;
141
+ string activeChainId = null ;
142
+ string activeChainRpc = null ;
136
143
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.") ;
139
149
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 !") ;
142
152
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!") ;
144
155
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 ;
146
159
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
+ }
160
178
}
161
179
162
- // Set up storage and gasless options (if any)
180
+ options . supportedChains = supportedChainData . ToArray ( ) ;
163
181
164
- var options = new ThirdwebSDK . Options ( ) ;
182
+ // Set up storage and gasless options (if any)
165
183
166
184
if ( ! string . IsNullOrEmpty ( storageIpfsGatewayUrl ) )
167
185
{
@@ -200,43 +218,17 @@ private void Awake()
200
218
{
201
219
factoryAddress = factoryAddress ,
202
220
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 ,
205
223
entryPointAddress = string . IsNullOrEmpty ( entryPointAddress ) ? Thirdweb . AccountAbstraction . Constants . DEFAULT_ENTRYPOINT_ADDRESS : entryPointAddress ,
206
224
} ;
207
225
208
226
// Set up Client ID
209
227
210
228
options . clientId = string . IsNullOrEmpty ( clientId ) ? null : clientId ;
211
229
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
239
231
240
- SDK = new ThirdwebSDK ( chainOrRPC , chainId , options ) ;
232
+ SDK = new ThirdwebSDK ( activeChainRpc , BigInteger . Parse ( activeChainId ) , options ) ;
241
233
}
242
234
}
0 commit comments