Skip to content

Commit b4f557d

Browse files
committed
fix: make sponsor url as optional
1 parent d922b2b commit b4f557d

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/paymasterclient.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,42 @@ export type Bundle = {
5858
}
5959

6060
export class PaymasterClient {
61-
private sponsorClient: ethers.JsonRpcProvider
6261
private userClient: ethers.JsonRpcProvider
63-
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+
*/
6476
constructor(
6577
userUrl: string | FetchRequest,
66-
sponsorUrl: string | FetchRequest,
78+
sponsorUrl?: string | FetchRequest,
6779
network?: Networkish,
6880
options?: JsonRpcApiProviderOptions
6981
) {
7082
this.userClient = new ethers.JsonRpcProvider(userUrl, network, options)
71-
this.sponsorClient = new ethers.JsonRpcProvider(sponsorUrl, network, options)
83+
if (sponsorUrl) {
84+
this.sponsorClient = new ethers.JsonRpcProvider(sponsorUrl, network, options)
85+
}
7286
}
7387

7488
async chainID(): Promise<string> {
7589
return await this.userClient.send('eth_chainId', [])
7690
}
7791

7892
async isSponsorable(tx: TransactionRequest, opts: IsSponsorableOptions = {}): Promise<IsSponsorableResponse> {
79-
if (opts.PrivatePolicyUUID) {
80-
// Create a new provider with the updated header
93+
if (this.sponsorClient && opts.PrivatePolicyUUID) {
8194
const newConnection = this.sponsorClient._getConnection();
8295
newConnection.setHeader("X-MegaFuel-Policy-Uuid", opts.PrivatePolicyUUID);
8396

84-
// Create a new provider with the modified connection
8597
const sponsorProviderWithHeader = new ethers.JsonRpcProvider(
8698
newConnection,
8799
(this.sponsorClient as any)._network,
@@ -98,10 +110,7 @@ export class PaymasterClient {
98110
}
99111

100112
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
113+
if (this.sponsorClient && (opts.UserAgent || opts.PrivatePolicyUUID)) {
105114
const newConnection = this.sponsorClient._getConnection();
106115

107116
if (opts.UserAgent) {
@@ -111,8 +120,7 @@ export class PaymasterClient {
111120
newConnection.setHeader("X-MegaFuel-Policy-Uuid", opts.PrivatePolicyUUID);
112121
}
113122

114-
// Create a new provider with the modified connection
115-
sponsorProvider = new ethers.JsonRpcProvider(
123+
const sponsorProvider = new ethers.JsonRpcProvider(
116124
newConnection,
117125
(this.sponsorClient as any)._network,
118126
{
@@ -121,10 +129,10 @@ export class PaymasterClient {
121129
polling: (this.sponsorClient as any).polling
122130
}
123131
);
124-
}
125132

126-
if (opts.PrivatePolicyUUID) {
127-
return await sponsorProvider.send('eth_sendRawTransaction', [signedTx]);
133+
if (opts.PrivatePolicyUUID) {
134+
return await sponsorProvider.send('eth_sendRawTransaction', [signedTx]);
135+
}
128136
}
129137
return await this.userClient.send('eth_sendRawTransaction', [signedTx]);
130138
}
@@ -145,7 +153,7 @@ export class PaymasterClient {
145153
return await this.userClient.send('pm_getBundleByUuid', [bundleUuid])
146154
}
147155

148-
getSponsorProvider(): ethers.JsonRpcProvider {
156+
getSponsorProvider(): ethers.JsonRpcProvider | undefined {
149157
return this.sponsorClient
150158
}
151159

0 commit comments

Comments
 (0)