Understanding Mappings Solidity’s Key, Valuable Data Type

Dean Schmid
3 min readAug 2, 2019

--

Prior to Solidity, I had only dabbled in the JavaScript ecosystem. Compared to feature-rich ES6, I found Solidity a little underwhelming and at times restricting. What I thought was eloquent design, the compiler called a type violation.

Solidity I want to store an array of structs inside my struct. This custom data type owns a bunch of those data types. Stop killing my buzz.

Eventually, I had a realisation. Don’t fight it, just put everything in a mapping.

Mappings are kind of like objects. They have a key and a value and if you reference the key you have access to the value.

mapping (uint => address) public testMapping; 

A step by step recipe for tasty solidity mappings

  1. First, you write the word mapping.

This is a type declaration and it tells Ethereum to expect a mapping. Every mapping starts with the word mapping.

2. inside a pair of brackets (key => value)

These are the data types of your key and your value.

(uint => address) sets up a data structure where a numbered reference points to an address. Say you had 5 addresses and you want to be able to label them 1–5. Your key is a number of type uint. The value attached to that key is of type address.

Another example, say you want to send tokens to an address you would reverse the uint => address.

mapping (address => uint) public accountBalance; 

Now every address in the mapping can have a value associated with it. A key of type address has a value of type uint. The value represents a number of tokens (or any other positive number)

3. public means that this mapping has a getter function automatically created for it. Beyond the scope of this article. Just write public there and DYOR.

4. Testmapping and accountBalance are the respective names of these mapping. Having the name right at the end is a little confusing, but you’ll get used to it.

How To Add A Value To A Solidity Mapping

This is a fundamental solidity programming skill. Open up remix and try it yourself. If you can’t do this, you can’t make Ethereum dapps. Sorry about it.

mapping (address => uint) public myMapping;

First, write your solidity function and mapping

mapping (address => uint) public myMapping;function addValueToMapping(recipient, amountOfCoins) public {

// code that adds amount of coins to recipient in myMapping
myMapping[recipient] += amountOfCoins;
}

Wait, just a second. This isn’t JavaScript. The function parameters need types.

mapping (address => uint) public myMapping;function addValueToMapping(address recipient, uint amountOfCoins) public {

// code that adds amount of coins to recipient in myMapping
myMapping[recipient] += amountOfCoins;
}

Mapping Syntax Explained

myMapping[recipient] += amountOfCoins;

You access a position in the mapping with JavaScript array access syntax [ ]. This means, go to [this position] in the mapping. Then we += the value. If [this position] in the mapping is empty: This happens 0 += value . If the address already has a balance, we are adding the value to the existing balance.

Calling the Solidity Mapping Function in Remix

Here in the nice remix GUI, a box has been created for our addValueToMaping function.

The contract is asking for two parameters (address recipient, uint amountOfCoins)

We give it an address for the recipient

and an uint for the amount of coins.

--

--

Dean Schmid
Dean Schmid

Written by Dean Schmid

Full-Stack Developer, Web Designer. I’m a Lover of the Internet and all the Opportunity it Brings.