Skip to content

Commit

Permalink
feat: add Paymaster client
Browse files Browse the repository at this point in the history
  • Loading branch information
roushou committed Jun 21, 2024
1 parent 72ee2da commit 447e575
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/paymaster/src/bundler/estimate-user-operation-gas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

export type EstimateUserOperationGasParameters = Extract<
RpcRequestConfig,
{ method: "eth_estimateUserOperationGas" }
>["parameters"];

export type EstimateUserOperationGasResponse = Extract<
RpcRequestConfig,
{ method: "eth_estimateUserOperationGas" }
>["response"]["result"];

/**
* @returns The gas estimates of the operation
*
* @param rpc - RPC Client {@link RpcClient}
* @param parameters - User operation {@link EstimateUserOperationGasParameters}
*
* @example
*
* TODO
*
*/
export async function estimateUserOperationGas(
rpc: RpcClient,
params: EstimateUserOperationGasParameters,
): Promise<EstimateUserOperationGasResponse> {
const response = await rpc.request({
method: "eth_estimateUserOperationGas",
parameters: params,
});
return response.result;
}
30 changes: 30 additions & 0 deletions packages/paymaster/src/bundler/get-supported-entrypoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

export type GetSupportedEntrypointsResponse = Extract<
RpcRequestConfig,
{ method: "eth_supportedEntryPoints" }
>["response"]["result"];

/**
* @returns The list of supported entrypoints
*
* @param rpc - RPC Client {@link RpcClient}
*
* @example
*
* const rpcClient = createRpcClient({
* apiKey: API_KEY,
* rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base",
* });
*
* await getSupportedEntrypoints(rpcClient);
*
*/
export async function getSupportedEntrypoints(
rpc: RpcClient,
): Promise<GetSupportedEntrypointsResponse> {
const response = await rpc.request({
method: "eth_supportedEntryPoints",
});
return response.result;
}
41 changes: 41 additions & 0 deletions packages/paymaster/src/bundler/get-user-operation-receipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

export type GetUserOperationReceiptParameters = Extract<
RpcRequestConfig,
{ method: "eth_getUserOperationReceipt" }
>["parameters"];

export type GetUserOperationReceiptResponse = Extract<
RpcRequestConfig,
{ method: "eth_getUserOperationReceipt" }
>["response"]["result"];

/**
* @returns The user operation receipt
*
* @param rpc - RPC Client {@link RpcClient}
* @param parameters - Operation hash {@link GetUserOperationReceiptParameters}
*
* @example
*
* const rpcClient = createRpcClient({
* apiKey: API_KEY,
* rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base",
* });
*
* await getUserOperationReceipt(
* rpcClient,
* ["0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122"],
* );
*
*/
export async function getUserOperationReceipt(
rpc: RpcClient,
params: GetUserOperationReceiptParameters,
): Promise<GetUserOperationReceiptResponse> {
const response = await rpc.request({
method: "eth_getUserOperationReceipt",
parameters: params,
});
return response.result;
}
41 changes: 41 additions & 0 deletions packages/paymaster/src/bundler/get-user-operations-by-hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

export type GetUserOperationByHashParameters = Extract<
RpcRequestConfig,
{ method: "eth_getUserOperationByHash" }
>["parameters"];

export type GetUserOperationByHashResponse = Extract<
RpcRequestConfig,
{ method: "eth_getUserOperationByHash" }
>["response"]["result"];

/**
* @returns The user operation
*
* @param rpc - RPC Client {@link RpcClient}
* @param parameters - Operation hash {@link GetUserOperationByHashParameters}
*
* @example
*
* const rpcClient = createRpcClient({
* apiKey: API_KEY,
* rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base",
* });
*
* await getUserOperationByhash(
* rpcClient,
* ["0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122"],
* );
*
*/
export async function getUserOperationByHash(
rpc: RpcClient,
params: GetUserOperationByHashParameters,
): Promise<GetUserOperationByHashResponse> {
const response = await rpc.request({
method: "eth_getUserOperationByHash",
parameters: params,
});
return response.result;
}
31 changes: 31 additions & 0 deletions packages/paymaster/src/bundler/send-user-operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

export type SendUserOperationParameters = Extract<
RpcRequestConfig,
{ method: "eth_sendUserOperation" }
>["parameters"];

export type SendUserOperationResponse = Extract<
RpcRequestConfig,
{ method: "eth_sendUserOperation" }
>["response"]["result"];

/**
* @param rpc - RPC Client {@link RpcClient}
* @param parameters - Operation {@link SendUserOperationParameters}
*
* @example
*
* TODO
*
*/
export async function sendUserOperation(
rpc: RpcClient,
params: SendUserOperationParameters,
): Promise<SendUserOperationResponse> {
const response = await rpc.request({
method: "eth_sendUserOperation",
parameters: params,
});
return response.result;
}
150 changes: 150 additions & 0 deletions packages/paymaster/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import {
type EstimateUserOperationGasParameters,
type EstimateUserOperationGasResponse,
estimateUserOperationGas,
} from "./bundler/estimate-user-operation-gas";
import {
type GetSupportedEntrypointsResponse,
getSupportedEntrypoints,
} from "./bundler/get-supported-entrypoints";
import {
type GetUserOperationReceiptParameters,
type GetUserOperationReceiptResponse,
getUserOperationReceipt,
} from "./bundler/get-user-operation-receipt";
import {
type GetUserOperationByHashParameters,
type GetUserOperationByHashResponse,
getUserOperationByHash,
} from "./bundler/get-user-operations-by-hash";
import {
type SendUserOperationParameters,
type SendUserOperationResponse,
sendUserOperation,
} from "./bundler/send-user-operation";
import { createRpcClient } from "./rpc";
import {
type SponsorUserOperationParameters,
type SponsorUserOperationResponse,
sponsorUserOperation,
} from "./sponsor/sponser-user-operation";

export type Client = {
/**
* @example
*
* TODO
*
*/
estimateUserOperationGas: (
parameters: EstimateUserOperationGasParameters,
) => Promise<EstimateUserOperationGasResponse>;
/**
* @example
*
* client.getUserOperationByHash([
* "0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122",
* ]);
*
*/
/**
* @example
*
* client.getSupportedEntrypoints();
*
*/
getSupportedEntrypoints: () => Promise<GetSupportedEntrypointsResponse>;
/**
* @example
*
* client.getUserOperationByHash([
* "0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122",
* ]);
*
*/
getUserOperationByHash: (
parameters: GetUserOperationByHashParameters,
) => Promise<GetUserOperationByHashResponse>;
/**
* @example
*
* client.getUserOperationReceipt([
* "0x77c0b560eb0b042902abc5613f768d2a6b2d67481247e9663bf4d68dec0ca122",
* ]);
*
*/
getUserOperationReceipt: (
parameters: GetUserOperationReceiptParameters,
) => Promise<GetUserOperationReceiptResponse>;
/**
* @example
*
* TODO
*
*/
sendUserOperation: (
parameters: SendUserOperationParameters,
) => Promise<SendUserOperationResponse>;
/**
* @example
*
* TODO
*
*/
sponsorUserOperation: (
parameters: SponsorUserOperationParameters,
) => Promise<SponsorUserOperationResponse>;
};

export type ClientConfig = {
apiKey: string;
/**
* Coinbase platform RPC endpoint.
* Defaults to `https://api.developer.coinbase.com/rpc/v1/base`
*/
rpcUrl?: string;
};

/**
* @returns The Client
*
* @param apiKey - Your API key
* @param rpcUrl - Your RPC url. Defaults to `https://api.developer.coinbase.com/rpc/v1/base`
*
* @example
*
* const client = createClient({
* apiKey: API_KEY,
* rpcUrl: "https://api.developer.coinbase.com/rpc/v1/base",
* });
*
*/
export function createClient(config: ClientConfig): Client {
const rpcClient = createRpcClient({
apiKey: config.apiKey,
url: config.rpcUrl,
});

const client: Client = {
estimateUserOperationGas: async (parameters) => {
return await estimateUserOperationGas(rpcClient, parameters);
},
getSupportedEntrypoints: async () => {
return await getSupportedEntrypoints(rpcClient);
},
getUserOperationByHash: async (parameters) => {
return await getUserOperationByHash(rpcClient, parameters);
},
getUserOperationReceipt: async (parameters) => {
return await getUserOperationReceipt(rpcClient, parameters);
},
sendUserOperation: async (parameters) => {
return await sendUserOperation(rpcClient, parameters);
},
sponsorUserOperation: async (parameters) => {
return await sponsorUserOperation(rpcClient, parameters);
},
};

return client;
}
31 changes: 31 additions & 0 deletions packages/paymaster/src/sponsor/sponser-user-operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

export type SponsorUserOperationParameters = Extract<
RpcRequestConfig,
{ method: "pm_sponsorUserOperation" }
>["parameters"];

export type SponsorUserOperationResponse = Extract<
RpcRequestConfig,
{ method: "pm_sponsorUserOperation" }
>["response"]["result"];

/**
* @param rpc - RPC Client {@link RpcClient}
* @param parameters - Operation {@link SponsorUserOperationParameters}
*
* @example
*
* TODO
*
*/
export async function sponsorUserOperation(
rpc: RpcClient,
params: SponsorUserOperationParameters,
): Promise<SponsorUserOperationResponse> {
const response = await rpc.request({
method: "pm_sponsorUserOperation",
parameters: params,
});
return response.result;
}

0 comments on commit 447e575

Please sign in to comment.