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 ...
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 ...
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♦
- 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 ...
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;
...
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. ...
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 ...
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 ...
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)
...
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
...
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;
...
16
votes
Accepted
Solidity error when encoding arguments to query public mapping
The address needs be surrounded with quotes.
"0xca35b7d915458ef540ade6068dfe2f44e8fa733c" should work.
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
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;
...
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
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 ...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ...
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.
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, ...
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 ...
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 ...
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);
}
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mapping × 526solidity × 407
struct × 143
arrays × 108
contract-development × 66
contract-design × 58
remix × 57
storage × 52
addresses × 24
gas × 22
web3js × 21
string × 15
memory × 13
truffle × 10
erc-20 × 10
evm × 9
go-ethereum × 7
blockchain × 7
tokens × 7
erc-721 × 7
events × 6
interfaces × 6
solidity-0.8.x × 6
bytes32 × 6
require × 6