@@ -9,6 +9,14 @@ export type IsSponsorableResponse = {
9
9
SponsorWebsite : string
10
10
}
11
11
12
+ export type IsSponsorableOptions = {
13
+ PrivatePolicyUUID ?: string
14
+ }
15
+
16
+ export type SendRawTransactionOptions = {
17
+ PrivatePolicyUUID ?: string
18
+ }
19
+
12
20
export enum GaslessTransactionStatus { New = 0 , Pending = 1 , Confirmed = 2 , Failed = 3 , Invalid = 4 }
13
21
14
22
export type GaslessTransaction = {
@@ -48,36 +56,61 @@ export type Bundle = {
48
56
readonly ChainID : number
49
57
}
50
58
51
- export class PaymasterClient extends ethers . JsonRpcProvider {
52
- constructor ( url ?: string | FetchRequest , network ?: Networkish , options ?: JsonRpcApiProviderOptions ) {
53
- super ( url , network , options )
59
+ export class PaymasterClient {
60
+ private sponsorClient : ethers . JsonRpcProvider
61
+ private userClient : ethers . JsonRpcProvider
62
+
63
+ constructor (
64
+ userUrl : string | FetchRequest ,
65
+ sponsorUrl : string | FetchRequest ,
66
+ network ?: Networkish ,
67
+ options ?: JsonRpcApiProviderOptions
68
+ ) {
69
+ this . userClient = new ethers . JsonRpcProvider ( userUrl , network , options )
70
+ this . sponsorClient = new ethers . JsonRpcProvider ( sponsorUrl , network , options )
54
71
}
55
72
56
73
async chainID ( ) : Promise < string > {
57
- return await this . send ( 'eth_chainId' , [ ] )
74
+ return await this . userClient . send ( 'eth_chainId' , [ ] )
58
75
}
59
76
60
- async isSponsorable ( tx : TransactionRequest ) : Promise < IsSponsorableResponse > {
61
- return await this . send ( 'pm_isSponsorable' , [ tx ] )
77
+ async isSponsorable ( tx : TransactionRequest , opts : IsSponsorableOptions = { } ) : Promise < IsSponsorableResponse > {
78
+ if ( opts . PrivatePolicyUUID ) {
79
+ this . sponsorClient . _getConnection ( ) . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID )
80
+ return await this . sponsorClient . send ( 'pm_isSponsorable' , [ tx ] )
81
+ }
82
+ return await this . userClient . send ( 'pm_isSponsorable' , [ tx ] )
62
83
}
63
84
64
- async sendRawTransaction ( signedTx : string ) : Promise < string > {
65
- return await this . send ( 'eth_sendRawTransaction' , [ signedTx ] )
85
+ async sendRawTransaction ( signedTx : string , opts : SendRawTransactionOptions = { } ) : Promise < string > {
86
+ if ( opts . PrivatePolicyUUID ) {
87
+ this . sponsorClient . _getConnection ( ) . setHeader ( "X-MegaFuel-Policy-Uuid" , opts . PrivatePolicyUUID )
88
+ return await this . sponsorClient . send ( 'eth_sendRawTransaction' , [ signedTx ] )
89
+ }
90
+ return await this . userClient . send ( 'eth_sendRawTransaction' , [ signedTx ] )
66
91
}
67
92
68
93
async getGaslessTransactionByHash ( hash : string ) : Promise < GaslessTransaction > {
69
- return await this . send ( 'eth_getGaslessTransactionByHash' , [ hash ] )
94
+ return await this . userClient . send ( 'eth_getGaslessTransactionByHash' , [ hash ] )
70
95
}
71
96
72
97
async getSponsorTxByTxHash ( hash : string ) : Promise < SponsorTx > {
73
- return await this . send ( 'pm_getSponsorTxByTxHash' , [ hash ] )
98
+ return await this . userClient . send ( 'pm_getSponsorTxByTxHash' , [ hash ] )
74
99
}
75
100
76
101
async getSponsorTxByBundleUuid ( bundleUuid : string ) : Promise < SponsorTx > {
77
- return await this . send ( 'pm_getSponsorTxByBundleUuid' , [ bundleUuid ] )
102
+ return await this . userClient . send ( 'pm_getSponsorTxByBundleUuid' , [ bundleUuid ] )
78
103
}
79
104
80
105
async getBundleByUuid ( bundleUuid : string ) : Promise < Bundle > {
81
- return await this . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
106
+ return await this . userClient . send ( 'pm_getBundleByUuid' , [ bundleUuid ] )
107
+ }
108
+
109
+ getSponsorProvider ( ) : ethers . JsonRpcProvider {
110
+ return this . sponsorClient
111
+ }
112
+
113
+ getUserProvider ( ) : ethers . JsonRpcProvider {
114
+ return this . userClient
82
115
}
83
116
}
0 commit comments