Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [state-variable]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
14 views

Solidity Value Assignment

Is there any way to assign a solidity variables value to current ether price and if it changes value will to?
seojunchian's user avatar
0 votes
2 answers
21 views

When should I choose lower-size bytes variables (e.g. bytes23) over bytes32 variables?

I am trying to manage strings on a smart contract. I know these strings will be 46 bytes long so I thought I could convert them into bytes and split them in two bytes32 arrays. Then I realized smaller ...
Pietro D'Amico's user avatar
1 vote
1 answer
25 views

Populate mapping in global scope returns error

I'm confused about mappings. I always used them by populating them through functions. Now I need a simple mapping with a handful of items and wanted to populate it like this: mapping(uint8 => ...
user130657's user avatar
1 vote
0 answers
43 views

Extract read/write set of state variables from a smart contract

Is there any way that I can extract read and write set of Ethereum state from a smart contract? To be specific, I want to figure out what are read and write functions from an Ethereum state. My one ...
SourKey's user avatar
  • 11
0 votes
1 answer
127 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
  • 918
0 votes
0 answers
46 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
27 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
22 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
64 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
668 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
729 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
150 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
  • 170
0 votes
0 answers
20 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
  • 368
0 votes
1 answer
716 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
84 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
54 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
666 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
  • 339
1 vote
1 answer
90 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
497 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
50 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
571 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
61 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
13 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
1k 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,703
1 vote
1 answer
474 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
292 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,703
0 votes
2 answers
506 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
385 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
185 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
  • 918
14 votes
5 answers
32k 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
694 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
90 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
78 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
459 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
69 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
843 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
237 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
  • 663
0 votes
2 answers
186 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
669 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
46 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,720
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
848 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
241 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
655 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
338 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
  • 419
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