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.

Filter by
Sorted by
Tagged with
65 votes
2 answers
77k views

How can I figure out if a certain key exists in a mapping, Struct defined inside Library (in Solidity)?

pragma solidity ^0.4.8; library Library { struct data { unit val; } } contract Array{ using Library for Library.data; mapping(address => Library.data) clusterContract; ...
alper's user avatar
  • 8,395
45 votes
3 answers
56k views

How to loop through mapping in solidity?

I have a mapping in my contract like below: mapping (address => uint) public voterCount; To calculate the total votes, I would like to loop through the mapping and sum the uint values. How do I do ...
maheshmurthy's user avatar
  • 1,237
40 votes
1 answer
38k views

How to delete an element from a mapping?

I have a mapping like this: struct data { string name; string nickname; } mapping(address => data) public user; What is the correct way to delete one element from the variable user? ...
Bumblebee's user avatar
  • 1,751
37 votes
4 answers
21k views

Store data in mapping vs. array

I see there're some Solidity examples that are using mapping for storing state, and some are using arrays. For example this example is using mapping: struct Campaign { .... } uint numCampaigns; ...
Igor Artamonov's user avatar
36 votes
2 answers
39k views

Can we get all elements stored in a mapping in the contract

Can we get all elements stored in a mapping in the contract using web3.js ?
takampika's user avatar
  • 385
33 votes
3 answers
24k views

Can I save structs in a Mapping?

Is it possible to store structs in a Mapping or any dynamic memory structure in my Solidity SmartContract?
arodriguezdonaire's user avatar
30 votes
2 answers
9k views

Is there a maximum number of entries for a mapping?

In many contracts, notably voting, there is a mapping of the kind mapping(address => bool) voted; How many voters can be registered in that way? is there a size limit? will the contract need more ...
Micha Roon's user avatar
  • 2,203
24 votes
1 answer
30k views

Mapping with array as Key Or Value data type

Is it possible to do a mapping with an Array as key/value ? Where an array would exist for each value ? I'm actually looking to store structs in a mapping but can't seem to have it work. Struct Foo{...
jayD's user avatar
  • 2,462
23 votes
4 answers
34k views

Solidity - List contains

given address[] wallets. What is the correct method to check that the list contains a given address? Does solidity provide any native list contains function? If not, is the only way to do a full ...
monokh's user avatar
  • 467
21 votes
6 answers
19k views

Solidity Error: Struct containing a (nested) mapping cannot be constructed

My version of solc: "solc": "^0.7.1", When I try to construct a struct that contains mapping, I got this error: "Struct containing a (nested) mapping cannot be constructed&...
Sang-hoon Shin's user avatar
21 votes
2 answers
7k views

What is the zero value for a string?

How to verify that a string, or a struct with only string properties, is initialised or not, inside of a mapping? According to the docs, it means checking that the element of the mapping is at its 0-...
lajarre's user avatar
  • 385
17 votes
2 answers
28k views

Return a mapping in a getAll function

pragma solidity ^0.4.24; contract User { mapping(uint=>address) addresses; uint addressRegistryCount; function set(address userAddress) public { addresses[...
UnexpectedCharacter's user avatar
15 votes
5 answers
24k views

How to delete a mapping?

Fixed question: I have a mapping: mapping (bytes32 => mapping (bytes32 => bytes32[])) items; With some elements in it: items["fruits"]["apples"].push("first apple"); items["fruits"]["apples"]...
Edward Ruchevits's user avatar
15 votes
1 answer
1k views

Struct not being stored in mapping when it contains more than two 'string' properties. Bug?

I am playing around with Solidity, and have encountered an extremely bemusing issue. I want to have a mapping of Address instances (postal addresses NOT the address type) Address is a custom defined ...
Thomas Clowes's user avatar
14 votes
3 answers
8k views

How to create an iterable key->value structure in Solidity?

I am trying to create a framework for users to offer generic goods for sale. For example, a single generic item (a Gizmo) might have 14 people selling it for different prices and from different parts ...
PANDA Stack's user avatar
13 votes
1 answer
16k views

What does address(0) mean? [duplicate]

Referring to this link: https://theethereum.wiki/w/index.php/ERC20_Token_Standard looking at the last contract. (Line 130) function totalSupply() public constant returns (uint) { return ...
s k's user avatar
  • 886
12 votes
1 answer
11k views

Is it possible to have mappings inside mappings?

Is this possible? mapping(address => mapping(uint => customStruct[])) someName Because I cannot debug and find my mistake... is there not enough gas, or it crashes on this code, while trying ...
mrAkax's user avatar
  • 183
12 votes
1 answer
32k views

How to loop through a array of addresses?

I have a mapping and an array that contains a list of addresses of students. When I loop over that array, the method .length is not working. How can I loop through the array Student_list shown below? ...
Mohamed's user avatar
  • 491
11 votes
3 answers
6k views

Internal compiler error: Accessors for mapping with dynamically-sized keys not yet implemented

I have a mapping(string => Person) public map: struct Person { string name; string description; address primaryAddress; string linkToWebsite; string ...
arodriguezdonaire's user avatar
11 votes
5 answers
22k views

How to list ALL Ethereum addresses with a positive balance

Using web3 I can query the balance of any fixed account with web3.eth.getBalance. Assuming I have the entire blockchain downloaded, how can I recover the list of all addresses that have a positive ...
Hooked's user avatar
  • 412
11 votes
3 answers
9k views

Solidity error when encoding arguments to query public mapping

While developing a Smart Contract with Solidity on Remix , after creating the contract I'm trying to query a public mapping: mapping (address => uint256) public balanceOf; but when I call ...
Juan Ignacio Pérez Sacristán's user avatar
11 votes
3 answers
32k views

Could mapping data structure return the number of items it points?

For example following code piece from https://ethereum.stackexchange.com/a/10763/4575 . I have added new lines that is followed by //*. pragma solidity ^0.4.2; contract test { struct my_struct {...
alper's user avatar
  • 8,395
11 votes
1 answer
5k views

Error when using string type as a mapping key

The following code doesn't compile, because compiler needs a mapping key to be of fixed-size type in order to create accessor for a public mapping. I'm using here string type. My strings are 24 byte ...
manidos's user avatar
  • 4,298
11 votes
1 answer
3k views

Does "delete" on mapping really frees the memory space?

I have the following mapping(uint => Foo[]) data structure. I have mapped 10 unique ids with 10 Foo structs, where each Foo is an arraylist of 10 items. Step 1: for(int i = 0; i < 10; i++) ...
alper's user avatar
  • 8,395
10 votes
2 answers
4k views

How to calculate the location index (slot) in storage of a mapping key

I read the documentation and tried some other approaches but it's not working for me. I'm not sure what I'm missing. Let's say I have a contract with a mapping and I would like to know the storage ...
Jeremy Then's user avatar
  • 4,599
10 votes
1 answer
4k views

Iterating over all public values of a mapping in web3

Let's assume we have the standard token contract with balances: mapping (address => uint256) public balanceOf; If I want to know balances of all addresses (who have ever transacted with the ...
Mikko Ohtamaa's user avatar
10 votes
3 answers
5k views

Can I call mapping as arguments of function?

I'd like to call mapping as arguments of function. I've tried the following code, but it returned the error. Could I call mapping as arguments as a function? mapping ( uint256 => mapping (bytes32 =...
Toshi's user avatar
  • 2,357
10 votes
1 answer
8k views

TypeError: Indexed expression has to be a type, mapping or array (is function (address) view external returns (string memory,string memory...)

I have 2 contracts first one is sub second one is main . First i deploy main and create a new subtoken. Then I want to access my maincontract in subToken contract but I cant :( When I compile this ...
Furkan Demirel's user avatar
9 votes
3 answers
1k views

Solc Compiler oversight? Innappropriate mapping declaration overwrites storage

I have written a contract that has the mapping storage variable A & B. These variables get initialized with some values in constructor. A method getBalance returns the balance from mapping A & ...
ACloudFan's user avatar
  • 101
9 votes
1 answer
12k views

How to return a mapping type?

I have a contract with a mapping inside a struct contract Voting { struct voter { address voterAddress; uint256 tokensBought; mapping (bytes32 => uint256) tokensUsed; } mapping (address ...
maheshmurthy's user avatar
  • 1,237
9 votes
2 answers
5k views

What would be the actual gas cost of a mapping and are there any size limitations?

I want to get an estimation of how much it would cost to store arbitrarily many elements in a mapping of the form BYTES32 -> BYTES32. I see here that a STORAGEADD operation costs 20000 gas, which ...
Symeof's user avatar
  • 1,434
9 votes
1 answer
895 views

Is it convenient to use mappings as temporary KeyValue instances?

Consider the following contract that illustrates the problem. Here we have a dynamic array of mappings, add() is meant to add a fresh new mapping to the end of the array, return the value for mapping ...
Oleksii Matiiasevych's user avatar
8 votes
3 answers
7k views

How assignment to a mapping work?

I'm trying to understand how mappings work. I've never seen anything like it before, but I'm sure many languages use something similar. Anyway, here's my code: What am I doing wrong here? Thank you!
manidos's user avatar
  • 4,298
8 votes
3 answers
9k views

using mappings inside structs

I'm trying to use a mapping inside a struct: struct PoolStruct { uint currentUserID; uint activeUserID; uint price; uint minimalReferrals; mapping(uint => address) poolUserList; ...
André Alçada Padez's user avatar
8 votes
1 answer
7k views

Array or mapping, which costs more gas?

I have been using arrays of struct for my contract. In Remix IDE with the private network, one function costs me around 300,000 gas. However, if I call the same function from web3js, it costs me a lot ...
Long Truong's user avatar
8 votes
1 answer
4k views

Is reading data free unlike a transaction? [duplicate]

I understand that writing data to a smart contract is a transaction and it will cost gas. Will reading data from blockchain cost gas? For example, I have a struct to collect data about a person - ...
Arun Kumar's user avatar
8 votes
1 answer
2k views

How does Ethereum fit a mapping into storage?

Probably a silly question, but how does Ethereum fit a mapping into storage? I am used to simple types like arrays, so I don't understand how "Solidity's mapping uses the entire 256-bit ...
Sam Lincoln's user avatar
7 votes
3 answers
11k views

Efficient approach to delete element from array in Solidity

For each user, I want to keep an array of holded assets (each asset has an ID). My solution up until now is : struct User { uint userId; uint[] assets; } For every asset the ...
Random's user avatar
  • 203
7 votes
3 answers
424 views

What is used for default string value in mapping?

For example, I have a contract like this: contract example { mapping (uint => string) map; function example () { map[123] = "123"; } function return1 () returns (string) {...
Sergey Potekhin's user avatar
7 votes
3 answers
3k views

How do I check if a bytes32 variable in a mapping has been assigned value?

For example, for: mapping(address => bytes32) public addressName; which one of the following syntax is correct? address _address = 0x...; //an actual address require(addressName[_address]!=""); ...
Daniel's user avatar
  • 71
7 votes
1 answer
2k views

How to derive the storage key of mapping to an account?

I am trying to derive changes of account storage by mapping accounts (here, the accounts represent the key address in the ERC20 balances mapping mapping(address => uint256)) to their corresponding ...
Daniel's user avatar
  • 73
7 votes
2 answers
923 views

Is mapping via msg.sender as secure as calling a function only by the owner?

On my implementation, only the owner of the contract's function was able to call some important functions that I defined. I changed the implementation as by removing onlyOwner, and adding ...
alper's user avatar
  • 8,395
7 votes
2 answers
2k views

Searching data on a smart contract?

I have a smart contract which contains some article data stored inside a struct, which is mapped with an id which uniquely identifies it with a mapping, like this: struct Foo { string a; ...
fergaral's user avatar
  • 171
7 votes
1 answer
9k views

Solidity function that accepts mapping as input

Is it possible for Solidity to accept mappings as input parameter in a function? I've been trying to create a function with a second parameter mapping (string => string) aMapping, pragma solidity ^0....
Juan Ignacio Pérez Sacristán's user avatar
7 votes
1 answer
9k views

Unable to add new struct item into mapping or array

trying to do a sample based on struct, storing a list of struct as a mapping. but when i add an item and trying to retrieve i cannot find that item in the mapping below is my contract contract ...
mahesh gupta's user avatar
6 votes
3 answers
13k views

Accessing a mapping variable from web3

Is there a way to access a mapping variable such as: mapping(bytes => bool) myMappingVar; which is NOT: Declared public explicitly without using indexed based id Also no corresponding public ...
InfinitePrime's user avatar
6 votes
3 answers
9k views

Default value of a struct

Consider the following contract in Solidity: pragma solidity ^0.4.2; contract Registry { struct Name { string first; string last; } mapping(address => Name) reg; ...
Nicola Squartini's user avatar
6 votes
5 answers
6k views

Is it possible to have enum as a mapping key type?

When trying to compile the following code in browser-solidity: pragma solidity ^0.4.4; contract TestContract { enum TestEnum { ONE, TWO, THREE } mapping (TestEnum => uint) testMapping; } ...
Sergei Tikhomirov's user avatar
6 votes
1 answer
5k views

copying of type struct memory memory to storage not yet supported

I am working on creating a basic voting smart contract. But when assigning Question obj in the map, I'm getting this error: pragma solidity ^0.8.0; contract Voting { struct Question { ...
VikasGS's user avatar
  • 95
6 votes
1 answer
2k views

array vs mapping in solidity: when to use one over the other?

When should you use array or mapping in solidity? Let's say I have multiple question questionnaire, with one question, and I want to store the responses of different people(address) in a smart ...
YulePale's user avatar
  • 1,843

1
2 3 4 5
11