top of page

LEVEL 6

You are almost there! Here is where you would be writing your last pieces of code to finish your smart contract! Hurraaaah on making it so far :)

Let us create 2 entrypoints in this level as well- now we are experts, aren't we :P?

Step 1:

First entrypoint: Our entrypoint from the previous level titled "create_superhero_with_values".

Instead of boilerplate code, let me share the actual function code here and explain it to you.

public stateful entrypoint create_superhero_with_values(name: string, power:int): int =

let new_avenger : avenger = {

name = name,

power = power}

put(state{map_avengers[name] = new_avenger})

put(state{index = (state.index + 1)})

state.index

The 2 main takeaway from the above syntax are:

  • The value of the state is accessible from inside the contract through an implicitly bound variable state.

  • State updates are performed by calling a function put : state => unit

In the above code, we are updating the value of the index as well as the avenger map to ensure that the information is recorded on the blockchain.

Step 2:

We will also create an entrypoint to get the values of our avengers once added. Name it - getSuperhero.

Here is the code for the second entrypoint:

public stateful entrypoint getSuperhero(name: string): avenger =

state.map_avengers[name]

Once you have put together the entire contract, please copy paste it in an editor for the next step.

avengers.jpg
Try my code
Show solution
Add boilerplate code
bottom of page