-
Notifications
You must be signed in to change notification settings - Fork 100
/
RpcInvokeResult.cs
77 lines (66 loc) · 2.22 KB
/
RpcInvokeResult.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
using Neo.IO.Json;
using Neo.SmartContract;
using System;
using System.Linq;
namespace Neo.Network.RPC.Models
{
public class RpcInvokeResult
{
public string Script { get; set; }
public VM.VMState State { get; set; }
public string GasConsumed { get; set; }
public ContractParameter[] Stack { get; set; }
public string Tx { get; set; }
public JObject ToJson()
{
JObject json = new JObject();
json["script"] = Script;
json["state"] = State;
json["gasconsumed"] = GasConsumed;
try
{
json["stack"] = new JArray(Stack.Select(p => p.ToJson()));
}
catch (InvalidOperationException)
{
// ContractParameter.ToJson() may cause InvalidOperationException
json["stack"] = "error: recursive reference";
}
if (!string.IsNullOrEmpty(Tx)) json["tx"] = Tx;
return json;
}
public static RpcInvokeResult FromJson(JObject json)
{
RpcInvokeResult invokeScriptResult = new RpcInvokeResult();
invokeScriptResult.Script = json["script"].AsString();
invokeScriptResult.State = json["state"].TryGetEnum<VM.VMState>();
invokeScriptResult.GasConsumed = json["gasconsumed"].AsString();
try
{
invokeScriptResult.Stack = ((JArray)json["stack"]).Select(p => ContractParameter.FromJson(p)).ToArray();
}
catch { }
invokeScriptResult.Tx = json["tx"]?.AsString();
return invokeScriptResult;
}
}
public class RpcStack
{
public string Type { get; set; }
public string Value { get; set; }
public JObject ToJson()
{
JObject json = new JObject();
json["type"] = Type;
json["value"] = Value;
return json;
}
public static RpcStack FromJson(JObject json)
{
RpcStack stackJson = new RpcStack();
stackJson.Type = json["type"].AsString();
stackJson.Value = json["value"].AsString();
return stackJson;
}
}
}