-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddressControler.cs
More file actions
319 lines (245 loc) · 10.3 KB
/
Copy pathAddressControler.cs
File metadata and controls
319 lines (245 loc) · 10.3 KB
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
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using System.IO;
using SAPI.API.Model;
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
namespace SAPI.API.Controllers
{
[Route("api/[controller]")]
public class AddressController : BaseController
{
public AddressController(IHostingEnvironment hostingEnvironment, ILogger<AddressController> log) : base(hostingEnvironment, log)
{
}
/// <summary>
/// Returns address balance
/// </summary>
[HttpGet("balance/{address}", Name = "Balance")]
public IActionResult GetBalance(string address)
{
List<AddressBalance> balance = new List<AddressBalance>();
string selectString = @" SELECT TOP 1 Address,
Received,
Sent,
Balance
FROM [vAddressBalance]
WHERE Address = @Address";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(selectString, conn))
{
comm.Parameters.AddWithValue("@Address", address);
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
balance = DataReaderMapToList<AddressBalance>(dr);
}
}
catch (Exception ex)
{
return BadRequest(ex.ToErrorObject());
}
}
return new ObjectResult(balance);
}
}
/// <summary>
/// Returns address balance
/// </summary>
[HttpPost("deposits", Name = "DepositHistory")]
public IActionResult GetDepositHistory([FromBody] DepositHistoryRequestModel data)
{
if (data.PageSize > 100)
data.PageSize = 100;
if (data.PageNumber < 1)
data.PageNumber = 1;
List<DepositHistory> history = new List<DepositHistory>();
string cmdString = "EXEC Address_Deposit_History @address, @dateFrom, @dateTo, @PageNumber, @PageSize";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("@address", data.Address);
comm.Parameters.AddWithValue("@dateFrom", (object)data.DateFrom ?? DBNull.Value);
comm.Parameters.AddWithValue("@dateTo", (object)data.DateTo ?? DBNull.Value);
comm.Parameters.AddWithValue("@PageNumber", (object)data.PageNumber ?? 1);
comm.Parameters.AddWithValue("@PageSize", (object)data.PageSize ?? 10);
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
history = DataReaderMapToList<DepositHistory>(dr);
}
}
catch (Exception ex)
{
return BadRequest(ex.ToErrorObject());
}
}
}
return new ObjectResult(history);
}
[HttpPost("balances/", Name = "Balances")]
public IActionResult GetBalances([FromBody] List<string> addresses)
{
List<AddressBalance> balance = new List<AddressBalance>();
List<string> query = new List<string>();
foreach (var item in addresses)
{
query.Add("'" + item + "'");
}
string selectString = @"
SELECT a.Address,
a.Received,
a.Sent,
a.Balance
FROM vAddressBalance a
WHERE a.Address in (" + string.Join(",", query) + ")";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(selectString, conn))
{
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
balance = DataReaderMapToList<AddressBalance>(dr);
}
}
catch (Exception ex)
{
return BadRequest(ex.ToErrorObject());
}
}
return new ObjectResult(balance);
}
}
[HttpGet("unspent/{address}", Name = "Unspent")]
public IActionResult GetUnspent(string address)
{
List<AddressUnspent> unspent = new List<AddressUnspent>();
string selectString = @" SELECT Txid,
[Index],
Address,
Value
FROM [vAddressUnspent]
WHERE Address = @Address";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(selectString, conn))
{
comm.Parameters.AddWithValue("@Address", address);
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
unspent = DataReaderMapToList<AddressUnspent>(dr);
}
}
catch (Exception ex)
{
return BadRequest(ex.ToErrorObject());
}
}
return new ObjectResult(unspent);
}
}
[HttpPost("unspent/AvailableInputs", Name = "GetAvailableInputs")]
public IActionResult GetAvailableInputs([FromBody] AvailableInputsRequest request)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
List<AddressUnspent> unspent = new List<AddressUnspent>();
string selectString = @" EXEC Address_Available_Inputs @Address";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(selectString, conn))
{
comm.Parameters.AddWithValue("@Address", request.Address);
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
unspent = DataReaderMapToList<AddressUnspent>(dr);
}
}
catch (Exception ex)
{
return BadRequest(ex.ToErrorObject());
}
}
}
var memPool = CoinService.GetRawMemPool(false);
var memInputs = new List<string>();
foreach (var item in memPool.TxIds)
{
memInputs.AddRange((CoinService.GetRawTransaction(item, 1).Vin).Select(t => t.TxId));
}
unspent = unspent.Where(u => !memInputs.Contains(u.Txid)).ToList();
decimal fee = 0.001m;
var newFee = (((unspent.Count * 148) + (2 * 34) + 10 + 9) / 1024m) * fee;
if (newFee > fee)
fee = newFee;
fee = (decimal)RoundUp((double)fee, 3);
if (unspent.Count == 0 && memInputs.Count > 0)
return BadRequest(new Exception("Transactions being confirmed, please try again in one minute.").ToErrorObject());
if (unspent.Sum(u => u.Value) < (request.Amount + fee))
return BadRequest(new Exception("Amount exceeds the balance of [" + unspent.Sum(u => u.Value).ToString() + "]").ToErrorObject());
List<AddressUnspent> data = new List<AddressUnspent>();
foreach (var item in unspent)
{
data.Add(item);
if (data.Sum(d => d.Value) >= request.Amount)
break;
}
fee = 0.001m;
newFee = (((data.Count * 148) + (2 * 34) + 10 + 9) / 1024m) * fee;
if (newFee > fee)
fee = newFee;
fee = (decimal)RoundUp((double)fee, 3);
string scriptPubKey = string.Empty;
foreach (var item in data)
{
var raw = CoinService.GetRawTransaction(item.Txid, 1);
foreach (var v in raw.Vout)
{
if (Convert.ToDecimal(v.Value) == item.Value && v.ScriptPubKey.Addresses.ToList().Contains(item.Address))
{
scriptPubKey = v.ScriptPubKey.Hex;
break;
}
}
if (!string.IsNullOrEmpty(scriptPubKey))
break;
}
uint blockHeight = CoinService.GetBlockCount();
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
return new ObjectResult(new
{
BlockHeight = blockHeight,
Fee = fee,
ScriptPubKey = scriptPubKey,
Inputs = data,
Execution = elapsedMs
});
}
public static double RoundUp(double input, int places)
{
double multiplier = Math.Pow(10, Convert.ToDouble(places));
return Math.Ceiling(input * multiplier) / multiplier;
}
}
}