Get DeFi Projects data with DeFi Pulse API
Tags: Dotnet, Defi, Ethereum, Cryptocurrency, Blockchain

DeFi Pulse

DeFi Pulse is a website where you can get DeFi protocol’s analytics and ranking. It’s used a lot by the community to keep track of a protocol’s Total Value Locked (TVL).

DeFi Pulse offers API Endpoints where we can get data from multiple protocols. For example you can call the API to get a detailed breakdown of the Total Value Locked (TVL) by protocol or get the current borrowing and lending rates from DeFi projects. The API also allow us to get information about Ethereum’s Gas Price, which can be very helpful. The API documentation can be found below:

In order to call the API you will need an API Key. In order to create one, you have to register on their website (https://data.defipulse.com/).The API key will go on the url as a query string when making a request.

Building the sdk

As we will be making HTTP calls to the API, the class will need to get the HttpClient class by construction injection. We will be using the IHttpClientFactory in order to create the HttpClient. This is the recommended approach when dealing with HTTP requests.

We will create a class called DeFiPulse.cs that will take 2 parametes, the HttpClient created by the IHttpClientFactory and the API Key:

DeFiPulse.cs

Get DeFi Projects Market Overview Endpoint

With an instance of HttpClient and the API Key, we can call the API endpoints. Instead of doing a simple _httpClient.GetAsync() , validate the payload and then deserialize it into our class, we will download the System.Net.Http.Json library and use the _httpClient.GetFromJsonAsync<T>() extension method that already does that for us. In order to add it to our project, we will simply run the following cli command:

dotnet add package System.Net.Http.Json --version 5.0.0

In example below we will call the GetDeFiProjectsMarketOverview Endpoint:

Call the GetDeFiProjectsMarketOverview Endpoint

We will get the following JSON response:

GetDeFiProjectsMarketOverview JSON response

Add DeFiPulse class to the Dependency Injection Container

In order to have acess to the DeFiPulse class we have built, we will add it to the DI Container. In order to do so, we will create the IDeFiPulse interface and create the extension method AddDeFiPulse as we can see in the code below. We create an extension method for the IServiceCollection interface because when using this sdk, we will simply add in the StartUp.cs class the services.AddDeFiPulse("Your API Key here"); .

AddDeFiPulse extension method

In the code above we have used the HttpClient typed client that is just an HttpClient that's pre-configured for some specific use.

Create Custom DeFiPulse Exception

C# has built-in exception types such as ArgumentNullException and NullReferenceException . We can create our own custom Exception that fits our business needs. In our case we will create a DeFiPulseException . In order to do that, we will create our class that derives from the Exception class:

We can now catch and throw our DeFiPulseException as seen in the example below when calling the API in order to get the Historical Total Value Locked in the DeFi protocols:

Use the DeFiPulse sdk

Now that we have built the sdk, we can use it in our own APIs. In the example below, we will get the IDeFiPulse interface via constructor injection and then we are able to call its methods as seen in the sample controller below:

Use IDeFiPulse.cs

DeFiPulse dotnet standard library

This complete library is free and can be downloaded and added to your project by running the cli command:

dotnet add package DeFiPulse --version 1.0.1

You can also add this package by going to nuget, the official package manager for .NET or by going to GitHub:

Conclusion

Decentralized Finance is an exciting field and it will for sure keep growing in the next years. If you are interested in learning more about DeFi, in the References section below there are some interesting articles about the subject.

In this article we have built a dotnet wrapper for the DeFiPulse API in order to get the DeFi Protocols analytics, ranking and get the Gas Price from the Ethereum blockchain.

References

Also, Read