Skip to content

Commit

Permalink
Improve crypto matching (#72)
Browse files Browse the repository at this point in the history
* - 'Borrowed' the crypto list from ghostfolio
- use a fixed list of crypto to convert coins

* fixing tests
  • Loading branch information
VibeNL authored Dec 13, 2023
1 parent 4525c44 commit 50f8005
Show file tree
Hide file tree
Showing 6 changed files with 8,654 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task ConvertActivitiesForAccount_SingleBuy_Converted()
var account = fixture.Build<Account>().With(x => x.Balance, Balance.Empty(DefaultCurrency.EUR)).Create();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByIdentifier("USDC", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset1);
api.Setup(x => x.FindSymbolByIdentifier("USD Coin", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset1);

// Act
account = await parser.ConvertActivitiesForAccount(account.Name, new[] { "./FileImporter/TestFiles/Nexo/BuyOrders/single_buy.csv" });
Expand Down Expand Up @@ -96,8 +96,8 @@ public async Task ConvertActivitiesForAccount_SingleConvert_Converted()
var account = fixture.Build<Account>().With(x => x.Balance, Balance.Empty(DefaultCurrency.EUR)).Create();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByIdentifier("USDC", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset1);
api.Setup(x => x.FindSymbolByIdentifier("BTC", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset2);
api.Setup(x => x.FindSymbolByIdentifier("USD Coin", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset1);
api.Setup(x => x.FindSymbolByIdentifier("Bitcoin", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset2);

// Act
account = await parser.ConvertActivitiesForAccount(account.Name, new[] { "./FileImporter/TestFiles/Nexo/BuyOrders/single_convert.csv" });
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task ConvertActivitiesForAccount_SingleCashbackCrypto_Converted()
var account = fixture.Build<Account>().With(x => x.Balance, Balance.Empty(DefaultCurrency.EUR)).Create();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByIdentifier("BTC", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset);
api.Setup(x => x.FindSymbolByIdentifier("Bitcoin", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset);

// Act
account = await parser.ConvertActivitiesForAccount(account.Name, new[] { "./FileImporter/TestFiles/Nexo/Specials/single_cashback_crypto.csv" });
Expand Down Expand Up @@ -224,7 +224,7 @@ public async Task ConvertActivitiesForAccount_SingleReferralBonusApproved_Conver
var account = fixture.Build<Account>().With(x => x.Balance, Balance.Empty(DefaultCurrency.EUR)).Create();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByIdentifier("BTC", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset1);
api.Setup(x => x.FindSymbolByIdentifier("Bitcoin", It.IsAny<Currency>(), It.IsAny<AssetClass?[]>(), It.IsAny<AssetSubClass?[]>())).ReturnsAsync(asset1);

// Act
account = await parser.ConvertActivitiesForAccount(account.Name, new[] { "./FileImporter/TestFiles/Nexo/Specials/single_referralbonus_approved.csv" });
Expand Down
29 changes: 29 additions & 0 deletions GhostfolioSidekick/FileImporter/Crypto/CryptoMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Newtonsoft.Json.Linq;

namespace GhostfolioSidekick.FileImporter.Crypto
{
internal class CryptoMapper
{
private Dictionary<string, string> mappings = new Dictionary<string, string>();

private CryptoMapper()
{
var assembly = typeof(CryptoMapper).Assembly;
using var resource = assembly.GetManifestResourceStream("GhostfolioSidekick.FileImporter.Crypto.cryptocurrencies.json");
using var streamReader = new StreamReader(resource);
var fileContent = streamReader.ReadToEnd();
var obj = JObject.Parse(fileContent);
foreach (var item in obj)
{
mappings.Add(item.Key, item.Value.ToString());
}
}

public static readonly CryptoMapper Instance = new CryptoMapper();

internal string? GetFullname(string symbol)
{
return mappings.TryGetValue(symbol, out var value) ? value : symbol;
}
}
}
Loading

0 comments on commit 50f8005

Please sign in to comment.