Batch contract writes with Multicall3
mainMulticall3 can execute multiple state-changing calls in a single transaction.
Security Warning: This usage is unaudited. While Multicall3 is a stateless contract and should not hold funds, you must follow best practices: never approve Multicall3 to spend your tokens.
Critical Implementation Details
1. msg.sender Behavior
- Calling from an EOA: If you call Multicall3 directly from an Externally Owned Account (EOA), the
msg.senderfor all sub-calls will be the Multicall3 contract address. Use this only if the identity of the caller does not matter to the target contracts. - Calling from a Contract (via CALL): If a contract calls Multicall3 using the
CALLopcode, themsg.senderfor the sub-calls will be the calling contract. - Calling from a Contract (via DELEGATECALL): If a contract calls Multicall3 using
DELEGATECALL, the context is preserved. Themsg.senderandmsg.valuefor the sub-calls will be the same as the original caller. This is how tools like Gnosis Safe's Transaction Builder batch calls.
2. msg.value Risks
Because msg.value does not change during a DELEGATECALL, you must be extremely careful when relying on msg.value within a multicall context.