Our contract is almost finished! Now let's add an event
.
Events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen.
# declare an event
event NewPersonAdded:
name: String[64]
age: uint256
people: HashMap[String[64], uint256]
@external
def addNewPerson(name: String[64], age: uint256):
self.people[name] = age
# fire an event to let the app know the function was called
log NewPersonAdded(name, age)
Your app front-end could then listen for the event. A javascript implementation would look something like:
YourContract.NewPersonAdded(function(error, result) {
// do something with result
}
We want an event to let our front-end know every time a new pokemon was created, so the app can display it.
- Create an event named
NewPokemonCreated
. It should have 3 arguments:
name
ofString[32]
typedna
ofuint256
typeHP
ofuint256
type
- Use the
log
keyword to fire the event inside the_createPokemon
function just before thereturn
statement.