Questions tagged [state-variable]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
46 views

Find slot of private variable

How do I find the slot of a private variable? I know that Foundry has the method stdstorage.sig(), but that only works for variables with public getters, so private ones are out of scope. Thanks!
dNyrM's user avatar
  • 711
0 votes
0 answers
16 views

preventSwapBefore token contracts

I'm looking for a way to determine to determine tokens' contract private state variables such as _preventSwapBefore, or _reduceTaxAt, etc... Theese variables or often in token's contract, but in ...
Newly Sama's user avatar
0 votes
1 answer
24 views

Solidity variable for contract scope (not state variables)?

I use state variables for the interaction of calls (fallback) between contracts. But copying and clearing state variables uses a lot of gas. Perhaps there is a better practice, how to have temporary ...
Andriy's user avatar
  • 1
0 votes
1 answer
15 views

Expected identifier but got ';' ? I am writing a poker game in Solidity and having problems declaring sortedCards as a state variable. Help:)

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract NoLimitPoker { address public dealer; address public smallBlind; address public bigBlind; uint public minBuyIn; ...
Etherbase's user avatar
0 votes
2 answers
50 views

Enhancing testability of contracts with immutable state variables

I have been encountering challenges when testing contracts that utilize immutable state variables. As we know, immutable variables are assigned only once during contract deployment and cannot be ...
Ahmed Ihsan Tawfeeq's user avatar
1 vote
1 answer
475 views

Are immutable variables part of the contract storage?

Suppose that you define an immutable variable like this: contract Contract { uint256 public immutable foo; uint256 public bar; constructor() { foo = 50; } Will bar be the ...
Paul Razvan Berg's user avatar
1 vote
1 answer
531 views

Error: invalid BigNumber value (argument="value", value=null, code=INVALID_ARGUMENT, version=bignumber/5.7.0)

Within my front end part, I am using Wagmi's usePrepareContractWrite and useContractWrite hooks to execute a function of my smart contract. The function demands a uint argument. Therefore my '...
Sina Rahimi's user avatar
0 votes
1 answer
89 views

How variable work when I predefined a private variable from inheritanced Contract?

I have see a question here : link . Where someone have some problem with ERC0 _burn(), to the point I see that He redefined 2 variable _totalSupply, _balances in his contract to use : even though ...
rived's user avatar
  • 160
0 votes
0 answers
18 views

string storage vs memory

i have a function in my smart contract which needs a string to work, and for this i have two options : OPTION 1 : Store my string in a state variable : contract A { string public immutable ...
noro meb's user avatar
  • 328
0 votes
1 answer
463 views

setState is not working

This is a function where I try to pin an NFT metadata on IPFS and then store the URL in a state variable called tokenUrl. Once the pinning process is done, I need my related state variable to be ...
Sina Rahimi's user avatar
1 vote
1 answer
55 views

If blockchain is immutable and the data once stored on blockchain can not be changed, then how can we change the state variable in solidity?

I am new to blockchain. I have some idea about blockchain and solidity. Smart contract get stored on blockchain as byte code. And the state variables we create also stored in blockchain. How is it ...
Mayank Patidar's user avatar
0 votes
2 answers
44 views

I'm new to Solidity. I'm trying to create a getter function for an array which should return type address, but I'm getting the attached error

contract StorageFactory { SimpleStorage[] public simpleStorageArray; function createSimpleStorageContract() public { SimpleStorage simpleStorage = new SimpleStorage(); ...
Abishek Murali's user avatar
2 votes
1 answer
467 views

Why and When To Use uint8 instead of uint?

As far as I know uint8 costs more gas than uint256 (or uint they are same). Let's say my variable can't be bigger than 100, and I already check for it with an if statement. I saw many examples of ...
Yetik's user avatar
  • 314
1 vote
1 answer
81 views

Regarding storage and memory key word

Recently I was going through https://docs.soliditylang.org/en/v0.8.13/types.html#data-location-and-assignment-behaviour which explains the data locations and its behaviour.. there in the given code ...
CaptPython's user avatar
1 vote
0 answers
448 views

Retrieve Storage Variable Name given Storage Location and Contract Source

Is there any easy way to get the storage variable names provided you have the source code and the storage locations of the variables for a contract? For example, for the following contract I can use ...
Travis Carpenter's user avatar
0 votes
1 answer
44 views

Solidity: How can I access a variable dynamically by name?

Is there a way to access variables in solidity using another variable? Something like this: contract Test { uint age; string varAge = "age"; function setAge() public { //...
Savvy Sage's user avatar
0 votes
1 answer
519 views

How do I change smart contract state with web3.js?

I am using web3.js to interact with a smart contract. I can read values, but I can't change the state with setting functions in the contract. contract MyContract { uint number; function ...
EKN's user avatar
  • 31
0 votes
0 answers
59 views

The state of the contract does not change

I deployed the contract on my private geth network for testing. pragma solidity ^0.4.26; contract MyContract { mapping (address => uint) private balances; function depositFunds() public ...
thomascoopm1337's user avatar
0 votes
1 answer
12 views

What happens when assigning an equal value to an existing variable?

Basically the question is what happens if you try to assign the same value to the(state) variable: address someAddress = 0x...abcdef; function setSomeAddress(address _input) public { //...
smenir443's user avatar
  • 125
2 votes
2 answers
866 views

What are the gas costs for type casting or conversions?

I frequently cast unsigned integer types (e.g. uint8) to signed integer types (e.g. int24), this requires 2 casts first to the unsigned type (e.g. uint24) then to the signed type (int24). I know I ...
MShakeG's user avatar
  • 1,418
1 vote
1 answer
388 views

State variable not updated after DelegateCall

I deployed two contracts 'WalletLibrary' and 'Wallet': most of the functions in Wallet delegateCall() the functions from WalletLibrary. I want to recreate an Attack, where I call the initWallet() ...
nicollos_'s user avatar
0 votes
1 answer
273 views

What is the most gas efficient way to read and write to global contract state?

Say I have a complex(list, mapping or struct) global/contract variable. What is the most gas efficient way to update that variable in a function that changes the value of one or more indexes, values ...
MShakeG's user avatar
  • 1,418
0 votes
2 answers
455 views

Better way to retrieve the values of all public state variables of a Solidity contract

My solidity "0.7.0" contract pragma solidity ^0.7.0; contract DoubleEscrowTrade { uint256 private locked = 2; //2: unlocked, 1: locked. uint256 is gas efficient than bool uint256 ...
user938363's user avatar
0 votes
1 answer
381 views

Initialize state variables vs constructor [duplicate]

What is the purpose of Constructor if I can initialize the state variables directly. contract ConZ { address public owner = msg.sender; uint public x = 123; } _ contract Conx { address ...
trunks's user avatar
  • 1
0 votes
1 answer
169 views

Add new variable to defined struct

Is it possible to create a new variable/key -with its corresponding value- for an already defined Struct? Kind of like objects in JS. struct MyStruct { //defined elsewhere in SomeLib uint num; ...
dNyrM's user avatar
  • 711
12 votes
5 answers
29k views

Why is an 'UNPREDICTABLE_GAS_LIMIT' error thrown when calling a view function created from a public variable?

I am using the Hardhat console to test my smart contract, which worked flawlessly until I removed the getter functions I wrote for my public state variables in favor of the automatically generated ...
josephawallace's user avatar
0 votes
1 answer
606 views

ERC20 token, override functions but state variable do not update as expected

I am trying to implement and test an ERC20 token by inheriting from ERC20.sol from OpenZeppelin. I want most of the functions to be by default from OpenZeppelin but I also want to overrride some of ...
katerith's user avatar
1 vote
0 answers
84 views

Read smart contract data from blockchain itself

I'm trying to figure out if it's possible to read smart contract data from the blockchain itself. I know in ordinary circumstances, smart contracts sit on top of the chain in the ledger. I'd like to ...
pdmoerman's user avatar
  • 131
0 votes
0 answers
76 views

State (Dynamic) Array Returning Null (using Web.js)

I am trying to return a dynamic state array full of hashes, using Web.js. Currently the function on the Web3.js side is returning null in the console.log. Here's my Contract: pragma solidity >=0.4....
Chase Denson's user avatar
1 vote
1 answer
431 views

Shadowing Inherited State Variables

I was editing and trying out the code from https://solidity-by-example.org/shadowing-inherited-state-variables and got the following output. pragma solidity >=0.8.0 <0.9.0; //SPDX-License-...
SomeEarthDev's user avatar
0 votes
0 answers
59 views

Race condition when trying to read state after transaction

I am using web3.js library to interact with a smart contract, and am having issues with a race condition and not sure how to fix it. I am sending a transaction to the chain, and then waiting for the ...
pdmoerman's user avatar
  • 131
1 vote
1 answer
791 views

Where is the proper place to assign variables in Solidity?

uint x = 6; uint y; constructor(uint _y) { y = _y; } If the value of x is known beforehand, is it better to assign its value when it is declared or use the constructor (as with y)? The docs ...
imissjerrygarcia's user avatar
0 votes
1 answer
224 views

wrapping state variables in struct

I asked this question in OZ forum and I'm asking it here again. The question is Why a state variable is wrapped in a struct in their Counters library. Please see the question in that post. Thanks.
Maxareo's user avatar
  • 633
0 votes
2 answers
169 views

Does it matter if state variables are assigned value in line or in the constructor?

For example, is there a difference between these two snippets of code, or is there a best practice? From my experimentation, it doesn't seem like it makes a difference. Thanks. contract A { uint ...
Brandon's user avatar
  • 31
1 vote
0 answers
663 views

The data type of the return value of the solidity function has changed

As you can see, I have designed a struct in the solidity.There is a function in the solidity with the return value type User mapping(address => User) public _userInfoMap; struct User{ uint ...
wei wang's user avatar
  • 437
1 vote
1 answer
1k views

How to protect your state variables from getting modified?

from what I understood, any state variables that are not private can be called and modified by any contract that inherets the parent contract. it usually takes more than one contract to develope a ...
Amin's user avatar
  • 76
0 votes
0 answers
45 views

Does delegatecall() use only the calling function?

Let me explain with the code: contract A { function f() public { [...] g(); } function g() private { [...] } } contract B { A a; function delegateA() public { (bool success, ) = address(...
slimad's user avatar
  • 1
2 votes
1 answer
37 views

Difference in cost between external contract as state variable vs. instantiation of contract?

For pragma solidity ^0.6.0; interface ExternalContract { function initialize() external; } contract Test { ExternalContract c; constructor(address externalContractAddress) public { ...
Joël's user avatar
  • 1,710
3 votes
1 answer
1k views

Gas cost of setting a state-variable to the same value

According to this spec, setting a state-variable to the same value consumes 200 gas. However, I conducted a test over ganache-core v2.10.2, and the result was closer to 800 gas: Solidity Contract: ...
goodvibration's user avatar
2 votes
1 answer
6k views

Member "push" not found or not visible after argument-dependent lookup in address[] storage ref

I am failing to use the push method against a dynamic array declared as a state variable. How can I do this? I declare a dynamic array of addresses as a state variable like below: contract Sample { ...
sflow's user avatar
  • 373
7 votes
2 answers
826 views

How to call a method after a specific tx in a block

With web3, it is possible to call a method of a smart contract at a specific block : myContract.methods.methodName().call(transactionObject, blockNumber, callback) Is there a way to call it after a ...
Xavier59's user avatar
  • 532
1 vote
1 answer
237 views

How to find value of non-public state variable

I understand that public state variables automatically creates a getter function when deployed, its value can be accessed via the getter function. However, if a state variable is not public, how can ...
Huadong Feng's user avatar
3 votes
2 answers
2k views

Getter function for public variables

Many token contracts have unrestricted getter functions for state variables, which seems redundant (eg, see OpenZepplin's ERC20 contract). For example, a contract could have it this way: uint private ...
Eric Falkenstein's user avatar
0 votes
1 answer
629 views

Accessing dynamic variable name - external contract

Contract A wants to call an external contract B/C/D/E/F but the variable name is different within each contract B/C/D/E/F, and contract A does not know the variable name for B/C/D/E/F at the start. ---...
Decc9a881's user avatar
0 votes
1 answer
327 views

What if millions of records to be fetched from blockchain?

This is just a hypothetical scenario. Let's suppose someone saved million of information in blockchain with huge gas consumption so far. As for reading data in ethereum smart-contract cost no gas. ...
NinjaMAN's user avatar
  • 409
0 votes
0 answers
18 views

The code is not returning the value of x? [duplicate]

pragma solidity >=0.5.0 <0.7.0; contract Example{ uint256 public x=3; function setArr(uint256 y) public returns(uint256){ x=y; return getArr(x); } function getArr(...
user61878's user avatar
1 vote
1 answer
477 views

State Variables: What are they and how to create them?

I am new to ethereum and Solidity. I thought state variables are supposed to retain their value between calls. In the Solidity documentation, in the section State Variables, it says that state ...
Zephyrus's user avatar
  • 350
0 votes
0 answers
120 views

if "address public x = tx.origin", why does "require (tx.origin == x") work and "require (tx.origin==tx.origin) not

Below is a contract that i have commented on, hoping to understand the logic of the arguments. Below in bold is the question in question. The .sol currently works fine. pragma solidity ^0.5.11; ...
rubecuber's user avatar
1 vote
1 answer
100 views

Receiving error while getting state variable value after using selfdestruct method "Error: Returned values aren't valid, did it run Out of Gas?"

I am using selfdestruct method and passing the payable address to it but receiving the following error when trying to get the value of a state variable : "Error: Returned values aren't valid, did it ...
Shubham Pal's user avatar
23 votes
2 answers
15k views

What is the immutable keyword in Solidity?

What is the immutable keyword in Solidity and how do I use it?
Shane Fontaine's user avatar