-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor userOp builder to use generic implementation
- Loading branch information
1 parent
84024f6
commit 601a2fa
Showing
5 changed files
with
68 additions
and
70 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
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
48 changes: 48 additions & 0 deletions
48
advanced/wallets/react-wallet-v2/src/utils/UserOpBuilderUtil.ts
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,48 @@ | ||
import { SafeUserOpBuilder } from '@/lib/smart-accounts/builders/SafeUserOpBuilder' | ||
import { UserOpBuilder } from '@/lib/smart-accounts/builders/UserOpBuilder' | ||
import { | ||
Address, | ||
Chain, | ||
createPublicClient, | ||
getAddress, | ||
http, | ||
PublicClient, | ||
size, | ||
slice | ||
} from 'viem' | ||
import { publicClientUrl } from './SmartAccountUtil' | ||
import { | ||
SAFE_4337_MODULE_ADDRESSES, | ||
SAFE_FALLBACK_HANDLER_STORAGE_SLOT | ||
} from '@/consts/smartAccounts' | ||
|
||
type GetUserOpBuilderParams = { | ||
account: Address | ||
chain: Chain | ||
publicClient?: PublicClient | ||
} | ||
|
||
export async function getUserOpBuilder(params: GetUserOpBuilderParams): Promise<UserOpBuilder> { | ||
let publicClient = params.publicClient | ||
if (!publicClient) { | ||
publicClient = createPublicClient({ | ||
transport: http(publicClientUrl({ chain: params.chain })) | ||
}) | ||
} | ||
if (await isSafeAccount(publicClient, params.account)) { | ||
return new SafeUserOpBuilder(params.account, params.chain.id) | ||
} | ||
throw new Error('Unsupported implementation type') | ||
} | ||
|
||
async function isSafeAccount(publicClient: PublicClient, address: Address): Promise<Boolean> { | ||
const storageValue = await publicClient.getStorageAt({ | ||
address, | ||
slot: SAFE_FALLBACK_HANDLER_STORAGE_SLOT | ||
}) | ||
if (!storageValue) { | ||
return false | ||
} | ||
const safe4337ModuleAddress = getAddress(slice(storageValue, size(storageValue) - 20)) | ||
return SAFE_4337_MODULE_ADDRESSES.includes(safe4337ModuleAddress) | ||
} |