Exercise: Validation

Practice using validation and structs in Solidity.

In this exercise, we'll practice using validation in Solidity. We'll update our smart contract to ensure not only that all the registered names are unique, but also that all the registered Ethereum addresses are unique. In other words, users won’t be able to register the same address under different names.

We'll implement this change in two steps. Firstly, we'll add a new struct type that we'll use in the nameForAddress mapping as the new value type. Secondly, we'll update the smart contract to use the new type and enforce the new requirement.

Task 1: Add a new struct type

For the purpose of this exercise, we assume that any string is a valid name to register, so we won’t be able to use the value returned from nameForAddress to determine if a particular address is already registered. To check if an address with a particular name exists, we'll use the same pattern we used in the previous lesson, and define a new struct with the boolean exists field that we'll use to check if an address already exists.

First, we need to add the new struct type and use it in the nameForAddress mapping instead of the string value type.

Get hands-on with 1200+ tech skills courses.