In the last chapter we created Pokemons. But without any trainers to train the pokemons, our game would be boring. Let's add trainers to the game. Each trainer would get a pokemon by default.
To do this, we'll need a new data type: address
.
The Ethereum blockchain is made up of accounts, which you can think of like bank accounts. An account has a balance of Ether (the currency used on the Ethereum blockchain), and you can send and receive Ether payments to other accounts, just like your bank account can wire transfer money to other bank accounts.
Each account has an address
, which you can think of like a bank account number. It's a unique identifier that points to that account, and it looks like this:
0x073Ab1C0CAd3677cDe9BDb0cDEEDC2085c029579
(This address belongs to the Vyper.fun team. If you're enjoying Vyper.fun, you can send us some Ether! 😉 )
We'll get into the nitty gritty of addresses in a later lesson, but for now you only need to understand that an address is owned by a specific user (or a smart contract).
So we can use it as a unique ID for ownership of a trainer. When a user creates a new trainer by interacting with our app, we'll set ownership of the trainer to the Ethereum address that called the function.
A nested mapping looks like the following:
# maps a teacher to a list of students
teacherToStudents: HashMap[String[32], HashMap[uint256, String[32]]]
@external
def addStudent(teacherName: String[32], studentName: String[32], rollnumber: uint256):
teacherToStudents[teacherName][rollnumber] = studentName
The teacherToStudents
mapping maps a teacher's name (String[32]
) to another sub-mapping which represents a list of students. This list of students maps student roll number (uint256
) to their names (String[32]
).
The teacherToStudents
can be populated by adding 2 keys: first key for String[32]
(teacherName
) and second key for uint256
(rollnumber
). The value assigned is a String[32]
(studentName
).
First, to create a trainer, we would need to create a trainer struct. Then To store the ownership of a trainer and their pokemon, we are going to use 3 mappings.
- Create a
Trainer
struct with a single property:name
(String[32]
). - Create a mapping,
trainerPokemonCount
with a key ofaddress
type and a value ofuint256
type. - Create a mapping,
trainerList
with a key ofaddress
type and a value ofTrainer
type. - Create a nested mapping,
trainerToPokemon
with a key of typeaddress
which maps to another sub-mapping. The sub-mapping has a key of typeuint256
and a value of typePokemon
.