Adam Bavosa
Sep 2 · 3 min read

A JavaScript SDK for Ethereum and the Compound Protocol

Announcing Compound.js a JavaScript SDK for Ethereum and Compound
Announcing Compound.js a JavaScript SDK for Ethereum and Compound

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 and . Compound.js works in the web browser and Node.js. It has just one dependency: the latest version of .

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 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 .

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 .

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 here).

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

More governance related methods are defined in the .

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 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

is in open beta and is available for free on . 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 and send us a message in the #💻development channel.


Compound is an algorithmic, autonomous interest rate protocol built for developers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store