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.

522 questions
5
votes
1 answer

In Ethereum how to check if in mapping, the key already exists?

In this Proof of existence, integrity, and ownership contract of a file why files[fileHash].timestamp == 0 is used? why timestamp? contract Proof { struct FileDetails { uint timestamp; string owner; } mapping (string =>…
Yash Shukla
  • 375
  • 1
  • 4
  • 11
2
votes
0 answers

Tuple as mapping key?

Can we use tuple as mapping keys? For example: mapping ((address, uint256) => uint256) myMapping; If not, what's the closest workaround? Consider the two components of the tuple can go up to uint16. EDIT To be more explicit, I can think of two…
Roy
  • 213
  • 2
  • 7
2
votes
0 answers

Why can mappings not be used as parameters or return values of public functions?

The solidity docs say this about mappings, but don't give a reason why: Mappings can only have a data location of storage and thus are allowed for state variables, as storage reference types in functions, or as parameters for library functions.…
0
votes
1 answer

DeclarationError: identifier not found or not unique 'candidate'

I got the above error at the mapping 'candidate' pragma solidity ^0.4.22; contract Election { struct Candidates { string name; uint voteCount; } struct voter { bool authorized; bool voted; uint vote; } address…
chuks
  • 1
  • 1
0
votes
1 answer

How can I add an element to a mapping array (mapping[])?

Remix accepts the following code and allows me to deploy the contract: pragma solidity 0.4.24; contract bytes32array { mapping(address => uint)[] balances; function addBalance(address _address, uint _index, uint _amount) public…
CreatedAMadman
  • 190
  • 1
  • 14
0
votes
1 answer

Sha256 Digest Mapping to Address

Sha256 Message Digests are 32 bytes, however they are typically expressed as a 64 character string. Is it possible to map the string to an address? Something like mapping (stringToBytes32(bytes32) => address) public digestToAddress; I'd rather…
0
votes
1 answer

Mapping: Does the length of the key as string affects the gas usage?

I am using string as a key on mapping data structure. [Q] While we are use mapping, when the length of the key string increases, does it also increase the gas usage? mapping(string => int) map; contract.array("mykey"); …
alper
  • 8,395
  • 11
  • 63
  • 152
0
votes
1 answer

Nested mapping in solidity

this code require two input(_Id,_name) to get age i need to get name and age from input id only // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract nestmap { mapping(uint256=>mapping(string=>uint256)) public User; …
0
votes
1 answer

Mapping both ways

if I want to have a mapping both ways (getting value by key and key by value) is it the most efficient approach to simply initialize 2 mappings pointing in the opposite directions? mapping(uint64 => address) public frontEndTags; mapping(address =>…
Jan Beneš
  • 569
  • 1
  • 7
  • 14
0
votes
2 answers

How does `mapping(address => uint) public balances` get balances?

I noticed that balances as a mapping mapping(address => uint) public balances; and acts as a dictionary where addresses are mapped to balances.(address -> balance). For a typical dictionary to do so, we need to first provide/initialise the…
Pe Dro
  • 127
  • 5
0
votes
1 answer

How to store large amount of addresses

I was looking into how to store large amount of addreses for example to adding atributes to different token holders. As far as i know, most people use mapping, i was researching how that works but the only information i could find was "You can think…