Skip to content

Commit 828322c

Browse files
committed
changing CMC API
1 parent 89077c1 commit 828322c

File tree

6 files changed

+67
-40
lines changed

6 files changed

+67
-40
lines changed
+30-18
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
1-
namespace GasPrice.Data.Models
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GasPrice.Data.Models
25
{
6+
public class Status
7+
{
8+
public DateTime timestamp { get; set; }
9+
public int error_code { get; set; }
10+
public object error_message { get; set; }
11+
public int elapsed { get; set; }
12+
public int credit_count { get; set; }
13+
public object notice { get; set; }
14+
}
15+
316
public class USD
417
{
5-
public decimal price { get; set; }
18+
public double price { get; set; }
619
public double volume_24h { get; set; }
7-
public double market_cap { get; set; }
820
public double percent_change_1h { get; set; }
921
public double percent_change_24h { get; set; }
1022
public double percent_change_7d { get; set; }
23+
public double market_cap { get; set; }
24+
public DateTime last_updated { get; set; }
1125
}
1226

13-
public class Quotes
27+
public class Quote
1428
{
1529
public USD USD { get; set; }
1630
}
1731

18-
public class Data
32+
public class Datum
1933
{
2034
public int id { get; set; }
2135
public string name { get; set; }
2236
public string symbol { get; set; }
23-
public string website_slug { get; set; }
24-
public int rank { get; set; }
37+
public string slug { get; set; }
38+
public int num_market_pairs { get; set; }
39+
public DateTime date_added { get; set; }
40+
public List<string> tags { get; set; }
41+
public int? max_supply { get; set; }
2542
public double circulating_supply { get; set; }
2643
public double total_supply { get; set; }
27-
public double? max_supply { get; set; }
28-
public Quotes quotes { get; set; }
29-
public int last_updated { get; set; }
30-
}
31-
32-
public class Metadata
33-
{
34-
public int timestamp { get; set; }
35-
public object error { get; set; }
44+
public object platform { get; set; }
45+
public int cmc_rank { get; set; }
46+
public DateTime Last_updated { get; set; }
47+
public Quote quote { get; set; }
3648
}
3749

3850
public class CoinMarketCapResult
3951
{
40-
public Data data { get; set; }
41-
public Metadata metadata { get; set; }
52+
public Status status { get; set; }
53+
public List<Datum> data { get; set; }
4254
}
4355
}

GasPrice.Data/Models/GasMeasurement.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public decimal RskGasPriceInUsd()
3333

3434
public decimal EthGasPriceInUsd()
3535
{
36-
var ethInBtc = EthGasPriceStandard / 10000000000;
36+
var ethInBtc = EthGasPriceStandard / 1000000000;
3737

3838
return ethInBtc * EthVsUsd * 21000;
3939
}

GasPrice.Data/Services/CryptoPriceService.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Net.Http;
1+
using System;
2+
using System.Configuration;
3+
using System.Net;
4+
using System.Net.Http;
25
using GasPrice.Data.Models;
36
using Newtonsoft.Json;
47

@@ -8,37 +11,40 @@ public class CoinMarketCapPriceService : ICryptoPriceService
811
{
912
/*
1013
COINMARKETCAP
11-
Please limit requests to no more than 30 per minute.
14+
Please limit requests to no more than 333 per day. (4´30´´ MAX)
1215
Endpoints update every 5 minutes.
13-
BTC: https://api.coinmarketcap.com/v2/ticker/1/
14-
ETH: https://api.coinmarketcap.com/v2/ticker/1027/
16+
https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest
1517
*/
1618

1719
public decimal GetBtcInUsd()
1820
{
19-
var c = new HttpClient();
20-
21-
var r = c.GetStringAsync("https://api.coinmarketcap.com/v2/ticker/1/").Result;
22-
23-
var o = JsonConvert.DeserializeObject<CoinMarketCapResult>(r);
24-
25-
return o.data.quotes.USD.price;
21+
return GetInUsd().Item1;
2622
}
2723

2824
public decimal GetEthInUsd()
2925
{
30-
var c = new HttpClient();
26+
return GetInUsd().Item2;
27+
}
3128

32-
var r = c.GetStringAsync("https://api.coinmarketcap.com/v2/ticker/1027/").Result;
29+
public Tuple<decimal, decimal> GetInUsd()
30+
{
31+
var c = new WebClient();
32+
c.Headers.Add("X-CMC_PRO_API_KEY", ConfigurationManager.AppSettings["CMC_PRO_API_KEY"]);
33+
34+
var r = c.DownloadString("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=2&convert=USD");
3335

3436
var o = JsonConvert.DeserializeObject<CoinMarketCapResult>(r);
3537

36-
return o.data.quotes.USD.price;
38+
return new Tuple<decimal, decimal>((decimal)o.data[0].quote.USD.price,
39+
(decimal)o.data[1].quote.USD.price);
40+
3741
}
3842
}
3943

4044
public interface ICryptoPriceService
4145
{
46+
Tuple<decimal, decimal> GetInUsd();
47+
4248
decimal GetBtcInUsd();
4349

4450
decimal GetEthInUsd();

GasPrice.Data/Services/GasPriceService.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ public GasPriceService(ICryptoPriceService cs)
2323
public GasMeasurement GetGasMeasure()
2424
{
2525
var ethPrice = GetEthGasPrice();
26-
26+
var cryptoPrice = _cs.GetInUsd();
27+
2728
return new GasMeasurement
2829
{
2930
RSKMinGasPrice = GetRskMinGasPrice(),
3031
EthGasPriceLow = ethPrice.Item1,
3132
EthGasPriceStandard = ethPrice.Item2,
3233
EthGasPriceFast = ethPrice.Item3,
33-
BtcVsUsd = _cs.GetBtcInUsd(),
34-
EthVsUsd = _cs.GetEthInUsd(),
34+
BtcVsUsd = cryptoPrice.Item1,
35+
EthVsUsd = cryptoPrice.Item2,
3536
};
3637
}
3738

@@ -73,9 +74,9 @@ public Tuple<decimal, decimal, decimal> GetEthGasPrice()
7374
var o = JsonConvert.DeserializeObject<EthGasPriceDTOModel>(r);
7475

7576
return new Tuple<decimal, decimal, decimal>(
76-
decimal.Parse(o.safeLow.ToString(CultureInfo.InvariantCulture)),
77-
decimal.Parse(o.average.ToString(CultureInfo.InvariantCulture)),
78-
decimal.Parse(o.fast.ToString(CultureInfo.InvariantCulture)));
77+
(decimal)o.safeLow / 100,
78+
(decimal)o.average / 100,
79+
(decimal)o.fast / 100);
7980
}
8081
}
8182

GasPrice.Job/Functions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace GasPrice.Job
99
{
1010
public class Functions
1111
{
12-
public static void GasRunner([TimerTrigger("0 * * * * *", RunOnStartup = true)] TimerInfo timerInfo, TextWriter log)
12+
public static void GasRunner([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timerInfo, TextWriter log)
1313
{
1414
log.WriteLine($"C# Timer trigger function executed at: {DateTime.Now}");
1515

GasPrice.Tests/CoinMarketCapTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ public void WhenLookingForETHWithUSDRateThenGetTheRate()
2525
Console.WriteLine(s.GetEthInUsd());
2626

2727
}
28+
29+
[TestMethod]
30+
public void WhenLookingForCryptoWithUSDRateThenGetTheRate()
31+
{
32+
var s = new CoinMarketCapPriceService();
33+
var r = s.GetInUsd();
34+
Console.WriteLine($"{r.Item1} {r.Item2}");
35+
}
2836
}
2937
}

0 commit comments

Comments
 (0)