0

I know I am doing something wrong here but cannot for the life of me see what the issue is. Everything I see in the docs points me to assume I'm doing this all right. I'm definitely doing something wrong tho.

I can't read a view function from ethers:

My call is:

const borrowersHealth = await this.poolInstance.getUserData(
  this.borrower.address
);
console.log("borrowersHealth", Number(borrowersHealth));
// prints out "NaN"

In this issue the following:

3 Contracts

  • Pool.sol
  • Logic.sol
  • Token.sol

I have a function in Pool.sol that calls a function in Logic.sol that calls a function in Token.sol

Here's an example of the situation:

Pool.sol

  function getUserData(
      address user
  ) public override view returns (
      uint256 userCollateralValueAvailable,
      uint256 userDebtValue,
      uint256 overallHealth
  ) {

    (userCollateralValueAvailable,
      userDebtValue,
      overallHealth
    ) = Logic.calculateUserData(
      user,
      reserves,
      reservesList,
      reservesCount
    );
  }

Logic.sol

    function calculateUserData(
          address user,
          mapping(address => UniverseStorage.ReserveData) storage reservesData,
          mapping(uint256 => address) storage reservesList,
          uint256 _reservesCount
      )
      external
      view
      returns (
          uint256,
          uint256,
          uint256
      ) {
          calculateUserDataLocalVars memory vars;
            
          for (uint i = 0; i < _reservesCount; i++) {

            // reserve data retrieved
            // successfully getting wrappedTokenAddrss and fixedDebtTokenAddress

            vars.userDepositsBalance = IERC20(currentReserveData.wrappedTokenAddress).balanceOf(user);
            console.log("userDepositsBalance", vars.userDepositsBalance); // returns 0
    
            vars.userDebtBalance = IERC20(currentReserveData.fixedDebtTokenAddress).balanceOf(user);
            console.log("userDebtBalance ", vars.userDebtBalance ); // returns 0        

            // add up values to get vars.availableAsDebtValue, vars.userDebtBalanceValue
            // removed for brevity
          }
            
    
          // vars.overallHealth calculations that depend on debt variables
    
          return (vars.availableAsDebtValue, vars.userDebtBalanceValue, vars.overallHealth);
      }

Token.sol (this goes for both fixed and wrapped)

  • note: both are doing calculations with SafeMath

  • note: they both return values with hardhat console.log

  • note: even with i do the basic erc20 balanceOf() the issue persists

  • note: I was possibly assuming doing calculations was messing it up but the previous confirmed that wasn't the issue

function balanceOf() public view virtual override returns (uint256) {
    console.log(super.balanceOf()); // returns correct value
    return super.balanceOf();
}
  • note: Within the balanceOf() in the actual contract, there are SafeMath calculations but I console.log them and they return the correct values. They just aren't being returned to the Logic.sol

Is there something making it so these balanceOf's aren't being returned.

  • Note: When I run this method with a transaction, the values are returned. Obviously this has something to do with it but since they are just calculations, I figure, why can't I get them returned and what's stopping it from returning?
1
  • Did you try console.log(borrowersHealth)? It should show an array with three entries. In that case Number(borrowersHealth) obviously print Nan because you cannot convert an array to a number.
    – Ismael
    Jul 22 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.