forked from Relfos/neo-debugger-tools
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added client code generator (Neo-Lux only for now )
- Loading branch information
Showing
5 changed files
with
192 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using Neo.Emulation; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Neo.Debugger.Core.Generator | ||
{ | ||
public static class NeoLux | ||
{ | ||
public static string ConvertType(Emulator.Type type) | ||
{ | ||
switch (type) | ||
{ | ||
case Emulator.Type.Boolean: return "bool"; | ||
case Emulator.Type.String: return "string"; | ||
|
||
case Emulator.Type.Integer: return "BigInteger"; | ||
case Emulator.Type.ByteArray: return "byte[]"; | ||
case Emulator.Type.Array: return "object[]"; | ||
|
||
default: throw new ArgumentException("Invalid type: " + type); | ||
} | ||
} | ||
|
||
public static string GenerateInterface(ABI abi) | ||
{ | ||
var code = new StringBuilder(); | ||
|
||
code.AppendLine("using NeoLux;"); | ||
code.AppendLine("using Neo.Cryptography;"); | ||
code.AppendLine("using System.Numerics;"); | ||
code.AppendLine(); | ||
|
||
|
||
var contractName = abi.fileName; | ||
while (contractName.Contains(".")) | ||
{ | ||
contractName = Path.GetFileNameWithoutExtension(contractName); | ||
} | ||
|
||
code.AppendLine("public class " + contractName + " {"); | ||
code.AppendLine(); | ||
|
||
code.AppendLine("\tpublic string ContractHash { get; private set; }"); | ||
code.AppendLine(); | ||
|
||
code.AppendLine("\tpublic " + contractName + "(string contractHash) {"); | ||
code.AppendLine("\t\tthis.ContractHash = contractHash;"); | ||
code.AppendLine("\t}"); | ||
|
||
|
||
foreach (var entry in abi.functions) | ||
{ | ||
if (entry.Key == abi.entryPoint.name) | ||
{ | ||
continue; | ||
} | ||
|
||
var method = entry.Value; | ||
|
||
bool isPure = false; | ||
|
||
var returnType = ConvertType(method.returnType); | ||
code.AppendLine(); | ||
|
||
code.Append("\tpublic " + returnType + " " + entry.Key + "("); | ||
|
||
int count = 0; | ||
|
||
if (!isPure) | ||
{ | ||
code.Append("KeyPair from_key"); | ||
count++; | ||
} | ||
|
||
foreach (var arg in method.inputs) | ||
{ | ||
if (count > 0) code.Append(","); | ||
|
||
var argType = ConvertType(arg.type); | ||
code.Append(argType+" " +arg.name); | ||
count++; | ||
} | ||
|
||
code.AppendLine(") {"); | ||
|
||
code.Append("\t\tvar response = api.CallContract(from_key, contractHash, \"" + method.name + "\", new object[] { "); | ||
|
||
count = 0; | ||
if (!isPure) | ||
{ | ||
code.Append("from_key"); | ||
count++; | ||
} | ||
|
||
|
||
foreach (var arg in method.inputs) | ||
{ | ||
if (count > 0) code.Append(","); | ||
|
||
code.Append(arg.name); | ||
count++; | ||
} | ||
|
||
code.AppendLine("});"); | ||
|
||
switch (method.returnType) | ||
{ | ||
case Emulator.Type.Integer: code.AppendLine("\t\tvar result = new BigInteger((byte[])response.result[0]);"); break; | ||
case Emulator.Type.String: code.AppendLine("\t\tvar result = System.Text.Encoding.UTF8.GetString((byte[])response.result[0]);"); break; | ||
case Emulator.Type.Boolean: code.AppendLine("\t\tvar result = (bool)response.result[0];"); break; | ||
case Emulator.Type.ByteArray: code.AppendLine("\t\tvar result = (byte[])response.result[0];"); break; | ||
case Emulator.Type.Array: code.AppendLine("\t\tvar result = response.result[0];"); break; | ||
default: throw new ArgumentException("Invalid type: " + method.returnType); | ||
} | ||
|
||
|
||
code.AppendLine("\t\treturn result;"); | ||
|
||
code.AppendLine("\t}"); | ||
} | ||
|
||
code.AppendLine("}"); | ||
|
||
return code.ToString(); | ||
} | ||
} | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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