Lesson 4: Library - DeclarationError: Undeclared identifier. How should we call a function from library that doesn't require parameter? #1908
-
I added a function (function getEthPrice()) that doesn't accept parameter in PriceConverter library. How should we use it in other contract? library PriceConverter{
function getEthPrice() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 price,,,) = priceFeed.latestRoundData();
return uint256(price * 1e10);
}
function getUSDPrice(uint256 ethAmount) public view returns(uint256){
uint256 ethPrice = getEthPrice();
uint256 ethUSD = (ethPrice * ethAmount) / 1e18;
return ethUSD;
}
} FundMe.sol
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
The following should fix it: function fund() public payable{
require(msg.value.getUSDPrice() >= minimumUSD, "Insufficient amount mo!");
senders[msg.sender] = msg.value.getUSDPrice(); // this one works fine
- getEthPrice(); // This error would be thrown DeclarationError: Undeclared identifier.
+ uint256 ethPrice = getEthPrice();
} |
Beta Was this translation helpful? Give feedback.
-
@krakxn PriceConverter.sol import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter{
function getEthPrice() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 price,,,) = priceFeed.latestRoundData();
return uint256(price * 1e10);
}
function getUSDPrice(uint256 ethAmount) public view returns(uint256){
uint256 ethPrice = getEthPrice();
uint256 ethUSD = (ethPrice * ethAmount) / 1e18;
return ethUSD;
//
}
} FundMe.sol //SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConverter.sol";
contract FundMe{
using PriceConverter for uint256;
uint256 public nValue;
uint256 public constant minimumUSD = 10 * 1e18;
address[] public funders;
mapping(address => uint256) public sender;
address public owner;
constructor(){
owner = msg.sender;
}
function fund() public payable{
//want to set minimum fund amount in USD
// Sending ETH to this contract
require(msg.value.getUSDPrice() >= minimumUSD, "Insufficient amount mo!"); // 1e18 == 1* 10 ** 18 == 1000000000000000000
funders.push(msg.sender);
sender[msg.sender] = getUSD(msg.value);//msg.value.getUSDPrice();
}
function getUSD(uint256 ethAmount) public view returns(uint256){
uint256 ethPrice = getEthPrice(); // this would throw DeclarationError: Undeclared identifier.
uint256 ethUSD = (ethPrice * ethAmount) / 1e18;
return ethUSD;
} |
Beta Was this translation helpful? Give feedback.
-
@nsawit You can call it like this PriceConverter.getEthPrice(); |
Beta Was this translation helpful? Give feedback.
@nsawit You can call it like this