Create a Compound Smart Contract API With Infura

Adam Bavosa
Compound Labs
Published in
6 min readMar 27, 2020

In this guide, we’ll review the concepts and code required to build your own API to invoke Compound protocol smart contracts using conventional HTTP requests; we’ll use Infura as the bridge between the Ethereum network and your application.

Compound is an interest rate protocol for supplying and borrowing crypto assets on Ethereum.

Infura is an API access provider & development suite for the Ethereum and IPFS networks.

The Ethereum network hosts smart contracts which can be invoked by Ethereum users. Calling and executing smart contract functions can be done in 2 ways.

  1. Via your own Ethereum network node.
  2. Via an API provider like Infura that uses their own Ethereum nodes under the hood.

Companies can build public or private APIs that enable Web2 technology to interface with the Ethereum network. Popular programming languages can be used to create such an API without a profound amount of effort, thanks to APIs and Web3 libraries.

A non-exhaustive list of community maintained Web3 libraries in various languages:

  • JavaScript, TypeScript, & Node.js - Web3.js
  • Python - Web3.py
  • Go - Geth (a.k.a Go Ethereum)
  • Swift - Geth wrapper or Web3.js
  • Java - web3j
  • Kotlin - web3j
  • Scala - web3j
  • Elixir - Ethereumex

Web3.js, a JavaScript library, is considered most popular and is improved very frequently at the time of this guide’s writing.

There are many implementation patterns for creating an API for this use case. Let’s run through building one with the following components:

  • Web Server Application - A conventional web server application that services HTTP requests. This will power our public or private API.
  • Web3 SDK - Implementation of a Web3 SDK which constructs and signs transactions to be sent to the Ethereum network.
  • Ethereum Provider - Implementation of a Web3 infrastructure provider (we’ll use Infura), via their API, using an HTTP library that is conventional in your selected programming language.

The following is merely an example, and should not be implemented in production without a professional software engineer’s review and professional codebase auditing. The API example code is available on GitHub under the MIT license.

Here’s an architecture diagram of our possible web API implementation:

Building the Web Server Application

When creating your own API that interfaces with Ethereum smart contracts, you can first make a web server application that services HTTP requests made by clients.

Here are possible endpoints for an API that allows users to supply or redeem crypto assets from the Compound protocol. You can create more endpoints for particular Ethereum ERC-20 tokens that the Compound protocol supports.

Get Wallet Balance ETH

  • GET /wallet-balance/eth/
  • Returns (number) Amount of ETH in the wallet.

Get Wallet Balance cETH

  • GET /wallet-balance/ceth/
  • Returns (number) Amount of cETH in the wallet.

Get Compound Protocol Balance

  • GET /protocol-balance/eth/
  • Returns (number) Amount of ETH that the wallet has currently supplied in the Compound Protocol.

Supply

  • GET /supply/eth/{amount}
  • URL Slug Parameter
  • amount (number) — The amount of Ether that the user wants to supply to the Compound protocol.
  • Returns (status code) 200 OK if the operation succeeds without error.

Redeem

  • GET /redeem/eth/{cTokenAmount}
  • URL Slug Parameter
  • cTokenAmount (number) — The number of cETH tokens that the user wants to redeem. This will transfer underlying ETH back to the wallet.
  • Returns (status code) 200 OK if the operation succeeds without error.

With Node.js and Express, the beginning of the server application code would look like this:

Web3 SDK Implementation

Node.js has a robust implementation of a Web3 library, called web3.js. Not only does the library take care of signing requests with a wallet’s private key, it provides classes for intuitive object orientation. This allows developers to write clean JavaScript to interface with the blockchain.

Add the following lines to the top of the web server code. In the Web3 object initialization, we pass the URL of Infura’s API. This will enable JSON RPC calls to be made to the network.

We initialize a single account using an existing Ethereum wallet private key, which is accessed by an environment variable. Then we initialize a contract object using the address and ABI of the live smart contract.

Before we use Infura’s JSON RPC API to interact with the blockchain, let’s go over the types of invocations that we will use.

Call - This type of call is considered a “get” method when accessing the blockchain. It does not modify or write any data to the blockchain. It does not require gas fees in order to execute.

Send - This is the inverse of call. Send is a “set” method, and will modify the blockchain. Depending on the depth of the operation, and network conditions, this type of transaction will require a gas fee, which is paid with Ether.

Each of the following operations can be invoked using Infura’s API (contains cURL examples). The operations are slightly more complex if you are using a programming language that does not have a robust Web3 library. More on that later.

The example API endpoint for getting the wallet’s cETH balance utilizes “call,” which, again, does not modify the blockchain; It merely reads from it.

The Supply endpoint utilizes “send.” It invokes the contract mint function using the wallet’s Ether. The Ether is supplied to the Compound protocol, and it is also used for the transaction’s gas costs.

The full code for the example API is available, open source. Here is an example output for a series of calls made to our API.

The final ETH balance in the wallet is less than the original balance because some ETH was burned due to gas fees. The ETH was not supplied to the protocol long enough to recoup those gas expenses via interest.

Ethereum Provider Implementation

The example API in this guide utilizes Infura’s JSON RPC API on the backend. The JSON RPC API methods are common across setups like Infura because it follows the Ethereum JSON RPC specification.

Infura provides both an HTTP API and a Websocket API. The Websocket API can be utilized if your use-case requires real-time, event-driven functionality. Get your own Infura Project ID here, for free.

If your programming language of choice for building a smart contract API does not have a robust Web3 library, you will have to be less abstract with your code implementation.

In order to invoke calls and sends with the blockchain, every transaction will need to follow these steps to execute:

  1. Collect and calculate each of your transaction’s parameters.
  2. Perform the cryptographic signature operation using the wallet private key and the contract’s ABI.
  3. Submit the transaction to the JSON RPC API by using a conventional HTTP request library in your language of choice.

The code example in this guide does not need to perform steps like the signature explicitly because the JavaScript Web3 library does this for you under the hood.

A home-built smart contract API can be a powerful tool; depending on your use-case, the API developed in this guide might be the right puzzle piece. To discuss this guide or brainstorm the architecture for your codebase, get in touch in the #development room on our very active Discord server.

--

--