title | actions | material | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Static variables & Storage of Key-Value Pairs |
|
|
You may have noticed that a new field was added for the Alien class, called Id
. The goal is to implement a couner that increments each time an alien is created, and use that as a unique id for each new alien.
Normally in C#, we would write a static field inside the Alien class like this
static uint counter = 0;
But Neo contracts does not support static fields, unless it is read-only, which means only constant variables such as owner of the contract can have the static
keyword. Here is an example of such:
public static readonly byte[] Owner = "ATrzHaicmhRj15C3Vv6e6gLfLqhSD2PtTr";
With this line, users or developers can call the Owner
variable to access the address that owns this contract. And since it is readonly
, it cannot be changed once deployed.
One work-around would be to put the number of aliens created so far into Neo network's persistent storage.
Neo provides the storage class to store data on the blockchain. All data are stored in key-value pairs. Here are some of the attributes and methods inside the Storage
class.
Storage.CurrentContext
: Returns aStorageContext
type, specific to the current contract.Storage.Put (StorageContext, key, value)
Storage.Put (key, value)
Storage.Get (StorageContext, key)
Storage.Get (key)
Storage.Delete (StorageContext)
Storage.Delete (key)
Note that StorageContext
is a custom type provided in Neo's framework. Each contract has its own StorageContext
, by default, all Put ()
and Get ()
operations passes in the current context unless otherwise specified. Contracts sometimes pass their own StorageContext
to other contracts as a way of authorising other code to modify their storage.
key
can be of type string
or byte[]
.
value
can be of type string
, BigInteger
or byte[]
.
Here is an example of storing, accessing, and deleting data on the blockchain:
// Key is totalSupply and value is 100000000
Storage.Put (Storage.CurrentContext, "totalSupply", 100000000);
BigInteger totalSupply;
var value = Storage.Get ("totalSupply"); // returns a byte[]
if (value.Length != 0)
totalSupply = value.ToBigInteger (); // totalSupply is 100000000
Storage.Delete ("totalSupply");
Storage.Get ("totalSupply"); // returns a byte[] of length 0
- In
GetCounter ()
, initiatecounter
with value of 0. If the value in storage with keyalienCount
does not return a byte[] of Length 0, setcounter
to that value. Otherwise, return counter with value of 0. - In
UpdateCounter ()
, initiatecounter
and pass in the result ofGetCounter ()
. Incrementcounter
by 1. Save the value into storage with keyalienCount
. Return the newcounter
.- Note: conventional increment statments
counter++
cannot be interpreted by Neo compiler, usecounter = counter + 1
instead.
- Note: conventional increment statments
- In the
new Alien
call, add a third line, and set Id to the return value ofUpdateCoutner ()
. - Change the return value to the new Alien's Id.