I'm trying to understand below code:

pragma solidity ^0.4.23;

contract Relay {
    address public currentVersion;
    address public owner;

    modifier onlyOwner() {
        require(msg.sender == owner);
    function Relay(address initAddr) {
        currentVersion = initAddr;
        owner = msg.sender; // this owner may be another contract with multisig, not a single contract owner

    function changeContract(address newVersion) public onlyOwner(){
        currentVersion = newVersion;

    function() {

This code is for maintaining version of a contract, but I'm not getting use of fallback function here.
I know that msg.data is complete calldata which is non-modifiable, but have no idea use of delegatecall here.

1 Answer 1


The fallback function gets called when no other function matches the calldata signature. In this code, the fallback functions is used as a universal function.

This is done by forwarding the calldata to the contract at currentVersion. Forwarding the calldata to another smart contract results in it running the proper funcion. There are two ways to do this: address.call and address.delegatecall. The difference between those is the context in which the receiving contract is going to run the code.

With delegatecall, the receiving contract accesses the Relay contract's storage. This way, the receiving contract can alter all fields of the Relay.

Effectively, by updating currentVersion, Relay can update it's code without changing it's address or migrating it's storage contents.


  • Thanks! could you please explain what do you mean by Forwarding the calldata to another smart contract results in it running the proper funcion
    – Div
    Commented Aug 9, 2018 at 9:52
  • As currentVersion is not a contract right? it's a state variable
    – Div
    Commented Aug 9, 2018 at 9:55
  • 1
    That's correct, but it's an address that should be pointing to a smart contract, as external accounts can't be called
    – Henk
    Commented Aug 9, 2018 at 10:16
  • Okay, I understand it partially, I think in order to understand this code I would need to understand fallback function and delegateCall first. I'm having doubt regarding fallback.
    – Div
    Commented Aug 9, 2018 at 10:21
  • I've updated my answer with a couple of links to the documentation. Be sure to check them out
    – Henk
    Commented Aug 9, 2018 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.