Questions tagged [constant]
Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant.
35 questions
81
votes
2
answers
32k
views
Solidity: What is the difference between `view` and `constant`?
Solidity 0.4.16 introduced the view and constant function modifiers. The documentation says:
constant for functions: Same as view.
Does this mean view is just an alias for constant? If so, why do ...
51
votes
4
answers
41k
views
When to use "View" and "Pure" in place of "Constant" [duplicate]
According to the solidity 0.4.17 Release Notes
this release finally checks the modifiers view (used to be named
constant) and pure on functions. As a rule of thumb, use view if your
function ...
31
votes
3
answers
11k
views
What is the difference between the constant and immutable keywords in Solidity?
What is the difference between the constant and immutable keywords in Solidity and how do I use them?
6
votes
3
answers
2k
views
Where are the smart contract constants stored?
I want to double-check something.
I had been writing upgradable smart contact that uses a lot of constants.
As far as I can see, smart contracts aren't storing constant variables in their storage.
...
5
votes
2
answers
4k
views
Standard work-around for using a Solidity constant array (which is not supported) in pure function?
I have an array in my contract like this and a pure method which uses it like this (simplified exmaple because my real exmaple is many more lines):
uint[16] constant x = [10, 2, 6, 42, 19, 5, 4, 36, ...
4
votes
1
answer
2k
views
Is it possible to have an immutable or constant struct?
Is it possible to have a struct that is or has fields that are constant or immutable?
// struct with constant or immutable fields:
struct MyStruct {
uint constant number;
int immutable integer;
}
...
4
votes
1
answer
2k
views
Overwriting of constant variables by inheritance: Why are parent variables only overwritten in functions of the child?
Let's assume I have the following two contracts Hello and Goodbye, and Goodbye inherits from Hello. Both declare a constant variable aString, i.e. the child contract Goodbye overwrites the parent's ...
3
votes
1
answer
440
views
Why are functions that access immutable variables not pure?
My understanding is that immutable variables are basically equivalent to constant variables however the former is initialised in the constructor whereas the later at declaration, however both are ...
2
votes
1
answer
964
views
Why transaction cost for same function differs even though content of function is same?
I have written following contracts to test the gas used by a simple function depends on what parameters. As described here the four use cases, each gives different transaction cost even though content ...
2
votes
2
answers
343
views
Why does balanceof() return a constant?
The ERC20 token standard specifies balanceof() to return a constant.
function balanceOf(address _owner) constant returns (uint256 balance)
But the balance amount might change or am I getting ...
2
votes
2
answers
501
views
Solidity immutable equivalent in huff
In the huff language, I am able to specify a constant variable with the constant keyword. (huff docs)
#define constant NUM = 0x420
#define constant HELLO_WORLD = 0x48656c6c6f2c20576f726c6421
#define ...
2
votes
3
answers
155
views
What other constants depend on the current call like msg.sender?
I recently noticed that msg.sender isn't always the original caller's address (see here). It's the address of the current call. So if foo() calls bar(), then msg.sender in bar() will be the address of ...
2
votes
1
answer
3k
views
Is the gas cost for constant and immutable about equal?
According to this question the Solidity 0.5.0 Compiler cannot compute a constant state variable that invokes a function, however I'm also experiencing this issue on Solidity 0.8.10 using the ...
2
votes
1
answer
316
views
How to get constants value by using Contract name
for example:
contract A {
uint constant public START_TIME = 10;
}
import "./A.sol"
contract B {
function test() public {
uint startTime = A.START_TIME;
}
}
If I only need the ...
2
votes
2
answers
173
views
expected { but found constant
My code is:
function equity_in_Hadcoins(address investor) external constant returns (uint) {
return equity_Hadcoins[investor];
}
// getting the equity in USD of an investor
function equity_in_usd(...
2
votes
0
answers
175
views
Support FACTORY_ADDRESS and INIT_CODE_HASH in different forks of UniswapV2
I'm trying to parse transactions of different UniswapV2 like DEXes and have problems adding new forks of UniswapV2 to my project, such as SushiSwap, etc.
I'm using @uniswap/v2-sdk for transactions of ...
2
votes
0
answers
28
views
Why No Constant Linking?
Similarly to library linking
https://docs.soliditylang.org/en/latest/using-the-compiler.html?highlight=compiler%20options#library-linking
why is there no constant linking for the compiler?
The only ...
2
votes
1
answer
263
views
Any way to do compile-time computation using rational_const and a constant value?
Of the following three declarations
ufixed constant TEST1 = 256;
uint constant TEST2 = 256;
uint[] testTable1 = [uint(0.001 * TEST1), uint(0.01 * TEST1), uint(1.1 * TEST1)];
uint[] testTable2 = [uint(...
1
vote
2
answers
1k
views
Is it possible to read constants from external contracts in Solidity?
Say you have a contract defined like this:
pragam solidity >=0.8.0;
contract Foo {
uint256 public constant MY_CONSTANT = 6174;
}
If I just import Foo in another contract, without inheriting ...
1
vote
1
answer
504
views
Are constant state variables cached in the EVM?
From the docs:
constant for state variables: Disallows assignment (except
initialisation), does not occupy storage slot.
Does this mean that the EVM caches constants across all contracts using ...
1
vote
1
answer
940
views
How to define a constant `bytes32` in Solidity v0.8 and above?
I want to define a constant bytes32 like this:
bytes32 internal constant FOO = bytes32(0x01);
But I'm getting the following error when compiling the code above:
Explicit type conversion not allowed ...
1
vote
0
answers
86
views
How is the derivation of the Constant Product AMM formula of Uniswap?
I was researching about Uniswap v1 and I found that the code is using the following formula to get the amount out of a swap:
Formula:
Reference to code
Everyone says that this formula is derived of ...
1
vote
0
answers
33
views
Does Solidity inline mathematical calculations that involve only constants?
Take the following code:
pragma solidity >=0.8.19 <0.9.0;
contract Foo {
uint256 public constant SIZE = 10;
function func(uint256 x) external pure returns (uint256) {
uint256 y ...
1
vote
2
answers
197
views
Inconsistent Result for Web3 Constant Method Call
Deployed a local private Ethereum environment. While trying to invoke a constant method of a solidity contract getting inconsistent results.
Sometimes the result is returned and mostly not.
When I ...
1
vote
1
answer
1k
views
How to properly call getter methods from a specific keypair in web3
In some of my getter methods I'm using msg.sender to deal with access rights. However this just gave me errors, because every single call just comes from the first account of web3.eth.accounts. My ...
1
vote
1
answer
435
views
Constant function returns empty array + web3js
I have a simple solidity script where I tested in remix (injected Web3 using geth local environment) and worked fine returning an array of data from a constant method.
pragma solidity ^0.4.11;
...
0
votes
1
answer
490
views
Is it possible to change a constant variable value when using upgradeable pattern and deploying a new version?
The title is pretty self explanatory.
Let's say I have a contract where I defined a constant variable and this contract is using some upgradeable pattern. When I deploy a new version of the contract, ...
0
votes
2
answers
685
views
Set and change constant variables in hardhat tests
I have a constant variable in my solidity:
address private constant MY_ADDRESS = 0x000000000
However, MY_ADDRESS may change for my tests, but in my contract I want to keep it as a constant.
How do I ...
0
votes
1
answer
722
views
How to transfer custom ERC-20 token with Ethereum in one transaction
I would like to send custom ERC-20 token with Ethereum in one transaction.
I know how to send both of them alone...
How to do it it one transaction?
SEND CUSTOM ERC-20 TOKEN (There USDC):
<script ...
0
votes
1
answer
222
views
Memory Efficiency of Constant Variables
Say I have a contract with several functions that apply various particular constants.
circumference = 628318 * radius / 1e5;
dividend = balance * 123 / 1e4;
It would be easier for readers to ...
0
votes
2
answers
452
views
Remix shows values from getters only declaring "constant"
pragma solidity ^0.4.0;
contract MyFirstContract {
uint private age;
function setAge(uint newAge) {
age = newAge;
}
function getAge() returns (uint) {
return age;
...
0
votes
1
answer
117
views
How to return a constant variable in huff
Let's say I have a constant variable in huff:
#define constant myAddress = 0x0000000000000000000000000000000000000000
How would I make a macro to read it? Since it doesn't have a storage slot, I ...
0
votes
1
answer
431
views
Solidity: Error with declaring a constant variable
I getting an error while declaring a conatant variable, my code is given below:
pragma solidity ^0.5.1;
contract Governmental{
function lendGovernmentMoney ( address buddy ) public returns ( bool ...
0
votes
2
answers
778
views
Remix warning - Is constant but potentially should not be.
I am getting the warning on the code shown below. Could you please assist me know to resolve this warning ?
warning message - "Is constant but potentially should not be"
0
votes
1
answer
58
views
Initiate a transaction from within a constant function
Good day,
I know that using a constant function means that no change will be done on the blockchain, but I'd like to have a smart contract that verifies the user input WITHOUT exposing what the input ...