-
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Firstly i will explain the problems, then explain my solutions. Problemuint256 public MINIMUM_USD = 50 * 1e18; or uint256 public MINIMUM_USD = 50 * 10 ** 18; The above code will convert 50 to 50eth but in wei. require(msg.value.getConversionRate() >= MINIMUM_USD), "Didn't send enough ETH"); SolutionI created a function in the PriceConverter.sol library that allows us to convert usd to wei equivalent of eth function convertUsdToEth(uint256 _ethAmountInUsd) internal view returns (uint256) {
uint256 ethPrice = getPrice() / 1e18;
uint256 ethAmountInWei = (_ethAmountInUsd * 1e18 ) / ethPrice ;
return ethAmountInWei;
} Basically the function takes the amount in USD, the get the price of Eth and divide it with 10 ** 18 to get the exact price of eth, then we proceed to multiplying our USD value with 10 ** 18 to convert it to wei equivalent we then proceed to divide it by the exact eth price in USD and return that amount which will be in wei. Additional Problem II couldn't assign the value from the convertUsdToEth function to the MINIMUM_USD constant because constant variable needs to be assign at compile time, SolutionTo fix this i removed the constant from the variable name hence i have something like this Additional Problem IIfunction fund() public payable {
require(msg.value.getConversionRate() >= MINIMUM_USD), "Didn't send enough ETH");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = msg.value;
} When i called the fund function and pass in as little as 1 USD in wei this passed the required statetment. Basically the require statement is comparing the msg.value in USD to the wei value of MINIMUM_USD, in this case the require statement will always pass. SolutionTo fix this i used the same getConversionRate function on the MIMIMUM_USD to convert the wei value back to USD, therefore the required statement is comparing both values in USD. function fund() public payable {
require(msg.value.getConversionRate() >= MINIMUM_USD.getConversionRate(), "Didn't send enough ETH");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = msg.value;
} |
Beta Was this translation helpful? Give feedback.
Firstly i will explain the problems, then explain my solutions.
Problem
or
The above code will convert 50 to 50eth but in wei.
if we now call the fund() function we need to send more than 50eth in wei for the transaction to pass
Solution
I created a function in the PriceConverter.sol library that allows us to convert usd to wei equivalent of eth