Skip to content

Instantly share code, notes, and snippets.

@ajb413
ajb413 / getAllAssetInfos.js
Created October 20, 2023 16:46
Find out the price feed information for each asset supported by Compound Comet
const ethers = require('ethers'); // v5
const providerUrl = process.env.POLYGON_PROVIDER_URL;
(async () => {
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const cometAddress = '0xF25212E676D1F7F89Cd72fFEe66158f541246445';
const cometAbi = [
'function getAssetInfo(uint8 i) public view returns (uint8 offset, address asset, address priceFeed, uint64 scale, uint64 borrowCollateralFactor, uint64 liquidateCollateralFactor, uint64 liquidationFactor, uint128 supplyCap)',
'function baseToken() public view returns (address)',
'function baseTokenPriceFeed() public view returns (address)',
@ajb413
ajb413 / comet-abi-98f438b.json
Last active May 31, 2023 21:45
Compound III Comet Main Interface at compound/finance/comet repository commit 98f438b.
[{"inputs":[],"name":"Absurd","type":"error"},{"inputs":[],"name":"AlreadyInitialized","type":"error"},{"inputs":[],"name":"BadAmount","type":"error"},{"inputs":[],"name":"BadAsset","type":"error"},{"inputs":[],"name":"BadDecimals","type":"error"},{"inputs":[],"name":"BadDiscount","type":"error"},{"inputs":[],"name":"BadMinimum","type":"error"},{"inputs":[],"name":"BadNonce","type":"error"},{"inputs":[],"name":"BadPrice","type":"error"},{"inputs":[],"name":"BadSignatory","type":"error"},{"inputs":[],"name":"BorrowCFTooLarge","type":"error"},{"inputs":[],"name":"BorrowTooSmall","type":"error"},{"inputs":[],"name":"InsufficientReserves","type":"error"},{"inputs":[],"name":"InvalidInt104","type":"error"},{"inputs":[],"name":"InvalidInt256","type":"error"},{"inputs":[],"name":"InvalidUInt104","type":"error"},{"inputs":[],"name":"InvalidUInt128","type":"error"},{"inputs":[],"name":"InvalidUInt64","type":"error"},{"inputs":[],"name":"InvalidValueS","type":"error"},{"inputs":[],"name":"InvalidValueV","type":"error"}
@ajb413
ajb413 / compound-liquidation-example.md
Created March 16, 2022 19:45
A Compound v2 Liquidation Example

A borrower account has Dai collateral and an open WBTC borrow. It has become under-collateralized. I can seize their cDai by liquidating their WBTC borrow. I have a balance of WBTC. I call approve on cWBTC and pass the amount of WBTC that I want to repay (or greater). I then call liquidateBorrow on cWBTC and pass the borrower address, amount of WBTC to liquidate, and the cDai address. If the liquidation is successful, my WBTC balance goes down and my cDai balance goes up. If I redeem that cDai immediately, the underlying Dai is worth the same amount as the amount of WBTC I gave up plus 8%.

@ajb413
ajb413 / add-ctoken-to-app.md
Created March 7, 2022 19:07
Torrey's instructions for adding a new asset to the Compound app at https://app.compound.finance

Hey, I noticed that no one actually added the newest asset, USDP, to the Compound Front-end aka Palisade and I wanted to show the steps so that others can feel comfortable adding in the future for other assets added to the protocol.

The 1st step is updating the contract address and abi config repo containing the newest token added. This step can be tricky to get right as the config repo is how we keep track of all abis and addresses used. This was done and merged here: compound-finance/compound-config#49

Step 2 is to add the the new styles for the asset in the shared components repo. A regular asset icon is needed as well as cToken asset as shown here: https://github.com/compound-finance/compound-components/pull/58/commits/88a3cc2dfe2b2f72c0f15b8fc321ecc601ed1370

The 3rd and final step is to open a PR on Palisade which updates the dependencies for the config repo and the shared components repo as shown here: compound-finance/palisade#12

I'm going to merge a

@ajb413
ajb413 / GovernorBravoHarness.sol
Last active October 5, 2021 03:52
The Compound Protocol's Testnet version of Governor Bravo. Deploy using the README.md file below.
pragma solidity ^0.5.16;
pragma experimental ABIEncoderV2;
import "../../contracts/Governance/GovernorBravoDelegate.sol";
// @notice Only use this contract for internal testing
contract GovernorBravoDelegateHarness is GovernorBravoDelegate {
// @notice Harness initiate the GovenorBravo contract
// @dev This function bypasses the need to initiate the GovernorBravo contract from an existing GovernorAlpha for testing.
// Actual use will only use the _initiate(address) function
@ajb413
ajb413 / minte-usdc.js
Created October 21, 2020 00:04
Local host fork of mainnet, "steal" some test USDC from a whale (cUSDC contract).
// First run Ganache locally with `cUsdc` address unlocked
const Web3 = require('web3');
const web3 = new Web3('http://127.0.0.1:8545');
const usdcAbi = [{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"authorizer","type":"address"},{"indexed":true,"internalType":"bytes32","name":"nonce","type":"bytes32"}],"name":"AuthorizationCanceled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"authorizer","type":"address"},{"indexed":true,"internalType":"bytes32","name":"nonce","type":"bytes32"}],"name":"AuthorizationUsed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_account","type":"address"}],"name":"Blacklist
@ajb413
ajb413 / net_apy.md
Last active November 28, 2023 06:11
How to calculate the Net APY that is displayed for users on https://app.compound.finance/

Net APY Calculation

  1. Convert all supplied and borrowed asset amounts to a single asset (like USD or ETH).
  2. Calculate the sum of (suppliedAmount * supplyApyAsDecimal - borrowedAmount * borrowApyAsDecimal) for all underlying assets.
  3. If the calculated sum from the previous step is >0 then Net APY = 100 * (sum / totalSuppliedValue). If the calculation from the previous step is <0 then Net APY = 100 * (sum / totalBorrowedValue). If the calculation from the previous step is 0 then Net APY = 0.

Example

Net APY:

  • -7.29%
@ajb413
ajb413 / getCompAccrued.js
Last active January 20, 2021 01:22
Get COMP accrued for an account on mainnet using Compound.js (https://github.com/compound-finance/compound-js)
const Compound = require('@compound-finance/compound-js');
const provider = 'https://mainnet.infura.io/v3/' + process.env.infuraApiKey;
// mainnet
const CompoundLens = Compound.util.getAddress(Compound.CompoundLens);
const LensAbi = Compound.util.getAbi(Compound.CompoundLens);
const COMP = Compound.util.getAddress(Compound.COMP);
const Comptroller = Compound.util.getAddress(Compound.Comptroller);
const me = '0xa0df350d2637096571F7A701CBc1C5fdE30dF76A';
@ajb413
ajb413 / EIP712.js
Last active September 3, 2023 15:01
Module for creating EIP-712 signatures with Ethers.js as the only dependency. Works in the browser and Node.js (Ethers.js Web3 Provider / JSON RPC Provider).
// Based on https://github.com/ethereum/EIPs/blob/master/assets/eip-712/Example.js
const ethers = require('ethers');
function abiRawEncode(encTypes, encValues) {
const hexStr = ethers.utils.defaultAbiCoder.encode(encTypes, encValues);
return Buffer.from(hexStr.slice(2, hexStr.length), 'hex');
}
function keccak256(arg) {
@ajb413
ajb413 / Get_COMP_APY.md
Last active August 8, 2023 11:03
Finding the COMP APY

From Blockchain With JSON RPC and JavaScript

const Compound = require('@compound-finance/compound-js');

const provider = 'https://mainnet.infura.io/v3/' + process.env.infuraApiKey;

const cTokenToGetCompApy = Compound.cUSDC; // Pick an asset

const underlying = cTokenToGetCompApy.slice(1, 10);
const underlyingDecimals = Compound.decimals[underlying];