@@ -21,7 +21,7 @@ public ChainData(string identifier, string chainId, string rpcOverride)
2121public 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}
0 commit comments