transmissions11/libcompound

Feature Request: Get number of tokens minted for number of tokens input to cToken

0xAlcibiades opened this issue · 2 comments

const cTokenDecimals = 8; // all cTokens have 8 decimal places
const underlying = new web3.eth.Contract(erc20Abi, batAddress);
const cToken = new web3.eth.Contract(cTokenAbi, cBatAddress);
const underlyingDecimals = await underlying.methods.decimals().call();
const exchangeRateCurrent = await cToken.methods.exchangeRateCurrent().call();
const mantissa = 18 + parseInt(underlyingDecimals) - cTokenDecimals;
const oneCTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
console.log('1 cBAT can be redeemed for', oneCTokenInUnderlying, 'BAT');

Essentially implementing this on top of LibCompound.viewExchangeRate(CERC20 token), which, by the way, should be public rather than internal, yes?

function getcTokenMintAmount(CERC20 cToken, uint256 numTokensToSupply) public view returns (uint256) would be one interface.

The other would be:
function getTokenRedeemAmount(CERC20 cToken, uint256 numcTokensToRedeem) public view returns (uint256)

gonna close as the math for this is relatively simple and not in the scope of this lib imo— as always feel free to fork 👍

Essentially implementing this on top of LibCompound.viewExchangeRate(CERC20 token), which, by the way, should be public rather than internal, yes?

nah this is meant to be inlined as an internal library (like safeerc20 or safemath), not delegatecall'd as a linked public one