Skip to content

Commit

Permalink
Merge pull request #50 from pimlicolabs/feat/get-required-prefund
Browse files Browse the repository at this point in the history
Add util function get pre fund
  • Loading branch information
kristofgazso authored Dec 12, 2023
2 parents 4d33ea7 + 2857b89 commit f1f250e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-years-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"permissionless": patch
---

Added getRequiredPrefund utility function
2 changes: 1 addition & 1 deletion src/accounts/signerToSafeSmartAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const SAFE_VERSION_TO_ADDRESSES_MAP: {
} = {
"1.4.1": {
ADD_MODULES_LIB_ADDRESS: "0x8EcD4ec46D4D2a6B64fE960B3D64e8B94B2234eb",
SAFE_4337_MODULE_ADDRESS: "0x39E54Bb2b3Aa444b4B39DEe15De3b7809c36Fc38",
SAFE_4337_MODULE_ADDRESS: "0xa581c4A4DB7175302464fF3C06380BC3270b4037",
SAFE_PROXY_FACTORY_ADDRESS:
"0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
SAFE_SINGLETON_ADDRESS: "0x41675C099F32341bf84BFc5382aF534df5C7461a",
Expand Down
31 changes: 31 additions & 0 deletions src/utils/getRequiredPrefund.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { UserOperation } from "../types"

export type GetRequiredPrefundReturnType = {
userOperation: UserOperation
}

/**
*
* Returns the minimum required funds in the senders's smart account to execute the user operation.
*
* @param arags: {userOperation} as {@link UserOperation}
* @returns requiredPrefund as {@link bigint}
*
* @example
* import { getRequiredPrefund } from "permissionless/utils"
*
* const requiredPrefund = getRequiredPrefund({
* userOperation
* })
*/
export const getRequiredPrefund = ({
userOperation
}: GetRequiredPrefundReturnType): bigint => {
const multiplier = userOperation.paymasterAndData.length > 2 ? 3n : 1n
const requiredGas =
userOperation.callGasLimit +
userOperation.verificationGasLimit * multiplier +
userOperation.preVerificationGas

return BigInt(requiredGas) * BigInt(userOperation.maxFeePerGas)
}
6 changes: 6 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { Account, Address } from "viem"
import {
type GetRequiredPrefundReturnType,
getRequiredPrefund
} from "./getRequiredPrefund.js"
import {
type GetUserOperationHashParams,
getUserOperationHash
Expand All @@ -16,6 +20,8 @@ export function parseAccount(account: Address | Account): Account {

export {
getUserOperationHash,
getRequiredPrefund,
type GetRequiredPrefundReturnType,
type GetUserOperationHashParams,
signUserOperationHashWithECDSA,
AccountOrClientNotFoundError
Expand Down
31 changes: 30 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { beforeAll, describe, expect, test } from "bun:test"
import dotenv from "dotenv"
import { getSenderAddress, getUserOperationHash } from "permissionless"
import { signUserOperationHashWithECDSA } from "permissionless/utils"
import {
getRequiredPrefund,
signUserOperationHashWithECDSA
} from "permissionless/utils"
import { buildUserOp, getAccountInitCode } from "./userOp.js"
import {
getBundlerClient,
Expand Down Expand Up @@ -163,4 +166,30 @@ describe("test public actions and utils", () => {

expect(signature).toEqual(userOperation.signature)
})

test("getRequiredGas", async () => {
const bundlerClient = getBundlerClient()
const eoaWalletClient = getEoaWalletClient()
const userOperation = await buildUserOp(eoaWalletClient)

const gasParameters = await bundlerClient.estimateUserOperationGas({
userOperation,
entryPoint: getEntryPoint()
})

userOperation.callGasLimit = gasParameters.callGasLimit
userOperation.verificationGasLimit = gasParameters.verificationGasLimit
userOperation.preVerificationGas = gasParameters.preVerificationGas

const requiredGas = getRequiredPrefund({
userOperation
})

expect(requiredGas).toBe(
(gasParameters.callGasLimit +
gasParameters.verificationGasLimit +
gasParameters.preVerificationGas) *
userOperation.maxFeePerGas
)
})
})

0 comments on commit f1f250e

Please sign in to comment.