Hot answers tagged

53 votes
Accepted

How can you 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 ...
  • 52.5k
49 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 ...
  • 4,200
40 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: ...
  • 79.7k
37 votes
Accepted

Store data in mapping vs. array

mapping is generally recommended. For this use case of a contract, which could have an unlimited number of documents, which could be updated, the recommendation holds. The main advantage of an array ...
  • 79.7k
33 votes
Accepted

Can I save structs in a Mapping?

You can store structs as the values of your mapping, not as the key. Here is some more info: http://solidity.readthedocs.org/en/latest/types.html#structs struct Funder { address addr; uint amount;...
  • 1,025
32 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 ...
  • 4,855
30 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; ...
29 votes
Accepted

Mapping with array as Key Or Value data type

Mappings can only use elementary types (address, uint, bytes, string) as keys, and more specifically any type for which sha3() is defined. This means structs and arrays currently can't be used as keys....
  • 34.8k
28 votes
Accepted

What is the zero value for a string?

One way is to check for the length of a String: if (bytes(m1[msg.sender]).length != 0 && bytes(m2[msg.sender].someString).length != 0) // do your thing See the answer I posted here
  • 6,113
24 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. ...
  • 52.5k
24 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 ...
21 votes
Accepted

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

The thing was that my mapping was public, and there are no public accessors implemented for it yet. The solution is just to not declare the mapping as public.
19 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 ...
16 votes
Accepted

Solidity error when encoding arguments to query public mapping

The address needs be surrounded with quotes. "0xca35b7d915458ef540ade6068dfe2f44e8fa733c" should work.
  • 356
16 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 ...
  • 1,257
16 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) ...
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 ...
  • 26.9k
14 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; ...
13 votes
Accepted

Iterating over all public values of a mapping in web3

You can't iterate over the key of balanceOf as the data stored in the mapping to represent the key is the sha3 of the specified key. From Solidity In Depth - Mappings, The key data is not actually ...
12 votes
Accepted

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

You could just store the index of the mapping in an array. uint[] indexes; mapping (uint => uint) example; function add(uint x){ example[indexes.length] = x; indexes.push(indexes.length); } ...
  • 1,916
12 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; ...
  • 11.1k
12 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, ...
  • 326
11 votes
Accepted

Is it possible to have mappings inside mappings?

I suppose customStruct is a struct with timestamp property. Then your code should work. However here my test scenario: import "dapple/test.sol"; contract MyTest is Test { struct Struct { uint ...
  • 891
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 ...
  • 1,436
11 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 ...
  • 2,031
11 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 ...
  • 52.5k
10 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 ...
9 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 ...
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 ...
  • 2,305
9 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 ...
  • 1,751

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