@@ -15,6 +15,7 @@ export type IsSponsorableOptions = {
15
15
16
16
export type SendRawTransactionOptions = {
17
17
PrivatePolicyUUID ?: string
18
+ UserAgent ?: string
18
19
}
19
20
20
21
export enum GaslessTransactionStatus { New = 0 , Pending = 1 , Confirmed = 2 , Failed = 3 , Invalid = 4 }
@@ -74,20 +75,58 @@ export class PaymasterClient {
74
75
return await this . userClient . send ( 'eth_chainId' , [ ] )
75
76
}
76
77
77
- async isSponsorable ( tx : TransactionRequest , opts : IsSponsorableOptions = { } ) : Promise < IsSponsorableResponse > {
78
+ async isSponsorable ( tx : TransactionRequest , opts : IsSponsorableOptions = { } ) : Promise < IsSponsorableResponse > {
78
79
if ( opts . PrivatePolicyUUID ) {
79
- this . sponsorClient . _getConnection ( ) . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID )
80
- return await this . sponsorClient . send ( 'pm_isSponsorable' , [ tx ] )
80
+ // Create a new provider with the updated header
81
+ const newConnection = this . sponsorClient . _getConnection ( ) ;
82
+ newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
83
+
84
+ // Create a new provider with the modified connection
85
+ const sponsorProviderWithHeader = new ethers . JsonRpcProvider (
86
+ newConnection ,
87
+ ( this . sponsorClient as any ) . _network ,
88
+ {
89
+ staticNetwork : ( this . sponsorClient as any ) . _network ,
90
+ batchMaxCount : ( this . sponsorClient as any ) . batchMaxCount ,
91
+ polling : ( this . sponsorClient as any ) . polling
92
+ }
93
+ ) ;
94
+
95
+ return await sponsorProviderWithHeader . send ( 'pm_isSponsorable' , [ tx ] ) ;
81
96
}
82
- return await this . userClient . send ( 'pm_isSponsorable' , [ tx ] )
97
+ return await this . userClient . send ( 'pm_isSponsorable' , [ tx ] ) ;
83
98
}
84
99
85
- async sendRawTransaction ( signedTx : string , opts : SendRawTransactionOptions = { } ) : Promise < string > {
100
+ 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
105
+ const newConnection = this . sponsorClient . _getConnection ( ) ;
106
+
107
+ if ( opts . UserAgent ) {
108
+ newConnection . setHeader ( "User-Agent" , opts . UserAgent ) ;
109
+ }
110
+ if ( opts . PrivatePolicyUUID ) {
111
+ newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID ) ;
112
+ }
113
+
114
+ // Create a new provider with the modified connection
115
+ sponsorProvider = new ethers . JsonRpcProvider (
116
+ newConnection ,
117
+ ( this . sponsorClient as any ) . _network ,
118
+ {
119
+ staticNetwork : ( this . sponsorClient as any ) . _network ,
120
+ batchMaxCount : ( this . sponsorClient as any ) . batchMaxCount ,
121
+ polling : ( this . sponsorClient as any ) . polling
122
+ }
123
+ ) ;
124
+ }
125
+
86
126
if ( opts . PrivatePolicyUUID ) {
87
- this . sponsorClient . _getConnection ( ) . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID )
88
- return await this . sponsorClient . send ( 'eth_sendRawTransaction' , [ signedTx ] )
127
+ return await sponsorProvider . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
89
128
}
90
- return await this . userClient . send ( 'eth_sendRawTransaction' , [ signedTx ] )
129
+ return await this . userClient . send ( 'eth_sendRawTransaction' , [ signedTx ] ) ;
91
130
}
92
131
93
132
async getGaslessTransactionByHash ( hash : string ) : Promise < GaslessTransaction > {
0 commit comments