Skip to main content
62 votes
Accepted

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

You can't directly find out if any key exists in a mapping, ever, because they all exist. mapping(key => value) name; creates a namespace in which all possible keys exist, and values are ...
Rob Hitchens's user avatar
  • 55.5k
52 votes
Accepted

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

Mappings do not store their keys, only the value which is stored at the state memory address calculated by a sha3 hash of the the key itself. Any lookup into a mapping has to provide that original key ...
o0ragman0o's user avatar
  • 4,330
47 votes
Accepted

How to delete an element from a mapping?

Yes, delete user[someAddress]; will work with structs that do not contain a mapping. For this question, because name and nickname are not mappings, they will be deleted (set to zero) automatically: ...
eth's user avatar
  • 86.3k
38 votes
Accepted

Solidity - List contains

Solidity doesn't provide a contains method, you'd have to manually iterate and check. Using an array for what you're trying to achieve would be a highly inefficient pattern. The best and most cost ...
Miguel's user avatar
  • 5,193
33 votes

Solidity - List contains

If I were you, I would use a pattern like this: contract myWallets { mapping (address => bool) public Wallets; function setWallet(address _wallet) public{ Wallets[_wallet]=true; ...
István András Seres's user avatar
30 votes
Accepted

How to loop through mapping in solidity?

You can't loop through the keys or count the keys in a mapping (but patterns exist for accomplishing such functionality using a few more variables). You're on the right path, tracking it yourself. ...
Rob Hitchens's user avatar
  • 55.5k
27 votes
Accepted

Return a mapping in a getAll function

You can't return a mapping directly. What you can do is to make an array and add all address values that is stored in the addresses mapping to it. Then return that array. function getAll() public ...
saman.shahmohamadi's user avatar
23 votes
Accepted

Is there a maximum number of entries for a mapping?

The cost of an addition to or a read from a mapping does not change with the number of keys mapped. The location of a value key is computed by sha3(key, p) where p is the storage slot that acts as the ...
Xavier Leprêtre B9lab's user avatar
20 votes

How to delete a mapping?

All problems in computer science can be solved by another level of indirection. pragma solidity ^0.4.20; contract DeletableMapping { // Input is keccak256(uint32 mappingVersion, string carVIN) ...
William Entriken's user avatar
19 votes
Accepted

How to delete a mapping?

Unfortunately you can't delete a mapping. From the solidity docs at: http://solidity.readthedocs.io/en/develop/types.html delete has no effect on whole mappings (as the keys of mappings may be ...
Adam Dossa's user avatar
  • 1,297
17 votes

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

from 0.7.0 do like below: struct Request{ string description; uint value; address recipient; bool complete; uint approvalsCount; ...
vijay patne's user avatar
16 votes
Accepted

Solidity error when encoding arguments to query public mapping

The address needs be surrounded with quotes. "0xca35b7d915458ef540ade6068dfe2f44e8fa733c" should work.
afu802's user avatar
  • 376
15 votes
Accepted

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

The problem is in the construction, i.e., Request({ description: description }) Therefore changing the location of newRequest from memory to storage will not help. If you really want to make it work, ...
hrkrshnn's user avatar
  • 426
14 votes
Accepted

Array or mapping, which costs more gas?

Array does cost more than mapping, but that's because it's not doing the same thing. An Array in Solidity is basically a struct with this structure struct Array{ mapping(uint => someType) items; ...
natewelch_'s user avatar
  • 12.2k
14 votes
Accepted

What does address(0) mean?

balances is a mapping of address to how many tokens that address owns. balances[address(0)] then means how many tokens the address 0 (0x00000000...00) owns. Although I don't like the practice, some ...
user19510's user avatar
  • 28.1k
14 votes

using mappings inside structs

The problem is that mappings can only live in storage. When you define PoolStruct memory pool;, the mapping member cannot be created in memory, and therefore the memory struct should be treated as if ...
hrkrshnn's user avatar
  • 426
13 votes

Efficient approach to delete element from array in Solidity

Source: https://github.com/su-squares/ethereum-contract/blob/master/contracts/SuNFT.sol Here you go: Algorithm: uint[] assets; mapping(uint=>uint) indexOfAsset; function removeAssetFromArray(uint ...
William Entriken's user avatar
13 votes
Accepted

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

I managed to solve it myself: // SPDX-License-Identifier: MIT pragma solidity ^0.8.16; contract MappingStorage { mapping(address => uint) public balances; uint8 constant ...
Jeremy Then's user avatar
  • 4,629
12 votes
Accepted

How to list ALL Ethereum addresses with a positive balance

simple: get all the blocks from each block get all the transactions filter all the transactions with a value > 0 record the list of all the to addresses filter out duplicates filter out addresses ...
Micha Roon's user avatar
  • 2,213
12 votes
Accepted

How to loop through a array of addresses?

You haven't described enough about your testing methodology for me to know why it appears that the .length method isn't working. It does. One possibility is you are expecting a response but the ...
Rob Hitchens's user avatar
  • 55.5k
11 votes

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

Rather than using the hash of the enum as the key, you could just explicitly cast the enum to uint. Because the actual uint size the compiler uses is just big enough to hold all enum values, it will ...
Ajoy Bhatia's user avatar
  • 1,446
11 votes

Can I save structs in a Mapping?

Yes, you can. For example, in the solidity code below. There is a struct User that has a mapping of friends, from address to another struct Friend. Then there's a mapping of users, from address to ...
Daniel Okwufulueze's user avatar
10 votes
Accepted

Is reading data free unlike a transaction?

As long as you are only reading data from the blockchain and not changing the blockchain, you don't need to carry out a transaction. The function you call is then carried out only by the node you are ...
gisdev_p's user avatar
  • 1,801
9 votes
Accepted

Can I call mapping as arguments of function?

The second error you get tells you that external functions cannot have internal types as arguments (i.e. structs and mappings). You can solve that one by specifying internal or private for your ...
dragosb's user avatar
  • 2,370
9 votes

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

You have to track an index of the elements you stored in the mapping. Some solution patterns here: Are there well-solved and simple storage patterns for Solidity? Hope it helps.
Rob Hitchens's user avatar
  • 55.5k
9 votes
Accepted

Is it possible to have a mapping to a tuple?

If you use a struct, eg struct Participant { bool value1; uint value2; } mapping (address => Participant) participants; I don't think there's any way of using an actual Tuple eg var (a, ...
willjgriff's user avatar
  • 1,658
9 votes

Accessing a mapping variable from web3

You can access a public mapping variable like a regular public variable with the exception that you need to provide the index value for the mapping. I have done the following example, and it worked ...
mdx1563's user avatar
  • 91
9 votes
Accepted

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

There's no way to check directly if something exists in a mapping. In solidity, everything is set to it's "default value" until it's changed. That means every integer starts as 0, every string starts ...
David Mihal's user avatar
9 votes
Accepted

How can I push data to array when using mapping (address => address[]) mappingName?

I would do as follow: mapping(address=>address[]) addressToMany; function addAddress(address _address) public { addressToMany[msg.sender].push(_address); }
strangethingspy's user avatar
9 votes
Accepted

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

It always depends on your needs. The biggest difference is that you can't iterate over a mapping. So if you store two entries (key => value) in a mapping, there is no way to get the values without ...
Lauri Peltonen's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible