Skip to content

Instantly share code, notes, and snippets.

@ajb413
Last active August 8, 2023 11:03
Show Gist options
  • Save ajb413/d32442edae9251ad395436d5b80d4480 to your computer and use it in GitHub Desktop.
Save ajb413/d32442edae9251ad395436d5b80d4480 to your computer and use it in GitHub Desktop.

Revisions

  1. ajb413 revised this gist Jul 4, 2023. 1 changed file with 0 additions and 23 deletions.
    23 changes: 0 additions & 23 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,3 @@
    ## Using the Compound API

    **UPDATE**

    The Compound cToken API now returns the values for you. See `comp_borrow_apy` and `comp_supply_apy` when requesting https://api.compound.finance/api/v2/ctoken

    **OLD METHOD:**
    1. Pick a supported cToken and its underlying asset
    2. Query https://api.compound.finance/api/v2/governance/comp
    3. Take the value for `supplier_daily_comp` or `borrower_daily_comp`
    4. Query https://api.compound.finance/api/v2/ctoken
    5. Take the value for `total_supply`, `exchange_rate`, and `total_borrows`
    6. Query https://prices.compound.finance/
    7. Take COMP price and also the particular asset's price

    In JavaScript

    ```js
    borrowCompApy = 100 * (Math.pow((1 + (comp_usd_price * borrower_daily_comp / (total_borrows * asset_usd_price))), 365) - 1);

    supplyCompApy = 100 * (Math.pow((1 + (comp_usd_price * supplier_daily_comp / (total_supply * exchange_rate * asset_usd_price))), 365) - 1);
    ```

    ## From Blockchain With JSON RPC and JavaScript
    ```js
    const Compound = require('@compound-finance/compound-js');
  2. ajb413 revised this gist Nov 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ const cTokenDecimals = 8; // always 8
    const comptroller = Compound.util.getAddress(Compound.Comptroller);
    const pf = Compound.util.getAddress(Compound.PriceFeed);
    const cTokenAddr = Compound.util.getAddress(cTokenToGetCompApy);
    const apxBlockSpeedInSeconds = 13.15;
    const apxBlockSpeedInSeconds = 12;

    (async function() {

  3. ajb413 revised this gist Jun 22, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ The Compound cToken API now returns the values for you. See `comp_borrow_apy` an
    2. Query https://api.compound.finance/api/v2/governance/comp
    3. Take the value for `supplier_daily_comp` or `borrower_daily_comp`
    4. Query https://api.compound.finance/api/v2/ctoken
    5. Take the value for `total_supply` or `exchange_rate` and `total_borrows`
    5. Take the value for `total_supply`, `exchange_rate`, and `total_borrows`
    6. Query https://prices.compound.finance/
    7. Take COMP price and also the particular asset's price

    @@ -18,7 +18,7 @@ In JavaScript
    ```js
    borrowCompApy = 100 * (Math.pow((1 + (comp_usd_price * borrower_daily_comp / (total_borrows * asset_usd_price))), 365) - 1);

    supplyCompApy = 100 * (Math.pow((1 + (comp_usd_price * supplier_daily_comp / (total_supply * asset_usd_price))), 365) - 1);
    supplyCompApy = 100 * (Math.pow((1 + (comp_usd_price * supplier_daily_comp / (total_supply * exchange_rate * asset_usd_price))), 365) - 1);
    ```

    ## From Blockchain With JSON RPC and JavaScript
  4. ajb413 revised this gist Jun 22, 2022. 1 changed file with 32 additions and 30 deletions.
    62 changes: 32 additions & 30 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -23,15 +23,6 @@ supplyCompApy = 100 * (Math.pow((1 + (comp_usd_price * supplier_daily_comp / (to

    ## From Blockchain With JSON RPC and JavaScript
    ```js
    // To use this script, copy it to a file on your machine. (getCompApy.js)
    // Insert your Infura project ID on line 12 to access the blockchain
    // Install Node.js
    // Navigate to the folder of getCompApy.js using the command line

    // npm init -y
    // npm install @compound-finance/compound-js
    // node getCompApy.js

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

    const provider = 'https://mainnet.infura.io/v3/' + process.env.infuraApiKey;
    @@ -42,35 +33,42 @@ const underlying = cTokenToGetCompApy.slice(1, 10);
    const underlyingDecimals = Compound.decimals[underlying];
    const cTokenDecimals = 8; // always 8
    const comptroller = Compound.util.getAddress(Compound.Comptroller);
    const opf = Compound.util.getAddress(Compound.PriceFeed);
    const pf = Compound.util.getAddress(Compound.PriceFeed);
    const cTokenAddr = Compound.util.getAddress(cTokenToGetCompApy);
    const apxBlockSpeedInSeconds = 13.15;

    (async function() {

    let compSpeedSupply = await Compound.eth.read(
    // let compSpeed = await Compound.eth.read(
    // comptroller,
    // 'function compSpeeds(address cToken) public returns (uint)',
    // [ cTokenAddr ],
    // { provider }
    // );

    let compBorrowSpeed = await Compound.eth.read(
    comptroller,
    'function compSupplySpeeds(address cToken) public returns (uint)',
    'function compBorrowSpeeds(address cToken) public returns (uint)',
    [ cTokenAddr ],
    { provider }
    );

    let compSpeedBorrow = await Compound.eth.read(
    let compSupplySpeed = await Compound.eth.read(
    comptroller,
    'function compBorrowSpeeds(address cToken) public returns (uint)',
    'function compSupplySpeeds(address cToken) public returns (uint)',
    [ cTokenAddr ],
    { provider }
    );

    let compPrice = await Compound.eth.read(
    opf,
    pf,
    'function price(string memory symbol) external view returns (uint)',
    [ Compound.COMP ],
    { provider }
    );

    let assetPrice = await Compound.eth.read(
    opf,
    pf,
    'function price(string memory symbol) external view returns (uint)',
    [ underlying ],
    { provider }
    @@ -101,29 +99,33 @@ const apxBlockSpeedInSeconds = 13.15;
    const mantissa = 18 + parseInt(underlyingDecimals) - cTokenDecimals;
    exchangeRate = +exchangeRate.toString() / Math.pow(10, 18);

    compSpeedSupply = compSpeedSupply / 1e18; // COMP has 18 decimal places
    compSpeedBorrow = compSpeedBorrow / 1e18; // COMP has 18 decimal places
    // compSpeed = compSpeed / 1e18; // COMP has 18 decimal places
    compBorrowSpeed = compBorrowSpeed / 1e18;
    compSupplySpeed = compSupplySpeed / 1e18;
    compPrice = compPrice / 1e6; // price feed is USD price with 6 decimal places
    assetPrice = assetPrice / 1e6;
    totalBorrows = +totalBorrows.toString() / (Math.pow(10, underlyingDecimals));
    totalSupply = (+totalSupply.toString() * exchangeRate) / (Math.pow(10, underlyingDecimals));

    console.log('compSpeedSupply:', compSpeedSupply);
    console.log('compSpeedBorrow:', compSpeedBorrow);
    console.log('compPrice:', compPrice);
    console.log('assetPrice:', assetPrice);
    console.log('totalBorrows:', totalBorrows);
    console.log('totalSupply:', totalSupply);
    console.log('exchangeRate:', exchangeRate);
    // console.log('compSpeed:', compSpeed);
    // console.log('compBorrowSpeed:', compBorrowSpeed);
    // console.log('compSupplySpeed:', compSupplySpeed);
    // console.log('compPrice:', compPrice);
    // console.log('assetPrice:', assetPrice);
    // console.log('totalBorrows:', totalBorrows);
    // console.log('totalSupply:', totalSupply);
    // console.log('exchangeRate:', exchangeRate);

    const compPerDaySupply = compSpeedSupply * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);
    const compPerDayBorrow = compSpeedBorrow * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);
    // const compPerDay = compSpeed * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);
    const compToBorrowersPerDay = compBorrowSpeed * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);
    const compToSuppliersPerDay = compSupplySpeed * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);

    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compPerDaySupply / (totalSupply * assetPrice))), 365) - 1);
    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compPerDayBorrow / (totalBorrows * assetPrice))), 365) - 1);
    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compToBorrowersPerDay / (totalBorrows * assetPrice))), 365) - 1);
    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compToSuppliersPerDay / (totalSupply * assetPrice))), 365) - 1);

    console.log('COMP Supply APY %:', compSupplyApy);
    console.log('COMP Borrow APY %:', compBorrowApy);
    console.log('COMP Supply APY %:', compSupplyApy);

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

    ```
  5. ajb413 revised this gist Nov 16, 2021. 1 changed file with 23 additions and 13 deletions.
    36 changes: 23 additions & 13 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -48,9 +48,16 @@ const apxBlockSpeedInSeconds = 13.15;

    (async function() {

    let compSpeed = await Compound.eth.read(
    let compSpeedSupply = await Compound.eth.read(
    comptroller,
    'function compSpeeds(address cToken) public returns (uint)',
    'function compSupplySpeeds(address cToken) public returns (uint)',
    [ cTokenAddr ],
    { provider }
    );

    let compSpeedBorrow = await Compound.eth.read(
    comptroller,
    'function compBorrowSpeeds(address cToken) public returns (uint)',
    [ cTokenAddr ],
    { provider }
    );
    @@ -94,26 +101,29 @@ const apxBlockSpeedInSeconds = 13.15;
    const mantissa = 18 + parseInt(underlyingDecimals) - cTokenDecimals;
    exchangeRate = +exchangeRate.toString() / Math.pow(10, 18);

    compSpeed = compSpeed / 1e18; // COMP has 18 decimal places
    compSpeedSupply = compSpeedSupply / 1e18; // COMP has 18 decimal places
    compSpeedBorrow = compSpeedBorrow / 1e18; // COMP has 18 decimal places
    compPrice = compPrice / 1e6; // price feed is USD price with 6 decimal places
    assetPrice = assetPrice / 1e6;
    totalBorrows = +totalBorrows.toString() / (Math.pow(10, underlyingDecimals));
    totalSupply = (+totalSupply.toString() * exchangeRate) / (Math.pow(10, underlyingDecimals));

    // console.log('compSpeed:', compSpeed);
    // console.log('compPrice:', compPrice);
    // console.log('assetPrice:', assetPrice);
    // console.log('totalBorrows:', totalBorrows);
    // console.log('totalSupply:', totalSupply);
    // console.log('exchangeRate:', exchangeRate);
    console.log('compSpeedSupply:', compSpeedSupply);
    console.log('compSpeedBorrow:', compSpeedBorrow);
    console.log('compPrice:', compPrice);
    console.log('assetPrice:', assetPrice);
    console.log('totalBorrows:', totalBorrows);
    console.log('totalSupply:', totalSupply);
    console.log('exchangeRate:', exchangeRate);

    const compPerDay = compSpeed * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);
    const compPerDaySupply = compSpeedSupply * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);
    const compPerDayBorrow = compSpeedBorrow * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);

    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compPerDay / (totalBorrows * assetPrice))), 365) - 1);
    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compPerDay / (totalSupply * assetPrice))), 365) - 1);
    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compPerDaySupply / (totalSupply * assetPrice))), 365) - 1);
    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compPerDayBorrow / (totalBorrows * assetPrice))), 365) - 1);

    console.log('COMP Borrow APY %:', compBorrowApy);
    console.log('COMP Supply APY %:', compSupplyApy);
    console.log('COMP Borrow APY %:', compBorrowApy);

    })().catch(console.error);
    ```
  6. ajb413 revised this gist Feb 16, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ The Compound cToken API now returns the values for you. See `comp_borrow_apy` an
    4. Query https://api.compound.finance/api/v2/ctoken
    5. Take the value for `total_supply` or `exchange_rate` and `total_borrows`
    6. Query https://prices.compound.finance/
    7. Take COMP price
    7. Take COMP price and also the particular asset's price

    In JavaScript

  7. ajb413 revised this gist Feb 16, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,9 @@ The Compound cToken API now returns the values for you. See `comp_borrow_apy` an
    In JavaScript

    ```js
    borrowCompApy = 100 * (Math.pow((1 + (comp_price * borrower_daily_comp / total_borrows)), 365) - 1)
    borrowCompApy = 100 * (Math.pow((1 + (comp_usd_price * borrower_daily_comp / (total_borrows * asset_usd_price))), 365) - 1);

    supplyCompApy = 100 * (Math.pow((1 + (comp_price * supplier_daily_comp / (total_supply * exchange_rate))), 365) - 1)
    supplyCompApy = 100 * (Math.pow((1 + (comp_usd_price * supplier_daily_comp / (total_supply * asset_usd_price))), 365) - 1);
    ```

    ## From Blockchain With JSON RPC and JavaScript
  8. ajb413 revised this gist Feb 16, 2021. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,8 @@ const provider = 'https://mainnet.infura.io/v3/' + process.env.infuraApiKey;

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

    const underlyingDecimals = Compound.decimals[cTokenToGetCompApy.slice(1, 10)];
    const underlying = cTokenToGetCompApy.slice(1, 10);
    const underlyingDecimals = Compound.decimals[underlying];
    const cTokenDecimals = 8; // always 8
    const comptroller = Compound.util.getAddress(Compound.Comptroller);
    const opf = Compound.util.getAddress(Compound.PriceFeed);
    @@ -61,6 +62,13 @@ const apxBlockSpeedInSeconds = 13.15;
    { provider }
    );

    let assetPrice = await Compound.eth.read(
    opf,
    'function price(string memory symbol) external view returns (uint)',
    [ underlying ],
    { provider }
    );

    let totalBorrows = await Compound.eth.read(
    cTokenAddr,
    'function totalBorrowsCurrent() returns (uint)',
    @@ -84,23 +92,25 @@ const apxBlockSpeedInSeconds = 13.15;

    // Total supply needs to be converted from cTokens
    const mantissa = 18 + parseInt(underlyingDecimals) - cTokenDecimals;
    exchangeRate = +exchangeRate.toString() / Math.pow(10, mantissa);
    exchangeRate = +exchangeRate.toString() / Math.pow(10, 18);

    compSpeed = compSpeed / 1e18; // COMP has 18 decimal places
    compPrice = compPrice / 1e6; // price feed is USD price with 6 decimal places
    assetPrice = assetPrice / 1e6;
    totalBorrows = +totalBorrows.toString() / (Math.pow(10, underlyingDecimals));
    totalSupply = (+totalSupply.toString() * exchangeRate) / (Math.pow(10, underlyingDecimals));

    // console.log('compSpeed:', compSpeed);
    // console.log('compPrice:', compPrice);
    // console.log('assetPrice:', assetPrice);
    // console.log('totalBorrows:', totalBorrows);
    // console.log('totalSupply:', totalSupply);
    // console.log('exchangeRate:', exchangeRate);

    const compPerDay = compSpeed * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);

    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compPerDay / totalBorrows)), 365) - 1);
    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compPerDay / totalSupply)), 365) - 1);
    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compPerDay / (totalBorrows * assetPrice))), 365) - 1);
    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compPerDay / (totalSupply * assetPrice))), 365) - 1);

    console.log('COMP Borrow APY %:', compBorrowApy);
    console.log('COMP Supply APY %:', compSupplyApy);
  9. ajb413 revised this gist Sep 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ const provider = 'https://mainnet.infura.io/v3/' + process.env.infuraApiKey;
    const cTokenToGetCompApy = Compound.cUSDC; // Pick an asset

    const underlyingDecimals = Compound.decimals[cTokenToGetCompApy.slice(1, 10)];
    const cTokenDecimals = 6; // always 6
    const cTokenDecimals = 8; // always 8
    const comptroller = Compound.util.getAddress(Compound.Comptroller);
    const opf = Compound.util.getAddress(Compound.PriceFeed);
    const cTokenAddr = Compound.util.getAddress(cTokenToGetCompApy);
  10. ajb413 revised this gist Sep 11, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    ## Using the Compound API

    **UPDATE**

    The Compound cToken API now returns the values for you. See `comp_borrow_apy` and `comp_supply_apy` when requesting https://api.compound.finance/api/v2/ctoken

    **OLD METHOD:**
  11. ajb413 revised this gist Sep 11, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    ## Using the Compound API

    **UPDATE**
    The Compound cToken API now returns the values for you. See `comp_borrow_apy` and `comp_supply_apy` when requesting https://api.compound.finance/api/v2/ctoken

    **OLD METHOD:**
    1. Pick a supported cToken and its underlying asset
    2. Query https://api.compound.finance/api/v2/governance/comp
    3. Take the value for `supplier_daily_comp` or `borrower_daily_comp`
  12. ajb413 revised this gist Aug 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ borrowCompApy = 100 * (Math.pow((1 + (comp_price * borrower_daily_comp / total_b
    supplyCompApy = 100 * (Math.pow((1 + (comp_price * supplier_daily_comp / (total_supply * exchange_rate))), 365) - 1)
    ```

    ## With JSON RPC and JavaScript
    ## From Blockchain With JSON RPC and JavaScript
    ```js
    // To use this script, copy it to a file on your machine. (getCompApy.js)
    // Insert your Infura project ID on line 12 to access the blockchain
  13. ajb413 created this gist Aug 25, 2020.
    103 changes: 103 additions & 0 deletions Get_COMP_APY.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    ## Using the Compound API
    1. Pick a supported cToken and its underlying asset
    2. Query https://api.compound.finance/api/v2/governance/comp
    3. Take the value for `supplier_daily_comp` or `borrower_daily_comp`
    4. Query https://api.compound.finance/api/v2/ctoken
    5. Take the value for `total_supply` or `exchange_rate` and `total_borrows`
    6. Query https://prices.compound.finance/
    7. Take COMP price

    In JavaScript

    ```js
    borrowCompApy = 100 * (Math.pow((1 + (comp_price * borrower_daily_comp / total_borrows)), 365) - 1)

    supplyCompApy = 100 * (Math.pow((1 + (comp_price * supplier_daily_comp / (total_supply * exchange_rate))), 365) - 1)
    ```

    ## With JSON RPC and JavaScript
    ```js
    // To use this script, copy it to a file on your machine. (getCompApy.js)
    // Insert your Infura project ID on line 12 to access the blockchain
    // Install Node.js
    // Navigate to the folder of getCompApy.js using the command line

    // npm init -y
    // npm install @compound-finance/compound-js
    // node getCompApy.js

    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 underlyingDecimals = Compound.decimals[cTokenToGetCompApy.slice(1, 10)];
    const cTokenDecimals = 6; // always 6
    const comptroller = Compound.util.getAddress(Compound.Comptroller);
    const opf = Compound.util.getAddress(Compound.PriceFeed);
    const cTokenAddr = Compound.util.getAddress(cTokenToGetCompApy);
    const apxBlockSpeedInSeconds = 13.15;

    (async function() {

    let compSpeed = await Compound.eth.read(
    comptroller,
    'function compSpeeds(address cToken) public returns (uint)',
    [ cTokenAddr ],
    { provider }
    );

    let compPrice = await Compound.eth.read(
    opf,
    'function price(string memory symbol) external view returns (uint)',
    [ Compound.COMP ],
    { provider }
    );

    let totalBorrows = await Compound.eth.read(
    cTokenAddr,
    'function totalBorrowsCurrent() returns (uint)',
    [],
    { provider }
    );

    let totalSupply = await Compound.eth.read(
    cTokenAddr,
    'function totalSupply() returns (uint)',
    [],
    { provider }
    );

    let exchangeRate = await Compound.eth.read(
    cTokenAddr,
    'function exchangeRateCurrent() returns (uint)',
    [],
    { provider }
    );

    // Total supply needs to be converted from cTokens
    const mantissa = 18 + parseInt(underlyingDecimals) - cTokenDecimals;
    exchangeRate = +exchangeRate.toString() / Math.pow(10, mantissa);

    compSpeed = compSpeed / 1e18; // COMP has 18 decimal places
    compPrice = compPrice / 1e6; // price feed is USD price with 6 decimal places
    totalBorrows = +totalBorrows.toString() / (Math.pow(10, underlyingDecimals));
    totalSupply = (+totalSupply.toString() * exchangeRate) / (Math.pow(10, underlyingDecimals));

    // console.log('compSpeed:', compSpeed);
    // console.log('compPrice:', compPrice);
    // console.log('totalBorrows:', totalBorrows);
    // console.log('totalSupply:', totalSupply);
    // console.log('exchangeRate:', exchangeRate);

    const compPerDay = compSpeed * parseInt((60 * 60 * 24) / apxBlockSpeedInSeconds);

    const compBorrowApy = 100 * (Math.pow((1 + (compPrice * compPerDay / totalBorrows)), 365) - 1);
    const compSupplyApy = 100 * (Math.pow((1 + (compPrice * compPerDay / totalSupply)), 365) - 1);

    console.log('COMP Borrow APY %:', compBorrowApy);
    console.log('COMP Supply APY %:', compSupplyApy);

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