-
Notifications
You must be signed in to change notification settings - Fork 6
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: add support for base paymaster #18
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,83 @@ | ||
import { SponsorUserOperationReturnType } from 'permissionless/actions/pimlico'; | ||
import { PartialUserOperation } from '../types'; | ||
import { Paymaster } from './Paymaster'; | ||
import { PimlicoBundlerClient } from 'permissionless/clients/pimlico'; | ||
|
||
export class BasePaymaster { | ||
public endpoint: string; | ||
|
||
export class BasePaymaster extends Paymaster { | ||
constructor(endpoint: string) { | ||
this.endpoint = endpoint; | ||
super(endpoint); | ||
} | ||
|
||
// eslint-disable | ||
public async sponsorUserOperation( | ||
userOp: PartialUserOperation, | ||
userOperation: PartialUserOperation, | ||
entryPoint: `0x${string}`, | ||
bundlerClient: PimlicoBundlerClient, | ||
): Promise<`0x${string}` | SponsorUserOperationReturnType> { | ||
throw new Error('This is a base class and should not be called directly.'); | ||
const userOp = this.convertBigIntsToString(userOperation); | ||
|
||
const chainIdAsNumber = await bundlerClient.chainId(); | ||
const chainId = '0x' + chainIdAsNumber.toString(16); | ||
|
||
const data = { | ||
id: 1, | ||
jsonrpc: '2.0', | ||
method: 'eth_paymasterAndDataForEstimateGas', | ||
params: [userOp, entryPoint, chainId], | ||
}; | ||
|
||
const response = await fetch(this.endpoint, { | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
|
||
const json = await response.json(); | ||
|
||
const paymasterAndData = json.result; | ||
|
||
const gasConfig = await bundlerClient.estimateUserOperationGas({ | ||
userOperation: Object.assign(userOperation, { paymasterAndData: paymasterAndData }), | ||
entryPoint, | ||
}); | ||
|
||
// Adding gas headroom for safety margin to ensure paymaster signs for enough gas based on estimations | ||
gasConfig.preVerificationGas = gasConfig.preVerificationGas + 2000n; | ||
gasConfig.verificationGasLimit = gasConfig.verificationGasLimit + 4000n; | ||
|
||
const stringifyGasConfig = this.convertBigIntsToString(gasConfig); | ||
|
||
const finalCallData = { | ||
id: 1, | ||
jsonrpc: '2.0', | ||
method: 'eth_paymasterAndDataForUserOperation', | ||
params: [Object.assign(userOp, stringifyGasConfig), entryPoint, chainId], | ||
}; | ||
|
||
const finalResponse = await fetch(this.endpoint, { | ||
method: 'POST', | ||
body: JSON.stringify(finalCallData), | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
|
||
const finalJson = await finalResponse.json(); | ||
|
||
return { | ||
...gasConfig, | ||
paymasterAndData: finalJson.result, | ||
}; | ||
} | ||
|
||
// Helper function to convert bigints to hexadecimal strings, which is what base api expects | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private convertBigIntsToString(obj: any) { | ||
for (const key in obj) { | ||
if (typeof obj[key] === 'bigint') { | ||
// Convert 0n to '0x', and other BigInts to their string representation | ||
// NOTE: base expects gas values to be non-zero, but nonce might be zero so we need to be sure to exclude it | ||
obj[key] = obj[key] === 0n && key !== 'nonce' ? '0x1' : '0x' + obj[key].toString(16); | ||
} | ||
} | ||
|
||
return obj; | ||
} | ||
// eslint-enable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { SponsorUserOperationReturnType } from 'permissionless/actions/pimlico'; | ||
import { PartialUserOperation } from '../types'; | ||
import { PimlicoBundlerClient } from 'permissionless/clients/pimlico'; | ||
|
||
export class Paymaster { | ||
public endpoint: string; | ||
|
||
constructor(endpoint: string) { | ||
this.endpoint = endpoint; | ||
} | ||
|
||
public async sponsorUserOperation( | ||
userOp: PartialUserOperation, | ||
entryPoint: `0x${string}`, | ||
bundlierClient: PimlicoBundlerClient, | ||
): Promise<`0x${string}` | SponsorUserOperationReturnType> { | ||
throw new Error('This is a base class and should not be called directly.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './BasePaymaster'; | ||
export * from './Paymaster'; | ||
export * from './PimlicoPaymaster'; | ||
export * from './StackUpPaymaster'; | ||
export * from './BasePaymaster'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think you can use something like
client.request
from viem library