Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Load Compound rates faster #3

Open
pi0neerpat opened this issue Jan 10, 2020 · 4 comments
Open

[Improvement] Load Compound rates faster #3

pi0neerpat opened this issue Jan 10, 2020 · 4 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@pi0neerpat
Copy link
Collaborator

pi0neerpat commented Jan 10, 2020

Problem

Currently we are using the compound historical API to calculate interest sent over time. This requires a single call (potentially more in the future) to the API for each loan transfer event. The API has performed well so far, but I'd prefer if we moved to something faster, more robust, and that we not depend on a 3rd party for certain features to work.

Suggested Solution

Get the historical rate directly from Eth network using the getStorageAt(contract, slot, blockNumber) method. To make this easier, I suggest finding an existing library to help compute the Compound rate directly from contract state, without writing all this logic over again.

@pi0neerpat pi0neerpat added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 10, 2020
@hellwolf
Copy link

Would CToken.exchangeRateStored be of help?

    /**
     * @notice Calculates the exchange rate from the underlying to the CToken
     * @dev This function does not accrue interest before calculating the exchange rate
     * @return Calculated exchange rate scaled by 1e18
     */
    function exchangeRateStored() public view returns (uint256) {

@hellwolf
Copy link

ah, sorry, you need the rates...

How about InterestRateModel.getBorrowRate?

interface InterestRateModel {
    /**
      * @notice Gets the current borrow interest rate based on the given asset, total cash, total borrows
      *         and total reserves.
      * @dev The return value should be scaled by 1e18, thus a return value of
      *      `(true, 1000000000000)` implies an interest rate of 0.000001 or 0.0001% *per block*.
      * @param cash The total cash of the underlying asset in the CToken
      * @param borrows The total borrows of the underlying asset in the CToken
      * @param reserves The total reserves of the underlying asset in the CToken
      * @return Success or failure and the borrow interest rate per block scaled by 10e18
      */
    function getBorrowRate(uint256 cash, uint256 borrows, uint256 reserves) external view returns (uint256, uint256);

https://github.com/rtoken-project/rtoken-contracts/blob/master/compound/contracts/InterestRateModel.sol

@pi0neerpat
Copy link
Collaborator Author

pi0neerpat commented Jan 13, 2020

Two things- 1) How would I call getBorrowRate at a specific block number?
2) We need supply rate not borrow rate

async _getCompoundRate(blockTimestamp) {

@hellwolf
Copy link

hellwolf commented Jan 14, 2020

for 2) use CErc20Interface.supplyRatePerBlock instead, to estimate that for the APR, I do not know what's the standard number of blocks / year people would use.

for 1) this can be solved with this undocumented feature of web3.js: web3/web3.js#1287

I tested it in remix:

CDAI.methods.supplyRatePerBlock().call({}, 9258683, console.log)

Note that 9258683 is the block number of the supplyRatePerBlock I wanted to query with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants