-
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.
* feat: added simple smart account client --------- Co-authored-by: GabiDev <[email protected]>
- Loading branch information
1 parent
c351b61
commit 01d4820
Showing
8 changed files
with
246 additions
and
2 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
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,129 @@ | ||
import type { Address, Hash, Hex } from "viem" | ||
import type { PartialBy } from "viem/chains" | ||
import type { ENTRYPOINT_ADDRESS_V07_TYPE } from "../../account/utils/types" | ||
|
||
export type UserOperationWithBigIntAsHex = { | ||
sender: Address | ||
nonce: Hex | ||
factory: Address | ||
factoryData: Hex | ||
callData: Hex | ||
callGasLimit: Hex | ||
verificationGasLimit: Hex | ||
preVerificationGas: Hex | ||
maxFeePerGas: Hex | ||
maxPriorityFeePerGas: Hex | ||
paymaster: Address | ||
paymasterVerificationGasLimit: Hex | ||
paymasterPostOpGasLimit: Hex | ||
paymasterData: Hex | ||
signature: Hex | ||
initCode?: never | ||
paymasterAndData?: never | ||
} | ||
|
||
export type BundlerRpcSchema<entryPoint extends ENTRYPOINT_ADDRESS_V07_TYPE> = [ | ||
{ | ||
Method: "eth_sendUserOperation" | ||
Parameters: [ | ||
userOperation: UserOperationWithBigIntAsHex, | ||
entryPoint: entryPoint | ||
] | ||
ReturnType: Hash | ||
}, | ||
{ | ||
Method: "eth_estimateUserOperationGas" | ||
Parameters: [ | ||
userOperation: PartialBy< | ||
UserOperationWithBigIntAsHex, | ||
| "callGasLimit" | ||
| "preVerificationGas" | ||
| "verificationGasLimit" | ||
| "paymasterVerificationGasLimit" | ||
| "paymasterPostOpGasLimit" | ||
>, | ||
entryPoint: entryPoint, | ||
stateOverrides?: StateOverrides | ||
] | ||
ReturnType: { | ||
preVerificationGas: Hex | ||
verificationGasLimit: Hex | ||
callGasLimit?: Hex | null | ||
paymasterVerificationGasLimit?: Hex | null | ||
paymasterPostOpGasLimit?: Hex | null | ||
} | ||
}, | ||
{ | ||
Method: "eth_supportedEntryPoints" | ||
Parameters: [] | ||
ReturnType: Address[] | ||
}, | ||
{ | ||
Method: "eth_chainId" | ||
Parameters: [] | ||
ReturnType: Hex | ||
}, | ||
{ | ||
Method: "eth_getUserOperationByHash" | ||
Parameters: [hash: Hash] | ||
ReturnType: { | ||
userOperation: UserOperationWithBigIntAsHex | ||
entryPoint: entryPoint | ||
transactionHash: Hash | ||
blockHash: Hash | ||
blockNumber: Hex | ||
} | ||
}, | ||
{ | ||
Method: "eth_getUserOperationReceipt" | ||
Parameters: [hash: Hash] | ||
ReturnType: UserOperationReceiptWithBigIntAsHex | ||
} | ||
] | ||
|
||
type UserOperationReceiptWithBigIntAsHex = { | ||
userOpHash: Hash | ||
sender: Address | ||
nonce: Hex | ||
actualGasUsed: Hex | ||
actualGasCost: Hex | ||
success: boolean | ||
receipt: { | ||
transactionHash: Hex | ||
transactionIndex: Hex | ||
blockHash: Hash | ||
blockNumber: Hex | ||
from: Address | ||
to: Address | null | ||
cumulativeGasUsed: Hex | ||
status: "0x0" | "0x1" | ||
gasUsed: Hex | ||
contractAddress: Address | null | ||
logsBloom: Hex | ||
effectiveGasPrice: Hex | ||
} | ||
logs: { | ||
data: Hex | ||
blockNumber: Hex | ||
blockHash: Hash | ||
transactionHash: Hash | ||
logIndex: Hex | ||
transactionIndex: Hex | ||
address: Address | ||
topics: Hex[] | ||
}[] | ||
} | ||
|
||
export type StateOverrides = { | ||
[x: string]: { | ||
balance?: bigint | undefined | ||
nonce?: bigint | number | undefined | ||
code?: Hex | undefined | ||
state?: { | ||
[x: Hex]: Hex | ||
} | ||
stateDiff?: { | ||
[x: Hex]: Hex | ||
} | ||
} | ||
} |
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,93 @@ | ||
import { | ||
type SmartAccountActions, | ||
type SmartAccountClientConfig, | ||
smartAccountActions | ||
} from "permissionless" | ||
|
||
import { type Chain, type Client, type Transport, createClient } from "viem" | ||
import type { Prettify } from "viem/chains" | ||
|
||
import type { | ||
ENTRYPOINT_ADDRESS_V07_TYPE, | ||
SmartAccount | ||
} from "../account/utils/types.js" | ||
import type { BundlerRpcSchema } from "../bundler/utils/types.js" | ||
|
||
export type SmartAccountClient< | ||
entryPoint extends ENTRYPOINT_ADDRESS_V07_TYPE, | ||
transport extends Transport = Transport, | ||
chain extends Chain | undefined = Chain | undefined, | ||
account extends SmartAccount<entryPoint> | undefined = | ||
| SmartAccount<entryPoint> | ||
| undefined | ||
> = Prettify< | ||
Client< | ||
transport, | ||
chain, | ||
account, | ||
BundlerRpcSchema<entryPoint>, | ||
SmartAccountActions<entryPoint, chain, account> | ||
> | ||
> | ||
|
||
/** | ||
* Creates a EIP-4337 compliant Bundler Client with a given [Transport](https://viem.sh/docs/clients/intro.html) configured for a [Chain](https://viem.sh/docs/clients/chains.html). | ||
* | ||
* - Docs: https://docs.pimlico.io/permissionless/reference/clients/smartAccountClient | ||
* | ||
* A Bundler Client is an interface to "erc 4337" [JSON-RPC API](https://eips.ethereum.org/EIPS/eip-4337#rpc-methods-eth-namespace) methods such as sending user operation, estimating gas for a user operation, get user operation receipt, etc through Bundler Actions. | ||
* | ||
* @param parameters - {@link WalletClientConfig} | ||
* @returns A Bundler Client. {@link SmartAccountClient} | ||
* | ||
* @example | ||
* import { createPublicClient, http } from 'viem' | ||
* import { mainnet } from 'viem/chains' | ||
* | ||
* const smartAccountClient = createSmartAccountClient({ | ||
* chain: mainnet, | ||
* transport: http(BUNDLER_URL), | ||
* }) | ||
*/ | ||
|
||
export function createSmartAccountClient< | ||
TSmartAccount extends SmartAccount<ENTRYPOINT_ADDRESS_V07_TYPE>, | ||
TTransport extends Transport = Transport, | ||
TChain extends Chain = Chain | ||
>( | ||
parameters: SmartAccountClientConfig< | ||
ENTRYPOINT_ADDRESS_V07_TYPE, | ||
TTransport, | ||
TChain, | ||
TSmartAccount | ||
> | ||
): SmartAccountClient< | ||
ENTRYPOINT_ADDRESS_V07_TYPE, | ||
TTransport, | ||
TChain, | ||
TSmartAccount | ||
> { | ||
const { | ||
key = "Account", | ||
name = "Smart Account Client", | ||
bundlerTransport | ||
} = parameters | ||
const client = createClient({ | ||
...parameters, | ||
key, | ||
name, | ||
transport: bundlerTransport, | ||
type: "smartAccountClient" | ||
}) | ||
|
||
return client.extend( | ||
smartAccountActions({ | ||
middleware: parameters.middleware | ||
}) | ||
) as SmartAccountClient< | ||
ENTRYPOINT_ADDRESS_V07_TYPE, | ||
TTransport, | ||
TChain, | ||
TSmartAccount | ||
> | ||
} |
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,4 @@ | ||
export { | ||
type SmartAccountClient, | ||
createSmartAccountClient | ||
} from "./createSmartAccountClient.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
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