I'm storing a product's USD value in contract. mapping (string => uint256) productUSD;

For example, if it's $52 I'd store it as productUSD[_product] = 52;

Now, when people paying in ETH, I want to make sure they sent in a correct converted amount.

i.e. 0.013 ETH

Chainlink's getThePrice() returns the latest ETH/USD price and found it was returning like this. price = 348365740274

    function getThePrice() public view returns (int) {
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        return price;

Simple math, I can drop 8 decimals from 348365740274 and use 3483 to get a converted ETH amount

i.e. 52 / 3483 = 0.0149296583405111 ETH

  1. how do I calculate this in solidity? not sure how to drop decimals from 348365740274
  2. how do I verify user has sent correct eth amount? i.e. require(msg.value >= ...)

As far as chainlink price feeds are concerned, ETH pairs (USD/ETH, AAVE/ETH, UNI/ETH, etc.) have 18 decimals while non-ETH pairs (ETH/USD, etc.) have 8 decimals. Source

solidity integer division automatically rounds off towards 0.(Docs Reference), so you can do price/10**8 to get rounded off integer for dividend representing 8 decimals.

To verify the value sent with the transaction is greater than some threshold when ETH compared with USD, you would divide the minimum required value in USD with USD per ETH, for e.g:

int min_required = 50; // minimum required in USD
int _price = getThePrice() / 10 ** 8; // price of 1 ether in USD

require(msg.value >= min_required / _price, "NOT ENOUGH ETHER");

you can omit using variables to save gas and use values directly, this is just for a clearer explanation.

If you are using a solidity compiler version less than 0.8.x then use SafeMath to take care of integer overflow and underflow


"These things do not affect the direction of the inequality:

Add (or subtract) a number from both sides Multiply (or divide) both sides by a positive number Simplify a side Example: 3x < 7+3 We can simplify 7+3 without affecting the inequality:

3x < 10

But these things do change the direction of the inequality ("<" becomes ">" for example):

Multiply (or divide) both sides by a negative number Swapping left and right hand sides Example: 2y+7 < 12 When we swap the left and right hand sides, we must also change the direction of the inequality:

12 > 2y+7

Here are the details:

Adding or Subtracting a Value We can often solve inequalities by adding (or subtracting) a number from both sides (just as in Introduction to Algebra), like this:

Example: x + 3 < 7 If we subtract 3 from both sides, we get:

x + 3 − 3 < 7 − 3

x < 4

And that is our solution: x < 4

In other words, x can be any value less than 4.

What did we do? We went from this:

To this:

number line inequality ``x+3 < 7``      

x+3 < 7

x < 4

And that works well for adding and subtracting, because if we add (or subtract) the same amount from both sides, it does not affect the inequality

Example: Alex has more coins than Billy. If both Alex and Billy get three more coins each, Alex will still have more coins than Billy.

What If I Solve It, But "x" Is On The Right? No matter, just swap sides, but reverse the sign so it still "points at" the correct value!

Example: 12 < x + 5 If we subtract 5 from both sides, we get':

  • okay thanks ............ – MOORETOKEN May 16 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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