1111using Nethereum . Contracts ;
1212using Nethereum . ABI . FunctionEncoding ;
1313using System ;
14+ using Newtonsoft . Json . Linq ;
1415
1516#pragma warning disable CS0618
1617
@@ -341,9 +342,9 @@ public async Task<string> Send(bool? gasless = null)
341342 await EstimateAndSetGasLimitAsync ( ) ;
342343 if ( Input . Value == null )
343344 Input . Value = new HexBigInteger ( 0 ) ;
344- bool isGaslessSetup = ThirdwebManager . Instance . SDK . Session . Options . gasless . HasValue && ThirdwebManager . Instance . SDK . Session . Options . gasless . Value . openzeppelin . HasValue ;
345+ bool isGaslessSetup = ThirdwebManager . Instance . SDK . Session . Options . gasless . HasValue && ! string . IsNullOrEmpty ( ThirdwebManager . Instance . SDK . Session . Options . gasless ? . engine . relayerUrl ) ;
345346 if ( gasless != null && gasless . Value && ! isGaslessSetup )
346- throw new UnityException ( "Gasless transactions are not enabled. Please enable them in the SDK options." ) ;
347+ throw new UnityException ( "Gasless relayer transactions are not enabled. Please enable them in the SDK options." ) ;
347348 bool sendGaslessly = gasless == null ? isGaslessSetup : gasless . Value ;
348349 if ( sendGaslessly )
349350 return await SendGasless ( ) ;
@@ -484,10 +485,10 @@ private async Task<string> SendGasless()
484485 }
485486 else
486487 {
487- string relayerUrl = ThirdwebManager . Instance . SDK . Session . Options . gasless . Value . openzeppelin ? . relayerUrl ;
488- string forwarderAddress = ThirdwebManager . Instance . SDK . Session . Options . gasless . Value . openzeppelin ? . relayerForwarderAddress ;
489- string forwarderDomain = ThirdwebManager . Instance . SDK . Session . Options . gasless . Value . openzeppelin ? . domainName ;
490- string forwarderVersion = ThirdwebManager . Instance . SDK . Session . Options . gasless . Value . openzeppelin ? . domainVersion ;
488+ string relayerUrl = ThirdwebManager . Instance . SDK . Session . Options . gasless ? . engine . relayerUrl ?? throw new UnityException ( "Relayer URL not set in SDK options." ) ;
489+ string forwarderAddress = ThirdwebManager . Instance . SDK . Session . Options . gasless ? . engine . relayerForwarderAddress ?? "0xD04F98C88cE1054c90022EE34d566B9237a1203C" ;
490+ string forwarderDomain = ThirdwebManager . Instance . SDK . Session . Options . gasless ? . engine . domainName ?? "GSNv2 Forwarder" ;
491+ string forwarderVersion = ThirdwebManager . Instance . SDK . Session . Options . gasless ? . engine . domainVersion ?? "0.0.1" ;
491492
492493 Input . Nonce = (
493494 await TransactionManager . ThirdwebRead < MinimalForwarder . GetNonceFunction , MinimalForwarder . GetNonceOutputDTO > (
@@ -506,6 +507,8 @@ private async Task<string> SendGasless()
506507 Data = Input . Data
507508 } ;
508509
510+ ThirdwebDebug . Log ( $ "Forwarding request: { JsonConvert . SerializeObject ( request ) } ") ;
511+
509512 var signature = await EIP712 . GenerateSignature_MinimalForwarder (
510513 forwarderDomain ,
511514 forwarderVersion ,
@@ -514,7 +517,13 @@ private async Task<string> SendGasless()
514517 request
515518 ) ;
516519
517- var postData = new RelayerRequest ( request , signature , forwarderAddress ) ;
520+ var postData = new RelayerRequest ( )
521+ {
522+ Type = "forward" ,
523+ Request = request ,
524+ Signature = signature ,
525+ ForwarderAddress = forwarderAddress ,
526+ } ;
518527
519528 using UnityWebRequest req = UnityWebRequest . Post ( relayerUrl , "" ) ;
520529 byte [ ] bodyRaw = System . Text . Encoding . UTF8 . GetBytes ( JsonConvert . SerializeObject ( postData ) ) ;
@@ -530,17 +539,32 @@ private async Task<string> SendGasless()
530539 }
531540 else
532541 {
533- var response = JsonConvert . DeserializeObject < RelayerResponse > ( req . downloadHandler . text ) ;
534- if ( response . status != "success" )
535- {
536- throw new UnityException (
537- $ "Forward Request Failed!\n Error: { req . downloadHandler . text } \n Relayer URL: { relayerUrl } \n Relayer Forwarder Address: { forwarderAddress } \n Request: { request } \n Signature: { signature } \n Post Data: { postData } "
538- ) ;
539- }
540- var result = JsonConvert . DeserializeObject < RelayerResult > ( response . result ) ;
541- return result . txHash ;
542+ var queueId = JsonConvert . DeserializeObject < JObject > ( req . downloadHandler . text ) [ "result" ] [ "queueId" ] . ToString ( ) ;
543+ ThirdwebDebug . Log ( $ "Forwarded request to relayer with queue ID: { queueId } ") ;
544+ return await FetchTxHashFromQueueId ( new Uri ( relayerUrl ) . GetLeftPart ( UriPartial . Authority ) , queueId ) ;
545+ }
546+ }
547+ }
548+
549+ private async Task < string > FetchTxHashFromQueueId ( string engineUrl , string queueId )
550+ {
551+ string txHash = null ;
552+ while ( string . IsNullOrEmpty ( txHash ) && Application . isPlaying )
553+ {
554+ using UnityWebRequest req = UnityWebRequest . Get ( $ "{ engineUrl } /transaction/status/{ queueId } ") ;
555+ await new WaitForSeconds ( 1f ) ;
556+ await req . SendWebRequest ( ) ;
557+ if ( req . result != UnityWebRequest . Result . Success )
558+ {
559+ throw new UnityException ( $ "Failed to fetch transaction hash from queue ID { queueId } .\n Error: { req . downloadHandler . text } ") ;
560+ }
561+ else
562+ {
563+ txHash = JsonConvert . DeserializeObject < JObject > ( req . downloadHandler . text ) [ "result" ] [ "transactionHash" ] . ToString ( ) ;
542564 }
543565 }
566+ ThirdwebDebug . Log ( $ "Transaction hash fetched from queue ID { queueId } : { txHash } ") ;
567+ return txHash ;
544568 }
545569
546570 private string GetTxBuilderRoute ( string action )
@@ -550,44 +574,19 @@ private string GetTxBuilderRoute(string action)
550574 }
551575 }
552576
553- [ System . Serializable ]
554- public struct RelayerResponse
555- {
556- [ JsonProperty ( "status" ) ]
557- public string status ;
558-
559- [ JsonProperty ( "result" ) ]
560- public string result ;
561- }
562-
563- [ System . Serializable ]
564- public struct RelayerResult
565- {
566- [ JsonProperty ( "txHash" ) ]
567- public string txHash ;
568- }
569-
570577 [ System . Serializable ]
571578 public struct RelayerRequest
572579 {
580+ [ JsonProperty ( "type" ) ]
581+ public string Type ;
582+
573583 [ JsonProperty ( "request" ) ]
574- public MinimalForwarder . ForwardRequest request ;
584+ public MinimalForwarder . ForwardRequest Request ;
575585
576586 [ JsonProperty ( "signature" ) ]
577- public string signature ;
587+ public string Signature ;
578588
579589 [ JsonProperty ( "forwarderAddress" ) ]
580- public string forwarderAddress ;
581-
582- [ JsonProperty ( "type" ) ]
583- public string type ;
584-
585- public RelayerRequest ( MinimalForwarder . ForwardRequest request , string signature , string forwarderAddress )
586- {
587- this . request = request ;
588- this . signature = signature ;
589- this . forwarderAddress = forwarderAddress ;
590- this . type = "forward" ;
591- }
590+ public string ForwarderAddress ;
592591 }
593592}
0 commit comments