-
Hello! Solidity code: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConverter.sol";
error Sub_NotQuantity();
error Already_SUB();
contract Code {
using PriceConverter for uint256;
//Variables
enum access {
DENIED,
GRANTED
}
struct Dades {
uint256 timer;
access s_access;
}
address payable [] private s_Wallets;
mapping (address => Dades) s_Registre;
address public immutable i_owner;
uint256 private s_SubPrice; //= 0.1 ether;
uint256 private EthPrice;
AggregatorV3Interface public priceFeed;
//access private s_access;
constructor (uint256 startPrice) {
i_owner = msg.sender;
s_SubPrice = startPrice;
priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
}
modifier onlyOwner{
require(msg.sender == i_owner);
_;
}
//Functions
function PriceConversion() public { //Function where I call the conversion
EthPrice = PriceConverter.ConversionToEth(s_SubPrice, priceFeed);
} Library: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter {
/**
* Network: Rinkeby
* Aggregator: ETH/USD
* Address: 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
*/
function getLatestPrice(AggregatorV3Interface priceFeed) internal view returns (uint256) {
(
/*uint80 roundID*/,
int256 price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return uint256(price/100000000);// Retorna 8 decimals
}
function ConversionToEth(uint256 DollaAmount, AggregatorV3Interface priceFeed) internal view returns(uint256) {
uint256 Price = getLatestPrice(priceFeed);
uint256 EthAmount = DollaAmount/Price;
return EthAmount;
}
} I can deploy and call the conversion in the rinkeby network, but as you can see the Ethvalue is still 0. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
uint256(price/100000000)
before, try follow the method introduced in the tutorial (giving them 18 decimal).uint256 EthAmount = DollaAmount/Price
, whenever the input Dollar is less than the current eth price (e.g. $110, eth price $1100) , the return will be zero since 0.1 is not an int256.