public static object[] Main(string operation, string entity, object[] args) { bool success = true; string message = ""; object[] results = { -1 }; //NeoVersionedAppUser AppVAU = NeoVersionedAppUser.New("NPC.dApps.NeoDraw.Main", 1, 1, 1, NeoEntityModel.GetInvokingAddressScriptHash()); //NeoVersionedAppUser AppVAU = NeoVersionedAppUser.New("NPC.dApps.NeoDraw.Main", 1, 1, 1, ExecutionEngine.ExecutingScriptHash); NeoVersionedAppUser AppVAU = NeoVersionedAppUser.New("NPC.dApps.NeoDraw.Main", 1, 1, 1, "7074acf3f06dd3f456e11053ebf61c5b04b07ebc".AsByteArray()); // DEBUG ONLY if (NeoTrace.TESTING) { NeoVersionedAppUser.LogExt("AppVAU", AppVAU); } //byte[] ownerScriptHash = NeoEntityModel.GetInvokingAddressScriptHash(); //if (NeoTrace.INFO) NeoTrace.Trace("ownerScriptHash", ownerScriptHash); //NeoVersionedAppUser.SetUserScriptHash(AppVAU, ownerScriptHash); //if (NeoTrace.INFO) NeoVersionedAppUser.LogExt("AppVAU", AppVAU); NeoDrawEntityType entityType = NeoDrawEntityType.UnknownEntityType; if (NeoTrace.SPLASH) { NeoTrace.Trace(""); } if (NeoTrace.SPLASH) { NeoTrace.Trace(SmartContractName + " ********************"); } if (NeoTrace.ARGSRESULTS) { NeoTrace.LogExt(); } if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("operation", operation); } if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("entity", entity); } if (entity == "user") { entityType = NeoDrawEntityType.User; } else if (entity == "point") { entityType = NeoDrawEntityType.Point; } else { message = "unknown entity type '" + entity + "'"; if (NeoTrace.ERROR) { NeoTrace.Trace("**ERROR** entity", message); } entityType = NeoDrawEntityType.UnknownEntityType; success = false; } if (NeoTrace.INFO) { NeoTrace.Trace("entityType", (BigInteger)((int)entityType)); } if (success) { if (operation == "add") { if (entityType == NeoDrawEntityType.User) { if (NeoTrace.INFO) { NeoTrace.Trace("add", "user"); } results = UserAdd(AppVAU, args); } else if (entityType == NeoDrawEntityType.Point) { if (NeoTrace.INFO) { NeoTrace.Trace("add", "point"); } results = PointAdd(AppVAU, args); } } else if (operation == "delete") { if (entityType == NeoDrawEntityType.User) { if (NeoTrace.INFO) { NeoTrace.Trace("delete", "user"); } results = UserDelete(AppVAU, args); } else if (entityType == NeoDrawEntityType.Point) { if (NeoTrace.INFO) { NeoTrace.Trace("delete", "point"); } results = PointDeleteLast(AppVAU, args); } } else if (operation == "get") { if (entityType == NeoDrawEntityType.User) { if (NeoTrace.INFO) { NeoTrace.Trace("get", "user"); } results = UserGetSingle(AppVAU, args); } else if (entityType == NeoDrawEntityType.Point) { if (NeoTrace.INFO) { NeoTrace.Trace("get", "point"); } results = PointGetSingle(AppVAU, args); } } else if (operation == "getall") { if (entityType == NeoDrawEntityType.User) { if (NeoTrace.INFO) { NeoTrace.Trace("getall", "user"); } results = UserGetAll(AppVAU, args); } else if (entityType == NeoDrawEntityType.Point) { if (NeoTrace.INFO) { NeoTrace.Trace("getall", "point"); } results = PointGetAll(AppVAU, args); } } else { if (NeoTrace.INFO) { NeoTrace.Trace("Unknown operation", operation); } message = "unknown operation '" + operation + "'"; if (NeoTrace.ERROR) { NeoTrace.Trace("**ERROR** operation", message); } success = false; } } if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("Main.results", results); } return(results); }
private static object[] PointAdd(NeoVersionedAppUser AppVAU, object[] args) { object[] results = { -1 }; if (NeoTrace.ARGSRESULTS) { NeoVersionedAppUser.LogExt("PointAdd.AppVAU", AppVAU); } byte[] encodedUsername = (byte[])args[0]; if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("PointAdd.encodedUsername", encodedUsername); } BigInteger x = (BigInteger)(args[1]); if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("PointAdd.x", x); } BigInteger y = (BigInteger)(args[2]); if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("PointAdd.y", y); } UserCredentials uc = FindUser(AppVAU, encodedUsername); if (UserCredentials.IsMissing(uc)) { if (NeoTrace.INFO) { UserCredentials.LogExt("PointAdd.user missing", uc); } } else { if (NeoTrace.INFO) { UserCredentials.LogExt("PointAdd.user exists", uc); } UserPoint up = UserPoint.New(x, y); BigInteger index = NeoCounter.TakeNextNumber(AppVAU, encodedUsername, NeoCounters.UserPointsCounter); if (NeoTrace.INFO) { NeoTrace.Trace("PointAdd.index", index); } UserPoint.PutElement(up, AppVAU, encodedUsername, (int)index); results = new object[] { index }; } if (NeoTrace.ARGSRESULTS) { NeoTrace.Trace("PointAdd.results", results); } return(results); }
public static void LogExt(string label, NeoCounter e) { NeoTrace.Trace(label, e._currentNumber, e._state); }
// Log/trace methods public static void Log(string label, UserPoint e) { NeoTrace.Trace(label, e._x, e._y); }
// Log/trace methods public static void Log(string label, UserCredentials e) { NeoTrace.Trace(label, e._encodedUsername, e._encodedPassword); }
public static object ProcessOperation(NPCNEP5Base tokenBase, string operation, object[] args) { object result = false; if (NeoTrace.VERBOSE) { NeoTrace.Trace("operation", operation); } if (operation == "deploy") { result = deploy(tokenBase); } else if (operation == "ping") { result = ping(); } else if (operation == "name") { result = name(tokenBase); } else if (operation == "symbol") { result = symbol(tokenBase); } else if (operation == "decimals") { result = decimals(tokenBase); } else if (operation == "totalSupply") { result = totalSupply(tokenBase); } else if (operation == "balanceOf") { byte[] account = (byte[])args[0]; if (NeoTrace.TESTING && account[0] == 0x0) // DEBUGGING { account = OwnerAccountScriptHash; if (NeoTrace.VERBOSE) { NeoTrace.Trace("DEBUGGING:from", account); } } result = balanceOf(account); } else if (operation == "transfer") { if (args.Length == 3) { // https://github.com/neo-project/examples-csharp/blob/master/ICO_Template/ICO_Template.cs#L59 byte[] from = (byte[])args[0]; byte[] to = (byte[])args[1]; BigInteger amount = (BigInteger)args[2]; if (NeoTrace.TESTING && from[0] == 0x0) // DEBUGGING { from = OwnerAccountScriptHash; if (NeoTrace.VERBOSE) { NeoTrace.Trace("DEBUGGING:from", from); } } result = transfer(from, to, amount); } } else if (operation == "allowance") { byte[] from = (byte[])args[0]; byte[] to = (byte[])args[1]; if (NeoTrace.TESTING && from[0] == 0x0) // DEBUGGING { from = OwnerAccountScriptHash; if (NeoTrace.VERBOSE) { NeoTrace.Trace("DEBUGGING:from", from); } } result = allowance(from, to); // optional } else if (operation == "transferFrom") { byte[] originator = OwnerAccountScriptHash; byte[] from = (byte[])args[0]; byte[] to = (byte[])args[1]; BigInteger amount = (BigInteger)args[2]; if (NeoTrace.TESTING && originator[0] == 0x0) // DEBUGGING { originator = OwnerAccountScriptHash; if (NeoTrace.VERBOSE) { NeoTrace.Trace("DEBUGGING:originator", originator); } } result = transferFrom(originator, from, to, amount); // optional } else if (operation == "approve") { byte[] originator = OwnerAccountScriptHash; byte[] to = (byte[])args[0]; BigInteger amount = (BigInteger)args[1]; if (NeoTrace.TESTING && originator[0] == 0x0) // DEBUGGING { originator = OwnerAccountScriptHash; if (NeoTrace.VERBOSE) { NeoTrace.Trace("DEBUGGING:originator", originator); } } result = approve(originator, to, amount); // optional } if (NeoTrace.VERBOSE) { NeoTrace.Trace("result", result); } return(result); }
public static bool transfer(byte[] from, byte[] to, BigInteger amount) { bool result = false; if (NeoTrace.VERBOSE) { NeoTrace.Trace("transfer():entered", from, to, amount); } // https://github.com/neo-project/examples-csharp/blob/master/ICO_Template/ICO_Template.cs#L146 if (amount > 0) { if (NeoTrace.VERBOSE) { NeoTrace.Trace("transfer():amount > 0"); } //if (Neo.SmartContract.Framework.Services.Neo.Runtime.CheckWitness(from)) { if (from == to) { if (NeoTrace.VERBOSE) { NeoTrace.Trace("transfer():from == to"); } result = true; } else { if (NeoTrace.VERBOSE) { NeoTrace.Trace("transfer():from != to"); } NPCNEP5LedgerEntry fromLedgerEntry = NPCNEP5LedgerEntry.Get(from); if (NeoTrace.VERBOSE) { NPCNEP5LedgerEntry.Log("transfer().fromLedgerEntry", fromLedgerEntry); } BigInteger fromBalance = NPCNEP5LedgerEntry.GetBalance(fromLedgerEntry); if (fromBalance >= amount) { if (NeoTrace.VERBOSE) { NeoTrace.Trace("transfer():fromBalance >= amount"); } //NPCEnvironment env = NPCEnvironment.New(); //NPCEnvironment.Initialize(env); //BigInteger timestamp = NPCEnvironment.GetBlockTimestamp(env); //uint h = Blockchain.GetHeight(); //Header header = Blockchain.GetHeader(h); //BigInteger timestamp = header.Timestamp; //Block block = Blockchain.GetBlock(h); // NEO Debugger: not implemented //BigInteger timestamp = block.Timestamp; BigInteger timestamp = 0; NPCNEP5LedgerEntry.Set(fromLedgerEntry, timestamp, 0 - amount, fromBalance - amount); NPCNEP5LedgerEntry.Put(fromLedgerEntry, from); if (NeoTrace.VERBOSE) { NPCNEP5LedgerEntry.Log("transfer().fromLedgerEntry", fromLedgerEntry); } NPCNEP5LedgerEntry toLedgerEntry = NPCNEP5LedgerEntry.Get(to); if (NeoTrace.VERBOSE) { NPCNEP5LedgerEntry.Log("transfer().toLedgerEntry", toLedgerEntry); } BigInteger toBalance = NPCNEP5LedgerEntry.GetBalance(toLedgerEntry); NPCNEP5LedgerEntry.Set(toLedgerEntry, timestamp, 0 + amount, toBalance + amount); NPCNEP5LedgerEntry.Put(toLedgerEntry, to); if (NeoTrace.VERBOSE) { NPCNEP5LedgerEntry.Log("transfer().toLedgerEntry", toLedgerEntry); } Transfer(from, to, amount); result = true; } } } } return(result); }