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: setting up base paymaster and pimlico paymaster #13

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 4 additions & 6 deletions src/gasless-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import init from 'debug';
import { createPublicClient, concat, encodeFunctionData, Hex } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { PimlicoBundlerClient, PimlicoPaymasterClient } from 'permissionless/clients/pimlico';

Check warning on line 7 in src/gasless-provider.ts

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

'PimlicoPaymasterClient' is defined but never used
import { UserOperation } from 'permissionless/types';
import { getSenderAddress, signUserOperationHashWithECDSA } from 'permissionless';
import * as constants from '../src/constants';
import { BasePaymaster } from './paymasters';

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

Expand All @@ -21,7 +22,7 @@
protected readonly _wrappedProvider: EIP1193Provider,
public readonly chain: string,
protected readonly bundlerClient: PimlicoBundlerClient,
protected readonly paymasterClient: PimlicoPaymasterClient,
protected readonly paymasterClient: BasePaymaster,
protected readonly publicClient: ReturnType<typeof createPublicClient>,
protected readonly _initCode: `0x${string}`,
protected readonly senderAddress: `0x${string}`,
Expand All @@ -38,7 +39,7 @@
_wrappedProvider: EIP1193Provider,
chain: string,
bundlerClient: PimlicoBundlerClient,
paymasterClient: PimlicoPaymasterClient,
paymasterClient: BasePaymaster,
publicClient: ReturnType<typeof createPublicClient>,
) {
// NOTE: Bundlers can support many entry points, but currently they only support one, we use this method so if they ever add a new one the entry point will still work
Expand Down Expand Up @@ -136,10 +137,7 @@
};

// REQUEST PIMLICO VERIFYING PAYMASTER SPONSORSHIP
const sponsorUserOperationResult = await this.paymasterClient.sponsorUserOperation({
userOperation,
entryPoint: this._entryPoint,
});
const sponsorUserOperationResult = await this.paymasterClient.sponsorUserOperation(userOperation, this._entryPoint);

const sponsoredUserOperation: UserOperation = {
...userOperation,
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extendProvider } from 'hardhat/config';
import { createPublicClient, http } from 'viem';
import { createPimlicoPaymasterClient, createPimlicoBundlerClient } from 'permissionless/clients/pimlico';
import { createPimlicoBundlerClient } from 'permissionless/clients/pimlico';
import { createPaymasterClient } from './paymaster';

import 'dotenv/config';

Expand All @@ -10,6 +11,7 @@ const log = init('hardhat:plugin:gasless');

import './type-extensions';
import { GaslessProvider } from './gasless-provider';
import { PaymasterType } from './types';

// NOTE: Network name has to match how pimlico names the network in their API calls
extendProvider(async (provider, config, networkName) => {
Expand Down Expand Up @@ -42,9 +44,11 @@ extendProvider(async (provider, config, networkName) => {
transport: http(sponsoredTransaction.bundlerUrl),
});

const paymasterClient = createPimlicoPaymasterClient({
transport: http(sponsoredTransaction.paymasterUrl),
});
// TODO: Change this to use our Paymaster classes based on the paymasterType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean with this comment?

const paymasterClient = createPaymasterClient(
sponsoredTransaction.paymasterType as PaymasterType,
sponsoredTransaction.paymasterUrl,
);

return await GaslessProvider.create(signer, provider, networkName, bundlerClient, paymasterClient, publicClient);
});
13 changes: 13 additions & 0 deletions src/paymaster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PaymasterType } from './types';
import { BasePaymaster } from './paymasters/BasePaymaster';
import * as Pm from './paymasters';

export function createPaymasterClient(paymasterType: PaymasterType, paymasterUrl: string): BasePaymaster {
switch (paymasterType) {
case PaymasterType.Pimlico:
return new Pm.PimlicoPaymaster(paymasterUrl);

default:
throw new Error(`Unknown paymaster type ${paymasterType}`);
}
}
13 changes: 13 additions & 0 deletions src/paymasters/BasePaymaster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PartialUserOperation } from '../types';

export class BasePaymaster {
public endpoint: string;

constructor(endpoint: string) {
this.endpoint = endpoint;
}

public async sponsorUserOperation(userOp: PartialUserOperation, entryPoint: `0x${string}`): Promise<any> {

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

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

'userOp' is defined but never used

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

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

'entryPoint' is defined but never used

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

View workflow job for this annotation

GitHub Actions / Run Linters (18.x)

Unexpected any. Specify a different type
throw new Error('This is a base class and should not be called directly.');
}
}
26 changes: 26 additions & 0 deletions src/paymasters/PimlicoPaymaster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BasePaymaster } from './BasePaymaster';
import { PartialUserOperation } from '../types';
import { http } from 'viem';
import { createPimlicoPaymasterClient } from 'permissionless/clients/pimlico';
import { SponsorUserOperationReturnType } from 'permissionless/actions/pimlico';

export class PimlicoPaymaster extends BasePaymaster {
public paymasterClient: ReturnType<typeof createPimlicoPaymasterClient>;

constructor(endpoint: string) {
super(endpoint);
this.paymasterClient = createPimlicoPaymasterClient({
transport: http(endpoint),
});
}

public async sponsorUserOperation(
userOperation: PartialUserOperation,
entryPoint: `0x${string}`,
): Promise<SponsorUserOperationReturnType> {
return await this.paymasterClient.sponsorUserOperation({
userOperation,
entryPoint,
});
}
}
2 changes: 2 additions & 0 deletions src/paymasters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './BasePaymaster';
export * from './PimlicoPaymaster';
3 changes: 3 additions & 0 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import 'hardhat/types/config';
import 'hardhat/types/runtime';
import { PaymasterTypeLiteral } from './types';

declare module 'hardhat/types/config' {
export interface HttpNetworkUserConfig {
sponsoredTransaction?: {
bundlerUrl: string;
paymasterUrl: string;
paymasterType: PaymasterTypeLiteral;
};
}

export interface HttpNetworkConfig {
sponsoredTransaction?: {
bundlerUrl: string;
paymasterUrl: string;
paymasterType: PaymasterTypeLiteral;
};
}
}
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type PartialUserOperation = {
sender: `0x${string}`;
nonce: bigint;
initCode: `0x${string}`;
callData: `0x${string}`;
maxFeePerGas: bigint;
maxPriorityFeePerGas: bigint;
signature: `0x${string}`;
};

export enum PaymasterType {
Pimlico = 'pimlico',
Biconomy = 'biconomy',
}

export type PaymasterTypeLiteral = keyof {
[K in keyof typeof PaymasterType as string]: K;
};
1 change: 1 addition & 0 deletions test/fixture-projects/hardhat-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const config: HardhatUserConfig = {
sponsoredTransaction: {
bundlerUrl: 'http://localhost:3000',
paymasterUrl: 'http://localhost:3001',
paymasterType: 'pimlico',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test for this?

},
},
},
Expand Down