Commit 848c1a11 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Made history smarter

parent 6ab3dbb1
...@@ -235,20 +235,28 @@ This endpoint will get the last address containing no transaction or will create ...@@ -235,20 +235,28 @@ This endpoint will get the last address containing no transaction or will create
### Responses ### Responses
``` ```
{ {
"transactions": [ "transactionsHistory": [
{ {
"address": "1H2jbtknP6jRYx2riaXJf3H9Mb1JC6kcL2", "type": "send",
"txId": "b800f9b24a9c49a375cddf4fc8c484722af0bec7d23ac65b782daf1b0089bb29", "id": "6358161c713688e372481fce7f20f3f8692ab2e4e657f3d9afa750ebee54e6c3",
"amount": -50360386, "amount": 500000,
"confirmed": true, "payments": [
"timestamp": "1337803568" {
"destinationAddress": "mt7W2Zf69KC9472TPCzUeLLhBDSmC82AWz",
"amount": 500000
}
],
"fee": 100000,
"confirmedInBlock": 1122310,
"timestamp": "1494594937"
}, },
{ {
"address": "1H2jbtknP6jRYx2riaXJf3H9Mb1JC6kcL2", "type": "received",
"txId": "9c0560a34f88573a71ebf68a2540cb7215b55bc2ddee0af3cb1dc343f2f3e0da", "toAddress": "mnDsG7kTYCeVNqnEmfvdYeNgZwxhjqm2jc",
"amount": 53845026, "id": "75ce74643aae01ccbe2bbc05efb4788cc9a16a9192add4d7082561a40a541057",
"confirmed": true, "amount": 110000000,
"timestamp": "1337605326" "confirmedInBlock": 1122292,
"timestamp": "1494591670"
} }
] ]
} }
......
...@@ -185,26 +185,50 @@ namespace Breeze.Wallet.Controllers ...@@ -185,26 +185,50 @@ namespace Breeze.Wallet.Controllers
try try
{ {
WalletHistoryModel model = new WalletHistoryModel { Transactions = new List<TransactionItem>() }; WalletHistoryModel model = new WalletHistoryModel { TransactionsHistory = new List<TransactionItemModel>() };
// get transactions contained in the wallet // get transactions contained in the wallet
var addresses = this.walletManager.GetHistoryByCoinType(request.WalletName, request.CoinType); var addresses = this.walletManager.GetHistoryByCoinType(request.WalletName, request.CoinType);
foreach (var address in addresses) foreach (var address in addresses.Where(a => !a.IsChangeAddress()))
{ {
foreach (var transaction in address.Transactions) foreach (var transaction in address.Transactions)
{ {
model.Transactions.Add(new TransactionItem TransactionItemModel item = new TransactionItemModel();
if (transaction.Amount > Money.Zero)
{ {
Amount = transaction.Amount, item.Type = TransactionItemType.Received;
Confirmed = transaction.IsConfirmed(), item.ToAddress = address.Address;
Timestamp = transaction.CreationTime, item.Amount = transaction.Amount;
TransactionId = transaction.Id, }
Address = address.Address else
}); {
item.Type = TransactionItemType.Send;
item.Amount = Money.Zero;
item.Payments = new List<PaymentDetailModel>();
foreach (var payment in transaction.Payments)
{
item.Payments.Add(new PaymentDetailModel
{
DestinationAddress = payment.DestinationAddress,
Amount = payment.Amount
});
item.Amount += payment.Amount;
}
var changeAddress = addresses.Single(a => a.IsChangeAddress() && a.Transactions.Any(t => t.Id == transaction.Id));
item.Fee = transaction.Amount.Abs() - item.Amount - changeAddress.Transactions.First(t => t.Id == transaction.Id).Amount;
}
item.Id = transaction.Id;
item.Timestamp = transaction.CreationTime;
item.ConfirmedInBlock = transaction.BlockHeight;
model.TransactionsHistory.Add(item);
} }
} }
model.Transactions = model.Transactions.OrderByDescending(t => t.Timestamp).ToList(); model.TransactionsHistory = model.TransactionsHistory.OrderByDescending(t => t.Timestamp).ToList();
return this.Json(model); return this.Json(model);
} }
catch (Exception e) catch (Exception e)
......
...@@ -4,35 +4,75 @@ using Breeze.Wallet.JsonConverters; ...@@ -4,35 +4,75 @@ using Breeze.Wallet.JsonConverters;
using NBitcoin; using NBitcoin;
using NBitcoin.JsonConverters; using NBitcoin.JsonConverters;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace Breeze.Wallet.Models namespace Breeze.Wallet.Models
{ {
public class WalletHistoryModel public class WalletHistoryModel
{ {
[JsonProperty(PropertyName = "transactions")] [JsonProperty(PropertyName = "transactionsHistory")]
public List<TransactionItem> Transactions { get; set; } public List<TransactionItemModel> TransactionsHistory { get; set; }
} }
public class TransactionItem public class TransactionItemModel
{ {
[JsonProperty(PropertyName = "type")]
[JsonConverter(typeof(StringEnumConverter), true)]
public TransactionItemType Type { get; set; }
/// <summary> /// <summary>
/// The Base58 representation of this address. /// The Base58 representation of this address.
/// </summary> /// </summary>
[JsonProperty(PropertyName = "address")] [JsonProperty(PropertyName = "toAddress", NullValueHandling = NullValueHandling.Ignore)]
public string Address { get; set; } public string ToAddress { get; set; }
[JsonProperty(PropertyName = "txId")] [JsonProperty(PropertyName = "id")]
[JsonConverter(typeof(UInt256JsonConverter))] [JsonConverter(typeof(UInt256JsonConverter))]
public uint256 TransactionId { get; set; } public uint256 Id { get; set; }
[JsonProperty(PropertyName = "amount")] [JsonProperty(PropertyName = "amount")]
public Money Amount { get; set; } public Money Amount { get; set; }
[JsonProperty(PropertyName = "confirmed")] /// <summary>
public bool Confirmed { get; set; } /// A list of payments made out in this transaction.
/// </summary>
[JsonProperty(PropertyName = "payments", NullValueHandling = NullValueHandling.Ignore)]
public ICollection<PaymentDetailModel> Payments { get; set; }
[JsonProperty(PropertyName = "fee", NullValueHandling = NullValueHandling.Ignore)]
public Money Fee { get; set; }
/// <summary>
/// The height of the block in which this transaction was confirmed.
/// </summary>
[JsonProperty(PropertyName = "confirmedInBlock", NullValueHandling = NullValueHandling.Ignore)]
public int? ConfirmedInBlock { get; set; }
[JsonProperty(PropertyName = "timestamp")] [JsonProperty(PropertyName = "timestamp")]
[JsonConverter(typeof(DateTimeOffsetConverter))] [JsonConverter(typeof(DateTimeOffsetConverter))]
public DateTimeOffset Timestamp { get; set; } public DateTimeOffset Timestamp { get; set; }
} }
public class PaymentDetailModel
{
/// <summary>
/// The Base58 representation of the destination address.
/// </summary>
[JsonProperty(PropertyName = "destinationAddress")]
public string DestinationAddress { get; set; }
/// <summary>
/// The transaction amount.
/// </summary>
[JsonProperty(PropertyName = "amount")]
[JsonConverter(typeof(MoneyJsonConverter))]
public Money Amount { get; set; }
}
public enum TransactionItemType
{
Received,
Send
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment