Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/pink-moons-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rhinestone/sdk": patch
---

Support custom bundler / paymaster / provider transport using `factory` prop
1 change: 1 addition & 0 deletions bun.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"lockfileVersion": 1,
"configVersion": 0,
"workspaces": {
"": {
"dependencies": {
Expand Down
12 changes: 11 additions & 1 deletion src/accounts/json-rpc/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { http } from 'viem'
import { base, mainnet } from 'viem/chains'
import { describe, expect, test } from 'vitest'

import { createTransport } from './index'

describe('JSON-RPC', () => {
Expand All @@ -23,6 +23,16 @@ describe('JSON-RPC', () => {
expect(transport).toBeDefined()
})

test('Factory', () => {
const viemTransport = http('https://my-rpc.example.com')
const transport = createTransport(mainnet, {
type: 'factory',
getTransport: (_chainId: number) => viemTransport,
})
expect(transport).toBeDefined()
expect(transport).toBe(viemTransport)
})

test('Custom throws error when URL not configured for chain', () => {
expect(() =>
createTransport(mainnet, {
Expand Down
3 changes: 3 additions & 0 deletions src/accounts/json-rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function createTransport(chain: Chain, provider?: ProviderConfig): Transport {
const customUrl = getCustomUrl(chain.id, provider.urls)
return http(customUrl)
}
case 'factory': {
return provider.getTransport(chain.id)
}
}
}

Expand Down
65 changes: 33 additions & 32 deletions src/accounts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Address, Client, Hex } from 'viem'
import type { Address, Client, Hex, Transport } from 'viem'
import {
concatHex,
encodeAbiParameters,
Expand Down Expand Up @@ -227,21 +227,33 @@ async function getAccountNonce(
}

function getBundlerClient(config: RhinestoneConfig, client: Client) {
function getBundlerEndpoint(config: BundlerConfig, chainId: number) {
function getBundlerTransport(config: BundlerConfig, chainId: number) {
switch (config.type) {
case 'pimlico':
return `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${config.apiKey}`
return http(
`https://api.pimlico.io/v2/${chainId}/rpc?apikey=${config.apiKey}`,
)
case 'biconomy':
return `https://bundler.biconomy.io/api/v3/${chainId}/${config.apiKey}`
return http(
`https://bundler.biconomy.io/api/v3/${chainId}/${config.apiKey}`,
)
case 'factory':
return config.getTransport(chainId)
}
}

function getPaymasterEndpoint(config: PaymasterConfig, chainId: number) {
function getPaymasterTransport(config: PaymasterConfig, chainId: number) {
switch (config.type) {
case 'pimlico':
return `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${config.apiKey}`
return http(
`https://api.pimlico.io/v2/${chainId}/rpc?apikey=${config.apiKey}`,
)
case 'biconomy':
return `https://paymaster.biconomy.io/api/v2/${chainId}/${config.apiKey}`
return http(
`https://paymaster.biconomy.io/api/v2/${chainId}/${config.apiKey}`,
)
case 'factory':
return config.getTransport(chainId)
}
}

Expand All @@ -251,42 +263,31 @@ function getBundlerClient(config: RhinestoneConfig, client: Client) {
throw new Error('Chain id is required')
}

const endpoint = bundler
? getBundlerEndpoint(bundler, chainId)
: `https://public.pimlico.io/v2/${chainId}/rpc`
const paymasterEndpoint = paymaster
? getPaymasterEndpoint(paymaster, chainId)
const transport = bundler
? getBundlerTransport(bundler, chainId)
: http(`https://public.pimlico.io/v2/${chainId}/rpc`)
const paymasterTransport = paymaster
? getPaymasterTransport(paymaster, chainId)
: undefined
return createBundlerClient({
client,
transport: http(endpoint),
paymaster: paymasterEndpoint
transport: transport,
paymaster: paymasterTransport
? createPaymasterClient({
transport: http(paymasterEndpoint),
transport: paymasterTransport,
})
: undefined,
userOperation: {
estimateFeesPerGas: () => getGasPriceEstimate(endpoint),
estimateFeesPerGas: () => getGasPriceEstimate(transport),
},
})
}

async function getGasPriceEstimate(bundlerUrl: string) {
const response = await fetch(bundlerUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: Date.now(),
// TODO do not rely on vendor-specific methods
method: 'pimlico_getUserOperationGasPrice',
params: [],
}),
})

const json = (await response.json()) as UserOperationGasPriceResponse
async function getGasPriceEstimate(bundlerTransport: Transport) {
const json = (await bundlerTransport({}).request({
method: 'pimlico_getUserOperationGasPrice',
params: [],
})) as UserOperationGasPriceResponse

return {
maxFeePerGas: BigInt(json.result.fast.maxFeePerGas),
Expand Down
32 changes: 23 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Account, Address, Chain, Hex } from 'viem'
import type { Account, Address, Chain, Hex, Transport } from 'viem'
import type { WebAuthnAccount } from 'viem/account-abstraction'
import type { ModuleType } from './modules/common'
import type { EnableSessionData } from './modules/validators/smart-sessions'
Expand Down Expand Up @@ -88,16 +88,30 @@ type ProviderConfig =
type: 'custom'
urls: Record<number, string>
}
| {
type: 'factory'
getTransport: (chainId: number) => Transport
}

interface BundlerConfig {
type: 'pimlico' | 'biconomy'
apiKey: string
}
type BundlerConfig =
| {
type: 'pimlico' | 'biconomy'
apiKey: string
}
| {
type: 'factory'
getTransport: (chainId: number) => Transport
}

interface PaymasterConfig {
type: 'pimlico' | 'biconomy'
apiKey: string
}
type PaymasterConfig =
| {
type: 'pimlico' | 'biconomy'
apiKey: string
}
| {
type: 'factory'
getTransport: (chainId: number) => Transport
}

type OwnerSet =
| OwnableValidatorConfig
Expand Down
Loading