Skip to content

Repository files navigation

Compound.js

A JavaScript SDK for Ethereum and the Compound Protocol. Wraps around Ethers.js. Works in the web browser and Node.js.

Compound.js Documentation

This SDK is in open beta, and is constantly under development. USE AT YOUR OWN RISK.

Ethereum Read & Write

JSON RPC based Ethereum read and write.

Read

constCompound=require('@compound-finance/compound-js');// in Node.jsconstcUsdtAddress=Compound.util.getAddress(Compound.cUSDT);(asyncfunction(){letsupplyRatePerBlock=awaitCompound.eth.read(cUsdtAddress,'function supplyRatePerBlock() returns (uint)',[],// [optional] parameters{}// [optional] call options, provider, network, ethers.js "overrides");console.log('USDT supplyRatePerBlock:',supplyRatePerBlock.toString());})().catch(console.error);

Write

consttoAddress='0xa0df350d2637096571F7A701CBc1C5fdE30dF76A';(asyncfunction(){consttrx=awaitCompound.eth.trx(toAddress,'function send() external payable',[],{value: Compound._ethers.utils.parseEther('1.0'),// 1 ETHprovider: window.ethereum,// in a web browser});consttoAddressEthBalance=awaitCompound.eth.getBalance(toAddress);})().catch(console.error);

Compound Protocol

Simple methods for using the Compound protocol.

constcompound=newCompound(window.ethereum);// in a web browser// Ethers.js overrides are an optional 3rd parameter for `supply`// const trxOptions = { gasLimit: 250000, mantissa: false };(asyncfunction(){console.log('Supplying ETH to the Compound protocol...');consttrx=awaitcompound.supply(Compound.ETH,1);console.log('Ethers.js transaction object',trx);})().catch(console.error);

Install / Import

Web Browser

<scripttype="text/javascript" src="https://cdn.jsdelivr.net/npm/@compound-finance/compound-js@latest/dist/browser/compound.min.js"></script><scripttype="text/javascript">window.Compound;// or `Compound`</script>

Node.js

npm install @compound-finance/compound-js
constCompound=require('@compound-finance/compound-js');// or, when using ES6importCompoundfrom'@compound-finance/compound-js';

More Code Examples

To run, boot Ganache fork of mainnet locally

Instance Creation

The following are valid Ethereum providers for initialization of the SDK.

varcompound=newCompound(window.ethereum);// web browservarcompound=newCompound('http://127.0.0.1:8545');// HTTP providervarcompound=newCompound();// Uses Ethers.js fallback mainnet (for testing only)varcompound=newCompound('ropsten');// Uses Ethers.js fallback (for testing only)// Init with private key (server side)varcompound=newCompound('https://mainnet.infura.io/v3/_your_project_id_',{privateKey: '0x_your_private_key_',// preferably with environment variable});// Init with HD mnemonic (server side)varcompound=newCompound('mainnet'{mnemonic: 'clutch captain shoe...',// preferably with environment variable});

Constants and Contract Addresses

Names of contracts, their addresses, ABIs, token decimals, and more can be found in /src/constants.ts. Addresses, for all networks, can be easily fetched using the getAddress function, combined with contract name constants.

console.log(Compound.DAI,Compound.ETH,Compound.cETH);// DAI, ETH, cETHconstcUsdtAddress=Compound.util.getAddress(Compound.cUSDT);// Mainnet cUSDT address. Second parameter can be a network like 'ropsten'.

Mantissas

Parameters of number values can be plain numbers or their scaled up mantissa values. There is a transaction option boolean to tell the SDK what the developer is passing.

// 1 Daiawaitcompound.borrow(Compound.DAI,'1000000000000000000',{mantissa: true});// `mantissa` defaults to false if it is not specified or if an options object is not passedawaitcompound.borrow(Compound.DAI,1,{mantissa: false});

Transaction Options

Each method that interacts with the blockchain accepts a final optional parameter for overrides, much like Ethers.js overrides.

// The options object itself and all options are optionalconsttrxOptions={
mantissa,// Boolean, parameters array arg of 1 ETH would be '1000000000000000000' (true) vs 1 (false)
abi,// Definition string or an ABI array from a solc build
provider,// JSON RPC string, Web3 object, or Ethers.js fallback network (string)
network,// Ethers.js fallback network provider, "provider" has precedence over "network"
from,// Address that the Ethereum transaction is send from
gasPrice,// Ethers.js override `Compound._ethers.utils.parseUnits('10.0', 'gwei')`
gasLimit,// Ethers.js override - see https://docs.ethers.io/ethers.js/v5-beta/api-contract.html#overrides
value,// Number or string
data,// Number or string
chainId,// Number
nonce,// Number
privateKey,// String, meant to be used with `Compound.eth.trx` (server side)
mnemonic,// String, meant to be used with `Compound.eth.trx` (server side)};

API

The Compound API is accessible from Compound.js. The corresponding services are defined in the api namespace on the class.

  • Compound.api.account
  • Compound.api.cToken
  • Compound.api.marketHistory
  • Compound.api.governance

The governance method requires a second parameter (string) for the corresponding endpoint shown in the documentation.

  • proposals
  • voteReceipts
  • accounts

Here is an example for using the account endpoint. The network parameter in the request body is optional and defaults to mainnet.

constmain=async()=>{constaccount=awaitCompound.api.account({"addresses": "0xB61C5971d9c0472befceFfbE662555B78284c307","network": "ropsten"});letdaiBorrowBalance=0;if(Object.isExtensible(account)&&account.accounts){account.accounts.forEach((acc)=>{acc.tokens.forEach((tok)=>{if(tok.symbol===Compound.cDAI){daiBorrowBalance=+tok.borrow_balance_underlying.value;}});});}console.log('daiBorrowBalance',daiBorrowBalance);}main().catch(console.error);

Build for Node.js & Web Browser

git clone git@github.com:compound-finance/compound-js.git
cd compound-js/
npm install
npm run build

Web Browser Build

<!-- Local build (do `npm install` first) --><scripttype="text/javascript" src="./dist/browser/compound.min.js"></script><!-- Public NPM -> jsdeliver build --><scripttype="text/javascript" src="https://cdn.jsdelivr.net/npm/@compound-finance/compound-js@latest/dist/browser/compound.min.js"></script>

Node.js Build

// Local build (do `npm install` first)constCompound=require('./dist/nodejs/index.js');// Public NPM buildconstCompound=require('@compound-finance/compound-js');

About

A JavaScript SDK for Ethereum and the Compound Protocol.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages