Skip to content

Commit b6d136a

Browse files
committed
feat: support user agent header
1 parent a78b57f commit b6d136a

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/paymasterclient.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type IsSponsorableOptions = {
1515

1616
export type SendRawTransactionOptions = {
1717
PrivatePolicyUUID?: string
18+
UserAgent?: string
1819
}
1920

2021
export enum GaslessTransactionStatus { New = 0, Pending = 1, Confirmed = 2, Failed = 3, Invalid = 4}
@@ -74,20 +75,58 @@ export class PaymasterClient {
7475
return await this.userClient.send('eth_chainId', [])
7576
}
7677

77-
async isSponsorable(tx: TransactionRequest, opts: IsSponsorableOptions = {} ): Promise<IsSponsorableResponse> {
78+
async isSponsorable(tx: TransactionRequest, opts: IsSponsorableOptions = {}): Promise<IsSponsorableResponse> {
7879
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]);
8196
}
82-
return await this.userClient.send('pm_isSponsorable', [tx])
97+
return await this.userClient.send('pm_isSponsorable', [tx]);
8398
}
8499

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+
86126
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]);
89128
}
90-
return await this.userClient.send('eth_sendRawTransaction', [signedTx])
129+
return await this.userClient.send('eth_sendRawTransaction', [signedTx]);
91130
}
92131

93132
async getGaslessTransactionByHash(hash: string): Promise<GaslessTransaction> {

tests/paymaster.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ describe('paymasterQuery', () => {
133133
expect(res.Sponsorable).toEqual(true)
134134

135135
const txOpt: SendRawTransactionOptions = {
136-
PrivatePolicyUUID: PRIVATE_POLICY_UUID
136+
PrivatePolicyUUID: PRIVATE_POLICY_UUID,
137+
UserAgent: "TEST USER AGENT"
137138
};
138139

139140
const signedTx = await wallet.signTransaction(safeTransaction)

0 commit comments

Comments
 (0)