2

I have seen examples like the following code many times:

mapping (bytes32 => mapping (bytes32 => bytes32[])) items;

And I have tried to recreate something similar here:

mapping(address => uint) public addresskey1;

mapping(uint => mapping(address => uint)) public addresskey2;

function addAddress(address _address, uint _pin) public {

    addresskey1[_address] = ID;

    addresskey2[_pin] = addresskey1[_address];


}

Aside from remix telling me that mappings can't be assigned to, I can't quite figure out what this does. I know that a mapping creates a hash of the keytype which points to a location where the value is stored, but how does it work when

a hash of a key points to another hash of another key which points to the value? My first thought is that the hash immediately tells the computer where to go, and so mapping like this would just create two jumps to the desired value.

In other words, it is a way of creating multiple keys for the same value without having to save the value in multiple locations - but then, the hash of the first mapping must be stored separately anyway, so it wouldn't really save space.

I keep trying to play around with this, but I cannot figure out what it does.

3

It's like a two-dimensional array.

A common example is in pretty much every ERC20 token. In an ERC20 token, you have the ability to approve other people to spend your tokens. In a single-user case, you might have a mapping like this:

mapping(address => uint256) public allowance;

To look up how much Bob is allowed to spend, you would check allowance[Bob].

In the multi-user case, each user has their own mapping:

mapping(address => mapping(address => uint256)) public allowance;

To look up how much Alice has allowed Bob to spend, you check allowance[Alice][Bob].

  • Yes! Took me a bit to grasp this explanation. But basically it seems a unique hash is made based on - in this case - the two unique addresses in the order they appear, and stores the allowance amount at that location. So the interface can be set up so that msg.sender can enter another address of spender and the allowance amount. And they can always do this again later to change or zero out the allowance later. – CreatedAMadman Jun 10 '18 at 20:11
2

This can be explained with an analogy. Mappings can be understood as file organizers. For instance, mapping(address => uint) can be seen as Folder => data, so the address gives the name of the folder and the mapping goes to the folder and retrieve the data.

In a mapping of mappings like mapping(address => mapping(address => uint)) the equivalent analogy is that you are looking in a folder that has a subfolder and inside of the subfolder there is data.

Note that the name of the folders can be given by several types of variables, for instance, in mapping(uint => mapping(uint => uint)) the name of the folder is given by an integer number and the data is another integer number, in mapping(address => mapping(uint => bytes32)) the name of the main folder is given by an address, the subfolder name is given by an integer and the data is a bytes32 variable type.

I hope this helps.

  • Ah, this is a good too. The only thing I'm wondering about is that this sounds like - in mapping(address => mapping(uint => bytes32)) - the hash of the address points to the uint, and the hash of that points to the bytes32 value. But I would imagine that the hash of each would have to be combined to generate a unique location that is specific to the pair? Otherwise, in say the Alice & Bob example, if Alice and Joe want to give Bob different allowances, they would keep resetting each other's values, as their address would just lead to the hash of Bob's address, leading to the same uint. – CreatedAMadman Jun 10 '18 at 20:23
  • In the case of allowance you have mapping (address => mapping(address => uint) and following the nalogy Alice and Joe have each one a folder for Bob, this is because the location for each folder is selected with reference to the location of the main folder that is, the memory location given by the hash of the first address is added to the hash of the second addrees. As you can see, Joe and Alice reference the allowance to Bob in different memory positions. – Jaime Jun 11 '18 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.