@@ -58,30 +58,42 @@ export type Bundle = {
58
58
}
59
59
60
60
export class PaymasterClient {
61
- private sponsorClient : ethers . JsonRpcProvider
62
61
private userClient : ethers . JsonRpcProvider
63
-
62
+ private sponsorClient ?: ethers . JsonRpcProvider
63
+
64
+ /**
65
+ * Creates a new PaymasterClient with an optional sponsorUrl.
66
+ * If sponsorUrl is provided, it enables the use of private policies.
67
+ * The sponsorUrl is typically in the format: "https://open-platform-ap.nodereal.io/xxxx/megafuel-testnet"
68
+ * IsSponsorableOptions.PrivatePolicyUUID and SendRawTransactionOptions.PrivatePolicyUUID
69
+ * can only be used when sponsorUrl is provided.
70
+ *
71
+ * @param userUrl The URL for the user's JsonRpcProvider
72
+ * @param sponsorUrl Optional URL for the sponsor's JsonRpcProvider
73
+ * @param network Optional network information
74
+ * @param options Optional JsonRpcApiProviderOptions
75
+ */
64
76
constructor (
65
77
userUrl : string | FetchRequest ,
66
- sponsorUrl : string | FetchRequest ,
78
+ sponsorUrl ? : string | FetchRequest ,
67
79
network ?: Networkish ,
68
80
options ?: JsonRpcApiProviderOptions
69
81
) {
70
82
this . userClient = new ethers . JsonRpcProvider ( userUrl , network , options )
71
- this . sponsorClient = new ethers . JsonRpcProvider ( sponsorUrl , network , options )
83
+ if ( sponsorUrl ) {
84
+ this . sponsorClient = new ethers . JsonRpcProvider ( sponsorUrl , network , options )
85
+ }
72
86
}
73
87
74
88
async chainID ( ) : Promise < string > {
75
89
return await this . userClient . send ( 'eth_chainId' , [ ] )
76
90
}
77
91
78
92
async isSponsorable ( tx : TransactionRequest , opts : IsSponsorableOptions = { } ) : Promise < IsSponsorableResponse > {
79
- if ( opts . PrivatePolicyUUID ) {
80
- // Create a new provider with the updated header
93
+ if ( this . sponsorClient && opts . PrivatePolicyUUID ) {
81
94
const newConnection = this . sponsorClient . _getConnection ( ) ;
82
95
newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
83
96
84
- // Create a new provider with the modified connection
85
97
const sponsorProviderWithHeader = new ethers . JsonRpcProvider (
86
98
newConnection ,
87
99
( this . sponsorClient as any ) . _network ,
@@ -98,10 +110,7 @@ export class PaymasterClient {
98
110
}
99
111
100
112
async sendRawTransaction ( signedTx : string , opts : SendRawTransactionOptions = { } ) : Promise < string > {
101
- let sponsorProvider = this . sponsorClient ;
102
-
103
- if ( opts . UserAgent || opts . PrivatePolicyUUID ) {
104
- // Create a new provider with the updated headers
113
+ if ( this . sponsorClient && ( opts . UserAgent || opts . PrivatePolicyUUID ) ) {
105
114
const newConnection = this . sponsorClient . _getConnection ( ) ;
106
115
107
116
if ( opts . UserAgent ) {
@@ -111,8 +120,7 @@ export class PaymasterClient {
111
120
newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
112
121
}
113
122
114
- // Create a new provider with the modified connection
115
- sponsorProvider = new ethers . JsonRpcProvider (
123
+ const sponsorProvider = new ethers . JsonRpcProvider (
116
124
newConnection ,
117
125
( this . sponsorClient as any ) . _network ,
118
126
{
@@ -121,10 +129,10 @@ export class PaymasterClient {
121
129
polling : ( this . sponsorClient as any ) . polling
122
130
}
123
131
) ;
124
- }
125
132
126
- if ( opts . PrivatePolicyUUID ) {
127
- return await sponsorProvider . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
133
+ if ( opts . PrivatePolicyUUID ) {
134
+ return await sponsorProvider . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
135
+ }
128
136
}
129
137
return await this . userClient . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
130
138
}
@@ -145,7 +153,7 @@ export class PaymasterClient {
145
153
return await this . userClient . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
146
154
}
147
155
148
- getSponsorProvider ( ) : ethers . JsonRpcProvider {
156
+ getSponsorProvider ( ) : ethers . JsonRpcProvider | undefined {
149
157
return this . sponsorClient
150
158
}
151
159
0 commit comments