-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support private policy #5
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
Changes from 5 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,10 @@ export type IsSponsorableResponse = { | |
SponsorWebsite: string | ||
} | ||
|
||
export type SendRawTransactionOptions = { | ||
UserAgent?: string | ||
} | ||
|
||
export enum GaslessTransactionStatus { New = 0, Pending = 1, Confirmed = 2, Failed = 3, Invalid = 4} | ||
|
||
export type GaslessTransaction = { | ||
|
@@ -49,19 +53,86 @@ export type Bundle = { | |
} | ||
|
||
export class PaymasterClient extends ethers.JsonRpcProvider { | ||
constructor(url?: string | FetchRequest, network?: Networkish, options?: JsonRpcApiProviderOptions) { | ||
private privatePolicyUUID?: string | ||
|
||
private constructor( | ||
url?: string | FetchRequest, | ||
network?: Networkish, | ||
options?: JsonRpcApiProviderOptions, | ||
privatePolicyUUID?: string | ||
) { | ||
super(url, network, options) | ||
this.privatePolicyUUID = privatePolicyUUID | ||
} | ||
|
||
// Static method to create a new standard PaymasterClient | ||
static new( | ||
url?: string | FetchRequest, | ||
network?: Networkish, | ||
options?: JsonRpcApiProviderOptions | ||
): PaymasterClient { | ||
return new PaymasterClient(url, network, options) | ||
} | ||
|
||
// Static method to create a new PaymasterClient with private policy | ||
static newPrivatePaymaster( | ||
url: string | FetchRequest, | ||
privatePolicyUUID: string, | ||
network?: Networkish, | ||
options?: JsonRpcApiProviderOptions | ||
): PaymasterClient { | ||
return new PaymasterClient(url, network, options, privatePolicyUUID) | ||
} | ||
|
||
async chainID(): Promise<string> { | ||
return await this.send('eth_chainId', []) | ||
} | ||
|
||
async isSponsorable(tx: TransactionRequest): Promise<IsSponsorableResponse> { | ||
const policyUUID = this.privatePolicyUUID | ||
if (policyUUID) { | ||
const newConnection = this._getConnection() | ||
newConnection.setHeader("X-MegaFuel-Policy-Uuid", policyUUID) | ||
const sponsorProviderWithHeader = new ethers.JsonRpcProvider( | ||
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 as any)._network, | ||
{ | ||
staticNetwork: (this as any)._network, | ||
batchMaxCount: (this as any).batchMaxCount, | ||
polling: (this as any).polling | ||
} | ||
) | ||
return await sponsorProviderWithHeader.send('pm_isSponsorable', [tx]) | ||
} | ||
return await this.send('pm_isSponsorable', [tx]) | ||
} | ||
|
||
async sendRawTransaction(signedTx: string): Promise<string> { | ||
async sendRawTransaction(signedTx: string, opts: SendRawTransactionOptions = {}): Promise<string> { | ||
const policyUUID = this.privatePolicyUUID | ||
if (opts.UserAgent || this.privatePolicyUUID) { | ||
const newConnection = this._getConnection() | ||
|
||
if (opts.UserAgent) { | ||
newConnection.setHeader("User-Agent", opts.UserAgent) | ||
} | ||
if (policyUUID) { | ||
newConnection.setHeader("X-MegaFuel-Policy-Uuid", policyUUID) | ||
} | ||
|
||
const sponsorProvider = new ethers.JsonRpcProvider( | ||
unclezoro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newConnection, | ||
(this as any)._network, | ||
{ | ||
staticNetwork: (this as any)._network, | ||
batchMaxCount: (this as any).batchMaxCount, | ||
polling: (this as any).polling | ||
} | ||
) | ||
|
||
if (policyUUID) { | ||
return await sponsorProvider.send('eth_sendRawTransaction', [signedTx]) | ||
} | ||
} | ||
return await this.send('eth_sendRawTransaction', [signedTx]) | ||
} | ||
|
||
|
@@ -80,4 +151,4 @@ export class PaymasterClient extends ethers.JsonRpcProvider { | |
async getBundleByUuid(bundleUuid: string): Promise<Bundle> { | ||
return await this.send('pm_getBundleByUuid', [bundleUuid]) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.