This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathInvokeContractDialog.cs
320 lines (300 loc) · 11.4 KB
/
InvokeContractDialog.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using Neo.IO.Json;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Properties;
using Neo.SmartContract;
using Neo.UI.Wrappers;
using Neo.VM;
using Neo.Wallets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Neo.UI
{
internal partial class InvokeContractDialog : Form
{
private InvocationTransaction tx;
private JObject abi;
private UInt160 script_hash;
private ContractParameter[] parameters;
private ContractParameter[] parameters_abi;
private List<TransactionAttributeWrapper> temp_signatures = new List<TransactionAttributeWrapper>();
private static readonly Fixed8 net_fee = Fixed8.FromDecimal(0.001m);
class WalletEntry
{
public WalletAccount Account;
public override string ToString()
{
if (!string.IsNullOrEmpty(Account.Label))
{
return $"[{Account.Label}] " + Account.Address;
}
return Account.Address;
}
}
public InvokeContractDialog(InvocationTransaction tx = null)
{
InitializeComponent();
this.tx = tx;
if (tx != null)
{
tabControl1.SelectedTab = tabPage2;
textBox6.Text = tx.Script.ToHexString();
}
comboBoxSignature.Items.AddRange(Program.CurrentWallet.GetAccounts().Where(u => u.HasKey).Select(u => new WalletEntry() { Account = u }).ToArray());
}
public InvocationTransaction GetTransaction(Fixed8 fee, UInt160 Change_Address = null)
{
if (tx.Size > 1024)
{
Fixed8 sumFee = Fixed8.FromDecimal(tx.Size * 0.00001m) + Fixed8.FromDecimal(0.001m);
if (fee < sumFee)
{
fee = sumFee;
}
}
if (Helper.CostRemind(tx.Gas.Ceiling(), fee))
{
InvocationTransaction result = Program.CurrentWallet.MakeTransaction(new InvocationTransaction
{
Version = tx.Version,
Script = tx.Script,
Gas = tx.Gas,
Attributes = tx.Attributes,
Outputs = tx.Outputs
}, change_address: Change_Address, fee: fee);
return result;
}
else
{
return null;
}
}
private void UpdateParameters()
{
parameters = new[]
{
new ContractParameter
{
Type = ContractParameterType.String,
Value = comboBox1.SelectedItem
},
new ContractParameter
{
Type = ContractParameterType.Array,
Value = parameters_abi
}
};
}
private void UpdateScript()
{
if (parameters.Any(p => p.Value == null)) return;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitAppCall(script_hash, parameters);
textBox6.Text = sb.ToArray().ToHexString();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = UInt160.TryParse(textBox1.Text, out _);
}
private void button1_Click(object sender, EventArgs e)
{
script_hash = UInt160.Parse(textBox1.Text);
ContractState contract = Blockchain.Singleton.Store.GetContracts().TryGet(script_hash);
if (contract == null) return;
parameters = contract.ParameterList.Select(p => new ContractParameter(p)).ToArray();
textBox2.Text = contract.Name;
textBox3.Text = contract.CodeVersion;
textBox4.Text = contract.Author;
textBox5.Text = string.Join(", ", contract.ParameterList);
button2.Enabled = parameters.Length > 0;
UpdateScript();
}
private void button2_Click(object sender, EventArgs e)
{
using (ParametersEditor dialog = new ParametersEditor(parameters))
{
dialog.ShowDialog();
}
UpdateScript();
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
button3.Enabled = false;
button5.Enabled = textBox6.TextLength > 0;
}
private void button5_Click(object sender, EventArgs e)
{
byte[] script;
try
{
script = textBox6.Text.Trim().HexToBytes();
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
return;
}
if (tx == null) tx = new InvocationTransaction();
tx.Version = 1;
tx.Script = script;
tx.Attributes = temp_signatures.Select(p => p.Unwrap()).ToArray();
if (tx.Inputs == null) tx.Inputs = new CoinReference[0];
if (tx.Outputs == null) tx.Outputs = new TransactionOutput[0];
if (tx.Witnesses == null) tx.Witnesses = new Witness[0];
if (tx.Attributes != null)
{
try
{
ContractParametersContext context;
context = new ContractParametersContext(tx);
Program.CurrentWallet.Sign(context);
tx.Witnesses = context.GetWitnesses();
}
catch (InvalidOperationException)
{
MessageBox.Show(Strings.UnsynchronizedBlock);
return;
}
}
else
{
tx.Witnesses = new Witness[0];
}
using (ApplicationEngine engine = ApplicationEngine.Run(tx.Script, tx, testMode: true))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"VM State: {engine.State}");
sb.AppendLine($"Gas Consumed: {engine.GasConsumed}");
sb.AppendLine($"Evaluation Stack: {new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()))}");
JObject notifications = engine.Service.Notifications.Select(q =>
{
JObject notification = new JObject();
notification["contract"] = q.ScriptHash.ToString();
try
{
notification["state"] = q.State.ToParameter().ToJson();
}
catch (InvalidOperationException)
{
notification["state"] = "error: recursive reference";
}
return notification;
}).ToArray();
sb.AppendLine($"Notifications: {notifications}");
textBox7.Text = sb.ToString();
if (!engine.State.HasFlag(VMState.FAULT))
{
tx.Gas = engine.GasConsumed - Fixed8.FromDecimal(10);
if (tx.Gas < Fixed8.Zero) tx.Gas = Fixed8.Zero;
tx.Gas = tx.Gas.Ceiling();
Fixed8 fee = tx.Gas;
label7.Text = fee + " gas";
button3.Enabled = true;
}
else
{
MessageBox.Show(Strings.ExecutionFailed);
}
}
}
private void button6_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
textBox6.Text = File.ReadAllBytes(openFileDialog1.FileName).ToHexString();
}
private void button7_Click(object sender, EventArgs e)
{
if (openFileDialog2.ShowDialog() != DialogResult.OK) return;
abi = JObject.Parse(File.ReadAllText(openFileDialog2.FileName));
script_hash = UInt160.Parse(abi["hash"].AsString());
textBox8.Text = script_hash.ToString();
comboBox1.Items.Clear();
comboBox1.Items.AddRange(((JArray)abi["functions"]).Select(p => p["name"].AsString()).Where(p => p != abi["entrypoint"].AsString()).ToArray());
textBox9.Clear();
button8.Enabled = false;
}
private void button8_Click(object sender, EventArgs e)
{
using (ParametersEditor dialog = new ParametersEditor(parameters_abi))
{
dialog.ShowDialog();
}
UpdateParameters();
UpdateScript();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(comboBox1.SelectedItem is string method)) return;
JArray functions = (JArray)abi["functions"];
JObject function = functions.First(p => p["name"].AsString() == method);
JArray _params = (JArray)function["parameters"];
parameters_abi = _params.Select(p => new ContractParameter(p["type"].TryGetEnum<ContractParameterType>())).ToArray();
textBox9.Text = string.Join(", ", _params.Select(p => p["name"].AsString()));
button8.Enabled = parameters_abi.Length > 0;
UpdateParameters();
UpdateScript();
}
private void Button9_Click(object sender, EventArgs e)
{
if (comboBoxSignature.SelectedItem.ToString() == "")
{
MessageBox.Show("Please choose the address");
return;
}
var index = comboBoxSignature.SelectedIndex;
temp_signatures.Add(new TransactionAttributeWrapper
{
Usage = TransactionAttributeUsage.Script,
Data = comboBoxSignature.SelectedItem.ToString().ToScriptHash().ToArray()
});
MessageBox.Show("Success!");
comboBoxSignature.Items.RemoveAt(index);
if (comboBoxSignature.Items.Count > 0)
{
comboBoxSignature.SelectedIndex = 0;
}
else
{
comboBoxSignature.SelectedText = "";
}
}
private void button9_Click_1(object sender, EventArgs e)
{
byte[] script;
try
{
script = textBox6.Text.Trim().HexToBytes();
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
return;
}
if (tx == null) tx = new InvocationTransaction();
tx.Version = 1;
tx.Script = script;
tx.Attributes = temp_signatures.Select(p => p.Unwrap()).ToArray();
if (tx.Inputs == null) tx.Inputs = new CoinReference[0];
if (tx.Outputs == null) tx.Outputs = new TransactionOutput[0];
if (tx.Witnesses == null) tx.Witnesses = new Witness[0];
try
{
ContractParametersContext context;
context = new ContractParametersContext(tx);
Clipboard.SetDataObject(context.ToString());
MessageBox.Show("Copied to clipboard");
}
catch (InvalidOperationException)
{
MessageBox.Show(Strings.UnsynchronizedBlock);
return;
}
}
}
}