Callback

Callback is used to make a function call to an external contract. The external contract can perform an arbitrary logic using the bytes data and payable value provided by the call arguments.

One key usage for the callback is to make a token swap from a DEX contract outside of INIT.

The external contract must implement coreCallback payable function.

It is recommended to check that msg.sender of coreCallback is INIT_CORE to avoid unintended external calls from other users.

// Example external contract that uses InitCore's callback
contract ExternalContract {
    function coreCallback(address sender, bytes calldata data) external payable returns (bytes memory result) {
        // check msg.sender is InitCore
        require(msg.sender == INIT_CORE, 'sender not allowed');

        // perform a swap on a DEX
        
        // returns bytes result
    }
    
    function executeCallback(bytes calldata data) payable {
        IInitCore(INIT_CORE).callback{value: msg.value}(msg.sender, data);
    }
}

Last updated