Skip to content

Latest commit

 

History

History
79 lines (64 loc) · 3.05 KB

File metadata and controls

79 lines (64 loc) · 3.05 KB
title Wrapping Up
actions
checkAnswer
hints
material
editor
language startingCode answer
c#
public class AlienFinder : SmartContract { public class Alien { public uint xna; public string alienName; } public static void Main (string alienName) { ulong random = RandomNumber (); uint xna // Call FindXna () here Alien someAlien = new Alien { // Change the arguments here }; // Notify here } private static ulong RandomNumber () { uint blockHeight = Blockchain.GetHeight (); return Blockchain.GetHeader (blockHeight).ConsensusData; } private static uint FindXna (ulong randomNumber) { // Return the last 8 digits here } }
public class AlienFinder : SmartContract { public class Alien { public uint xna; public string alienName; } public static void Main (string alienName) { ulong randomNumber = RandomNumber (); uint xna = FindXna (randomNumber); Alien someAlien = new Alien { xna = xna, alienName = alienName, }; Runtime.Notify (alienName, "created!"); } private static ulong RandomNumber () { uint blockHeight = Blockchain.GetHeight (); return Blockchain.GetHeader (blockHeight).ConsensusData; } private static uint FindXna (ulong randomNumber) { return (uint)(randomNumber % 100000000); } }

The last thing to implement is the generation of XNA. Since the number ganerated from RandomNumber can be a very large integer (0 to 18,446,744,073,709), we will take the last 8 digits to form the XNA value.

The Runtime.Notify Method

We would also want to notify the client running the contract that a new alien has been created. In NEO, this can be achieved with Runtime.Notify. This function raises a 'Notify' event to the client. It can take any number of parameters of any type.

Runtime.Notify ("Hello", "world!", Blockchain.GetHeight ()); 

Instructions:

  • In FindXna () method, return the last 8 digits of randomNumber. (Hint: use %)
  • In the Main method, call FindXna (), use randomNumber as parameter, and assign the return value to the variable xna.

Now we can pass the generated random values to our newly created alien:

  • Add arguments into the new Alien initiator . Set the xna and alienName fields of this object to the corresponding variables.
  • Notify runtime with two string parameters. The first parameter is alien's name, the second is "created".