Questions tagged [mapping]
A type in solidity that allows access to arbitrary elements via a key, similar to a dictionary or hashmap in other languages.
526
questions
1
vote
1
answer
20
views
Is arrays a hash tables in Solidity?
I have one theoretic question. I know that mapping in Solidity is representation of hash tables, where we have hashed key and value stored to this key.
Also arrays in EVM stores values in keccak256(...
0
votes
1
answer
52
views
find which mapping and slot a key belongs to
How is it possible to know which mapping a key belongs to?
For storage mappings how is it possible to know if a key belongs to a mapping or another?
For example, there are 2 mappings on the contract (...
2
votes
1
answer
26
views
How to traverse mapping?
I have a solidity mapping like:
mapping(bytes32 => SomeStruct) myMap;
whose size dynamically increases.
Can I traverse myMap at first with keys and also without keys just with indexes, like we do ...
1
vote
2
answers
30
views
How can I byte pack these datastructures in solidity to be more efficient?
I have a bunch of mappings that I want to optimize in my Solidity smart contract, is there a way that I can use byte-packing to reduce the size needed to store all the data in these mappings?
...
5
votes
4
answers
131
views
Does it make sense to remove unused entries from contract mapping in 2024
im wondering if it still makes sense to remove entries from mappings in ethereum contract. It seems like initially there was a relatively big benefit of cleaning up the storage but from what i read ...
1
vote
1
answer
88
views
Inline assembly, how to get string from struct in mapping with Yul?
I don't get how to get next element in struct.
I can take id value, but what to do next?
i assume, i need to add 1 to slot, to get string value
but what it return, reference to the string? and what i ...
1
vote
1
answer
26
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 => ...
0
votes
1
answer
24
views
What could go wrong with saving data on mapping for the sake of UI use?
I have a Token contract and I want to record the token price once per day.
So I was thinking that to cut the trouble of going trough creating a server to save the data or using a service API I would ...
0
votes
1
answer
68
views
What's the cheapest way to store a large amount of data into a smart contract?
I'm working on a smart contract in which we need to store some addresses along with their pending rewards periodically,
so the most first approach is to have a mapping of addresses to an int, like ...
0
votes
1
answer
116
views
how to access to a public mapping from test script
i'm in hardhat
i'v this public mapping
mapping (address user => uint[] commentIds) public userComments;
and i'm trying to call it inside my test Script
console.log(`user1 comments: ${...
0
votes
0
answers
13
views
Can I delete the Values in the mappings using delete keyword [duplicate]
mapping(IERC => contract) public testing;
delete testing[IERC];
In the above code snippet I want to delete contract type (value) using the delete keyword can it be possible?
0
votes
1
answer
8
views
How to get the array index by a perticular condition
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract final_year_project {
struct hospital_record {
address patient;
string hospital;
string docter;
...
0
votes
1
answer
19
views
How much would be too much data to save in a public mapping?
So I know that in solidity for larger data we wouldn't use contract to save the data but rather we'd use some kind of external database.
But I'm not sure how large is too large and what would happen ...
0
votes
1
answer
42
views
What is the best gas efficient way to reset all values for keys in a mapping?
As far as I understand there is no way to automatically reset all values of a mapping to default in solidity. now lets say I have a mapping mapping with 7 keys and values set. to refresh all the ...
1
vote
1
answer
99
views
How to create a hash based on struct array types?
I have the following arguments to call a function in a smart contract:
ERC20Reward[] calldata _erc20Rewards
ERC721Reward[] calldata _erc721Rewards
ERC1155Reward[] calldata _erc1155Rewards
...
0
votes
1
answer
56
views
How to identify a specific ERC1155 token instance?
I'm trying to write a game item contract where the player can upgrade the items. For this, I need to be able to clearly distinct the different instances of the same tokenID.
Let me explain, if :
ID=0 -...
1
vote
1
answer
31
views
i have an upgradable smart contract with 3 mappings, i set only the first two in the initialize function
i have an upgradable smart contract with 3 mappings, i set only the first two in the initialize function .
pragma solidity ^0.8.0;
contract ContractA {
mapping(uint256 => address) public ...
1
vote
1
answer
188
views
What are the possible security threats when using mappings in Solidity?
Does the use of a nested mapping possess any threats to the security of the contract below?
contract Users {
struct User {
address payable regAddress;
uint64 timestamp;
...
1
vote
3
answers
58
views
How to access a mapping from another contract with enums
The second contract needs to check the mapping in the first contract to see if the person finished the required course. I checked a few old threads but they said it wasn't possibly with that version ...
0
votes
2
answers
23
views
Representing Price in a Mapping
I want to use a mapping that stores prices for each token in my ERC721 contract but am hitting an issue with the default uint value being 0. I'd like for my getPrice(uint256 tokenId) function to ...
0
votes
1
answer
42
views
Brownie not returning Mapping Values
Good day to everyone, I've been working with brownie for sometime now and have been running into a lot of challenges. I created a struct basically and a mapping to that struct, however when I deploy ...
1
vote
1
answer
28
views
How many strings can i store inside a mapping before the contract fails to work?
I have a single contract that is on the verge of its size limit (One more function and i wont be able to deploy anymore)
Within this contract, there is a mapping that I want to feed with strings after ...
0
votes
0
answers
66
views
Is there any way to get the array length inside a mapping without a getter function or count variable?
mapping(address => uint256[]) public submitterAddressToFormIdsAssigned;
This is a mapping from an address to an array of uint256s.
I want to get the length of array. I used a count variable before ...
2
votes
1
answer
156
views
Storage collision in vyper hashmap?
Can you get a storage collision in vyper?
The HashMap value is stored at the keccack256 of the key.
So wouldn't this mean storage collisions are possible?
0
votes
1
answer
23
views
Why we can't initial a public mapping in a struct?
contract MyContract {
struct Balance {
uint numDeposit;
mapping(address=> uint) public Deposit; //It will give an error due the public keyword
uint numWithdraw;
...
0
votes
2
answers
25
views
Initializing string variables from mapping values doesnt work
I try something like this:
mapping (uint256 =>string) public example;
then i initialze 'example' in the constructor
example[0]="apple";
example[1]="peach";
then i define a ...
1
vote
1
answer
2k
views
Error encoding arguments: Error: types/values length mismatch (count={"types":1,"values":3}
Can you please explain me why it's happening and how I can solve it ?
Error:
call to GameItem.storeWalletAddress errored: Error encoding arguments: Error: types/values length mismatch (count={"...
0
votes
1
answer
190
views
Do keys in a map also packed to storage as well?
If we have this mapping
mapping(uint128 => uint256) private first;
mapping(uint128 => uint256) private second;
Do the keys of uint128 from both first and second gets stored together in one ...
1
vote
1
answer
25
views
Return a reference of map item from a function
Hello Solidity Community,
I have a chunk of code that repeats a lot, wondering if I could make a function or something to shorten it. Similar chunks of code appear a lot as well.
Skin memory skin = ...
1
vote
0
answers
30
views
How do I conduct Presale off chain? Keeping the address and the amount of coins purchased
I'm creating a presale website and a presale coin in solidity. How do I keep tract of the poeple who bought it something like whitelisting right
1
vote
1
answer
28
views
Dynamic update of struct field inside mapping
I have a struct
struct Shots{
uint coverDrive;
uint straightDrive;
uint pull;
uint squareCut;
}
and I used a mapping
mapping(address => Shots) public BattingSkill;
Now I want to ...
2
votes
1
answer
468
views
Gas limit for calling a smart contract function varies greatly upon address list parameter
Consider the following smart contract:
pragma solidity ^0.8.19;
contract Test {
mapping (address => bool) registered;
function test(address[] calldata addresses) external {
...
0
votes
1
answer
17
views
Variable Values do not persist in between function calls
I am new to Solidity, and I am experimenting on smart contracts using Remix-IDE connected to a deployed local Geth instance.
What I am trying to do is to create a struct containing multiple fields for ...
1
vote
1
answer
64
views
Gas cost of mapping operations
Suppose, your contract has something like
if (_balances[account] >= someNumber) {
_balances[account] = _balances[account] - _balances[account]/10;
}
What is the gas cost of this operation? I ...
0
votes
1
answer
99
views
Function isn't returning the uint256 value after initializing mapping
I'm facing a problem with the function return type in solidity. Here is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Birth {
mapping(uint256 => bool) public ...
1
vote
1
answer
293
views
Mapping String to uint
I'm trying to map string to uint but for some reason I'm not able to return the value. However on the demo contract it is working fine. Can somebody help me find the difference?
// SPDX-License-...
0
votes
1
answer
25
views
Which one is gas efficient?
Lets say i want to store some data about each address ,through the mapping.
My first idea is: store in struct this way:
struct user {
address userAddress;
uint256 someUint:
string someString;
...
0
votes
1
answer
76
views
How to find all non zero mapping values?
This question is tightly related to how mappings are stored in the storage. Is there a way to search the storage and find all non-zero values of the mapping?
1
vote
1
answer
77
views
Using an Array to Delete Mappings
BEFORE YOU DOWNVOTE
I know questions on deleting mappings have come up thousands of times but I'd like to pose a question... can you delete it if it's in an array?
the following code allows the caller ...
0
votes
1
answer
105
views
Get mapping value with JSON-RPC → eth_getStorageAt || eth_call
How do I get mapping values from my contract with JSON-RPC (without using web3.js)?
In my contract:
struct Data {
address key;
string value;
}
mapping(bytes32 => Data) public values;
I can't ...
0
votes
1
answer
138
views
cost fee according to the size of a mapping
I try to improve my skills in solidity programming, for that i try to ode a dynamic NFT, which evolve according to 2 variables stored in a mapping.
struct NFTInfo {
uint32 nbOwners;
...
0
votes
2
answers
227
views
Storing address in a mapping
Is this code sufficient to store an address in a mapping?
mapping (address => bool) public registeredAddresses;
function register(address addr) public {
// check if Address hasn't been ...
2
votes
1
answer
97
views
Struct returns indexed numbers and property names
I'm using ethers and hardhat to run my React application. I want to pass an object as a parameter to a Solidity function. The function should map the object (struct) to the id provided as a property ...
0
votes
1
answer
437
views
Retreive a mapping nested inside a struct from Ethers
Hello Ethereum community,
I have couple of structs in my solidity code to organize things. Here is one struct from the code:
struct Staking {
uint total;
uint currentIndex;
mapping(address => ...
1
vote
0
answers
21
views
Why not repurpose mapping as array in solidity?
An array T[] can be implemented as a mapping(uint -> T) with indices acting as key in mapping and storing array length as separate uint.
I want to understand, if it is more gas efficient to use ...
0
votes
1
answer
418
views
ParserError: Source "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol" not found: File outside of allowed directories
I'm trying to compile a "FundMe.sol" contract: the issue is that when I try to compile it I keep getting the following Compiler Error:
Brownie v1.19.2 - Python development framework for ...
0
votes
1
answer
161
views
Copying an array of structs to memory
I have some code that attempts to copy an array to mapping:
// Mapping of user addresses to their payment configuration
mapping(address => PaymentConfiguration[]) internal configurations;
// Event ...
0
votes
2
answers
38
views
How can I return multiple value?
How can I return multiple value.
If I get string ,it return the last value.
How to get string ,its return multiple value?
pragma solidity ^0.8.0 ;
contract Simple {
mapping(string => uint) ...
1
vote
1
answer
67
views
Unable to reach storage struct via mapping
Gm,
I've got a smart contract for 2D board game purposes. There is a struct Unit
struct Unit
{
UnitType unitType;
uint16 x;
uint16 y;
int8 hp;
int8 dmg;
...
0
votes
2
answers
30
views
Updating a struct property in an array stored as mapping value
This is my data structure for storing a list of questions which will be different for every address
struct Question{
uint16 id;
string question_string;
string answer;
}
and this is stored ...