0

I have a contract that uses an eth/usd price feed library to convert a uint value into its USD equivalent.

pragma solidity 0.8.0;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

library PriceConverter {
  // No state variables inside of libraries
  // All functions must be internal

  function getPrice(AggregatorV3Interface priceFeed)
    internal
    view
    returns (uint256 _price)
  {
    (, int256 price, , , ) = priceFeed.latestRoundData();
    return uint256(price * 1e10);
  }

  function getConversionRate(uint256 ethAmount, AggregatorV3Interface priceFeed)
    internal
    view
    returns (uint256)
  {
    uint256 price = getPrice(priceFeed);
    uint256 ethAmountInUsd = (price * ethAmount) / 1e18;
    return (ethAmountInUsd);
  }
}

pragma solidity 0.8.0;

import "./PriceConverter.sol";

contract Foo {
  using priceConverter for uint256;
  MINIMUM_USD = 7000000000000
  
  function bar() public payable {
     require(msg.value.getConversionRate() > MINIMUM_USD)
  }
function getBalanceInUSD() public view returns (uint256) {
    uint256 balanceInUSD = (balances[msg.sender].getConversionRate(priceFeed));
    return (balanceInUSD);
  }
}

In my hardhat test I am trying to call the getConversionRate() uint256 function in my contract to test some values, but I am unsure how to access it. (I know I need to send 2 parameters when I call the getConversionRate() function and I will edit the code to reflect that once it at least recognizes the function)

describe("getBalanceInUSD", function () {
        it.only("should return the user's balance in terms of USD", async function () {
          const tx = await bank.MINIMUMUSD()
          console.log(tx.toString())
          const bal = await bank.getBalanceInUSD()
          console.log(bal)
          const expectedValue = await bank.sendValue.getConversionRate()
          console.log(expectedValue)
        })
      })

Here is the error message: TypeError: Cannot read properties of undefined (reading 'getConversionRate')

1 Answer 1

0

To reference them use the library name, like this PriceConverter.getConversionRate. Then fill the parameters as needed

AggregatorV3Interface _priceFeed; // required price feed

function bar() public payable {
    uint amount = PriceConverter.getConversionRate(msg.value, _priceFeed);
    require(amount >= MINIMUM_USD, "Insufficient amount");
}
3
  • My solidity code isn't the thing that I am trying to change, It is my hardhat js test that covers the contract.
    – Rohan Nero
    Aug 1 at 14:27
  • 1
    @RohanNero Can you share the test? How does it fail? You can edit the question to add more details.
    – Ismael
    Aug 1 at 21:35
  • Ok sorry, I updated it to include more information, I hope that helps!
    – Rohan Nero
    Aug 2 at 15:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.