-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add getSmartAccountWalletClient #49
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@graphprotocol/grc-20": patch | ||
| --- | ||
|
|
||
| add getSmartAccountWalletClient |
This file contains hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,84 @@ | ||
| import { toSafeSmartAccount } from 'permissionless/accounts'; | ||
| import { http, createPublicClient } from 'viem'; | ||
| import { entryPoint07Address } from 'viem/account-abstraction'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { getSmartAccountWalletClient } from './smart-wallet.js'; | ||
|
|
||
| // mock all external dependencies | ||
| vi.mock('permissionless', () => ({ | ||
| createSmartAccountClient: vi.fn().mockReturnValue({ mockSmartAccountClient: true }), | ||
| })); | ||
|
|
||
| vi.mock('permissionless/accounts', () => ({ | ||
| toSafeSmartAccount: vi.fn().mockResolvedValue({ mockSafeAccount: true }), | ||
| })); | ||
|
|
||
| vi.mock('permissionless/clients/pimlico', () => ({ | ||
| createPimlicoClient: vi.fn().mockReturnValue({ | ||
| mockPimlicoClient: true, | ||
| getUserOperationGasPrice: vi.fn().mockResolvedValue({ | ||
| fast: { maxFeePerGas: 1000000000n, maxPriorityFeePerGas: 100000000n }, | ||
| }), | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock('viem', () => ({ | ||
| createPublicClient: vi.fn().mockReturnValue({ mockPublicClient: true }), | ||
| http: vi.fn().mockImplementation(url => ({ mockTransport: true, url })), | ||
| })); | ||
|
|
||
| vi.mock('viem/accounts', () => ({ | ||
| privateKeyToAccount: vi.fn().mockReturnValue({ mockAccount: true }), | ||
| })); | ||
|
|
||
| describe('getSmartAccountWalletClient', () => { | ||
| const mockPrivateKey = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should create a client with the default RPC URL when no RPC URL is provided', async () => { | ||
| await getSmartAccountWalletClient({ privateKey: mockPrivateKey, }); | ||
|
|
||
| expect(http).toHaveBeenCalledWith('https://rpc-geo-genesis-h0q2s21xx8.t.conduit.xyz'); | ||
| expect(createPublicClient).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| transport: { mockTransport: true, url: 'https://rpc-geo-genesis-h0q2s21xx8.t.conduit.xyz' }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('should create a client with a custom RPC URL when provided', async () => { | ||
| const customRpcUrl = 'https://custom-rpc.example.com'; | ||
| await getSmartAccountWalletClient({ | ||
| privateKey: mockPrivateKey, | ||
| rpcUrl: customRpcUrl, | ||
| }); | ||
|
|
||
| expect(http).toHaveBeenCalledWith(customRpcUrl); | ||
| expect(createPublicClient).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| transport: { mockTransport: true, url: customRpcUrl }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('should initialize safe account with correct parameters', async () => { | ||
| await getSmartAccountWalletClient({ privateKey: mockPrivateKey, }); | ||
|
|
||
| expect(privateKeyToAccount).toHaveBeenCalledWith(mockPrivateKey); | ||
| expect(toSafeSmartAccount).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| client: { mockPublicClient: true }, | ||
| owners: [{ mockAccount: true }], | ||
| entryPoint: { | ||
| address: entryPoint07Address, | ||
| version: '0.7', | ||
| }, | ||
| version: '1.4.1', | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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,124 @@ | ||
| import { type SmartAccountClient, createSmartAccountClient } from 'permissionless'; | ||
| import { type SafeSmartAccountImplementation, toSafeSmartAccount } from 'permissionless/accounts'; | ||
| import { createPimlicoClient } from 'permissionless/clients/pimlico'; | ||
| import type { Address, Chain, Hex, HttpTransport } from 'viem'; | ||
| import { http, createPublicClient } from 'viem'; | ||
| import { type SmartAccountImplementation, entryPoint07Address } from 'viem/account-abstraction'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
|
|
||
| const DEFAULT_RPC_URL = 'https://rpc-geo-genesis-h0q2s21xx8.t.conduit.xyz'; | ||
|
|
||
| /** | ||
| * We provide a fallback API key for gas sponsorship for the duration of the | ||
| * Geo Genesis early access period. This API key is gas-limited. | ||
| */ | ||
| const DEFAULT_API_KEY = 'pim_KqHm63txxhbCYjdDaWaHqH'; | ||
|
|
||
| type GetSmartAccountWalletClientParams = { | ||
| privateKey: Hex; | ||
| rpcUrl?: string; | ||
| }; | ||
|
|
||
| type SafeSmartAccount = SafeSmartAccountImplementation<'0.7'> & { | ||
| address: Address; | ||
| getNonce: NonNullable<SmartAccountImplementation['getNonce']>; | ||
| isDeployed: () => Promise<boolean>; | ||
| type: 'smart'; | ||
| }; | ||
|
|
||
| type GeoSmartAccount = SmartAccountClient< | ||
| HttpTransport<undefined, false>, | ||
| Chain, | ||
| object & | ||
| SafeSmartAccount & { | ||
| address: Address; | ||
| getNonce: NonNullable<SmartAccountImplementation['getNonce']>; | ||
| isDeployed: () => Promise<boolean>; | ||
| type: 'smart'; | ||
| }, | ||
| undefined, | ||
| undefined | ||
| >; | ||
|
|
||
| // type GeoSmartAccountWalletClient = Promise<ReturnType<typeof createSmartAccountClient>>; | ||
|
|
||
| /** | ||
| * Get a smart account wallet client for your Geo account. | ||
| * | ||
| * IMPORTANT: Be careful with your private key. Don't commit it to version control. | ||
| * You can get your private key using https://www.geobrowser.io/export-wallet | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const smartAccountWalletClient = await getSmartAccountWalletClient({ | ||
| * privateKey: '0x...', | ||
| * rpcUrl: '...', // optional | ||
| * }); | ||
| * ``` | ||
| * @param params – {@link GetSmartAccountWalletClientParams} | ||
| * @returns – {@link SmartAccountClient} | ||
| */ | ||
| export const getSmartAccountWalletClient = async ({ | ||
| privateKey, | ||
| rpcUrl = DEFAULT_RPC_URL, | ||
| }: GetSmartAccountWalletClientParams): Promise<GeoSmartAccount> => { | ||
| const GEOGENESIS: Chain = { | ||
| id: Number('80451'), | ||
| name: 'Geo Genesis', | ||
| nativeCurrency: { | ||
| name: 'Ethereum', | ||
| symbol: 'ETH', | ||
| decimals: 18, | ||
| }, | ||
| rpcUrls: { | ||
| default: { | ||
| http: [rpcUrl], | ||
| }, | ||
| public: { | ||
| http: [rpcUrl], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const transport = http(rpcUrl); | ||
|
|
||
| const publicClient = createPublicClient({ | ||
| transport, | ||
| chain: GEOGENESIS, | ||
| }); | ||
|
|
||
| const safeAccount = await toSafeSmartAccount({ | ||
| client: publicClient, | ||
| owners: [privateKeyToAccount(privateKey)], | ||
| entryPoint: { | ||
| // optional, defaults to 0.7 | ||
| address: entryPoint07Address, | ||
| version: '0.7', | ||
| }, | ||
| version: '1.4.1', | ||
| }); | ||
|
|
||
| const bundlerTransport = http(`https://api.pimlico.io/v2/80451/rpc?apikey=${DEFAULT_API_KEY}`); | ||
| const paymasterClient = createPimlicoClient({ | ||
| transport: bundlerTransport, | ||
| chain: GEOGENESIS, | ||
| entryPoint: { | ||
| address: entryPoint07Address, | ||
| version: '0.7', | ||
| }, | ||
| }); | ||
|
|
||
| const smartAccount = createSmartAccountClient({ | ||
| chain: GEOGENESIS, | ||
| account: safeAccount, | ||
| paymaster: paymasterClient, | ||
| bundlerTransport, | ||
| userOperation: { | ||
| estimateFeesPerGas: async () => { | ||
| return (await paymasterClient.getUserOperationGasPrice()).fast; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return smartAccount; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.