Compound.js

Comet Methods

These methods facilitate interactions with Compound III.

Compound III Supply

Supplies the user’s Ethereum asset to Compound Comet.

  • from (string) A string of the address that the supplied asset is supplied from. This allows approved account managers to supply on behalf of an account that has already approved their ERC-20 asset to be transferred to the Comet contract. To supply on behalf of the sender, this should be set to the sender’s address.
  • dst (string) A string of the address that the supplied asset is credited to within Comet. To supply to the sender’s account, this should be set to the sender’s address.
  • asset (string) A string of the name of the asset to supply.
  • amount (number | string | BigNumber) A string, number, or BigNumber object of the amount of an asset to supply. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • noApprove (boolean) Explicitly prevent this method from attempting an ERC-20 approve transaction prior to sending the supply transaction.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction. A passed gasLimit will be used in both the approve (if not supressed) and supply transactions.
  • RETURN (object) Returns an Ethers.js transaction object of the supply transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

// Ethers.js overrides are an optional last parameter
// const trxOptions = { gasLimit: 250000, mantissa: false };

(async function() {

  const me = '0xSenderAddress'; // can be compound._provider.address

  console.log('Supplying ETH to Compound Comet...');
  const trx = await comet.supply(
    me, // supplied asset comes from this account
    me, // supplied asset is credited to this account's balance
    Compound.WBTC,
    3
  );
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);

Allow

Allows or disallows an address to withdraw or transfer on behalf of the Sender’s address.

  • manager (string) The address of the manager.
  • isAllowed (boolean) True to add the manager and false to remove the manager.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the allow transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const address = '0xManagerAddressHere';
  const trx = await comet.allow(address, true);
  console.log('Ethers.js transaction object', trx);
})().catch(console.error);

Allow By Signature

Enable or disable a Comet account manager using an EIP-712 signature.

  • owner (string) The address of the account that is changing a manager.
  • manager (string) The address of the manager of the account.
  • isAllowed (boolean) Pass true to enable a manager, false to disable.
  • nonce (number) The contract state required to match the signature. This can be retrieved from the contract’s public nonces mapping.
  • expiry (number) The time at which to expire the signature. A block timestamp as seconds since the unix epoch.
  • signature (object) An object that contains the v, r, and, s values of an EIP-712 signature.
  • [options] (CallOptions) Options to set for eth_call, optional ABI (as JSON object), and Ethers.js method overrides. The ABI can be a string of the single intended method, an array of many methods, or a JSON object of the ABI generated by a Solidity compiler.
  • RETURN (object) Returns an Ethers.js transaction object of the allow transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function() {
  const allowTx = await comet.allowBySig(
    '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa',
    '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    true,
    42,
    9999999999,
    {
      v: '0x1b',
      r: '0x130dbca2fafa07424c033b4479687cc1deeb65f08809e3ab397988cc4c6f2e78',
      s: '0x1debeb8250262f23906b1177161f0c7c9aa3641e8bff5b6f5c88a6bb78d5d8cd'
    }
  );
  console.log('Ethers.js transaction object', allowTx);
})().catch(console.error);

Create Allow Signature

Create an EIP-712 signature for enabling or disabling a Comet account manager. Anyone can post it to the blockchain using the allowBySig method, which does have gas costs.

  • manager (string) The address of the manager of the account.
  • isAllowed (boolean) Pass true to enable a manager, false to disable.
  • [expiry] (number) The time at which to expire the signature. A block timestamp as seconds since the unix epoch. Defaults to 10e9.
  • RETURN (object) Returns an object that contains the v, r, and s components of an Ethereum signature as hexadecimal strings.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async () => {

  const allowSignature = await comet.createAllowSignature(
    '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    true
  );
  console.log('allowSignature', allowSignature);

})().catch(console.error);

Transfer

Transfers an asset to another account within Compound Comet.

  • src (string | boolean) The source account address in the transfer. If the transfer is on behalf of the sender instead of a manager, true can be passed instead of an address as a string.
  • dst (string) The desination account address in the transfer.
  • asset (string) A string of the name of the asset to transfer.
  • amount (number | string | BigNumber) A string, number, or BigNumber object of the amount of an asset to transfer. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the transfer transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

// Ethers.js overrides are an optional last parameter
// const trxOptions = { gasLimit: 250000 };

(async function() {

  console.log('Transferring WETH in Compound Comet...');
  const trx = await comet.transfer(
    true, // on behalf of the sender
    destinationAddress,
    Compound.WETH,
    '10000000',
    trxOptions
  );
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);

Withdraw

Withdraws an asset from Compound Comet from the sender’s account to itself.

  • asset (string) A string of the name of the asset to withdraw.
  • amount (number | string | BigNumber) A string, number, or BigNumber object of the amount of an asset to withdraw. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the withdraw transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

// Ethers.js overrides are an optional last parameter
// const trxOptions = { gasLimit: 250000 };

(async function() {

  console.log('Withdrawing DAI from my account...');
  const trx = await comet.withdraw(
    Compound.DAI,
    10,
    trxOptions
  );
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);

Withdraw To

Withdraws an asset from Compound Comet from the sender’s account to another.

  • dst (string) The desination account address in the withdrawal.
  • asset (string) A string of the name of the asset to withdraw.
  • amount (number | string | BigNumber) A string, number, or BigNumber object of the amount of an asset to withdraw. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the withdraw transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

// Ethers.js overrides are an optional last parameter
// const trxOptions = { gasLimit: 250000 };

(async function() {

  console.log('Withdrawing DAI from my account to dst account...');
  const trx = await comet.withdrawTo(
    dst, // destination, the address that the withdrawn asset is sent to
    Compound.DAI,
    10,
    trxOptions
  );
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);

Withdraw From

Withdraws an asset from Compound Comet from one account to another. The caller must be an allowed manager for the source account.

  • src (string) The source account address in the withdrawal. The sender must be an allowed manager for the source account.
  • dst (string) The desination account address in the withdrawal.
  • asset (string) A string of the name of the asset to withdraw.
  • amount (number | string | BigNumber) A string, number, or BigNumber object of the amount of an asset to withdraw. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the withdraw transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

// Ethers.js overrides are an optional last parameter
// const trxOptions = { gasLimit: 250000 };

(async function() {

  console.log('Withdrawing DAI from src account to dst account...');
  const trx = await comet.withdrawFrom(
    src, // source address, sender must be an allowed manager for the address
    dst, // destination, the address that the withdrawn asset is sent to
    Compound.DAI,
    10,
    trxOptions
  );
  console.log('Ethers.js transaction object', trx);

})().catch(console.error);

Get Supply Rate

Gets the supply rate. This method returns the current supply rate as the decimal representation of a percentage scaled up by 10 ^ 18.

  • [utilization] (string | number | BigNumber) A number representing the utilization rate in which to get the corresponding supply rate. The current utilization rate can be fetched by using Compound.comet.getUtilization().
  • RETURN (string) Returns a string of the numeric value of the supply rate.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const supplyRate = await comet.getSupplyRate();
  console.log('Supply Rate', supplyRate);
})().catch(console.error);

Get Borrow Rate

Gets the borrow rate. This method returns the current borrow rate as the decimal representation of a percentage scaled up by 10 ^ 18.

  • [utilization] (string | number | BigNumber) A number representing the utilization rate in which to get the corresponding supply rate. The current utilization rate can be fetched by using Compound.comet.getUtilization().
  • RETURN (string) Returns a string of the numeric value of the borrow rate.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const borrowRate = await comet.getBorrowRate();
  console.log('Borrow Rate', borrowRate);
})().catch(console.error);

Get Utilization

Gets the utilization rate.

  • RETURN (string) Returns the current protocol utilization as a percentage as a decimal, represented by an unsigned integer, scaled up by 10 ^ 18.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const utilization = await comet.getUtilization();
  console.log('Utilization', utilization);
})().catch(console.error);

Absorb

This method triggers the liquidation of one or many underwater accounts.

  • absorber (string) The account that is issued liquidator points during successful execution.
  • accounts (string | string[]) A string of one or an array of many addresses of underwater accounts.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the absorb transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const addresses = [
    '0xUnderwaterAccountAddress1',
  ];
  const trx = await comet.absorb(addresses);
  console.log('Ethers.js transaction object', trx);
})().catch(console.error);

Get Reserves

Gets the Comet protocol reserves for the base asset as an integer.

  • RETURN (string) Returns the current protocol reserves in in the base asset as an unsigned integer, scaled up by 10 to the “decimals” integer in the base asset’s contract.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const reserves = await comet.getReserves();
  console.log('Reserves', reserves);
})().catch(console.error);

Target Reserves

Gets the Comet protocol target reserves.

  • RETURN (string) Returns the protocol target reserves in the base asset as an unsigned integer, scaled up by 10 to the “decimals” integer in the base asset’s contract.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const target = await comet.targetReserves();
  console.log('Target Reserves', target);
})().catch(console.error);

Is Borrow Collateralized

Gets the collateralization of an account as a boolean.

  • account (string) The account address as a string.
  • RETURN (boolean) Returns the collateralization of the account as a boolean.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const address = '0xAccountThatBorrows';
  const isCollateralized = await comet.isBorrowCollateralized(address);
  console.log('Is Collateralized', isCollateralized);
})().catch(console.error);

Is Liquidatable

Checks if the passed account is presently liquidatable.

  • account (string) The account address as a string.
  • RETURN (boolean) Returns the ability to liquidate the account as a boolean.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const address = '0xAccountThatBorrows';
  const isLiquidatable = await comet.isLiquidatable(address);
  console.log('Is Liquidatable', isLiquidatable);
})().catch(console.error);

Quote Collateral

Gets the price of the asset that is passed to it in USD as an unsigned integer, scaled up by 10 ^ 8.

  • asset (string) A string of the name of the asset.
  • baseAmount (number | string | BigNumber) A string, number, or BigNumber object of the amount of the base asset to get a quote. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (string) Returns the price of the asset that is passed to it in USD as an unsigned integer, scaled up by 10 ^ 6.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const price = await comet.quoteCollateral(Compound.UNI, '1000000000');
  console.log('Price quote of 1000 base asset of UNI', price);
})().catch(console.error);

Buy Collateral

Buys discounted collateral from the protocol. This collateral is available after an insolvent borrower account has been absorbed by the protocol. Collateral is only sold when the target reserves amount is not yet reached. The mantissa call option is applied to both the minAmount and baseAmount parameters.

  • asset (string) A string of the name of the asset to buy.
  • minAmount (number | string | BigNumber) A string, number, or BigNumber object of the minimum amount of an asset to buy from the protocol. Use the mantissa boolean in the options parameter to indicate if this value is scaled up (so there are no decimals) or in its natural scale.
  • baseAmount (number | string | BigNumber) A string, number, or BigNumber object of the amount of base asset used to buy the collateral.
  • recipient (string) The desination account address of the collateral that is purchased.
  • noApprove (boolean) Explicitly prevent this method from attempting an ERC-20 approve transaction prior to buying collateral using the base asset.
  • [options] (CallOptions) Call options and Ethers.js overrides for the transaction.
  • RETURN (object) Returns an Ethers.js transaction object of the buy transaction.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function() {

  const me = '0xRecipient';

  console.log('Buying collateral...');
  const trx = await comet.buyCollateral(
    Compound.WBTC,
    1,
    10000
  );
  console.log('Ethers.js transaction object', trx);
  await trx.wait(1);

})().catch(console.error);

Compound III Get Price

Gets the price of the asset that is passed to it in USD as an unsigned integer, scaled up by 10 ^ 8.

  • asset (string) A string of the symbol of the asset.
  • RETURN (string) Returns the price of the asset that is passed to it in USD as an unsigned integer, scaled up by 10 ^ 8.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const price = await comet.getPrice(Compound.WBTC);
  console.log('Price of WBTC', price);
})().catch(console.error);

Borrow Balance Of

Gets the current borrow balance of an account as an unsigned integer. If the account has a non-negative base asset balance, it will return 0.

  • account (string) The account address as a string.
  • RETURN (string) Returns the collateralization of the account as an integer.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const address = '0xAccountThatBorrows';
  const bal = await comet.borrowBalanceOf(address);
  console.log('Borrow Balance', bal.toString());
})().catch(console.error);

Collateral Balance Of

Gets the current balance of the collateral asset for the specified account.

  • account (string) The account address as a string.
  • asset (string) The name of the collateral asset.
  • RETURN (string) Returns the collateral balance as an integer.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const address = '0xAccountThatSupplied';
  const balance = await comet.collateralBalanceOf(address, Compound.WBTC);
  console.log('Collateral balance', balance);
})().catch(console.error);

Get Asset Info

Gets the stored information for a supported asset.

  • assetIndex (number | string | BigNumber) The index of the asset in the array in the Comet contract.
  • RETURN (AssetInfo) Returns a tuple of the asset’s information.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const assetInfo = await comet.getAssetInfo(2);
  console.log('Asset Info', assetInfo);
})().catch(console.error);

Get Asset Info By Address

Gets the stored information for a supported asset.

  • _address (string) The contract address of the supported asset.
  • RETURN (AssetInfo) Returns a tuple of the asset’s information.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const assetInfo = await comet.getAssetInfoByAddress('0xContract');
  console.log('Asset Info', assetInfo);
})().catch(console.error);

Get Asset Info By Symbol

Gets the stored information for a supported asset.

  • symbol (string) The symbol of the supported asset.
  • RETURN (AssetInfo) Returns a tuple of the asset’s information.
const compound = new Compound(window.ethereum);
const comet = compound.comet.MAINNET_USDC();

(async function () {
  const assetInfo = await comet.getAssetInfoBySymbol(Compound.WETH);
  console.log('Asset Info', assetInfo);
})().catch(console.error);

Get Supported Deployments

Gets an array of the supported Compound III deployment names.

  • RETURN (string[]) Returns an array of strings that are used to refer to each Compound III deployment.
const networkNames = Compound.comet.getSupportedDeployments();

Get Supported Collaterals

Gets an array of the supported collateral assets in the specified Comet instance.

  • deployment (string?) The specific deployment in which to get supported collaterals. The key is usually ${network}_${baseAssetSymbol}. Use getSupportedDeployments to get proper values for this parameter. Defaults to cUSDCv3 on Ethereum Mainnet (mainnet_usdc) if nothing is passed.
  • RETURN (string[]) Returns an array of strings of the asset names.
const collaterals = Compound.comet.getSupportedCollaterals();

Get Base Asset Name

Gets the name of the base asset in the specified instance.

  • deployment (string?) The specific deployment in which to get supported collaterals. The key is usually ${network}_${baseAssetSymbol}. Use getSupportedDeployments to get proper values for this parameter. Defaults to cUSDCv3 on Ethereum Mainnet (mainnet_usdc) if nothing is passed.
  • RETURN (string) Returns a string of the base asset name.
const baseAssetName = Compound.comet.getBaseAssetName();