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.

Filter by
Sorted by
Tagged with
0
votes
0answers
21 views

How to optimally use map of maps in solidity?

I have the code: struct A { mapping(uint256 => uint256) b; } mapping(uint256 => A) public a; and I want to have the following code as gas efficient as possible: a[key_a].b[key_b] = a[...
0
votes
1answer
33 views

Adding element to mapping (solidity) is not working, even if the method is actually called in the transaction

I'm currently working (as a begineer) at a project involving ethereum blockchain and solidity. In my contract I have a mapping with addresses as key and the value of an array of a FileDetails ...
0
votes
0answers
16 views

In Solidity, Modifiers & Mapping variable are stored where - storage or memory?

In Solidity, Modifiers & Mapping variable are stored where - storage or memory ?
1
vote
1answer
19 views

another “invalid opcode” error

I am stuck on this little problem in my chat app smart contract(this is only the faulty piece of code) contract ChatApp { struct user{ bool acceptReq; uint nreq; ...
0
votes
1answer
21 views

Get struct from mapping that include another mapping

I have an already deployed contract with a mapping that looks this way: struct Details { address a; address b; } struct Params { uint a; uint b; //..... uint n; mapping(address =&...
0
votes
0answers
18 views

VM Exception when accessing mapping

I created a contract with a User struct than contains an age, a first name, and a last name. The function getUser() should return this information for an inputted address. But when I call my getUser() ...
0
votes
1answer
18 views

Solidity: How to do 2D mapping?/Syntax Error

I am getting a syntax error. I am not sure if its a problem related to 2D mapping or 2D array. My code is given below: contract Stealing{ mapping (address => uint) balances; mapping (...
0
votes
0answers
19 views

How many key-value entries can be stored in Solidity Mapping data structure?

mapping(uint => uint) public myMap; function store(uint key, uint val) public { myMap[key]=val; } function retrieve(uint key) public view returns (uint){ return myMap[key]; } Considering ...
0
votes
1answer
33 views

How to initialize a mapping of structs with a mappings?

I have a mapping of a struct, which has a mapping to another struct: uint256 public questionIdCounter; mapping(uint256 => Question) public questions; struct Question { uint256 ...
0
votes
1answer
22 views

Passing dynamic data to a contract

I'm trying to store records in a contract that are supposed to have an arbitrary number of fields. For example, one record would have "bitrate", another "ISBN" etc., in addition to ...
0
votes
1answer
19 views

mapping not updated

I'm testing a very primitive contract using mapping. contract PhysicalContract { uint numContracts; mapping (address => uint) contracts_test; function newDeal(address adr, ...
0
votes
0answers
24 views

How to retrieve all the values form the mapping?

Yield farming. Unaudited food token. Anon founder. Crazy APR%. Of course, if it is "too good to be true" it probably is... I would like to "don't trust, verify" and verify who is ...
0
votes
1answer
24 views

Storing multiple values in one key mapping dynamically

I am making a blockchain project using solidity which requires storing various objectIDs corresponding to a personId. The person can add and remove objects and we cannot hard-code the number of ...
0
votes
2answers
23 views

Is it possible to emit a mapping?

I have a mapping in my code that maps an address of a recycler to a plastic bottle address as below. mapping (address => address)plasticBale; Is there a way I could emit this mapping? or do I ...
0
votes
0answers
14 views

Acess position in array vs Access key in mapping (uint IDs) - What's the efficient way?

I have a doubt about efficiency in solidity, when searching in lists of integer values, in mapping vs array. Array Imagine that I use the array position as element ID, and the content as the value, as ...
0
votes
1answer
24 views

Mapping keys optimization in Solidity

I've looked through all answers here but still have the question for the latest version of Solidity. Perhaps, there exist new libraries Is it possible to optimize this mapping(address => uint) ...
1
vote
1answer
27 views

Add and compare items of struct in Solidity (dealing with struct in Solidity)

How can I know the whole entityData of the struct and add a new entity only if it didn't exist before (ensure no replicated data)? struct EntityStruct { uint entityData; bool isEntity; bool ...
0
votes
1answer
18 views

Struct is not saved in mapping for some unknown reason

I don't understand why my struct is not saved in mapping. I've tried diffrerent methods of initializing struct I found all over the web and it didn't work on development network (ganache + truffle). ...
1
vote
1answer
28 views

I do not understand the definition used to update a mapping stored inside a struct type as explained below

struct Payment { uint amount; uint timestamp; } struct Balance { uint totalBalance; uint numPayments; mapping(uint => Payment) payments; } mapping(address => Balance) public ...
0
votes
0answers
14 views

Transfer ownership of specific mapped array to a different address

I have this contract with a mapped array of the struct Item linked to a specific address. I want to transfer a specific array in Item[] to a different owner/address. What is the best way to achieve ...
0
votes
0answers
12 views

Students certification architecture

I recently did a course on uDemy and get the first concepts. I'm starting to build a real-life app for a training center. I want to register in the blockchain that Jhon Doe, ID (document) xxxx, ...
0
votes
0answers
20 views

Mapping duplicate

I have a mapping in my smart contract, which works as a user's historic. Each time the user adds or transfer a kit, the information about this said kit has added to the mapping. These kits have an id ...
0
votes
0answers
18 views

Mapping shared between contracts

I have enums that I use in many contracts of one project. Sometimes, they need to be mapped to a uint256. Their uint256 values do not relate to their positions in the enum list, so I can't just cast ...
0
votes
0answers
15 views

How can I delete values from nested mapping after a sum

I'm making an staking contract in solidity and i have used structs and nested mapping for saving users staking amount and its staking time, At the time of calling unstake function msg.sender will ...
1
vote
1answer
128 views

Workaround for TypeError: Struct containing a (nested) mapping cannot be constructed

Following example code piece works on solidity v0.6.*: // SPDX-License-Identifier: MIT pragma solidity >=0.7.1; contract Campaign { struct Status { uint32 cost; mapping(...
2
votes
4answers
672 views

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

My version of solc: "solc": "^0.7.1", When I try to construct a struct that contains mapping, I got this error: "Struct containing a (nested) mapping cannot be constructed&...
1
vote
0answers
37 views

Huge gas cost for deleting an element of a map

As you can see with the code below the last person to call claimAuction cleans up the AuctionProposal but has to pay 383729 gas instead of 73846 for that cleanup, a 519% increase! My question is ...
0
votes
1answer
63 views

Create map of existing objects in mint function for a smart contract

I'm really new to solidity and there is still alot I dont fully get. I have created this smart contract. I am getting an error while performing a test stating that the id to be set by the push cant be ...
0
votes
3answers
32 views

Get a blank address

I have a little problem with my code. I'm trying to follow who are the current sender and the previous sender of a transaction respectively with the variable currentSender and lastSender, and then ...
0
votes
1answer
42 views

Getter of mapping inside a struct

I wanted to create a getter function to access a mapping nested inside a struct. But the function that I made required me to use the expiremental version (I'm on Remix). So I used the expiremental ...
1
vote
1answer
54 views

Finding values within an Array/Mapping based off min and max values

I want a user to be able to place a bet on some number, between a range of numbers. So the user wants to place a bet on the numbers 50-500. Then there is a function that sees if the number has been ...
0
votes
0answers
12 views

Raise & return error with message if no key found in mapping

I am writing a test that should fail because it looks for a key that doesn't exist on a mapping. How can I raise the error with and error message in solidity? this is the test: it('Should NOT update ...
1
vote
1answer
20 views

Array of mapping

I would like an array of mappings, is this possible? mapping (address >= value) sentValue; sentValue[] public sentValues; I want to record amounts sent by address, and I want each send event to ...
1
vote
0answers
31 views

How to update multiple values in a mapping without a loop?

if you have a mapping like: mapping (uint => bool) owned; How would you go through and update 1000 items in that mapping to set them all to true? Without using a loop due to gas prices. Is there a ...
1
vote
1answer
56 views

Mapping vs Arrays: duplicating variables gas consumption

I have a contract that has a struct including address and unit, and mapping of a struct, like this. struct User { uint count; address userAddress; } mapping (address => User) users; ...
0
votes
1answer
22 views

When using mapping, why use an address over bytes32?

From an end user standpoint, they will never want to use the address for searching for mapped data when they could just submit a username for function parameters, so is there any specific reason why ...
0
votes
1answer
24 views

Solidity get() function only returning one string when i ask for all strings in mapping

Can anyone tell why my get() function only returns the string "name" in Remix? I'd like it to return all the values within the users mapping like i've defined, can you help me accomplish ...
1
vote
1answer
303 views

using mappings inside structs

I'm trying to use a mapping inside a struct: struct PoolStruct { uint currentUserID; uint activeUserID; uint price; uint minimalReferrals; mapping(uint => address) poolUserList; ...
0
votes
0answers
70 views

Storing data directly on chain

Im trying to store data directly on the blockchain, not on a smartcountract as they have a limit of what they can handle and it would slow down the chain to go above the limit. My initial plan is to ...
1
vote
0answers
45 views

how to display a struct inside mapping inside struct in solidity on react js frontend

I have a mapping of struct (mapping (address => struct)) inside another struct. I'm having issue in how to display the struct in the front end while using Reactjs.
0
votes
1answer
39 views

Mapping didn't storing unique values

I wrote this piece of code with the goal to store unique documents hash. Used mapping to accomplished that. pragma solidity ^0.5.16; contract DocumentHash{ string public documentHash; ...
0
votes
2answers
315 views

Solidity 0.4.26 check if element already exists in array

How do I make it so this only adds an identifier to the dataArray if it doesn't exist already? pragma experimental ABIEncoderV2; contract structWithMapping{ struct Data{ string[] user; ...
0
votes
1answer
30 views

Read and write private variable from child contract

I have a contract MyContract is ERC721. MyContract needs to concatenate a string with an existing token URI using this function: string(abi.encodePacked(_tokenURIs[_tokenId], _URI)); Problem is all ...
0
votes
1answer
75 views

Accessing array within struct in a mapping using web3.js

I have created a struct patient as follows, and a mapping of uint->patient. struct patient { uint pId; string name; string phone; address payable owner; uint numRecords; uint[]...
0
votes
1answer
196 views

Check if user exists using mapping in solidity

I use solidity to store users information so before that i want to check if the user id already exist or no, Can any one help me with this code ? i see some solution using array or require all ...
1
vote
0answers
27 views

vyper no mappings allowed when adding a function to initialize a struct that has a map inside

Version Information vyper Version (output of vyper --version): 0.1.0b17+commit.eaa6754 OS: Debian GNU/Linux 10 (buster) Python Version - 3.6.2 Environment (output of pip freeze): asttokens==2.0.3 ...
0
votes
1answer
38 views

Delete mapping within a mapping

I have a mapping with the following structure: mapping(uint256 => mapping(address => SOME_STRUCT)) public submissions; delete submissions[uint256] is not allowed(compiler error). delete ...
0
votes
1answer
906 views

How to access data stored in Ethereum Blockchain using Smart contract?

I want to write solidity contract which stores hash table as mapping(uint => string) documents data structure in ethereum blockchain. Now, each time I run a function "storeDocument" in this contract,...
0
votes
1answer
23 views

Mapping multiple structs to a struct and call them

I have a question about how I should design my contract to achieve the following: I want to create products identifiers (can be struct or string) something like: "product#123abc". Then I want to map a ...
0
votes
0answers
15 views

How to save/read mappings within struct?

I am trying to create a mapping inside struct and then create a mapping of the struct. Take a look at below example: // ------------------------------------------------------------------------ ...

1
2 3 4 5
7