Call external function in remix.ethereum

Viewed 1618

enter image description here

Hi I am new but I want to learn, I have just forked a project in Solidity and deployed it. I would like to know how to use calldata in order to call an external function. For instance:

function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }

Can you provide any example? I deployed and I would like to call the function from https://remix.ethereum.org/

Thanks

1 Answers

The CALLDATA field in Remix allows you to manually input any value of the data field in Ethereum transaction.

The data field always starts with 0x, then contains 4 bytes (8 hex characters) of the function signature, and the rest of data is the function arguments.


Function signature is calculated as first 4 bytes of keccak-256 hash of a string containing the function name and argument types.

Example:

  • transferOwnership(address) - this is the string that is going to be hashed
  • f2fde38b092330466c661fc723d5289b90272a3e580e3187d1d7ef788506c557 is the keccak-256 hash of this string
  • f2fde38b is the first 4 bytes, which is the function signature

The arguments are then ordered in the same way as in the function (in your case, there's only one argument). The length of each value depends on its datatype (some are fixed, some dynamic). In case of address type, the length is 256bit (64 hex characters).

So if you have an address 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF, the actual value passed as an argument is going to be 000000000000000000000000ffffffffffffffffffffffffffffffffffffffff (left-padded zeros to the length of 256bit).


All combined together:

0xf2fde38b000000000000000000000000ffffffffffffffffffffffffffffffffffffffff

calls the function transferOwnership(address) and passes address 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF as an argument

Note: You can find another example in my answer to a similar question.

Related