DEV Community

Murat Can Yüksel
Murat Can Yüksel

Posted on

Write and view functions in Solidity

As a frontend developer building dApps on Ethereum, you need to know the difference between "view" and "write" functions.

When you write a function in Solidity to do something, to change something in the state of the blockchain, that function will cost gas. It will require a fee, real money to be executed.

View functions are the opposite. When a Solidity function has the "view" keyword on it, and does not do any operation that changes the state of the blockchain, it will not cost gas.

Say that you just want to see how much of some token an account has, since that knowledge is public, and you accessing it does not change the state of the blockchain, the function in Solidity would be a view function.

When interacting with Solidity functions using wagmi React hooks, you use "useWriteContract" hook to interact with functions that change the state, like sending a token from one contract to another.

While interacting with view functions, you'd use the "useReadContract" hook. When you compare the two hooks, you'll see that the useWriteContract takes arguments while the useReadContract does not.

Top comments (0)