Skip to content

Commit

Permalink
feat: support user agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryTong65 committed Oct 15, 2024
1 parent a78b57f commit b6d136a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
55 changes: 47 additions & 8 deletions src/paymasterclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type IsSponsorableOptions = {

export type SendRawTransactionOptions = {
PrivatePolicyUUID?: string
UserAgent?: string
}

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

async isSponsorable(tx: TransactionRequest, opts: IsSponsorableOptions = {} ): Promise<IsSponsorableResponse> {
async isSponsorable(tx: TransactionRequest, opts: IsSponsorableOptions = {}): Promise<IsSponsorableResponse> {
if (opts.PrivatePolicyUUID) {
this.sponsorClient._getConnection().setHeader("X-MegaFuel-Policy-Uuid", opts.PrivatePolicyUUID)
return await this.sponsorClient.send('pm_isSponsorable', [tx])
// Create a new provider with the updated header
const newConnection = this.sponsorClient._getConnection();
newConnection.setHeader("X-MegaFuel-Policy-Uuid", opts.PrivatePolicyUUID);

// Create a new provider with the modified connection
const sponsorProviderWithHeader = new ethers.JsonRpcProvider(
newConnection,
(this.sponsorClient as any)._network,
{
staticNetwork: (this.sponsorClient as any)._network,
batchMaxCount: (this.sponsorClient as any).batchMaxCount,
polling: (this.sponsorClient as any).polling
}
);

return await sponsorProviderWithHeader.send('pm_isSponsorable', [tx]);
}
return await this.userClient.send('pm_isSponsorable', [tx])
return await this.userClient.send('pm_isSponsorable', [tx]);
}

async sendRawTransaction(signedTx: string, opts: SendRawTransactionOptions= {}): Promise<string> {
async sendRawTransaction(signedTx: string, opts: SendRawTransactionOptions = {}): Promise<string> {
let sponsorProvider = this.sponsorClient;

if (opts.UserAgent || opts.PrivatePolicyUUID) {
// Create a new provider with the updated headers
const newConnection = this.sponsorClient._getConnection();

if (opts.UserAgent) {
newConnection.setHeader("User-Agent", opts.UserAgent);
}
if (opts.PrivatePolicyUUID) {
newConnection.setHeader("X-MegaFuel-Policy-Uuid", opts.PrivatePolicyUUID);
}

// Create a new provider with the modified connection
sponsorProvider = new ethers.JsonRpcProvider(
newConnection,
(this.sponsorClient as any)._network,
{
staticNetwork: (this.sponsorClient as any)._network,
batchMaxCount: (this.sponsorClient as any).batchMaxCount,
polling: (this.sponsorClient as any).polling
}
);
}

if (opts.PrivatePolicyUUID) {
this.sponsorClient._getConnection().setHeader("X-MegaFuel-Policy-Uuid", opts.PrivatePolicyUUID)
return await this.sponsorClient.send('eth_sendRawTransaction', [signedTx])
return await sponsorProvider.send('eth_sendRawTransaction', [signedTx]);
}
return await this.userClient.send('eth_sendRawTransaction', [signedTx])
return await this.userClient.send('eth_sendRawTransaction', [signedTx]);
}

async getGaslessTransactionByHash(hash: string): Promise<GaslessTransaction> {
Expand Down
3 changes: 2 additions & 1 deletion tests/paymaster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ describe('paymasterQuery', () => {
expect(res.Sponsorable).toEqual(true)

const txOpt: SendRawTransactionOptions = {
PrivatePolicyUUID: PRIVATE_POLICY_UUID
PrivatePolicyUUID: PRIVATE_POLICY_UUID,
UserAgent: "TEST USER AGENT"
};

const signedTx = await wallet.signTransaction(safeTransaction)
Expand Down

0 comments on commit b6d136a

Please sign in to comment.