0

So im new to solidity, I just want to check the ethers transferred to the contract are less equal to the value of variable I have in a contract. It is actually checking in wei and not ethers.

require(msg.value > 0 ether, "Please contribute atleast 1 ETH.");
require(msg.value+Amount_Raised <= Amount_ToBe_Raised, "Please contribute correct amount.");

So here I want to check with the uint Amount_ToBe_Raised , but it is checking in wei and not Ether.

2 Answers 2

0

1 ether is equivalent to 1e18(Wei) in solidity so you can check like this:

require(msg.value > 1 * 1e18, "Please contribute at least 1 ETH.");
2
  • No, actually I need something like this require(msg.value+Amount_Raised <= Amount_ToBe_Raised ether, "Please contribute correct amount."); Aug 13, 2022 at 15:12
  • Yes, in order to check in ethers just multiply it with 1e18 or if you want in gwei then 1e9. You can do it also while declaring variables. For example: uint256 public Amount_ToBe_Raised = whatever * 1e18. Aug 13, 2022 at 15:16
0

msg.value is always in wei, not in ethers, and there isn't floating point number in solidity, so you compare values in wei, or you compare msg.value / 1e18 with a value in ether and of course you lose the precision.

1 ether just represents 1e18.

So you can compare like this:

require(msg.value > 1 ether, "Please contribute atleast 1 ETH.");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.