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
42
votes
2answers
40k views

How can you 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; ...
30
votes
2answers
26k 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 ?
30
votes
3answers
27k 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 ...
28
votes
2answers
13k 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?
28
votes
3answers
14k 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; ...
25
votes
1answer
19k 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? ...
20
votes
2answers
5k 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 ...
19
votes
2answers
4k views

What is the zero value for a string?

How to verify that a string, or an enum 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-...
18
votes
1answer
16k 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{...
15
votes
1answer
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 ...
14
votes
4answers
12k 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"]...
11
votes
4answers
16k 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 ...
11
votes
3answers
5k 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 ...
11
votes
1answer
4k 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 ...
11
votes
1answer
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++) ...
10
votes
3answers
8k 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 ...
10
votes
3answers
6k 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 ...
10
votes
1answer
8k 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 ...
10
votes
1answer
8k 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 ...
10
votes
2answers
3k 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 =...
9
votes
1answer
10k 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 ...
9
votes
1answer
663 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 ...
8
votes
4answers
11k 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 ...
8
votes
3answers
7k views

How does 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!
8
votes
1answer
3k 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 ...
8
votes
2answers
15k 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 {...
8
votes
2answers
2k 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 ...
7
votes
5answers
4k 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&...
7
votes
1answer
8k 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[...
7
votes
3answers
325 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) {...
7
votes
2answers
577 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 ...
6
votes
3answers
5k 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; ...
6
votes
4answers
4k 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; } ...
6
votes
1answer
13k 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?...
6
votes
3answers
699 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 & ...
6
votes
1answer
2k 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 ...
6
votes
2answers
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; ...
6
votes
1answer
1k views

2 mapped structs with an address array - push not working

Can someone help me understand why the function add in the contract one is not working and in the contract two is? Both are mined and are inserted into the blockchain. Thanks. Not working contract ...
6
votes
1answer
1k views

Problem setting uint value to zero

I made a little smart contracts with solidity browser, This is my code: pragma solidity ^0.4.0; contract Test { struct User { string name; string surname; uint active; ...
6
votes
1answer
8k 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 ...
6
votes
1answer
246 views

Could more than one mapping's key point to the same memory space?

Since memory is already allocated on (array[0] = value;), I just want to change its pointing key value. Such as array[1] will allow me to access value now. Is it possible to change the key of ...
5
votes
1answer
3k 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 ...
5
votes
1answer
2k 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 - ...
5
votes
2answers
4k views

Mapping member isn't initialized when creating a struct

pragma solidity ^0.4.0; contract TestcaseResetObject { MyObject ob; struct MyObject { mapping(uint8 => uint) map; } event Output(uint number); function makeNew() { ...
5
votes
1answer
3k views

Solidity function gas requirement is infinite

struct userData{ uint val; bool isvalue; string name; } mapping(uint => address) internal users; mapping(address => userData) internal bidirectionalUsers; function addMember(...
5
votes
1answer
2k views

assign a value to a member in a struct

I have the following: pragma solidity ^0.4.0; contract MyContract { struct FooFighter { bool foo; uint fighters; } mapping(bytes32 => FooFighter) public fooFighters; If i ...
5
votes
1answer
4k 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....
5
votes
1answer
2k views

Delete or clear a mapping

From what I am gathering based on this question and this question it seems that I am better off not trying to clear arrays or mappings. As the documentation on mappings indicates every possible key is ...
5
votes
1answer
2k views

Reset all values in a mapping

I am trying to figure out how to have the balance of all players reset to (amount) when a certain condition happens. This is the function that will update a specific player balance. How do I make it ...
5
votes
2answers
527 views

How to access dynamic list from web3?

In my application players can create and join games. I store every game in a mapping(uint => Game), and send out a GameCreated event whenever a game is created. My issue is efficiently getting a list ...

1
2 3 4 5
8