-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: paymaster first draft
- Loading branch information
Showing
17 changed files
with
182 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
PRIVATE_KEY= | ||
BUNDLER_URL=https://bundler.biconomy.io/api/v2/80001/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44 | ||
BUNDLER_URL= | ||
PAYMASTER_URL= |
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,7 +1,7 @@ | ||
name: Build | ||
on: | ||
pull_request: | ||
|
||
types: [opened, reopened, synchronize, ready_for_review] | ||
jobs: | ||
build: | ||
name: build | ||
|
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: size report | ||
on: | ||
pull_request: | ||
types: [opened, reopened, synchronize, ready_for_review] | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
size-report: | ||
name: size report | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
|
||
steps: | ||
- name: Clone repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install dependencies | ||
uses: ./.github/actions/install-dependencies | ||
|
||
- name: Report bundle size | ||
uses: andresz1/size-limit-action@master | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
package_manager: bun |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { sponsorUserOperation } from "./sponsorUserOperation.js" | ||
|
||
export type PaymasterRpcSchema = [ | ||
{ | ||
Method: "eth_chainId" | ||
Params: [] | ||
ReturnType: bigint | ||
} | ||
] | ||
|
||
export type PaymasterActions = Record< | ||
"sponsorUserOperation", | ||
() => Promise<number> | ||
> | ||
export const paymasterActions = () => (): PaymasterActions => ({ | ||
sponsorUserOperation: () => sponsorUserOperation() | ||
}) |
Empty file.
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,3 @@ | ||
export const sponsorUserOperation = async () => { | ||
return 1 | ||
} |
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,56 @@ | ||
import { | ||
type Account, | ||
type Chain, | ||
type Client, | ||
type PublicClientConfig, | ||
type Transport, | ||
createClient | ||
} from "viem" | ||
import { | ||
type PaymasterActions, | ||
type PaymasterRpcSchema, | ||
paymasterActions | ||
} from "./actions" | ||
|
||
export type PaymasterClient< | ||
TChain extends Chain | undefined = Chain | undefined | ||
> = Client< | ||
Transport, | ||
TChain, | ||
Account | undefined, | ||
PaymasterRpcSchema, | ||
PaymasterActions | ||
> | ||
|
||
/** | ||
* Creates a Paymaster Client with a given [Transport](https://viem.sh/docs/clients/intro.html) configured for a [Chain](https://viem.sh/docs/clients/chains.html). | ||
* | ||
* A Paymaster Client is an interface to "paymaster endpoints" [JSON-RPC API](https://docs..io/reference/verifying-paymaster/endpoints) methods such as sponsoring user operation, etc through Paymaster Actions. | ||
* | ||
* @param config - {@link PublicClientConfig} | ||
* @returns A Paymaster Client. {@link PaymasterClient} | ||
* | ||
* @example | ||
* import { createPublicClient, http } from 'viem' | ||
* import { polygonMumbai } from 'viem/chains' | ||
* | ||
* const PaymasterClient = createPaymasterClient({ | ||
* chain: polygonMumbai, | ||
* transport: http("https://paymaster.biconomy.io/api/v1/80001/YOUR_API_KEY_HERE"), | ||
* }) | ||
*/ | ||
export const createPaymasterClient = < | ||
transport extends Transport = Transport, | ||
chain extends Chain | undefined = undefined | ||
>( | ||
parameters: PublicClientConfig<transport, chain> | ||
): PaymasterClient => { | ||
const { key = "public", name = "Paymaster Client" } = parameters | ||
const client = createClient({ | ||
...parameters, | ||
key, | ||
name, | ||
type: "PaymasterClient" | ||
}) | ||
return client.extend(paymasterActions()) | ||
} |
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,3 @@ | ||
export type { PaymasterRpcSchema, PaymasterActions } from "./actions/index.js" | ||
export { paymasterActions } from "./actions/index.js" | ||
export { createPaymasterClient } from "./createPaymasterClient.js" |
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,18 @@ | ||
/** | ||
* Extracts the chain ID from a paymaster URL. | ||
* @param url - The bundler URL. | ||
* @returns The extracted chain ID. | ||
* @throws Error if the chain ID cannot be extracted or is invalid. | ||
*/ | ||
export const extractChainIdFromPaymasterUrl = (url: string): number => { | ||
try { | ||
const regex = /\/api\/v\d+\/(\d+)\// | ||
const match = regex.exec(url) | ||
if (!match) { | ||
throw new Error("Invalid URL format") | ||
} | ||
return Number.parseInt(match[1]) | ||
} catch (error) { | ||
throw new Error("Invalid chain id") | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { http } from "viem" | ||
import { describe, expect, it } from "vitest" | ||
|
||
import { privateKeyToAccount } from "viem/accounts" | ||
import { getChain } from "../src/account/utils/helpers.js" | ||
import { createPaymasterClient } from "../src/paymaster/createPaymasterClient.js" | ||
import { extractChainIdFromPaymasterUrl } from "../src/paymaster/utils/helpers.js" | ||
|
||
describe("Paymaster tests", () => { | ||
const paymasterUrl = process.env.PAYMASTER_URL ?? "" | ||
const chainId = extractChainIdFromPaymasterUrl(paymasterUrl) | ||
const chain = getChain(chainId) | ||
const account = privateKeyToAccount(`0x${process.env.PRIVATE_KEY}`) | ||
|
||
it("Should have the properties of a viem client", async () => { | ||
const paymasterClient = createPaymasterClient({ | ||
chain, | ||
transport: http(paymasterUrl) | ||
}) | ||
expect(paymasterClient.uid).toBeDefined() | ||
expect(paymasterClient?.chain?.id).toBe(chainId) | ||
expect(paymasterClient.pollingInterval).toBeDefined() | ||
}) | ||
|
||
it("Should have a paymaster specific action", async () => { | ||
const paymasterClient = createPaymasterClient({ | ||
chain, | ||
transport: http(paymasterUrl) | ||
}) | ||
const result = await paymasterClient.sponsorUserOperation() | ||
expect(result).toBeTruthy() | ||
}) | ||
}) |
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