Blog>DeFi-SDK — A Beginner’s Guide

DeFi-SDK — A Beginner’s Guide

Integrating with different Ethereum protocols and performing common tasks is cumbersome. Today we will explore DeFi-SDK and how it simplifies working with several DeFi protocols in parallel.

Quicknode

Quicknode

May 19, 2020 — 4 min read

DeFi-SDK — A Beginner’s Guide

DeFi Programming, Now Easier Than Ever

Working with many Ethereum protocols and doing common tasks is tedious. For example, you have to find smart contract addresses and ABIs for each DeFi protocol, then manage them all.

To fix this, the Zerion team launched DeFi SDK. Here's how it makes working with different DeFi protocols simpler.

What is DeFi-SDK?

DeFi-SDK is a set of smart contracts and APIs for common tasks across Ethereum DeFi protocols like Compound and Aave. From the official docs:

“DeFi SDK is an open-source system of smart contracts that makes it easier for developers to integrate DeFi protocols.”

What problem does DiFi-SDK solve?

DeFi-SDK makes it easy to work with several DeFi protocols and gives you one standard way to interact with them.

Supported Protocols

DeFi-SDK currently supports:

  • PieDAO

  • Multi-Collateral Dai

  • Bancor

  • DeFi Money Market

  • TokenSets

  • 0x Staking

  • Uniswap V1

  • Synthetix

  • PoolTogether

  • Dai Savings Rate

  • Chai

  • iearn.finance (v2, v3)

  • Idle

  • dYdX

  • Curve

  • Compound

  • Balancer

  • Aave

Install DeFi-SDK

First, we will create a new directory:

mkdir defi-sdk

DeFi-SDK is currently available only in Python and Go. Here, we'll use the Python version on Ubuntu. defisdk needs Python 3.8, so install that first.

Install Python 3.8 developer package:

sudo apt-get install python3.8-dev

Now install defisdk:

python3.8 -m pip install defisdk --no-cache-dir

If you encounter an error, jump in the Zerion discord group.

Working with DeFi-SDK

Now, let’s understand how defisdk works with some examples.

We will create a file index.py in our defi-sdk directory:

touch index.py

Next open this file, we will be using it for the defisdk examples.

Initial Setup

We need to initialize our defisdk . For that, we require an ETH MainNet web3 endpoint URL.

You can sign up for Quicknode and get the URL.

import asyncio
from defisdk import DeFiSDK
ETHEREUM_NODE_URL = 'YOUR_QUICKNODE_URL'
defi_sdk = DeFiSDK(ETHEREUM_NODE_URL)

Lines 2, 3, and 4 import the SDK and create an object by passing in an Ethereum node URL.

The first line imports a library we'll use later.

Note: If you're building an app for production, a service like Quicknode can save you time and money and make your app faster.

Getting All the Protocols

We have done the initial set up.

Now we will get all the protocols supported by the defisdk:

async def get_protocols():
    print(await defi_sdk.get_protocol_names())
    asyncio.run(get_protocols())

Here, defi_sdk.get_protocol_names() returns the protocol names. It's an async function because it calls smart contracts under the hood.

Add the above lines in the same file (index.py).

Now we will run our file to see the result:

python3.8 index.py

This prints every protocol defisdk supports:

defisdk-batman

Get Balance from All Protocols

In this example, we'll get a user's token balance across all protocols in one call.

USER_ADDRESS = '0xa218a8346454c982912cf6d14c714663c2d510d8'
async def get_account_balance():
    balance = await defi_sdk.get_account_balance(USER_ADDRESS)
    print(balance)
asyncio.run(get_account_balance())

Here, defi_sdk.get_account_balance(USER_ADDRESS) gets the user's balance across protocols.

Add the above lines to our index.py and let’s run it:

python3.8 index.py
defisdk-syntax

You can check out the complete documentation of the DeFi SDK to learn more about it.

DeFi-SDK Architecture

Now, let’s understand the DeFi-SDK architecture.

defisdk is a combination of two things:

1- Smart-contract adapters + a registry

2- A library written in python calling Defi SDK smart-contract functions

There are two types of Adapters:

  • ProtocolAdapter: a contract for each protocol that wraps all interactions with it. There are different types. An "Asset" adapter returns how many of an account's tokens are held in the protocol. A "Debt" adapter returns how much the account owes the protocol. Some protocols use complex derivatives instead of simple ERC20 tokens. Compound, for example, uses CTokens. The ProtocolAdapter also tells you which token types the protocol uses.

  • TokenAdapter: a contract for each derivative token type, like cTokens, aTokens, and yTokens. It provides ERC20-style token metadata and details about the underlying ERC20 tokens (like DAI for cDAI): their addresses, types, and rates.

a-begginers-guide
defi-sdk

Wrapping Up

Working with multiple protocols on Ethereum can still be hard, but new libraries are making it easier. We'll cover more libraries like this in future posts.


Need help with your project or have questions? Contact us via this form, on Twitter @Quicknode, or ping us on Discord!


About Quicknode

Founded in 2017, Quicknode deploys institutional-grade blockchain infrastructure for developers and enterprises. With 99.99% uptime and support for 77+ chains, teams build and scale onchain applications without compromise.

Start building today

Stay updated

The latest engineering insights, product updates, and web3 news delivered straight to your inbox.