Hot answers tagged

75

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 { string greeting; function UseConstant() public { greeting = "Hello"; } function SayHello() public constant returns(string says) { ...


18

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 constant after solidity 0.4.17 release : view and pure convey the meanings of their usage. view can be considered as the subset of constant that will read the ...


16

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 just querying the blockchain for its current state and nothing will be changed. But they do cost gas if called internally by another function. This means ...


12

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 for running your code. It doesn't matter that this particular function doesn't update state; it still takes resources to execute, so it costs gas.


5

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 function can strongly suggest this mode so it is the default process from a client perspective, by marking the function view or pure. view reads the state, such ...


5

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 is not involved. No fee needs to be paid because the miner didn't do anything at all.


5

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 and does not change any blockchain state. It's actually up to the client to decide which to do, but if a function is marked view, the default behavior of most ...


5

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) && (z >= y)); return z; } No state is being read here. Whereas String something; function tellMeSomething() returns (string whatever){ ...


4

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 blockchain) This is what you are used to. Second, if a function simply reads from storage, but doesn't write anything into storage ex: getTokenBalance(John), this ...


4

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; myMap[2] = 201; } That gives you a simple view function (myMap(uint) public view returns(uint)) that returns the number stored at an index. You can also make ...


4

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 interactions with any contract. This means such functions cannot use SSTORE, cannot send or receive ether and can only call other view or pure functions. the ...


3

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


3

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 the other side, if the transaction is sent to the network (as in your case) pure and view functions will consume gas. Hope this helps.


2

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 think they are. A pure function is a function of which the output is entirely dependent on the input parameters. They can never read from, nor store data to the ...


2

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, 19, 5, 4, 36, 6, 6, 5, 5, 5, 46, 4, 4]; return x[i] + x[j] * 12; } }


2

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"000804"; function test(uint8 key) external pure returns (byte) { return TEST_MAP[uint256(key)]; }


1

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 and view functions. If you are calling your own local node, then you can trust that the answer it gives is correct since you did not modify the original open ...


1

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 declared pure in which case they promise not to read from or modify the state." The important part here is that these functions types will not modify the ...


1

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 allocation per block, so a transaction can never exceed available gas allocated for one full block


1

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 by a contract. it is executed by others, the transaction may be recorded on blockchain.


1

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-transaction-costs A more detailed gas cost analysis based on what kind of operation it is: https://docs.google.com/spreadsheets/d/1m89CVujrQe5LAFJ8-...


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