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
### Responses
```
{
"transactions": [
"transactionsHistory": [
{
"address": "1H2jbtknP6jRYx2riaXJf3H9Mb1JC6kcL2",
"txId": "b800f9b24a9c49a375cddf4fc8c484722af0bec7d23ac65b782daf1b0089bb29",
"amount": -50360386,
"confirmed": true,
"timestamp": "1337803568"
"type": "send",
"id": "6358161c713688e372481fce7f20f3f8692ab2e4e657f3d9afa750ebee54e6c3",
"amount": 500000,
"payments": [
{
"destinationAddress": "mt7W2Zf69KC9472TPCzUeLLhBDSmC82AWz",
"amount": 500000
}
],
"fee": 100000,
"confirmedInBlock": 1122310,
"timestamp": "1494594937"
},
{
"address": "1H2jbtknP6jRYx2riaXJf3H9Mb1JC6kcL2",
"txId": "9c0560a34f88573a71ebf68a2540cb7215b55bc2ddee0af3cb1dc343f2f3e0da",
"amount": 53845026,
"confirmed": true,
"timestamp": "1337605326"
"type": "received",
"toAddress": "mnDsG7kTYCeVNqnEmfvdYeNgZwxhjqm2jc",
"id": "75ce74643aae01ccbe2bbc05efb4788cc9a16a9192add4d7082561a40a541057",
"amount": 110000000,
"confirmedInBlock": 1122292,
"timestamp": "1494591670"
}
]
}
......
......@@ -185,26 +185,50 @@ namespace Breeze.Wallet.Controllers
try
{
WalletHistoryModel model = new WalletHistoryModel { Transactions = new List<TransactionItem>() };
WalletHistoryModel model = new WalletHistoryModel { TransactionsHistory = new List<TransactionItemModel>() };
// get transactions contained in the wallet
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)
{
model.Transactions.Add(new TransactionItem
TransactionItemModel item = new TransactionItemModel();
if (transaction.Amount > Money.Zero)
{
Amount = transaction.Amount,
Confirmed = transaction.IsConfirmed(),
Timestamp = transaction.CreationTime,
TransactionId = transaction.Id,
Address = address.Address
});
item.Type = TransactionItemType.Received;
item.ToAddress = address.Address;
item.Amount = transaction.Amount;
}
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);
}
catch (Exception e)
......
......@@ -4,35 +4,75 @@ using Breeze.Wallet.JsonConverters;
using NBitcoin;
using NBitcoin.JsonConverters;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace Breeze.Wallet.Models
{
public class WalletHistoryModel
{
[JsonProperty(PropertyName = "transactions")]
public List<TransactionItem> Transactions { get; set; }
[JsonProperty(PropertyName = "transactionsHistory")]
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>
/// The Base58 representation of this address.
/// </summary>
[JsonProperty(PropertyName = "address")]
public string Address { get; set; }
[JsonProperty(PropertyName = "toAddress", NullValueHandling = NullValueHandling.Ignore)]
public string ToAddress { get; set; }
[JsonProperty(PropertyName = "txId")]
[JsonProperty(PropertyName = "id")]
[JsonConverter(typeof(UInt256JsonConverter))]
public uint256 TransactionId { get; set; }
public uint256 Id { get; set; }
[JsonProperty(PropertyName = "amount")]
public Money Amount { get; set; }
[JsonProperty(PropertyName = "confirmed")]
public bool Confirmed { get; set; }
/// <summary>
/// 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")]
[JsonConverter(typeof(DateTimeOffsetConverter))]
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