-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support private policy #5
Changes from 2 commits
a78b57f
d922b2b
b4f557d
babb35e
ca4c90e
54a7267
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,15 @@ export type IsSponsorableResponse = { | |
SponsorWebsite: string | ||
} | ||
|
||
export type IsSponsorableOptions = { | ||
PrivatePolicyUUID?: string | ||
} | ||
|
||
export type SendRawTransactionOptions = { | ||
PrivatePolicyUUID?: string | ||
UserAgent?: string | ||
} | ||
|
||
export enum GaslessTransactionStatus { New = 0, Pending = 1, Confirmed = 2, Failed = 3, Invalid = 4} | ||
|
||
export type GaslessTransaction = { | ||
|
@@ -48,36 +57,99 @@ export type Bundle = { | |
readonly ChainID: number | ||
} | ||
|
||
export class PaymasterClient extends ethers.JsonRpcProvider { | ||
constructor(url?: string | FetchRequest, network?: Networkish, options?: JsonRpcApiProviderOptions) { | ||
super(url, network, options) | ||
export class PaymasterClient { | ||
private sponsorClient: ethers.JsonRpcProvider | ||
private userClient: ethers.JsonRpcProvider | ||
|
||
constructor( | ||
userUrl: string | FetchRequest, | ||
sponsorUrl: string | FetchRequest, | ||
network?: Networkish, | ||
options?: JsonRpcApiProviderOptions | ||
) { | ||
this.userClient = new ethers.JsonRpcProvider(userUrl, network, options) | ||
this.sponsorClient = new ethers.JsonRpcProvider(sponsorUrl, network, options) | ||
} | ||
|
||
async chainID(): Promise<string> { | ||
return await this.send('eth_chainId', []) | ||
return await this.userClient.send('eth_chainId', []) | ||
} | ||
|
||
async isSponsorable(tx: TransactionRequest): Promise<IsSponsorableResponse> { | ||
return await this.send('pm_isSponsorable', [tx]) | ||
async isSponsorable(tx: TransactionRequest, opts: IsSponsorableOptions = {}): Promise<IsSponsorableResponse> { | ||
if (opts.PrivatePolicyUUID) { | ||
// 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( | ||
unclezoro marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if no new header to set here, can we not do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sponsorProviderWithHeader the name sponsor is not proper here. ------> rename to provider if no new header to set here, can we not do new ethers.JsonRpcProvider, but reuse the current one.
|
||
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]); | ||
} | ||
|
||
async sendRawTransaction(signedTx: string): Promise<string> { | ||
return await this.send('eth_sendRawTransaction', [signedTx]) | ||
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) { | ||
return await sponsorProvider.send('eth_sendRawTransaction', [signedTx]); | ||
} | ||
return await this.userClient.send('eth_sendRawTransaction', [signedTx]); | ||
} | ||
|
||
async getGaslessTransactionByHash(hash: string): Promise<GaslessTransaction> { | ||
return await this.send('eth_getGaslessTransactionByHash', [hash]) | ||
return await this.userClient.send('eth_getGaslessTransactionByHash', [hash]) | ||
} | ||
|
||
async getSponsorTxByTxHash(hash: string): Promise<SponsorTx> { | ||
return await this.send('pm_getSponsorTxByTxHash', [hash]) | ||
return await this.userClient.send('pm_getSponsorTxByTxHash', [hash]) | ||
} | ||
|
||
async getSponsorTxByBundleUuid(bundleUuid: string): Promise<SponsorTx> { | ||
return await this.send('pm_getSponsorTxByBundleUuid', [bundleUuid]) | ||
return await this.userClient.send('pm_getSponsorTxByBundleUuid', [bundleUuid]) | ||
} | ||
|
||
async getBundleByUuid(bundleUuid: string): Promise<Bundle> { | ||
return await this.send('pm_getBundleByUuid', [bundleUuid]) | ||
return await this.userClient.send('pm_getBundleByUuid', [bundleUuid]) | ||
} | ||
|
||
getSponsorProvider(): ethers.JsonRpcProvider { | ||
return this.sponsorClient | ||
} | ||
|
||
getUserProvider(): ethers.JsonRpcProvider { | ||
return this.userClient | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if a user:
But now, I have to input two URL. I think we can improve so that user just need input necessary one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the sponsorUrl optional, so that private policy UUID can only be sent if the user provides this URL. Add a comment explaining the purpose of sponsorUrl and Options.