Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Bavosa ajb413

View GitHub Profile
@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.
View comet-abi-98f438b.json
[{"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
View compound-liquidation-example.md

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
View add-ctoken-to-app.md

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.
View GovernorBravoHarness.sol
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).
View minte-usdc.js
// 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 March 10, 2023 12:13
How to calculate the Net APY that is displayed for users on https://app.compound.finance/
View net_apy.md

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)
View getCompAccrued.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 March 24, 2023 16:44
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).
View EIP712.js
// 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 March 22, 2023 10:06
Finding the COMP APY
View Get_COMP_APY.md
@ajb413
ajb413 / comp_earned.md
Last active November 1, 2022 17:09
How do I retrieve the "COMP earned" value from the Compound protocol? Get the amount of accrued COMP token for an address using the Ethereum blockchain.
View comp_earned.md

How do I retrieve the "COMP earned" value?

With a Smart Contract or JSON RPC

Get the value of COMP earned for an address that uses the Compound protocol. This can be done using the Lens contract with JSON RPC or another Smart Contract. If you do an eth_call with JSON RPC, it is free (no gas costs).

  1. Use the getCompBalanceMetadataExt method in the Lens contract https://etherscan.io/address/0xdCbDb7306c6Ff46f77B349188dC18cEd9DF30299#code
  2. Lens address is posted here https://github.com/compound-finance/compound-protocol/blob/master/networks/mainnet.json#L31
  3. Here is the definition https://github.com/compound-finance/compound-protocol/blob/master/contracts/Lens/CompoundLens.sol#L453