Skip to content

Commit 76a0437

Browse files
committed
Fixed an issue where market entries were fetched by cryptocurrency names instead of their IDs.
Changed CryptoNameResolver to CryptocurrencyResolver.
1 parent 5134c72 commit 76a0437

File tree

6 files changed

+36
-30
lines changed

6 files changed

+36
-30
lines changed
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4+
using CryptoStatsSource.model;
45

56
namespace CryptoStatsSource
67
{
7-
public interface ICryptoNameResolver
8+
public interface ICryptocurrencyResolver
89
{
9-
public Task<string> Resolve(string symbol);
10+
public Task<Cryptocurrency> Resolve(string symbol);
1011

1112
public Task Refresh();
1213
}
1314

14-
public class CryptoNameResolverImpl : ICryptoNameResolver
15+
public class CryptocurrencyResolverImpl : ICryptocurrencyResolver
1516
{
1617
private ICryptoStatsSource _cryptoStatsSource;
17-
private Dictionary<String, String> _symbolToNameMap;
18+
private Dictionary<String, Cryptocurrency> _nameToCryptocurrencyDictionary;
1819

19-
public CryptoNameResolverImpl(ICryptoStatsSource cryptoStatsSource)
20+
public CryptocurrencyResolverImpl(ICryptoStatsSource cryptoStatsSource)
2021
{
2122
_cryptoStatsSource = cryptoStatsSource;
2223
}
2324

2425
public async Task Refresh()
2526
{
2627
// TODO improve this
27-
_symbolToNameMap = new();
28+
_nameToCryptocurrencyDictionary = new();
2829
(await _cryptoStatsSource.GetAvailableCryptocurrencies()).ForEach(c =>
29-
_symbolToNameMap.TryAdd(c.Symbol, c.Name));
30+
_nameToCryptocurrencyDictionary.TryAdd(c.Symbol, c));
3031
}
3132

32-
public async Task<string> Resolve(string symbol)
33+
public async Task<Cryptocurrency> Resolve(string symbol)
3334
{
34-
if (_symbolToNameMap?.GetValueOrDefault(symbol) == null) await Refresh();
35+
if (_nameToCryptocurrencyDictionary?.GetValueOrDefault(symbol) == null) await Refresh();
3536

36-
return _symbolToNameMap.GetValueOrDefault(symbol, null);
37+
return _nameToCryptocurrencyDictionary.GetValueOrDefault(symbol, null);
3738
}
3839
}
3940
}

CryptoStatsSource/Model/MarketEntry.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
using System.Text.Json;
12
using Newtonsoft.Json;
23

34
namespace CryptoStatsSource.model
45
{
56
public record MarketEntry(string Id, string Symbol, string Name, decimal CurrentPrice, long MarketCap,
6-
[JsonProperty("price_change_24h")] float PriceChange24H, [JsonProperty("price_change_percentage_24h")] float PriceChangePercentage24H);
7+
[JsonProperty("price_change_24h", NullValueHandling = NullValueHandling.Ignore)]
8+
float? PriceChange24H = 0f,
9+
[JsonProperty("price_change_percentage_24h", NullValueHandling = NullValueHandling.Ignore)]
10+
float? PriceChangePercentage24H = 0f
11+
);
712

813
public record PriceEntry(string Id, string Symbol, string Name, decimal CurrentPrice,
914
float PriceChangePercentage24H);

Tests/Integration/CryptoStatsSource/CryptoNameResolverTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Tests.Integration.CryptoStatsSource
66
public class ResolveNameTest
77
{
88
private readonly CoingeckoSource _source;
9-
private readonly CryptoNameResolverImpl _resolver;
9+
private readonly CryptocurrencyResolverImpl _resolver;
1010

1111
public ResolveNameTest()
1212
{
@@ -17,10 +17,10 @@ public ResolveNameTest()
1717
[Fact]
1818
public async void SimpleThreeEntries()
1919
{
20-
Assert.Equal("Bitcoin", await _resolver.Resolve("btc"));
21-
Assert.Equal("Cardano", await _resolver.Resolve("ada"));
22-
Assert.Equal("Litecoin", await _resolver.Resolve("ltc"));
23-
Assert.Equal("Ethereum", await _resolver.Resolve("eth"));
20+
Assert.Equal(new("btc", "btc", "Bitcoin"), await _resolver.Resolve("btc"));
21+
Assert.Equal(new ("ada", "ada", "Cardano"), await _resolver.Resolve("ada"));
22+
Assert.Equal(new ("ltc", "ltc", "Litecoin"), await _resolver.Resolve("ltc"));
23+
Assert.Equal(new("eth", "eth", "Ethereum"), await _resolver.Resolve("eth"));
2424
Assert.Null(await _resolver.Resolve("abcefghbzbzrfoo"));
2525
}
2626
}

WebFrontend/Pages/PortfolioDetail.razor

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@inject IMarketOrderService MarketOrderService;
1212
@inject ICryptoStatsSource CryptoStatsSource;
1313
@inject ISummaryService SummaryService;
14-
@inject ICryptoNameResolver CryptoNameResolver;
14+
@inject ICryptocurrencyResolver CryptocurrencyResolver;
1515

1616
<style>
1717
.demo-mat-card {
@@ -140,15 +140,15 @@
140140
private async void _loadEntryInfo()
141141
{
142142
// resolve names of all portfolio entries
143-
await CryptoNameResolver.Refresh();
144-
var portfolioEntriesNames = await Task.WhenAll(
143+
await CryptocurrencyResolver.Refresh();
144+
var portfolioCryptocurrencyEntries = await Task.WhenAll(
145145
ActivePortfolioEntries.Select(
146-
async entry => (await CryptoNameResolver.Resolve(entry.Symbol)).ToLower())
146+
async entry => (await CryptocurrencyResolver.Resolve(entry.Symbol)))
147147
);
148148

149149
// fetch market entries of all entries of the portfolio
150150
var marketEntries = await CryptoStatsSource.GetMarketEntries(
151-
CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency).ToLower(), portfolioEntriesNames
151+
CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency).ToLower(), portfolioCryptocurrencyEntries.Select(c => c.Id).ToArray()
152152
);
153153

154154
// create a dictionary where a symbol is mapped to a market entry
@@ -191,13 +191,13 @@
191191

192192
// create portfolio entry table rows
193193
PortfolioEntryRows = entrySummaries.Zip(ActivePortfolioEntries).Select(
194-
(tuple) => new PortfolioEntryRow(
194+
tuple => new PortfolioEntryRow(
195195
// symbol of the portfolio entry
196196
tuple.Second.Symbol,
197197
// current price of the entry's asset
198198
symbolsToMarketEntries[tuple.Second.Symbol].CurrentPrice,
199199
// asset's price change since the last 24h
200-
new decimal(symbolsToMarketEntries[tuple.Second.Symbol].PriceChangePercentage24H),
200+
new decimal(symbolsToMarketEntries[tuple.Second.Symbol].PriceChangePercentage24H ?? 0),
201201
// percentage within the portfolio entry
202202
portfolioTotalMarketValue > 0 ? (tuple.First.MarketValue / portfolioTotalMarketValue) * 100 : 0,
203203
// absolute change within the portfolio entry

WebFrontend/Pages/PortfolioEntryDetail.razor

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@inject IPortfolioService PortfolioService
1111
@inject IPortfolioEntryService PortfolioEntryService
1212
@inject IMarketOrderService MarketOrderService
13-
@inject ICryptoNameResolver CryptoNameResolver
13+
@inject ICryptocurrencyResolver CryptocurrencyResolver
1414
@inject ICryptoStatsSource CryptoStatsSource
1515
@inject ISummaryService SummaryService
1616

@@ -50,9 +50,9 @@
5050
<div class="demo-mat-card-content">
5151
<MatHeadline6 class="clear-margin">
5252
<MatChipSet Style="align-items: center">
53-
@if (PortfolioEntryName != null)
53+
@if (PortfolioCryptocurrencyEntry != null)
5454
{
55-
<MatH5 Class="clear-margin-vertical">@PortfolioEntryName</MatH5>
55+
<MatH5 Class="clear-margin-vertical">@PortfolioCryptocurrencyEntry.Name</MatH5>
5656
}
5757
else
5858
{
@@ -216,7 +216,7 @@
216216
protected Portfolio ActivePortfolio;
217217
protected PortfolioEntry ActivePortfolioEntry;
218218
protected MarketEntry CurrentEntryAssetMarketEntry;
219-
protected string PortfolioEntryName = "Bitcoin";
219+
protected Cryptocurrency PortfolioCryptocurrencyEntry;
220220
protected ISummaryService.Summary EntrySummary;
221221
protected decimal TotalHoldings = 0;
222222

@@ -237,7 +237,7 @@
237237
protected override async Task OnInitializedAsync()
238238
{
239239
// resolve the name of the cryptocurrency (using the symbol)
240-
PortfolioEntryName = await CryptoNameResolver.Resolve(ActivePortfolioEntry.Symbol);
240+
PortfolioCryptocurrencyEntry = await CryptocurrencyResolver.Resolve(ActivePortfolioEntry.Symbol);
241241

242242
await UpdateEntrySummary();
243243
}
@@ -257,7 +257,7 @@
257257
// TODO null?
258258
CurrentEntryAssetMarketEntry = (await CryptoStatsSource.GetMarketEntries(
259259
CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency).ToLower(),
260-
PortfolioEntryName.ToLower()
260+
PortfolioCryptocurrencyEntry.Id
261261
))[0];
262262

263263
// get all orders of the portfolio entry

WebFrontend/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void ConfigureServices(IServiceCollection services)
5353
var db = new SqlKataDatabase(dbConnection, new SqliteCompiler());
5454
services.AddSingleton(ctx => db);
5555

56-
services.AddSingleton<ICryptoNameResolver, CryptoNameResolverImpl>();
56+
services.AddSingleton<ICryptocurrencyResolver, CryptocurrencyResolverImpl>();
5757
services.AddSingleton<ISummaryService, SummaryServiceImpl>();
5858

5959
services.AddSingleton<IPortfolioRepository, SqlKataPortfolioRepository>();

0 commit comments

Comments
 (0)