Created
August 28, 2019 14:38
-
-
Save hal0x2328/32671046d7186f49b6e1d6f23eb3ad85 to your computer and use it in GitHub Desktop.
Simple Neo Supply Chain Smart Contract Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Neo.SmartContract.Framework; | |
using Neo.SmartContract.Framework.Services.Neo; | |
using Neo.SmartContract.Framework.Services.System; | |
// simple supply chain tracking contract example | |
namespace Neo.SmartContract | |
{ | |
public struct TrackedItem | |
{ | |
public byte[] ID; | |
public string Payload; | |
public uint Timestamp; | |
} | |
public class Tracker : Framework.SmartContract | |
{ | |
private static readonly byte[] Owner = "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y".ToScriptHash(); | |
public static object Main(string operation, object[] args) | |
{ | |
if (operation == "putdata") | |
{ | |
if (Runtime.CheckWitness(Owner)) | |
{ | |
Transaction tx = ExecutionEngine.ScriptContainer as Transaction; | |
string payload = args[0] as string; | |
uint timestamp = Runtime.Time; | |
TrackedItem item = new TrackedItem | |
{ | |
ID = tx.Hash, | |
Payload = payload, | |
Timestamp = timestamp | |
}; | |
Storage.Put(tx.Hash, item.Serialize()); | |
return true; | |
} | |
return false; | |
} | |
if (operation == "getpayload") | |
{ | |
byte[] id = args[0] as byte[]; | |
TrackedItem item = (TrackedItem)Framework.Helper.Deserialize(Storage.Get(id)); | |
return item.Payload; | |
} | |
if (operation == "gettimestamp") | |
{ | |
byte[] id = args[0] as byte[]; | |
TrackedItem item = (TrackedItem)Framework.Helper.Deserialize(Storage.Get(id)); | |
return item.Timestamp; | |
} | |
// get the raw serialized bytearray containing all item properties | |
if (operation == "getserializeddata") | |
{ | |
byte[] id = args[0] as byte[]; | |
return Storage.Get(id); | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiles OK with VS and Neocompiler, fails to compile on NeoRay with the error