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
const Compound = require('@compound-finance/compound-js');
const cUsdtAddress = Compound.util.getAddress(Compound.cUSDT);
(async function() {
let supplyRatePerBlock = await Compound.eth.read(
cUsdtAddress,
'function supplyRatePerBlock() returns (uint)',
[],
{}
);
console.log('USDT supplyRatePerBlock:', supplyRatePerBlock.toString());
})().catch(console.error);
Write
const toAddress = '0xa0df350d2637096571F7A701CBc1C5fdE30dF76A';
(async function() {
const trx = await Compound.eth.trx(
toAddress,
'function send() external payable',
[],
{
value: Compound._ethers.utils.parseEther('1.0'),
provider: window.ethereum,
}
);
const toAddressEthBalance = await Compound.eth.getBalance(toAddress);
})().catch(console.error);
Compound Protocol
Simple methods for using the Compound protocol.
const compound = new Compound(window.ethereum);
(async function() {
console.log('Supplying ETH to the Compound protocol...');
const trx = await compound.supply(Compound.ETH, 1);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
Install / Import
Web Browser
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@compound-finance/compound-js@latest/dist/browser/compound.min.js"></script>
<script type="text/javascript">
window.Compound;
</script>
Node.js
npm install @compound-finance/compound-js
const Compound = require('@compound-finance/compound-js');
import Compound from '@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.
var compound = new Compound(window.ethereum);
var compound = new Compound('http://127.0.0.1:8545');
var compound = new Compound();
var compound = new Compound('ropsten');
var compound = new Compound('https://mainnet.infura.io/v3/_your_project_id_', {
privateKey: '0x_your_private_key_',
});
var compound = new Compound('mainnet' {
mnemonic: 'clutch captain shoe...',
});
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);
const cUsdtAddress = Compound.util.getAddress(Compound.cUSDT);
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.
await compound.borrow(Compound.DAI, '1000000000000000000', { mantissa: true });
await compound.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.
const trxOptions = {
mantissa,
abi,
provider,
network,
from,
gasPrice,
gasLimit,
value,
data,
chainId,
nonce,
privateKey,
mnemonic,
};
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
.
const main = async () => {
const account = await Compound.api.account({
"addresses": "0xB61C5971d9c0472befceFfbE662555B78284c307",
"network": "ropsten"
});
let daiBorrowBalance = 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
<script type="text/javascript" src="./dist/browser/compound.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@compound-finance/compound-js@latest/dist/browser/compound.min.js"></script>
Node.js Build
const Compound = require('./dist/nodejs/index.js');
const Compound = require('@compound-finance/compound-js');