@@ -57,61 +57,38 @@ export type Bundle = {
57
57
readonly ChainID : number
58
58
}
59
59
60
- export class PaymasterClient {
61
- private userClient : ethers . JsonRpcProvider
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
- */
76
- constructor (
77
- userUrl : string | FetchRequest ,
78
- sponsorUrl ?: string | FetchRequest ,
79
- network ?: Networkish ,
80
- options ?: JsonRpcApiProviderOptions
81
- ) {
82
- this . userClient = new ethers . JsonRpcProvider ( userUrl , network , options )
83
- if ( sponsorUrl ) {
84
- this . sponsorClient = new ethers . JsonRpcProvider ( sponsorUrl , network , options )
85
- }
60
+ export class PaymasterClient extends ethers . JsonRpcProvider {
61
+ constructor ( url ?: string | FetchRequest , network ?: Networkish , options ?: JsonRpcApiProviderOptions ) {
62
+ super ( url , network , options )
86
63
}
87
64
88
65
async chainID ( ) : Promise < string > {
89
- return await this . userClient . send ( 'eth_chainId' , [ ] )
66
+ return await this . send ( 'eth_chainId' , [ ] )
90
67
}
91
68
92
69
async isSponsorable ( tx : TransactionRequest , opts : IsSponsorableOptions = { } ) : Promise < IsSponsorableResponse > {
93
- if ( this . sponsorClient && opts . PrivatePolicyUUID ) {
94
- const newConnection = this . sponsorClient . _getConnection ( ) ;
70
+ if ( opts . PrivatePolicyUUID ) {
71
+ const newConnection = this . _getConnection ( ) ;
95
72
newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
96
73
97
74
const sponsorProviderWithHeader = new ethers . JsonRpcProvider (
98
75
newConnection ,
99
- ( this . sponsorClient as any ) . _network ,
76
+ ( this as any ) . _network ,
100
77
{
101
- staticNetwork : ( this . sponsorClient as any ) . _network ,
102
- batchMaxCount : ( this . sponsorClient as any ) . batchMaxCount ,
103
- polling : ( this . sponsorClient as any ) . polling
78
+ staticNetwork : ( this as any ) . _network ,
79
+ batchMaxCount : ( this as any ) . batchMaxCount ,
80
+ polling : ( this as any ) . polling
104
81
}
105
82
) ;
106
83
107
84
return await sponsorProviderWithHeader . send ( 'pm_isSponsorable' , [ tx ] ) ;
108
85
}
109
- return await this . userClient . send ( 'pm_isSponsorable' , [ tx ] ) ;
86
+ return await this . send ( 'pm_isSponsorable' , [ tx ] ) ;
110
87
}
111
88
112
89
async sendRawTransaction ( signedTx : string , opts : SendRawTransactionOptions = { } ) : Promise < string > {
113
- if ( this . sponsorClient && ( opts . UserAgent || opts . PrivatePolicyUUID ) ) {
114
- const newConnection = this . sponsorClient . _getConnection ( ) ;
90
+ if ( opts . UserAgent || opts . PrivatePolicyUUID ) {
91
+ const newConnection = this . _getConnection ( ) ;
115
92
116
93
if ( opts . UserAgent ) {
117
94
newConnection . setHeader ( "User-Agent" , opts . UserAgent ) ;
@@ -122,42 +99,34 @@ export class PaymasterClient {
122
99
123
100
const sponsorProvider = new ethers . JsonRpcProvider (
124
101
newConnection ,
125
- ( this . sponsorClient as any ) . _network ,
102
+ ( this as any ) . _network ,
126
103
{
127
- staticNetwork : ( this . sponsorClient as any ) . _network ,
128
- batchMaxCount : ( this . sponsorClient as any ) . batchMaxCount ,
129
- polling : ( this . sponsorClient as any ) . polling
104
+ staticNetwork : ( this as any ) . _network ,
105
+ batchMaxCount : ( this as any ) . batchMaxCount ,
106
+ polling : ( this as any ) . polling
130
107
}
131
108
) ;
132
109
133
110
if ( opts . PrivatePolicyUUID ) {
134
111
return await sponsorProvider . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
135
112
}
136
113
}
137
- return await this . userClient . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
114
+ return await this . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
138
115
}
139
116
140
117
async getGaslessTransactionByHash ( hash : string ) : Promise < GaslessTransaction > {
141
- return await this . userClient . send ( 'eth_getGaslessTransactionByHash' , [ hash ] )
118
+ return await this . send ( 'eth_getGaslessTransactionByHash' , [ hash ] )
142
119
}
143
120
144
121
async getSponsorTxByTxHash ( hash : string ) : Promise < SponsorTx > {
145
- return await this . userClient . send ( 'pm_getSponsorTxByTxHash' , [ hash ] )
122
+ return await this . send ( 'pm_getSponsorTxByTxHash' , [ hash ] )
146
123
}
147
124
148
125
async getSponsorTxByBundleUuid ( bundleUuid : string ) : Promise < SponsorTx > {
149
- return await this . userClient . send ( 'pm_getSponsorTxByBundleUuid' , [ bundleUuid ] )
126
+ return await this . send ( 'pm_getSponsorTxByBundleUuid' , [ bundleUuid ] )
150
127
}
151
128
152
129
async getBundleByUuid ( bundleUuid : string ) : Promise < Bundle > {
153
- return await this . userClient . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
154
- }
155
-
156
- getSponsorProvider ( ) : ethers . JsonRpcProvider | undefined {
157
- return this . sponsorClient
158
- }
159
-
160
- getUserProvider ( ) : ethers . JsonRpcProvider {
161
- return this . userClient
130
+ return await this . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
162
131
}
163
132
}
0 commit comments