-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathArtifacts.cs
161 lines (128 loc) · 5.43 KB
/
Artifacts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Neo.SmartContract.Manifest;
using System;
using System.Linq;
using System.Text;
namespace Neo.SmartContract.TestEngine
{
public class Artifacts
{
/// <summary>
/// Create source code from contract Abi
/// </summary>
/// <param name="name">Contract name</param>
/// <param name="abi">Abi</param>
/// <returns>Source</returns>
public static string CreateSourceFromManifest(string name, ContractAbi abi)
{
StringBuilder sourceCode = new();
sourceCode.AppendLine("using System.Collections.Generic;");
sourceCode.AppendLine("using System.Numerics;");
sourceCode.AppendLine("");
sourceCode.AppendLine("namespace Neo.TestEngine.Contracts;");
sourceCode.AppendLine("");
sourceCode.AppendLine($"public abstract class {name} : Neo.SmartContract.TestEngine.Mocks.SmartContract");
sourceCode.AppendLine("{");
// Crete events
if (abi.Events.Any())
{
sourceCode.AppendLine("#region Events");
foreach (var ev in abi.Events.OrderBy(u => u.Name))
{
sourceCode.Append(CreateSourceEventFromManifest(ev));
}
sourceCode.AppendLine("#endregion");
}
// Crete methods
if (abi.Methods.Any(u => u.Safe))
{
sourceCode.AppendLine("#region Safe methods");
foreach (var method in abi.Methods.Where(u => u.Safe).OrderBy(u => u.Name))
{
// This method can't be called, so avoid them
if (method.Name.StartsWith("_")) continue;
sourceCode.Append(CreateSourceMethodFromManifest(method));
}
sourceCode.AppendLine("#endregion");
}
if (abi.Methods.Any(u => !u.Safe))
{
sourceCode.AppendLine("#region Unsafe methods");
foreach (var method in abi.Methods.Where(u => !u.Safe).OrderBy(u => u.Name))
{
// This method can't be called, so avoid them
if (method.Name.StartsWith("_")) continue;
sourceCode.Append(CreateSourceMethodFromManifest(method));
}
sourceCode.AppendLine("#endregion");
}
// Create constructor
sourceCode.AppendLine("#region Constructor for internal use only");
sourceCode.AppendLine($" protected {name}(Neo.SmartContract.TestEngine.TestEngine testEngine, Neo.UInt160 hash) : base(testEngine, hash) {{}}");
sourceCode.AppendLine("#endregion");
sourceCode.AppendLine("}");
return sourceCode.ToString();
}
/// <summary>
/// Create source code from event
/// </summary>
/// <param name="ev">Event</param>
/// <returns>Source</returns>
private static string CreateSourceEventFromManifest(ContractEventDescriptor ev)
{
StringBuilder sourceCode = new();
sourceCode.Append($" public delegate void del{ev.Name}(");
bool isFirst = true;
foreach (var arg in ev.Parameters)
{
if (!isFirst) sourceCode.Append(", ");
else isFirst = false;
sourceCode.Append($"{TypeToSource(arg.Type)} {arg.Name}");
}
sourceCode.AppendLine(");");
sourceCode.AppendLine($" public event del{ev.Name}? {ev.Name};");
return sourceCode.ToString();
}
/// <summary>
/// Create source code from manifest method
/// </summary>
/// <param name="method">Contract method</param>
/// <returns>Source</returns>
private static string CreateSourceMethodFromManifest(ContractMethodDescriptor method)
{
StringBuilder sourceCode = new();
sourceCode.Append($" public abstract {TypeToSource(method.ReturnType)} {method.Name}(");
bool isFirst = true;
foreach (var arg in method.Parameters)
{
if (!isFirst) sourceCode.Append(", ");
else isFirst = false;
sourceCode.Append($"{TypeToSource(arg.Type)} {arg.Name}");
}
sourceCode.AppendLine(");");
return sourceCode.ToString();
}
/// <summary>
/// Type to source
/// </summary>
/// <param name="type">Type</param>
/// <returns>c# type</returns>
private static string TypeToSource(ContractParameterType type)
{
return type switch
{
ContractParameterType.Boolean => "bool",
ContractParameterType.Integer => "BigInteger",
ContractParameterType.String => "string",
ContractParameterType.Hash160 => "UInt160",
ContractParameterType.Hash256 => "UInt256",
ContractParameterType.PublicKey => "ECPoint",
ContractParameterType.ByteArray => "byte[]",
ContractParameterType.Signature => "byte[]",
ContractParameterType.Array => "List<object>",
ContractParameterType.Map => "IDictionary<object,object>",
ContractParameterType.Void => "void",
_ => "object",
};
}
}
}