-
I'm encountering a compilation error in my Solidity project when using the Chainlink AggregatorV3Interface. The error message is: The contract is designed to accept an address of a deployed AggregatorV3Interface during construction and use it for price conversion. Code Snippet: Steps Taken:
Questions: Why am I getting an invalid implicit conversion error when the types seem to match? How can I ensure the AggregatorV3Interface type is consistent across my codebase? Is there a specific version of Chainlink's contracts I should be using? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Hello @chapman91, Please show your |
Beta Was this translation helpful? Give feedback.
-
Below you have the Fundme contract`// SPDX-License-Identifier: MIT import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; error FundMe__NotOwner(); contract FundMe {
} Below is the PriceConverter Contract`// SPDX-License-Identifier: MIT import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; library PriceConverter {
} |
Beta Was this translation helpful? Give feedback.
-
FUNDME CONTRACT // SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
import {PriceConverter} from "./PriceConverter.sol";
error FundMe__NotOwner();
contract FundMe {
using PriceConverter for uint256;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public /* immutable */ i_owner;
uint256 public constant MINIMUM_USD = 5 * 10 ** 18; // ** acts as the exponentiation operator
AggregatorV3Interface private s_priceFeed;
constructor(address priceFeed) {
s_priceFeed = AggregatorV3Interface(priceFeed);
i_owner = msg.sender;
}
function fund() public payable {
require(msg.value.getConversionRate(s_priceFeed) >= MINIMUM_USD, "You need to spend more ETH!");
// require(PriceConverter.getConversionRate(msg.value) >= MINIMUM_USD, "You need to spend more ETH!");
addressToAmountFunded[msg.sender] += msg.value;
funders.push(msg.sender);
}
// getVersion Function
function getVersion() public view returns (uint256) {
return s_priceFeed.version();
}
modifier onlyOwner() {
if (msg.sender != i_owner) revert FundMe__NotOwner();
_;
}
function withdraw() public onlyOwner {
for (uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++) {
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
funders = new address[](0);
(bool callSuccess,) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed");
}
fallback() external payable {
fund();
}
receive() external payable {
fund();
}
} PRICE CONVERTER LIBRARY // SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter {
function getPrice(
AggregatorV3Interface priceFeed
) internal view returns (uint256) {
(, int256 answer, , , ) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
}
function getConversionRate(
uint256 ethAmount, // Amount of Ether (in wei) to be converteed
AggregatorV3Interface priceFeed // Price feed interface to get the current ETH/USD price
) internal view returns (uint256) {
uint256 ethPrice = getPrice(priceFeed); // Get the currenct ETH price in USD (with 18 decimals)
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000; // calculate the equivalent USD value
// the actual ETH/USD conversion rate, after adjusting the extra 0s.
return ethAmountInUsd;
}
} |
Beta Was this translation helpful? Give feedback.
The path you imported
AggregatorV3Interface
from in yourFundMe
contract isimport {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
. In contrast, the path you importedAggregatorV3Interface
from in yourPriceConverter
Library isimport {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
They are not the same, Please kindly make use of one path in both theFundMe
contract andPriceConverter
Library.