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 ...
51
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 ...
45
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♦
- 85k
41
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 ...
eth♦
- 85k
40
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;...
37
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 ...
34
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....
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;
...
31
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
28
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. ...
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 ...
22
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 ...
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
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
...
19
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)
...
16
votes
Accepted
Solidity error when encoding arguments to query public mapping
The address needs be surrounded with quotes.
"0xca35b7d915458ef540ade6068dfe2f44e8fa733c" should work.
16
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;
...
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, ...
14
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 ...
14
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);
}
...
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 ...
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 ...
13
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;
...
12
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 ...
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 ...
12
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 ...
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 ...
12
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mapping × 521solidity × 403
struct × 142
arrays × 107
contract-development × 66
contract-design × 58
remix × 57
storage × 51
addresses × 24
gas × 23
web3js × 21
string × 15
memory × 12
truffle × 10
erc-20 × 10
evm × 8
go-ethereum × 7
blockchain × 7
tokens × 7
erc-721 × 7
events × 6
interfaces × 6
solidity-0.8.x × 6
require × 6
dapps × 5