Exercise: Mappings

Practice using mappings in Solidity.

We'll cover the following

Now, it's time to practice using mappings in Solidity. In this short exercise, we'll implement a new feature in our smart contract and practice working with mappings in the process.

Get name by address

So far, our smart contract only allowed getting an address by providing the name it's associated with. Now, we'll update our smart contract to allow a reverse operation. It'll write a method that accepts an Ethereum address and return the name that's registered with it.

To implement this reverse search, we have to make three changes to our current smart contract:

  • Add a new mapping that'll allow getting an address by the name associated with it. It'll allow us to efficiently retrieve the name corresponding to a given address.

  • Update the new map in the registerNewName method to store a link between an address and its name in the reverse direction.

  • Implement a new method that allows getting a name associated with the provided address.

To keep track of the changes we need to make, we’ve added a few TODO statements in this exercise.

Get hands-on with 1200+ tech skills courses.