-
Notifications
You must be signed in to change notification settings - Fork 22
/
wallet.cs
761 lines (696 loc) · 33.7 KB
/
wallet.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TurtleWallet
{
public partial class wallet : Form
{
public static System.Windows.Forms.Timer statsTimer = new System.Windows.Forms.Timer();
public static int watchdogTimeout = 3;
public static int watchdogMaxTry = 3;
public static int currentTimeout = 0;
public static int currentTry = 0;
public static int staticFee = 10;
public static Label _selectedTab;
public static List<string> cachedTrx = new List<string>();
public static Process runningDaemon;
public static WindowLogger windowLogger;
public static int globalRefreshCount = 0;
public string walletPath
{
get;
set;
}
public string walletPassword
{
get;
set;
}
//List view header formatters
public static void colorListViewHeader(ref ListView list, Color backColor, Color foreColor)
{
list.OwnerDraw = true;
list.DrawColumnHeader +=
new DrawListViewColumnHeaderEventHandler
(
(sender, e) => headerDraw(sender, e, backColor, foreColor)
);
list.DrawItem += new DrawListViewItemEventHandler(bodyDraw);
list.DrawSubItem += List_DrawSubItem;
}
private static void List_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
e.DrawDefault = true;
}
private static void headerDraw(object sender, DrawListViewColumnHeaderEventArgs e, Color backColor, Color foreColor)
{
e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);
e.Graphics.DrawRectangle(SystemPens.GradientInactiveCaption,
new Rectangle(e.Bounds.X, 0, e.Bounds.Width, e.Bounds.Height));
e.Graphics.DrawString(e.Header.Text, e.Font, new SolidBrush(foreColor), e.Bounds);
}
private static void bodyDraw(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
public wallet(string _wallet, string _pass, System.Diagnostics.Process wd)
{
InitializeComponent();
runningDaemon = wd;
wallet.colorListViewHeader(ref txList, Color.FromArgb(29, 29, 29), Color.FromArgb(187, 186, 185));
_selectedTab = homeButton;
walletPath = _wallet;
walletPassword = _pass;
Properties.Settings.Default.walletPath = _wallet;
Properties.Settings.Default.hasWallet = true;
Properties.Settings.Default.Save();
//feeAmountText.Text = Properties.Settings.Default.defaultFee.ToString();
//feeAmountText.Enabled = false;
windowLogger = new WindowLogger();
walletTabControl.SelectedIndex = 0;
feeComboBox.SelectedIndex = 0;
}
private void wallet_Load(object sender, EventArgs e)
{
versionLabel.Text = "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
windowLogger.Log(LogTextbox, "TurtleCoin Wallet " + "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version + " has started ...");
new Thread(new ThreadStart(update_live_stats)).Start();
windowLogger.Log(LogTextbox, "Live Stats Update thread started ...");
statsTimer.Interval = 30000;
statsTimer.Tick += StatsTimer_Tick;
statsTimer.Start();
windowLogger.Log(LogTextbox, "Live Stats Update timer started ...");
resyncer.RunWorkerAsync();
windowLogger.Log(LogTextbox, "Network Sync Thread started ...");
}
private void StatsTimer_Tick(object sender, EventArgs e)
{
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Updating LiveStats ...";
updateLabel.ForeColor = Color.FromArgb(255, 128, 0);
});
new Thread(new ThreadStart(update_live_stats)).Start();
}
private void update_live_stats()
{
try
{
var jobj = ConnectionManager.get_live_stats();
if(jobj.Item1 == false)
{
this.heightAmountLabel.BeginInvoke((MethodInvoker)delegate () { heightAmountLabel.Text = "N/A"; });
this.difficultyAmountLabel.BeginInvoke((MethodInvoker)delegate () { difficultyAmountLabel.Text = "N/A"; });
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "LiveStats update failed ...";
updateLabel.ForeColor = Color.FromArgb(205, 12, 47);
});
System.Threading.Thread.Sleep(2000);
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Wallet Idle ...";
updateLabel.ForeColor = Color.Gray;
});
return;
}
var stats = (Newtonsoft.Json.Linq.JObject)jobj.Item2["network"];
if(stats.ContainsKey("difficulty"))
this.difficultyAmountLabel.BeginInvoke((MethodInvoker)delegate () { difficultyAmountLabel.Text = stats["difficulty"].ToString(); });
else
{
this.difficultyAmountLabel.BeginInvoke((MethodInvoker)delegate () { difficultyAmountLabel.Text = "N/A"; });
}
if (stats.ContainsKey("height"))
this.heightAmountLabel.BeginInvoke((MethodInvoker)delegate () { heightAmountLabel.Text = stats["height"].ToString(); });
else
{
this.heightAmountLabel.BeginInvoke((MethodInvoker)delegate () { heightAmountLabel.Text = "N/A"; });
}
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Updated livestats ...";
updateLabel.ForeColor = Color.FromArgb(0, 192, 0);
});
System.Threading.Thread.Sleep(2000);
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Wallet Idle ...";
updateLabel.ForeColor = Color.Gray;
});
}
catch (Exception)
{
try
{
this.heightAmountLabel.BeginInvoke((MethodInvoker)delegate () { heightAmountLabel.Text = "N/A"; });
this.difficultyAmountLabel.BeginInvoke((MethodInvoker)delegate () { difficultyAmountLabel.Text = "N/A"; });
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Livestats update failed ...";
updateLabel.ForeColor = Color.FromArgb(205, 12, 47);
});
windowLogger.Log(LogTextbox, "Livestats update failed ...");
System.Threading.Thread.Sleep(2000);
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Wallet Idle ...";
updateLabel.ForeColor = Color.Gray;
});
}
catch
{
//empty catch when application exits
}
}
}
private void refresh_ui()
{
globalRefreshCount++;
Newtonsoft.Json.Linq.JObject status = null;
Newtonsoft.Json.Linq.JToken blocks = null;
try
{
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Syncing Network ...";
updateLabel.ForeColor = Color.FromArgb(255, 128, 0);
});
var balances = ConnectionManager.request("getBalance");
if (balances.Item1 == false)
{
throw new Exception("getBalance call failed: " + balances.Item2);
}
float availableBal = (float)(balances.Item3["availableBalance"]) / 100;
float lockedBal = (float)(balances.Item3["lockedAmount"]) / 100;
this.availableAmountLabel.BeginInvoke((MethodInvoker)delegate () { this.availableAmountLabel.Text = availableBal.ToString("0.00") + " TRTL"; });
this.lockedAmountLabel.BeginInvoke((MethodInvoker)delegate () { this.lockedAmountLabel.Text = lockedBal.ToString("0.00") + " TRTL"; });
var Addresses = ConnectionManager.request("getAddresses");
if (Addresses.Item1 == false)
{
throw new Exception("getAddresses call failed: " + balances.Item2);
}
var addressList = Addresses.Item3["addresses"];
this.myAddressText.BeginInvoke((MethodInvoker)delegate () { myAddressText.Text = addressList[0].ToString(); });
status = ConnectionManager.request("getStatus").Item3;
var parameters = new Dictionary<string, object>()
{
{ "blockCount", (int)status["blockCount"] },
{ "firstBlockIndex", 1 },
{ "addresses", addressList }
};
var blocksRet = ConnectionManager.request("getTransactions", parameters);
if (blocksRet.Item1 == false)
{
throw new Exception("getTransactions call failed: " + balances.Item2);
}
blocks = blocksRet.Item3["items"];
currentTimeout = 0;
currentTry = 0;
}
catch(Exception ex)
{
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Daemon error, retrying ...";
windowLogger.Log(LogTextbox, "Daemon error, retrying ...");
updateLabel.ForeColor = Color.FromArgb(205, 12, 47);
});
if (currentTimeout >= watchdogTimeout)
{
if (currentTry <= watchdogMaxTry)
{
//daemon restart
}
else
{
MessageBox.Show("Turtle Wallet has tried numerous times to relaunch the needed daemon and has failed. Please relaunch the wallet!", "Walletd daemon could not be recovered!", MessageBoxButtons.OK, MessageBoxIcon.Error);
windowLogger.Log(LogTextbox, "Turtle Wallet has tried numerous times to relaunch the needed daemon and has failed. Please relaunch the wallet!");
this.Close();
}
}
else
currentTimeout++;
return;
}
if (blocks == null)
return;
foreach(var block in blocks.Reverse())
{
var bblock = (Newtonsoft.Json.Linq.JObject)block;
if(bblock.ContainsKey("transactions"))
{
foreach(var transaction in block["transactions"])
{
if (cachedTrx.Contains(transaction["transactionHash"].ToString()))
continue;
string address = "";
long desired_transfer_amount = 0;
if ((long)transaction["amount"] < 0)
{
desired_transfer_amount = ((long)transaction["amount"] + (long)transaction["fee"]) * -1;
}
else
{
desired_transfer_amount = ((long)transaction["amount"]);
}
foreach(var transfer in transaction["transfers"])
{
if((long)transfer["amount"] == desired_transfer_amount)
{
address = transfer["address"].ToString();
}
}
List<ListViewItem.ListViewSubItem> subitems = new List<ListViewItem.ListViewSubItem>();
if((long)transaction["unlockTime"] == 0 || (long)transaction["unlockTime"] <= (long)status["blockCount"] - 40)
{
var confirmItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, " 🐢 ✔", System.Drawing.Color.Green, System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 13.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(confirmItem);
}
else
{
var confirmItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, " 🐢 ✘", System.Drawing.Color.DarkRed, System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 13.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(confirmItem);
}
if((long)transaction["amount"] > 0)
{
var directionItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "IN\u2007\u2007⇚\u2007\u2007\u2007", System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0))))), System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(directionItem);
}
else
{
var directionItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, "OUT ⇛\u2007\u2007\u2007", System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0))))), System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(directionItem);
}
var amountItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, ((long)(transaction["amount"]) / 100).ToString("0.00"), System.Drawing.Color.White, System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(amountItem);
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds((long)(transaction["timestamp"])).ToLocalTime();
var ts = dtDateTime.ToString("yyyy/MM/dd HH:mm:ss tt");
var dateItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, ts, System.Drawing.Color.White, System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(dateItem);
var addItem = new System.Windows.Forms.ListViewItem.ListViewSubItem(null, address, System.Drawing.Color.White, System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(29)))), ((int)(((byte)(29))))), new System.Drawing.Font("Segoe UI Semibold", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))));
subitems.Add(addItem);
System.Windows.Forms.ListViewItem trxItem = new System.Windows.Forms.ListViewItem(subitems.ToArray(), -1);
trxItem.UseItemStyleForSubItems = false;
txList.BeginInvoke((MethodInvoker)delegate ()
{
if (globalRefreshCount > 1)
txList.Items.Insert(0, trxItem);
else
txList.Items.Add(trxItem);
foreach (ColumnHeader column in txList.Columns)
{
column.Width = -2;
}
});
cachedTrx.Add(transaction["transactionHash"].ToString());
windowLogger.Log(LogTextbox, "Found transaction " + transaction["transactionHash"].ToString() + ". Added to list ...");
}
}
}
string titleUpdate = "TurtleCoin Wallet - Network Sync [" + status["blockCount"].ToString() + " / " + status["knownBlockCount"].ToString() + "] | Peers: " + status["peerCount"].ToString() + " | Updated: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss tt");
this.BeginInvoke((MethodInvoker)delegate ()
{
this.Text = titleUpdate;
this.Update();
});
}
private void homeButton_MouseEnter(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void sendButton_MouseEnter(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void logButton_MouseEnter(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void rpcButton_MouseEnter(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void homeButton_MouseLeave(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void sendButton_MouseLeave(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void logButton_MouseLeave(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void rpcButton_MouseLeave(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
}
private void copyAddressButton_MouseEnter(object sender, EventArgs e)
{
var currentButton = (Label)sender;
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
private void copyAddressButton_MouseLeave(object sender, EventArgs e)
{
var currentButton = (Label)sender;
var backcolor = Color.FromArgb(52,52,52);
var forcolor = Color.FromArgb(224, 224, 224);
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
private void homeButton_Click(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
walletTabControl.SelectedIndex = 0;
_selectedTab = currentButton;
backcolor = Color.FromArgb(82, 82, 82);
forcolor = Color.FromArgb(39, 170, 107);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
}
}
private void sendButton_Click(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
walletTabControl.SelectedIndex = 1;
_selectedTab = currentButton;
backcolor = Color.FromArgb(82,82,82);
forcolor = Color.FromArgb(39, 170, 107);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
}
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
e.DrawText();
}
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
private void copyAddressButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(myAddressText.Text);
MessageBox.Show("Address copied to clipboard!", "TurtleCoin Wallet");
}
private void resyncer_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (true)
{
refresh_ui();
System.Threading.Thread.Sleep(5000);
this.updateLabel.BeginInvoke((MethodInvoker)delegate ()
{
updateLabel.Text = "Wallet Idle ...";
updateLabel.ForeColor = Color.Gray;
});
}
}
catch
{
//Empty catch for when the app exits, and the thread didnt have time to shut down.
}
}
private void wallet_FormClosing(object sender, FormClosingEventArgs e)
{
runningDaemon.Kill();
}
private void sendTrtlButton_Click(object sender, EventArgs e)
{
string sendAddr = recipientAddressText.Text;
int amount = 0;
int fee = 10;
if (!sendAddr.StartsWith("TRTL") || sendAddr.Length <= 50)
{
MessageBox.Show("The address you are sending to is invalid, please check it.", "TurtleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string myAddr = myAddressText.Text;
if(sendAddr == myAddr)
{
MessageBox.Show("Sending to yourself is not supported.", "TurtleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
amount = (int)(sendAmountText.Value * 100);
if(amount <= 0)
{
MessageBox.Show("Invalid send amount.", "TurtleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
catch(Exception ex)
{
MessageBox.Show("Invalid send amount.", "TurtleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (feeComboBox.SelectedIndex == 0)
fee = Percent(amount, 0.005);
else if (feeComboBox.SelectedIndex == 1)
fee = Percent(amount, 0.01);
else if (feeComboBox.SelectedIndex == 2)
fee = Percent(amount, 0.025);
else if (feeComboBox.SelectedIndex == 3)
fee = Percent(amount, 0.05);
if (fee < 10)
fee = 10;
int mixins = (int)mixinNumeric.Value;
List<int> TransactionAmounts = new List<int>();
int numberOfTransactions = 1;
if(amount > 50000000)
{
int wholeTrxs = (int)Math.Floor(amount / (double)50000000);
int diff = amount - (wholeTrxs * 50000000);
for (int i = 0; i < wholeTrxs; i++)
{
TransactionAmounts.Add(50000000);
}
TransactionAmounts.Add(diff);
}
else
{
TransactionAmounts.Add(amount);
}
var transfers = new List<Dictionary<string, object>>();
foreach (var Tamount in TransactionAmounts)
{
transfers.Add(new Dictionary<string, object>() { { "amount", Tamount }, { "address", sendAddr } });
}
var args = new Dictionary<string, object>()
{
{ "anonymity", mixins },
{ "fee", fee },
{ "transfers", transfers }
};
try
{
var resp = ConnectionManager.request("sendTransaction", args);
if(resp.Item1 == false)
{
MessageBox.Show("Error occured on send:" + Environment.NewLine + resp.Item2, "TurleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
windowLogger.Log(LogTextbox, "Error occured on send:" + Environment.NewLine + resp.Item2);
return;
}
string txhash = resp.Item3["transactionHash"].ToString();
MessageBox.Show("Transaction send was successful!" + Environment.NewLine + "Amount: " + ((float)amount / 100).ToString() + Environment.NewLine + "Mix: " + mixins.ToString() + Environment.NewLine + "To: " + sendAddr + Environment.NewLine + "Trx hash: " + txhash, "TurtleCoin Wallet", MessageBoxButtons.OK,MessageBoxIcon.Information);
windowLogger.Log(LogTextbox, "Transaction send was successful!" + Environment.NewLine + "Amount: " + ((float)amount / 100).ToString() + Environment.NewLine + "Mix: " + mixins.ToString() + Environment.NewLine + "To: " + sendAddr + Environment.NewLine + "Trx hash: " + txhash);
}
catch(Exception ex)
{
MessageBox.Show("Error occured on send:" + Environment.NewLine + ex.Message, "TurleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
windowLogger.Log(LogTextbox, "Error occured on send:" + Environment.NewLine + ex.Message);
}
}
private void sendRPCButton_Click(object sender, EventArgs e)
{
if(methodTextbox.Text == "")
{
MessageBox.Show("Invalid method on RPC send", "TurtleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (argTextbox.Text == "")
{
MessageBox.Show("Invalid argument on RPC send. If there are no arguments, use '{}'.", "TurtleCoin Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var req = ConnectionManager._requestRPC(methodTextbox.Text, argTextbox.Text);
rpcTextbox.AppendText(Environment.NewLine + req + Environment.NewLine);
}
catch(Exception ex)
{
rpcTextbox.AppendText(Environment.NewLine + "ERROR: " + ex.ToString() + Environment.NewLine);
}
}
private void logButton_Click(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
walletTabControl.SelectedIndex = 2;
_selectedTab = currentButton;
backcolor = Color.FromArgb(82, 82, 82);
forcolor = Color.FromArgb(39, 170, 107);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
}
}
private void rpcButton_Click(object sender, EventArgs e)
{
var currentButton = (Label)sender;
if (_selectedTab != currentButton)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
walletTabControl.SelectedIndex = 3;
_selectedTab = currentButton;
backcolor = Color.FromArgb(82, 82, 82);
forcolor = Color.FromArgb(39, 170, 107);
_selectedTab.BackColor = backcolor;
_selectedTab.ForeColor = forcolor;
}
}
private void sendAmountText_ValueChanged(object sender, EventArgs e)
{
if(sendAmountText.Value <= 100000)
{
mixinNumeric.Value = 3;
feeComboBox.SelectedIndex = 0;
sendButton.Enabled = true;
sendButton.Cursor = System.Windows.Forms.Cursors.Hand;
ToolTip disabledTooltip = new ToolTip();
disabledTooltip.SetToolTip(sendButton, "Send TRTL!");
}
else if(sendAmountText.Value <= 500000)
{
mixinNumeric.Value = 5;
feeComboBox.SelectedIndex = 1;
sendButton.Enabled = true;
sendButton.Cursor = System.Windows.Forms.Cursors.Hand;
ToolTip disabledTooltip = new ToolTip();
disabledTooltip.SetToolTip(sendButton, "Send TRTL!");
}
else if (sendAmountText.Value <= 1500000)
{
mixinNumeric.Value = 8;
feeComboBox.SelectedIndex = 2;
sendButton.Enabled = true;
sendButton.Cursor = System.Windows.Forms.Cursors.Hand;
ToolTip disabledTooltip = new ToolTip();
disabledTooltip.SetToolTip(sendButton, "Send TRTL!");
}
else if (sendAmountText.Value >= 1500001)
{
mixinNumeric.Value = 13;
feeComboBox.SelectedIndex = 3;
sendButton.Enabled = true;
sendButton.Cursor = System.Windows.Forms.Cursors.Hand;
ToolTip disabledTooltip = new ToolTip();
disabledTooltip.SetToolTip(sendButton, "Send TRTL!");
}
else
{
sendButton.Enabled = false;
sendButton.Cursor = System.Windows.Forms.Cursors.No;
ToolTip disabledTooltip = new ToolTip();
disabledTooltip.SetToolTip(sendButton, "Invalid Amount!");
}
}
public static int Percent(int number, double percent)
{
return (int)Math.Round(((double)number * percent) / 100);
}
}
}