@@ -9,12 +9,7 @@ export type IsSponsorableResponse = {
9
9
SponsorWebsite : string
10
10
}
11
11
12
- export type IsSponsorableOptions = {
13
- PrivatePolicyUUID ?: string
14
- }
15
-
16
12
export type SendRawTransactionOptions = {
17
- PrivatePolicyUUID ?: string
18
13
UserAgent ?: string
19
14
}
20
15
@@ -58,19 +53,46 @@ export type Bundle = {
58
53
}
59
54
60
55
export class PaymasterClient extends ethers . JsonRpcProvider {
61
- constructor ( url ?: string | FetchRequest , network ?: Networkish , options ?: JsonRpcApiProviderOptions ) {
56
+ private privatePolicyUUID ?: string
57
+
58
+ private constructor (
59
+ url ?: string | FetchRequest ,
60
+ network ?: Networkish ,
61
+ options ?: JsonRpcApiProviderOptions ,
62
+ privatePolicyUUID ?: string
63
+ ) {
62
64
super ( url , network , options )
65
+ this . privatePolicyUUID = privatePolicyUUID
66
+ }
67
+
68
+ // Static method to create a new standard PaymasterClient
69
+ static new (
70
+ url ?: string | FetchRequest ,
71
+ network ?: Networkish ,
72
+ options ?: JsonRpcApiProviderOptions
73
+ ) : PaymasterClient {
74
+ return new PaymasterClient ( url , network , options )
75
+ }
76
+
77
+ // Static method to create a new PaymasterClient with private policy
78
+ static newPrivatePaymaster (
79
+ url : string | FetchRequest ,
80
+ privatePolicyUUID : string ,
81
+ network ?: Networkish ,
82
+ options ?: JsonRpcApiProviderOptions
83
+ ) : PaymasterClient {
84
+ return new PaymasterClient ( url , network , options , privatePolicyUUID )
63
85
}
64
86
65
87
async chainID ( ) : Promise < string > {
66
88
return await this . send ( 'eth_chainId' , [ ] )
67
89
}
68
90
69
- async isSponsorable ( tx : TransactionRequest , opts : IsSponsorableOptions = { } ) : Promise < IsSponsorableResponse > {
70
- if ( opts . PrivatePolicyUUID ) {
71
- const newConnection = this . _getConnection ( ) ;
72
- newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
73
-
91
+ async isSponsorable ( tx : TransactionRequest ) : Promise < IsSponsorableResponse > {
92
+ const policyUUID = this . privatePolicyUUID
93
+ if ( policyUUID ) {
94
+ const newConnection = this . _getConnection ( )
95
+ newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , policyUUID )
74
96
const sponsorProviderWithHeader = new ethers . JsonRpcProvider (
75
97
newConnection ,
76
98
( this as any ) . _network ,
@@ -79,24 +101,24 @@ export class PaymasterClient extends ethers.JsonRpcProvider {
79
101
batchMaxCount : ( this as any ) . batchMaxCount ,
80
102
polling : ( this as any ) . polling
81
103
}
82
- ) ;
83
-
84
- return await sponsorProviderWithHeader . send ( 'pm_isSponsorable' , [ tx ] ) ;
104
+ )
105
+ return await sponsorProviderWithHeader . send ( 'pm_isSponsorable' , [ tx ] )
85
106
}
86
- return await this . send ( 'pm_isSponsorable' , [ tx ] ) ;
107
+ return await this . send ( 'pm_isSponsorable' , [ tx ] )
87
108
}
88
109
89
110
async sendRawTransaction ( signedTx : string , opts : SendRawTransactionOptions = { } ) : Promise < string > {
90
- if ( opts . UserAgent || opts . PrivatePolicyUUID ) {
91
- const newConnection = this . _getConnection ( ) ;
111
+ const policyUUID = this . privatePolicyUUID
112
+ if ( opts . UserAgent || this . privatePolicyUUID ) {
113
+ const newConnection = this . _getConnection ( )
92
114
93
115
if ( opts . UserAgent ) {
94
- newConnection . setHeader ( "User-Agent" , opts . UserAgent ) ;
116
+ newConnection . setHeader ( "User-Agent" , opts . UserAgent )
95
117
}
96
- if ( opts . PrivatePolicyUUID ) {
97
- newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
118
+ if ( policyUUID ) {
119
+ newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , policyUUID )
98
120
}
99
-
121
+
100
122
const sponsorProvider = new ethers . JsonRpcProvider (
101
123
newConnection ,
102
124
( this as any ) . _network ,
@@ -105,13 +127,13 @@ export class PaymasterClient extends ethers.JsonRpcProvider {
105
127
batchMaxCount : ( this as any ) . batchMaxCount ,
106
128
polling : ( this as any ) . polling
107
129
}
108
- ) ;
109
-
110
- if ( opts . PrivatePolicyUUID ) {
111
- return await sponsorProvider . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
130
+ )
131
+
132
+ if ( policyUUID ) {
133
+ return await sponsorProvider . send ( 'eth_sendRawTransaction' , [ signedTx ] )
112
134
}
113
135
}
114
- return await this . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
136
+ return await this . send ( 'eth_sendRawTransaction' , [ signedTx ] )
115
137
}
116
138
117
139
async getGaslessTransactionByHash ( hash : string ) : Promise < GaslessTransaction > {
@@ -129,4 +151,4 @@ export class PaymasterClient extends ethers.JsonRpcProvider {
129
151
async getBundleByUuid ( bundleUuid : string ) : Promise < Bundle > {
130
152
return await this . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
131
153
}
132
- }
154
+ }
0 commit comments