Mapping in solidity

Mapping in solidity

Of all the new things I've had to take in while learning solidity, Mapping has had a field trip taking heavy Jabs at my happiness. There are moments where I'd just yell out frustrated, swearing this would be the last time I touch this ridiculous language but of course, I couldn't stay off.

Let's just say this article is my preferred way to have a third person overview of mapping and explain the concept to myself. I hope this works otherwise I will bid solidity, farewell :p

Wtf is mapping?

Mapping is a data type in solidity that resembles dictionaries in python. This allows you to associate a bunch of data with another data. Some may even refer to Mapping as an associative array.

How does it work?

Here's how you declare a mapping datatype in solidity.

Mapping(address => int) balances

You have the data type name first, followed by the identifier - the name to call the mapping array.

If you run your code, this line goes ahead to create an array called balances.

(Address => Int)

That stuff above means that the content of balances would be an address linking to an integer. Think of this like a key-value pair in javascript objects.

So inside balances, you'd have several addresses each associated with an integer.

Here's a silly representation of that:

balances = [
    "0x6282...3738": 100,
    "0x4849...1938": 290
]

A literal way to think about it is, you are mapping addresses to integers inside a bucket called balances.

Whenever you need the balance of a particular address, like in the sample we have above, you simply do this

balances[address]

You'd replace address with the address you want to get or pass it as an argument.

And that's how the easy part of mapping operates.

The Other side of ~Pain~ Mapping

I have come across mappings that contain mappings. At that point, I lost it completely. Here's what they usually appear like.

Mapping(address => Mapping(address => Int)) vault

Let's use the former idea we discussed to decipher this. Here's what it sounds like to me; vault is an array that contains addresses. Each address is also mapped to a mapping of address to integers.

Lets do the silly representation thing again:

vault = [
    "0x73...72": {
            "0x82...383": 733
         },
    "0x83...96": {
            "0x92...535": 897,
            "0x92...535": 29
         }...
]

This is how vault internal structure would look like if everything works out fine.

To access the integer value linked to "0x92...535"

vault[0x83...96][0x92...535]

The first bracket contains the address in the first mapping and the second the address in the second mapping. That way, this call would return 29 - pretty broke dude.

In conclusion, with this way of thinking about mapping, I think I had saved my self another horror and can conveniently parse thorough solidity codes without being mentally frozen at this line.

Would love to here your thoughts and comments, and please, you could ask questions and be a total nerd.

Until next, be awesome.