Skip to content
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: getting gas configuration from the bundler if needed #17

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/gasless-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { UserOperation } from 'permissionless/types';
import { getSenderAddress, signUserOperationHashWithECDSA } from 'permissionless';
import * as constants from '../src/constants';
import { BasePaymaster } from './paymasters';
import { PartialBy } from 'viem/types/utils';
import { SponsorUserOperationReturnType } from 'permissionless/actions/pimlico';

const log = init('hardhat:plugin:gasless');

Expand Down Expand Up @@ -125,7 +127,10 @@ export class GaslessProvider extends ProviderWrapper {
});

// Construct UserOperation
const userOperation = {
const userOperation: PartialBy<
UserOperation,
'callGasLimit' | 'preVerificationGas' | 'verificationGasLimit' | 'paymasterAndData'
> = {
sender: this.senderAddress,
nonce: this._nonce,
initCode: this._nonce === 0n ? this._initCode : '0x',
Expand All @@ -140,9 +145,22 @@ export class GaslessProvider extends ProviderWrapper {
verificationGasLimit: 0n, // dummy value
};

// REQUEST PIMLICO VERIFYING PAYMASTER SPONSORSHIP
const sponsorUserOperationResult = await this.paymasterClient.sponsorUserOperation(userOperation, this._entryPoint);
const sponsoredUserOperation: UserOperation = Object.assign(userOperation, sponsorUserOperationResult);
const paymasterAndData: `0x${string}` | SponsorUserOperationReturnType =
await this.paymasterClient.sponsorUserOperation(userOperation, this._entryPoint);

let sponsoredUserOperation: UserOperation;

if (typeof paymasterAndData === 'string') {
// If our paymaster only returns its paymasterAndData and not the gas parameters, we need to estimate them ourselves
const gasConfig = await this.bundlerClient.estimateUserOperationGas({
userOperation: Object.assign(userOperation, { paymasterAndData: paymasterAndData }),
entryPoint: this._entryPoint,
});

sponsoredUserOperation = Object.assign(userOperation, gasConfig, { paymasterAndData: paymasterAndData });
} else {
sponsoredUserOperation = Object.assign(userOperation, paymasterAndData);
}

// SIGN THE USER OPERATION
const signature = await signUserOperationHashWithECDSA({
Expand Down
9 changes: 7 additions & 2 deletions src/paymasters/BasePaymaster.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SponsorUserOperationReturnType } from 'permissionless/actions/pimlico';
import { PartialUserOperation } from '../types';

export class BasePaymaster {
Expand All @@ -7,8 +8,12 @@
this.endpoint = endpoint;
}

// eslint-disable-next-line
public async sponsorUserOperation(userOp: PartialUserOperation, entryPoint: `0x${string}`): Promise<any> {
// eslint-disable
public async sponsorUserOperation(
userOp: PartialUserOperation,

Check warning on line 13 in src/paymasters/BasePaymaster.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

'userOp' is defined but never used
entryPoint: `0x${string}`,

Check warning on line 14 in src/paymasters/BasePaymaster.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

'entryPoint' is defined but never used
): Promise<`0x${string}` | SponsorUserOperationReturnType> {
throw new Error('This is a base class and should not be called directly.');
}
// eslint-enable
}
2 changes: 1 addition & 1 deletion src/paymasters/PimlicoPaymaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class PimlicoPaymaster extends BasePaymaster {
public async sponsorUserOperation(
userOperation: PartialUserOperation,
entryPoint: `0x${string}`,
): Promise<SponsorUserOperationReturnType> {
): Promise<`0x${string}` | SponsorUserOperationReturnType> {
return await this.paymasterClient.sponsorUserOperation({
userOperation,
entryPoint,
Expand Down
2 changes: 1 addition & 1 deletion src/paymasters/StackUpPaymaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class StackUpPaymaster extends BasePaymaster {
public async sponsorUserOperation(
userOperation: PartialUserOperation,
entryPoint: `0x${string}`,
): Promise<SponsorUserOperationReturnType> {
): Promise<`0x${string}` | SponsorUserOperationReturnType> {
return await this.paymasterClient.sponsorUserOperation({
userOperation,
entryPoint,
Expand Down