A test on functionCall in the Address library

Hi there, I did a small experiement with the Address library and got something I have not fully understood yet. The following two contracts are quite straightforward. The f() function in the MappingUser contract is not working, and f2() works fine.

It seems that since m is a dynamically generated contract, a functionCall() is not working for it. Is this true?

// SPDX-License-Identifier: MIT

pragma solidity 0.8.4;

contract MappingExample {
    mapping(address => uint) public balances;

    function update(uint newBalance) public {
        balances[msg.sender] = newBalance;
    }
}

contract MappingUser {
    
    using Address for address;
    
    function f() public returns (bytes memory) {
        MappingExample m = new MappingExample();
        // m.update(100);
        (bytes memory data) = address(m).functionCall(abi.encodeWithSignature("update(uint256)", 100));
        
        return data;
    }
    
    function f2(address adr) public returns(bytes memory) {
        return adr.functionCall(abi.encodeWithSignature("update(uint256)", 100));
    }
}

Good question.

I am not sure for this, but I think it is ok, let me have a look.

f() seems to be working, it does not revert.

Alright. The next question is if a contract is dynamically allocated and how does a low-level function call get made to it in the same contract? It may never get any practical use case, but I just happen to be wondering about it.

A side question is, does a dynamically generated contract get deployed onto a blockchain or inside the same chunk of data with the generating contract.

Using new deploys a new contract just like the one that’s running. It has its own address and its own code. So you can call a function on it normally.

I see. The new operation on a contract deploys a new instance of this contract onto a blockchain with its own address and code, however, the new operation on a struct or an array only creates new instances inside this contract. This subtlety is what I was trying to get clarified. Thanks a lot.

2 Likes

Ah, yes. I can see how this is really confusing.

1 Like