Questions tagged [state-variable]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
16 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 { //...
user avatar
0 votes
1 answer
131 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 ...
user avatar
  • 11
0 votes
0 answers
31 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 ...
user avatar
0 votes
1 answer
9 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 { //...
user avatar
0 votes
0 answers
159 views

What is a valid regular expression for a solidity variable or function?

Say I have a function or variable(either one doesn't really matter since functions and variables can have the same names) called foo, how do I test using a regular expression that this function/...
user avatar
  • 230
0 votes
1 answer
213 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 ...
user avatar
  • 230
0 votes
1 answer
87 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() ...
user avatar
0 votes
1 answer
56 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 ...
user avatar
  • 230
0 votes
2 answers
117 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 ...
user avatar
0 votes
1 answer
134 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 ...
user avatar
  • 1
0 votes
1 answer
25 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; ...
user avatar
  • 307
7 votes
3 answers
10k 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 ...
user avatar
0 votes
1 answer
311 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 ...
user avatar
0 votes
0 answers
107 views

can I change loop logic to an alternative logic in this staking contract?

I built a staking contract that allows users to enroll in auto compounding through a function that is basically adding the user address to an array UserList Now for auto compounding I have these ...
user avatar
1 vote
0 answers
45 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 ...
user avatar
0 votes
0 answers
49 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....
user avatar
1 vote
1 answer
157 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-...
user avatar
0 votes
0 answers
32 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 ...
user avatar
1 vote
1 answer
482 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 ...
user avatar
0 votes
1 answer
55 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.
user avatar
  • 439
0 votes
1 answer
48 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 ...
user avatar
  • 11
0 votes
0 answers
528 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 ...
user avatar
  • 387
1 vote
1 answer
682 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 ...
user avatar
  • 76
0 votes
0 answers
40 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(...
user avatar
  • 1
2 votes
1 answer
27 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 { ...
user avatar
  • 1,573
2 votes
1 answer
526 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: ...
user avatar
2 votes
1 answer
4k 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 { ...
user avatar
  • 353
6 votes
2 answers
671 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 ...
user avatar
  • 512
1 vote
1 answer
166 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 ...
user avatar
2 votes
2 answers
1k 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 ...
user avatar
0 votes
1 answer
371 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. ---...
user avatar
0 votes
1 answer
209 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. ...
user avatar
  • 359
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(...
user avatar
1 vote
1 answer
299 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 ...
user avatar
  • 190
0 votes
0 answers
75 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; ...
user avatar
1 vote
1 answer
85 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 ...
user avatar
18 votes
2 answers
10k views

What is the immutable keyword in Solidity?

What is the immutable keyword in Solidity and how do I use it?
user avatar
0 votes
3 answers
237 views

Length of state array is not changeable in Solidity

Consider there are 3 contracts: BankA, customer, and BankB.BankA transfers some ethers to customer and after that some customer's state variables must be changed (in a better words be updated). It ...
user avatar
  • 375
0 votes
1 answer
59 views

How Solidity field variable update is happening?

I am curious to know about, how the updation of field variables in solidity is happening ?. Whether world state is maintaining for each variable in the contract as like account balance?. And one more ...
user avatar
1 vote
1 answer
300 views

Why OpenZepplin always use private visibility for state variables?

Why OpenZepplin always use private visibility with getter functions for its state variables? I can't find a reason why they are not using public, is there any potential security or gas cost difference ...
user avatar
  • 193
1 vote
0 answers
53 views

Retrieving a State_object value on go-ethereum

I have a deployed contract address and I need to query a contract storage value, a token account balance that is stored on a mapping variable: mapping (address => uint256) balances; From go-...
user avatar
0 votes
1 answer
2k views

Why string variable must be memory from solidity v5.0?

May someone explain me why exactly string variable must be memory from solidity v5.0 and what does tha mean? Thanks for your help.
user avatar
-1 votes
2 answers
451 views

Truffle uint test script not updating storage variable, and strange behaviour

while testing I got a strange behavior of truffle, please see the subject smart contract code first uint256 public burningRatePercent = 50; function onlyOwnerSetBurningRate(uint256 ...
user avatar
1 vote
2 answers
508 views

Accessing private variables using assembly

I would like program an oracle service which I would like to charge a fee for. Therefore, I would store the data of interest as a private variable in my oracle contract. This private variable should ...
user avatar
  • 21
2 votes
1 answer
94 views

defining storage literals in Solidity

I am trying to write unit tests on functions that take in a storage reference as parameters. There is not much great documentation around how to create literals for these types. These include: address ...
user avatar
  • 125
2 votes
2 answers
898 views

state variable value not changing

I have very simple solidity code as below - pragma solidity ^0.5.6; contract SaveData { uint public salary; function setSalary(uint input) public { salary=input; } function ...
user avatar
1 vote
1 answer
131 views

Does a storage array have a unique address on the block-chain?

The question in the title is a little vague, so I will try to explain here. Suppose I have a contract with a storage array: pragma solidity 0.4.24; contract MyContract { uint256[10] public ...
user avatar
0 votes
1 answer
262 views

dApp Tech Stack

So I'm looking into building a dApp with standard app I functionality like user login/posts/likes/comments etc. Along with harnessing the power of ethereum to support betting and donations. Here is ...
user avatar
5 votes
1 answer
1k views

How State variable are stored and updated?

The Smart contract and the state variables are stored in the blockchain, let us say during deployment of a smart contract the state variable and smart contracts are stored in the block 1, and in ...
user avatar
1 vote
1 answer
333 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 ...
user avatar