Skip to main content

CallbackHandler

Description

This contract handles callbacks by registering a magic number together with the callback function's selector. It provides the _handleCallback function that inheriting contracts have to call inside their fallback() function (_handleCallback(msg.callbackSelector, msg.data)). This allows to adaptively register ERC standards (e.g., ERC-721, ERC-1115, or future versions of ERC-165) and returning the required magic numbers for the associated callback functions for the inheriting contract so that it doesn't need to be upgraded.

This callback handling functionality is intented to be used by executor contracts (i.e., DAO.sol).

Implementation

internal variable callbackMagicNumbers

A mapping between callback function selectors and magic return numbers.

mapping(bytes4 => bytes4) callbackMagicNumbers

internal variable UNREGISTERED_CALLBACK

The magic number refering to unregistered callbacks.

bytes4 UNREGISTERED_CALLBACK

error UnkownCallback

Thrown if the callback function is not registered.

error UnkownCallback(bytes4 callbackSelector, bytes4 magicNumber)
InputTypeDescription
callbackSelectorbytes4The selector of the callback function.
magicNumberbytes4The magic number to be registered for the callback function selector.

event CallbackReceived

Emitted when _handleCallback is called.

event CallbackReceived(address sender, bytes4 sig, bytes data)
InputTypeDescription
senderaddressWho called the callback.
sigbytes4The function signature.
databytesThe calldata.

internal function _handleCallback

Handles callbacks to adaptively support ERC standards.

function _handleCallback(bytes4 _callbackSelector, bytes _data) internal virtual returns (bytes4)
InputTypeDescription
_callbackSelectorbytes4The function selector of the callback function.
_databytesThe calldata.
Output
0bytes4The magic number registered for the function selector triggering the fallback.

This function is supposed to be called via _handleCallback(msg.sig, msg.data) in the fallback() function of the inheriting contract.

internal function _registerCallback

Registers a magic number for a callback function selector.

function _registerCallback(bytes4 _callbackSelector, bytes4 _magicNumber) internal virtual
InputTypeDescription
_callbackSelectorbytes4The selector of the callback function.
_magicNumberbytes4The magic number to be registered for the callback function selector.
© 2024