caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

8.8 KiB

ERC4337

Simple ERC4337 account implementation.

Recommended usage:

  1. Deploy the ERC4337 as an implementation contract, and verify it on Etherscan.
  2. Create a factory that uses LibClone.deployERC1967 or LibClone.deployDeterministicERC1967 to clone the implementation. See: ERC4337Factory.sol.

Note:

ERC4337 is a very complicated standard with many potential gotchas. Also, it is subject to change and has not been finalized (so accounts are encouraged to be upgradeable). Usually, ERC4337 account implementations are developed by companies with ample funds for security reviews. This implementation is intended to serve as a base reference for smart account developers working in such companies. If you are using this implementation, please do get one or more security reviews before deployment.

Inherits:

Structs

PackedUserOperation

struct PackedUserOperation {
    address sender;
    uint256 nonce;
    bytes initCode; // Factory address and `factoryData` (or empty).
    bytes callData;
    bytes32 accountGasLimits; // `verificationGas` (16 bytes) and `callGas` (16 bytes).
    uint256 preVerificationGas;
    bytes32 gasFees; // `maxPriorityFee` (16 bytes) and `maxFeePerGas` (16 bytes).
    bytes paymasterAndData; // Paymaster fields (or empty).
    bytes signature;
}

The packed ERC4337 user operation (userOp) struct.

Call

struct Call {
    address target;
    uint256 value;
    bytes data;
}

Call struct for the executeBatch function.

Constructor

_disableERC4337ImplementationInitializer()

function _disableERC4337ImplementationInitializer() internal virtual

Automatically initializes the owner for the implementation. This blocks someone
from initializing the implementation and doing a delegatecall to SELFDESTRUCT.
Proxies to the implementation will still be able to initialize as per normal.

Initializer

initialize(address)

function initialize(address newOwner) public payable virtual

Initializes the account with the owner. Can only be called once.

Entry Point

entryPoint()

function entryPoint() public view virtual returns (address)

Returns the canonical ERC4337 EntryPoint contract (0.7).
Override this function to return a different EntryPoint.

Validation Operations

validateUserOp(PackedUserOperation,bytes32,uint256)

function validateUserOp(
    PackedUserOperation calldata userOp,
    bytes32 userOpHash,
    uint256 missingAccountFunds
)
    external
    payable
    virtual
    onlyEntryPoint
    payPrefund(missingAccountFunds)
    returns (uint256 validationData)

Validates the signature and nonce.
The EntryPoint will make the call to the recipient only if
this validation call returns successfully.
Signature failure should be reported by returning 1 (see: _validateSignature).
This allows making a "simulation call" without a valid signature.
Other failures (e.g. nonce mismatch, or invalid signature format)
should still revert to signal failure.

_validateSignature(PackedUserOperation,bytes32)

function _validateSignature(
    PackedUserOperation calldata userOp,
    bytes32 userOpHash
) internal virtual returns (uint256 validationData)

Validate userOp.signature for the userOpHash.

_validateNonce(uint256)

function _validateNonce(uint256 nonce) internal virtual

Override to validate the nonce of the userOp.
This method may validate the nonce requirement of this account.
e.g.
To limit the nonce to use sequenced userOps only (no "out of order" userOps):
require(nonce < type(uint64).max)
For a hypothetical account that requires the nonce to be out-of-order:
require(nonce & type(uint64).max == 0)
The actual nonce uniqueness is managed by the EntryPoint, and thus no other
action is needed by the account itself.

payPrefund(uint256)

modifier payPrefund(uint256 missingAccountFunds) virtual

Sends to the EntryPoint (i.e. msg.sender) the missing funds for this transaction.
Subclass MAY override this modifier for better funds management.
(e.g. send to the EntryPoint more than the minimum required, so that in future transactions
it will not be required to send again)
missingAccountFunds is the minimum value this modifier should send the EntryPoint,
which MAY be zero, in case there is enough deposit, or the userOp has a paymaster.

onlyEntryPoint()

modifier onlyEntryPoint() virtual

Requires that the caller is the EntryPoint.

Execution Operations

execute(address,uint256,bytes)

function execute(address target, uint256 value, bytes calldata data)
    public
    payable
    virtual
    onlyEntryPointOrOwner
    returns (bytes memory result)

Execute a call from this account.

executeBatch(Call[])

function executeBatch(Call[] calldata calls)
    public
    payable
    virtual
    onlyEntryPointOrOwner
    returns (bytes[] memory results)

Execute a sequence of calls from this account.

delegateExecute(address,bytes)

function delegateExecute(address delegate, bytes calldata data)
    public
    payable
    virtual
    onlyEntryPointOrOwner
    delegateExecuteGuard
    returns (bytes memory result)

Execute a delegatecall with delegate on this account.

delegateExecuteGuard()

modifier delegateExecuteGuard() virtual

Ensures that the owner and implementation slots' values aren't changed.
You can override this modifier to ensure the sanctity of other storage slots too.

onlyEntryPointOrOwner()

modifier onlyEntryPointOrOwner() virtual

Requires that the caller is the EntryPoint, the owner, or the account itself.

Direct Storage Operations

storageLoad(bytes32)

function storageLoad(bytes32 storageSlot)
    public
    view
    virtual
    returns (bytes32 result)

Returns the raw storage value at storageSlot.

storageStore(bytes32,bytes32)

function storageStore(bytes32 storageSlot, bytes32 storageValue)
    public
    payable
    virtual
    onlyEntryPointOrOwner
    storageStoreGuard(storageSlot)

Writes the raw storage value at storageSlot.

storageStoreGuard(bytes32)

modifier storageStoreGuard(bytes32 storageSlot) virtual

Ensures that the storageSlot is prohibited for direct storage writes.
You can override this modifier to ensure the sanctity of other storage slots too.

Deposit Operations

getDeposit()

function getDeposit() public view virtual returns (uint256 result)

Returns the account's balance on the EntryPoint.

addDeposit()

function addDeposit() public payable virtual

Deposit more funds for this account in the EntryPoint.

withdrawDepositTo(address,uint256)

function withdrawDepositTo(address to, uint256 amount)
    public
    payable
    virtual
    onlyOwner

Withdraw ETH from the account's deposit on the EntryPoint.

Overrides

_checkOwner()

function _checkOwner() internal view virtual override(Ownable)

Requires that the caller is the owner or the account itself.
This override affects the onlyOwner modifier.

_guardInitializeOwner()

function _guardInitializeOwner()
    internal
    pure
    virtual
    override(Ownable)
    returns (bool)

To prevent double-initialization (reuses the owner storage slot for efficiency).

_erc1271Signer()

function _erc1271Signer()
    internal
    view
    virtual
    override(ERC1271)
    returns (address)

Uses the owner as the ERC1271 signer.

_erc1271CallerIsSafe()

function _erc1271CallerIsSafe()
    internal
    view
    virtual
    override(ERC1271)
    returns (bool)

Allow the entry point to skip the ERC7739 nested typed data workflow.
This is safe as the entry point already includes the smart account in the user op digest.

_authorizeUpgrade(address)

function _authorizeUpgrade(address)
    internal
    virtual
    override(UUPSUpgradeable)
    onlyOwner

To ensure that only the owner or the account itself can upgrade the implementation.

_useLibZipCdFallback()

function _useLibZipCdFallback() internal view virtual returns (bool)

If you don't need to use LibZip.cdFallback, override this function to return false.

fallback()

fallback() external payable virtual override(Receiver) receiverFallback

Handle token callbacks. If no token callback is triggered,
use LibZip.cdFallback for generalized calldata decompression.