const oneBTokenInUnderlying = exchangeRateCurrent / (1 * 10 ^ (18 + underlyingDecimals - bTokenDecimals))
Here is an example of finding the value of 1 bWETH in WETH with Web3.js JavaScript.
const bTokenDecimals = 8; // all bTokens have 8 decimal places
const underlying = new web3.eth.Contract(erc20Abi, ethAddress);
const bToken = new web3.eth.Contract(bTokenAbi, bEthAddress);
const underlyingDecimals = await underlying.methods.decimals().call();
const exchangeRateCurrent = await bToken.methods.exchangeRateCurrent().call();
const mantissa = 18 + parseInt(underlyingDecimals) - bTokenDecimals;
const oneBTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
console.log('1 bWETH can be redeemed for', oneBTokenInUnderlying, 'WETH');
To find the number of underlying tokens that can be redeemed for bTokens, multiply the number of bTokens by the above value oneBTokenInUnderlying
.
const underlyingTokens = bTokenAmount * oneBTokenInUnderlying