Hot answers tagged

78 votes

When to use "View" and "Pure" in place of "Constant"

In earlier versions, we would apply the constant modifier to indicate that a function doesn't change the storage state in any way. For example: pragma solidity 0.4.16; contract UseConstant { ...
user avatar
  • 51.9k
20 votes
Accepted

View/Pure Gas usage - Cost gas if called internally by another function?

As I understand, Pure and View functions don't cost any gas to call if they're called externally from outside In this type of case, there won't be any transaction initiated because this will be like ...
user avatar
18 votes

When to use "View" and "Pure" in place of "Constant"

The constant modifier has the meaning that the function wont modify the contract storage (But the the word constant didn't actually convey the meaning that it is used for). The new replacements for ...
user avatar
12 votes

View/Pure Gas usage - Cost gas if called internally by another function?

As the quote says, they don't cost anything if they're used to just read from your local node. They do cost gas if they're part of a transaction that needs to be mined. Gas compensates the network ...
user avatar
  • 26.7k
5 votes

Smart contract execution without making transaction on ethereum blockchain

Any function can be run in a read-only mode that does not (cannot) change the state and does return return values without waiting for confirmation. There are two ways to do that. In a contract, a ...
user avatar
  • 51.9k
5 votes

Smart contract execution without making transaction on ethereum blockchain

When calling a view or pure function (those that don't change any balance or state variables), there is no on-chain transaction and no fee is paid. All the computation is done client-side. The miner ...
user avatar
  • 9,167
5 votes

What happens if view function calls function that is neither 'view' nor 'pure'?

Functions can be invoked in two different ways: Via a transaction, which is broadcast to the network and changes blockchain state. Via a call, which is performed locally on a single Ethereum network ...
user avatar
  • 26.7k
5 votes

When to use "View" and "Pure" in place of "Constant"

Say you have a function like this one in the SafeMath library: function add(uint256 x, uint256 y) internal pure returns(uint256) { uint256 z = x + y; assert((z >= x) &&...
user avatar
  • 3,282
4 votes

When to use "View" and "Pure" in place of "Constant"

If a function in solidity writes into storage ex: sendTokenTo(John), it will not have one of these labels. (this function writes into storage because it must change someone's token balance on the ...
user avatar
  • 739
4 votes

Pure functions as a replacement of constant mappings?

I don't see a dilemma. You can have a mapping: mapping(uint => uint) public myMap; You can populate some locations in the constructor or elsewhere: constructor() public { myMap[1] = 101; ...
user avatar
  • 51.9k
4 votes

Gas cost call vs send for pure/view functions

There are two statements in https://github.com/ethereum/solidity/issues/992 the keyword view is introduced for functions (it replaces constant). Calling a view cannot alter the behavior of future ...
user avatar
  • 1,712
3 votes

Smart contract execution without making transaction on ethereum blockchain

There is no mining fee paid because no transaction is needed if no changes to the blockchain state trie are made.
user avatar
3 votes

Gas cost call vs send for pure/view functions

Pure and view functions do not consume gas when these are executed in your local node (or the node you are using as local, infura, etherscan, etc.). in such cases the transaction will not be sent. On ...
user avatar
  • 7,885
2 votes
Accepted

Pure function execution flow

we don't want to show this information to anyone else even miner, thus we are planing to use pure function. Maybe you should read the docs on pure functions. I have a feeling they are not what you ...
user avatar
  • 3,966
2 votes

Standard work-around for using a Solidity constant array (which is not supported) in pure function?

I think that this should work. pragma solidity >=0.7.0 <0.9.0; contract MyContract { function foo(uint i, uint j) external pure returns (uint) { uint8[16] memory x = [10, 2, 6, 42, ...
user avatar
  • 3,257
2 votes

Standard work-around for using a Solidity constant array (which is not supported) in pure function?

It seems that is an pending feature https://github.com/ethereum/solidity/issues/1772 However, if the desired range is from 0-255, there is a trick for you. bytes public constant TEST_MAP = hex"...
user avatar
1 vote
Accepted

Who pays for the gas used when reading from storage or calling pure / view functions?

View functions only use gas when they are called by non-view functions inside of the contract. View functions don't consume gas because you are only making a call to your Node, so you're not asking ...
user avatar
1 vote
Accepted

Why are functions that access immutable variables not pure?

Not reading the storage is not the only requisite to be a pure function. Summarizing discussions made on GitHub about this topic, pure implies that the output can be computed without any state, other ...
user avatar
1 vote

Are pure functions and events verifiable/guaranteed

The broader question that you are asking is How can I trust a foreign node with the information that it gives me, such as the result of a web3 call? The issue you are raising is not specific to pure ...
user avatar
  • 3,257
1 vote
Accepted

Are pure functions and events verifiable/guaranteed

Just to get some definitions out of the way: View Functions -- "Functions can be declared view in which case they promise not to modify the state." Pure Functions -- "Functions can be ...
user avatar
  • 223
1 vote
Accepted

What if running a function longs more than block time?

This is not possible, because EVM based blockchains are not Turing complete. Any transaction is eventually terminated by exhausting all of its gas. Transaction maxes out the gas usage as the max gas ...
user avatar
1 vote

Gas cost call vs send for pure/view functions

For calls, gas is charged only when called by a contract. When you call a view function, it is executed locally. the transaction is not broadcast. it is free. But when the view function is called ...
user avatar
1 vote

Gas cost call vs send for pure/view functions

Yes, if your function changes state, it will definitely use gas. Please refer this: http://ethdocs.org/en/latest/contracts-and-transactions/account-types-gas-and-transactions.html#estimating-...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible