Develop Ethereum Price Feed Plugin Using .Net Core

In my earlier post Bitcoin Price API, I talked about how to use data feed provided by CoinDesk to implement a .Net Core widget to display latest Bitcoin prices. I was also looking for data feed that would allow to build similar widget for Ethereum prices as well. Unfortunately I could not find any published API that could provide Ethereum price data. Then I ran into EthereumPrice.org web site. I did some digging into their HTTP requests and found that their page is making asyn requests to one of their end points to get Ethereum price data for last 10 minutes. I took that HTTP end point, looked at the response body and adapted to my .Net Core widget project.

EthereumPrice.org Feed

On their chart display page, the web site uses following GET requests to get the data.

https://api.ethereumdb.com/v1/timeseries?pair=ETH-USD&range=10mi&type=line

This GET request returns Json data in following format.

[
 {
   "price":2062.2012371001465,
   "quoteVolume24h":685300525.6392536,
    "timestamp":1617572490},
 {
    "price":2061.5985552933444,
    "quoteVolume24h":684877759.5591846,
    "timestamp":1617572480
}]

Actual response returns 60 elements in an array for last minutes. You can not request less than that data. It is controlled by range query string parameter. This was enough information for me to build my C# data structure to deserialize the response into proper format.

Ethereum Price

Feed Data Structure

public record EthereumPriceFeedModel : BaseAmgrModel
    {
        public record PriceIndexValue
        {
            [JsonProperty("price")]
            public double Rate { get; set; }
            [JsonProperty("timestamp")]
            public double UpdateTime { get; set; }
            [JsonProperty("quoteVolume24h")]
            public double Volume { get; set; }
        }
    }

MVC Controller To Fetch Data

[HttpPost]
        public async Task<IActionResult> LatestPrice()
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient(AmgrHttpDefaults.DefaultHttpClientForCompressedResponse);
                var priceFeed = await httpClient.GetStringAsync
                  (string.IsNullOrEmpty(_feedSettings.PriceFeedUrl) ? PriceIndexFeedUrl: _feedSettings.PriceFeedUrl);
                var priceIndexFeed = JsonConvert.DeserializeObject<IList<EthereumPriceFeedModel.PriceIndexValue>>(priceFeed);
                var priceIndexValues = new List<EthereumPriceModel>();
                if (null == priceIndexFeed) return Json(priceIndexValues);
                foreach (var priceIndexValue in priceIndexFeed)
                {
                    priceIndexValues.Add(new EthereumPriceModel
                    {
                        UpdatedAtUtc = _dateTimeHelper.ConvertToTimeFromUnixTime
                           (priceIndexValue.UpdateTime).ToString("MMM dd, yyyy HH:mm:ss UTC"),
                        Price = priceIndexValue.Rate
                    });
                }

                return Json(priceIndexValues);
            }
            catch (Exception ex)
            {
                await _logger.ErrorAsync($"Error getting latest ethereum price", ex);
            }

            return Json("");
        }

There is a small caveat in this implementation. The website returns data in compressed format. You will need to uncompress it before you can deserialize it. I will show that trick in another post. You can see this Widget in action in my another blog World of Hobby Craft where the prices are displayed in right side column of page layout.

Search

Social

Weather

7.9 °C / 46.2 °F

weather conditions Rain

Monthly Posts

Blog Tags