@@ -9,6 +9,10 @@ export type IsSponsorableResponse = {
9
9
SponsorWebsite : string
10
10
}
11
11
12
+ export type SendRawTransactionOptions = {
13
+ UserAgent ?: string
14
+ }
15
+
12
16
export enum GaslessTransactionStatus { New = 0 , Pending = 1 , Confirmed = 2 , Failed = 3 , Invalid = 4 }
13
17
14
18
export type GaslessTransaction = {
@@ -49,19 +53,84 @@ export type Bundle = {
49
53
}
50
54
51
55
export class PaymasterClient extends ethers . JsonRpcProvider {
52
- 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
+ ) {
53
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 )
54
85
}
55
86
56
87
async chainID ( ) : Promise < string > {
57
88
return await this . send ( 'eth_chainId' , [ ] )
58
89
}
59
90
60
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 )
96
+ const provider = new ethers . JsonRpcProvider (
97
+ newConnection ,
98
+ ( this as any ) . _network ,
99
+ {
100
+ staticNetwork : ( this as any ) . _network ,
101
+ batchMaxCount : ( this as any ) . batchMaxCount ,
102
+ polling : ( this as any ) . polling
103
+ }
104
+ )
105
+ return await provider . send ( 'pm_isSponsorable' , [ tx ] )
106
+ }
61
107
return await this . send ( 'pm_isSponsorable' , [ tx ] )
62
108
}
63
109
64
- async sendRawTransaction ( signedTx : string ) : Promise < string > {
110
+ async sendRawTransaction ( signedTx : string , opts : SendRawTransactionOptions = { } ) : Promise < string > {
111
+ const policyUUID = this . privatePolicyUUID
112
+ if ( opts . UserAgent || this . privatePolicyUUID ) {
113
+ const newConnection = this . _getConnection ( )
114
+
115
+ if ( opts . UserAgent ) {
116
+ newConnection . setHeader ( "User-Agent" , opts . UserAgent )
117
+ }
118
+ if ( policyUUID ) {
119
+ newConnection . setHeader ( "X-MegaFuel-Policy-Uuid" , policyUUID )
120
+ }
121
+
122
+ const provider = new ethers . JsonRpcProvider (
123
+ newConnection ,
124
+ ( this as any ) . _network ,
125
+ {
126
+ staticNetwork : ( this as any ) . _network ,
127
+ batchMaxCount : ( this as any ) . batchMaxCount ,
128
+ polling : ( this as any ) . polling
129
+ }
130
+ )
131
+
132
+ return await provider . send ( 'eth_sendRawTransaction' , [ signedTx ] )
133
+ }
65
134
return await this . send ( 'eth_sendRawTransaction' , [ signedTx ] )
66
135
}
67
136
@@ -80,4 +149,4 @@ export class PaymasterClient extends ethers.JsonRpcProvider {
80
149
async getBundleByUuid ( bundleUuid : string ) : Promise < Bundle > {
81
150
return await this . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
82
151
}
83
- }
152
+ }
0 commit comments