/
TransactionsService.cs
143 lines (126 loc) · 4.39 KB
/
TransactionsService.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VEDriversLite;
using VEDriversLite.Common;
using VEDriversLite.NeblioAPI;
public enum TxWay
{
In,
Out,
Sub,
}
public class TxDetails
{
public GetTransactionInfoResponse Info { get; set; } = new GetTransactionInfoResponse();
public string Sender { get; set; } = string.Empty;
public string Receiver { get; set; } = string.Empty;
public bool FromAnotherAccount { get; set; } = false;
public bool FromSubAccount { get; set; } = false;
public DateTime Time { get; set; } = DateTime.MinValue;
public TxWay Way
{
get
{
if (FromAnotherAccount)
return TxWay.In;
else if (FromAnotherAccount)
return TxWay.Sub;
return TxWay.Out;
}
}
}
public class TransactionsService
{
public async Task<List<string>> LoadTransactions(string address, NeblioAccount account, bool subAccount)
{
if (string.IsNullOrEmpty(address))
return new List<string>();
try
{
if (!subAccount)
{
var inf = await NeblioAPIHelpers.AddressInfoAsync(account.Address);
return inf.Transactions?.Reverse().ToList() ?? new List<string>();
}
else
{
if (account.SubAccounts.TryGetValue(address, out _))
{
var inf = await NeblioAPIHelpers.AddressInfoAsync(address);
return inf.Transactions?.Reverse().ToList() ?? new List<string>();
}
}
}
catch(Exception ex)
{
await Console.Out.WriteLineAsync("Cannot load txs: " + ex.Message);
}
return new List<string>();
}
public async Task<TxDetails> LoadTxDetails(string txid, NeblioAccount account)
{
if (string.IsNullOrEmpty(txid))
return new TxDetails();
try
{
var tinfo = await NeblioAPIHelpers.GetTransactionInfo(txid);
if (tinfo == null)
return new TxDetails();
string sender = await NeblioAPIHelpers.GetTransactionSender(txid, tinfo);
bool fromAnotherAccount = true;
bool fromSubAccount = true;
var sendbkm = account.IsInTheBookmarks(sender);
if (sender == account.Address)
{
if (sendbkm.Item1)
sender = sendbkm.Item2.Name;
else
sender = "Main Account";
fromAnotherAccount = false;
fromSubAccount = false;
}
else if (account.SubAccounts.TryGetValue(sender, out var sacc))
{
if (!string.IsNullOrEmpty(sacc.Name))
sender = sacc.Name;
else
sender = sacc.BookmarkFromAccount.Name;
if (sendbkm.Item1)
sender = sendbkm.Item2.Name;
fromAnotherAccount = false;
fromSubAccount = true;
}
string rec = await NeblioAPIHelpers.GetTransactionReceiver(txid, tinfo);
string receiver = string.Empty;
var recbkm = account.IsInTheBookmarks(rec);
if (rec == account.Address)
{
if (sendbkm.Item1)
sender = sendbkm.Item2.Name;
else
receiver = "Main Account";
}
else if (recbkm.Item1)
receiver = recbkm.Item2.Name;
if (string.IsNullOrEmpty(receiver))
receiver = rec;// NeblioTransactionHelpers.ShortenAddress(rec);
var time = TimeHelpers.UnixTimestampToDateTime((double)tinfo.Blocktime);
return new TxDetails()
{
FromAnotherAccount = fromAnotherAccount,
FromSubAccount = fromSubAccount,
Info = tinfo,
Receiver = receiver,
Sender = sender,
Time = time,
};
}
catch(Exception ex)
{
await Console.Out.WriteLineAsync("Cannot load tx details: " + ex.Message);
}
return new TxDetails();
}
}