This post was originally published on Compound

Adam Bavosa

Sep 2, 2020 · 3 min read

A JavaScript SDK for Ethereum and the Compound Protocol

We’re excited to announce a new software development kit that we’ve been working on. Enter Compound.js; a JavaScript SDK for Ethereum and the Compound Protocol. This tool streamlines the software development process for building DApps that accelerate DeFi.

At the time of this announcement, the SDK is officially in beta. It is available for all, open source, on GitHub and NPM. Compound.js works in the web browser and Node.js. It has just one dependency: the latest version of Ethers.js.

Built with simplicity in mind, developers can write intuitive, one-line methods to initialize JSON RPC requests that interact with the Compound Protocol.

In addition to Compound methods, developers can make generic Ethereum read and write calls to any smart contract. Lastly, REST API calls can be made to the Compound API that is hosted by Compound Labs.

Ethereum JSON RPC with Compound.js

Reading and writing from the blockchain can be done swiftly with Compound.js; no need to initialize an object.


In the following examples, A JSON RPC provider or Web3 provider can be passed as the provider attribute in the options object. When writing to the blockchain, a private key or mnemonic can be passed in the options object as well.

Write to the Blockchain (eth_sendTransaction)

Write operations are initialized with Compound.eth.trx. The return value is an Ethers.js transaction object. Here is an example for sending ETH in the web browser.

Read from the Blockchain (eth_call)

Reading from any smart contract on the blockchain can be done using Here is an example for reading the USDC balance in the Uniswap USDC-ETH pair.

A contract ABI can also be passed to the options object. In that case, you would then provide a string of the method name as the second parameter, instead of the whole method definition.

Compound Protocol Methods

Simple one-liners are available for major protocol methods like supply, redeem, borrow, repayBorrow, and more.

In examples like the following, a Compound object must be instantiated first. The constructor can have window.ethereum passed in the browser, or a private key/mnemonic alongside a JSON RPC provider if you’re using Node.js.

Supplying to the Protocol

Here is an example for supplying Ether to the Compound Protocol using Compound.js.

More examples of object initialization and deeper description of the transaction options parameter are available in the Compound.js GitHub repository readme.

Governance and COMP Methods

The SDK has elegant methods for Compound governance functionality through the COMP and Governor smart contracts.

Here is an example for fetching the amount of COMP token that has accrued for an address (more on COMP Distribution here).

Voting on a Compound governance proposal is as simple using the SDK’s castVote method.

More governance related methods are defined in the Compound.js Documentation.

Getting Prices

Fetching prices from the Open Price Feed can be done with the getPrice method.

The first parameter is the asset that you want the price of, and the second parameter (optional) is the asset you want that price converted to. The default output is USD with 6 decimal places.

This method supports all assets that are posted to the Open Price Feed, and also cTokens.

Compound API Methods

The Compound REST API can be queried using Compound.js. Each of the available services has their own method.

  • market
  • account
  • cToken
  • governance

Each of the request parameters defined in the documentation (linked above) can be passed in the JavaScript object parameter. Here is an example for querying the Market History Service.

Roadmap and Contribution

Compound.js is in open beta and is available for free on NPM. The plan for next steps is to add more Compound Protocol related methods for ease of development. Also, smarter logging of errors and results would greatly improve the developer experience.

Remember that the Compound Protocol is decentralized and upgraded by the community. As a result, this SDK will morph however the community sees fit!

A community contribution guide will be available in the future for anyone to help improve the SDK.

If you have questions or comments, please join the Compound Discord and send us a message in the #💻development channel.

Leave a Comment