TExcelTAD
Home › Solidity › view, pure & payable

view, pure & payable

Besides visibility, every function has a state mutability. It tells the compiler, and everyone reading the ABI, how the function interacts with the blockchain's state.

Mutability May read state May write state May receive ether
pure no no no
view yes no no
(none, "nonpayable") yes yes no
payable yes yes yes

The compiler enforces these rules, and it suggests a stricter mutability when it notices your function could have one.

view functions

A view function reads state but doesn't change it. When called from outside the blockchain (for example by a website using ethers.js), it runs on a single node for free; no transaction is needed.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Thermostat {
    int256 public target = 21;
    int256 public current = 17;

    function difference() external view returns (int256) {
        return target - current;          // reads state: view
    }

    function heatingNeeded() external view returns (bool) {
        return current < target;
    }

    function setCurrent(int256 value) external {
        current = value;                  // writes state: nonpayable
    }
}

contract Main {
    function main() external {
        Thermostat t = new Thermostat();
        console.logInt(t.difference());
        console.log("heating?", t.heatingNeeded());
        t.setCurrent(23);
        console.logInt(t.difference());
        console.log("heating?", t.heatingNeeded());
    }
}
4
heating? true
-2
heating? false

What counts as modifying state: writing state variables, emitting events, creating contracts, sending ether, calling a non-view function, using selfdestruct or certain inline assembly. Any of these inside a view function is a compile error.

pure functions

A pure function doesn't even read state: its result depends only on its arguments. Helpers for math and formatting are typically pure.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    uint256 public taxBps = 2000;   // 20%

    function addTax(uint256 net, uint256 bps) public pure returns (uint256) {
        return net + net * bps / 10_000;
    }

    function addCurrentTax(uint256 net) public view returns (uint256) {
        return addTax(net, taxBps);   // reading taxBps makes this view
    }

    function main() external view {
        console.log("pure:", addTax(1000, 500));
        console.log("view:", addCurrentTax(1000));
    }
}
pure: 1050
view: 1200

If you tried to read taxBps inside addTax, the compiler would stop with "Function declared as pure, but this expression reads from the environment or state".

payable functions

Only a payable function can accept ether. Sending ether to a non-payable function makes the call revert, which protects contracts from accidentally receiving money they can't handle. Inside the function, msg.value is the amount sent in wei.

To attach ether to a call from Solidity, use the {value: …} option:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract PiggyBank {
    uint256 public deposits;

    function deposit() external payable {
        require(msg.value > 0, "send some ether");
        deposits += 1;
        console.log("received", msg.value, "wei");
    }

    function balance() external view returns (uint256) {
        return address(this).balance;
    }
}

contract Main {
    constructor() payable {}   // Main starts with 100 ETH

    function main() external {
        PiggyBank bank = new PiggyBank();
        bank.deposit{value: 1 ether}();
        bank.deposit{value: 0.5 ether}();
        console.log("bank balance:", bank.balance());
        console.log("deposits    :", bank.deposits());
        console.log("Main left   :", address(this).balance);
    }
}
received 1000000000000000000 wei
received 500000000000000000 wei
bank balance: 1500000000000000000
deposits    : 2
Main left   : 98500000000000000000

1 ether and 0.5 ether are ether units: the compiler turns them into 1000000000000000000 and 500000000000000000 wei.

Here is what happens when ether is sent to a function that is not payable:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Strict {
    function ping() external pure returns (string memory) {
        return "pong";
    }
}

contract Main {
    constructor() payable {}

    function main() external {
        Strict s = new Strict();
        console.log(s.ping());
        (bool ok, ) = address(s).call{value: 1 ether}(abi.encodeCall(Strict.ping, ()));
        console.log("call with ether succeeded?", ok);
        console.log("Strict balance:", address(s).balance);
    }
}
pong
call with ether succeeded? false
Strict balance: 0

The compiler doesn't even let you write s.ping{value: 1 ether}() for a non-payable function, so the example uses the low-level call, which reports failure as a false return value instead of reverting. The ether never left Main.

Choosing a mutability

  1. Start with pure. If the function reads state, relax it to view.
  2. If it writes state, drop the keyword (nonpayable).
  3. Add payable only when the function is meant to accept ether.

Being strict is documentation: a front end knows view and pure functions are free to call, and auditors know which functions can move money.

Test yourself

Which mutability fits a function that computes a * b from its arguments only?

  1. payable
  2. view
  3. pure
  4. nonpayable (no keyword)

Which of these is NOT allowed in a view function?

  1. Reading a state variable
  2. Reading block.timestamp
  3. Emitting an event
  4. Calling a pure function

How do you send 2 ether along with a call to vault.deposit() from Solidity?

  1. vault.deposit(2 ether)
  2. vault.deposit{value: 2 ether}()
  3. vault.deposit().value(2)
  4. vault.send(2 ether).deposit()