Solidity Tutorial: all about Functions – Jean Cvllr

Free Bitcoins: FreeBitcoin | BonusBitcoin

Coins Kaufen: Bitcoin.deAnycoinDirektCoinbaseCoinMama (mit Kreditkarte)Paxfull

Handelsplätze / Börsen: Bitcoin.de | KuCoinBinanceBitMexBitpandaeToro

Lending / Zinsen erhalten: Celsius NetworkCoinlend (Bot)

Cloud Mining: HashflareGenesis MiningIQ Mining


What is the function signature?

Assuming that we want to call a function from a contract, our transaction data has to start with the function signature.

This enable to recognise which function is called. The function signature is also defined as the function selector.

The contract ABI specification in the Solidity documentation provides an extended explanation:

The first four bytes of the call data for a function call specifies the function to be called. It is the first (left, high-order in big-endian) four bytes of the Keccak-256 (SHA-3) hash of the signature of the function.

The signature is defined as the canonical expression of the basic prototype without data location specifier, i.e. the function name with the parenthesised list of parameter types. Parameter types are split by a single comma — no spaces are used.

How is the function signature determined?

The signature of a function is determined by hashing with keccak256 :

  1. the function’s name,
  2. the parameters types (the types only, not the parameter names).

Only the parameters are part of the signature, not the returned values.

function signature = first four bytes of the hash returned

NB: Bear in mind that if you use uint as a function parameter, like withdraw(uint amount) , the formula will be:

Basic example

Let’s take a look at what the hash looks like. with a simple example. We will use the function balanceOf() from the Open Zeppelin ERC20 token contract.

As we said, the name of the parameters are not taken into account. So the keccak256 is applied to what is in bold (don’t forget the closing parentheses!). The formula will be:

Here is the hash obtained below, with the function selector in bold.

Example with multiple parameters

Here is an example with the following function:

The part that we are going to hash will be :

We then apply the keccak256 hash…

…and obtain the following hash.

Again, we keep the first 4 bytes only. In our example, our function signature is 0xe0b6fcfc .

Getting a function signature in Solidity

It exists a shortcut in Solidity to retrieve the signature of a function. Consider the following contract with a unique function

You can get the function signature of the f() by using this.f.selector in another function. selector is a method that returns a value of bytes4.

Free Bitcoins: FreeBitcoin | BonusBitcoin

Coins Kaufen: Bitcoin.deAnycoinDirektCoinbaseCoinMama (mit Kreditkarte)Paxfull

Handelsplätze / Börsen: Bitcoin.de | KuCoinBinanceBitMexBitpandaeToro

Lending / Zinsen erhalten: Celsius NetworkCoinlend (Bot)

Cloud Mining: HashflareGenesis MiningIQ Mining

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close