console.log('Checking status of each asset...\n');
for (const asset of assets) {
// First we'll check balances in this loop and see if any of our
// balances come from borrows. For brevity, we prefix all properties
// about the underlying token with `u`
const cToken = new Contract(asset, cozyTokenAbi, signer);
const [name, symbol] = await Promise.all([cToken.name(), cToken.symbol()]);
console.log(`Current asset has name "${name}" and symbol "${symbol}"`);
// Let's also learn about the underlying token. First we determine
// if the underlying is ETH then get a contract instance for the
// underlying token if the underlying is not ETH
const uAddr = await cToken.underlying();
const isEth = uAddr === ethAddress; // true if underlying is ETH, false otherwise
const underlying = isEth ? null : new Contract(uAddr, erc20Abi, signer);
// Lastly we either read set the values if ETH is underlying, or
// read the values if it's a token
const uName = underlying ? await underlying.name() : 'Ether';
const uSymbol = underlying ? await cToken.symbol() : 'ETH';
const uDecimals = underlying ? await underlying.decimals() : 18;
console.log(` Underlying ${uName} (${uSymbol}) has ${uDecimals} decimals`);
// Get our balance of the Cozy token
const balance = await cToken.balanceOf(ourAddress);
const balanceScaled = formatUnits(balance, cTokenDecimals);
// Balance is zero, so we have not supplied the underlying token
console.log(' No Cozy Token balance, so nothing supplied');
// Balance is > 0, so we have supplied some amount of underlying
// tokens. We get exchange rate to figure out how much underlying
// we have. The exchange rate is a mantissa (18 decimal value), so
// the value returned is scaled by
// 10 ** (18 + underlyingDecimals - cTokenDecimals)
const exchangeRate = await cToken.exchangeRateStored();
const scale = 18 + uDecimals - cTokenDecimals;
const uBalance = balance.mul(exchangeRate);
const uBalanceScaled = formatUnits(uBalance, scale + cTokenDecimals);
console.log(` Balance of ${balanceScaled} Cozy Tokens (equal to ${uBalanceScaled} underlying)`);
// Now get our balance of the underlying token
const uBalance = underlying ? await underlying.balanceOf(ourAddress) : await signer.provider.getBalance(ourAddress);
// Underlying balance is zero, so we have not borrowed the
console.log(` No underlying ${uSymbol} balance`);
// Underlying balance is above zero, BUT we still may not have
// borrowed this token -- we may have already had some
const uBalanceScaled = formatUnits(uBalance, uDecimals);
console.log(` Balance of ${uBalanceScaled} underlying ${uSymbol} tokens`);
// Read the amount borrowed
const borrowBalance = await cToken.borrowBalanceStored(ourAddress);
const borrowBalanceScaled = formatUnits(borrowBalance, uDecimals); // scale to human readable units
// Now we determine if the funds were borrowed
if (borrowBalance.eq(Zero)) console.log(` None of the underlying ${uSymbol} tokens we have were borrowed`);
else if (borrowBalance.eq(uBalance)) console.log(` All the underlying ${uSymbol} tokens we have were borrowed`);
else console.log(` ${borrowBalanceScaled} of the ${uBalanceScaled} underlying ${uSymbol} tokens were borrowed`);