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')