How ensure a whole number deposit (Integer)

Hello Community,
please bear with me this might be a dump question but i can’t wrap my head around that:

User should be able to deposit an uint amount between 1 and 10 (integer 1,2,3,4,5,6,7,8,9 or 10) to take part in a raffle. I have a function taking users amount:

function takePartInRaffle(uint _amount) external activeContract {
require(_amount > 0, “amount is 0”);
require(_amount <= maxRaffleVouchers, “max vouchers limit”);

as the user is able to deposit any uint like 1.5 ETH or 1.22 ETH i am stuck and don’t know how to write a require precondition here. Also not sure if i am completely on a wrong path with my idea to prevent / force the user to deposit a whole / integer number.

Would be awesome to get some answers on that.

Kind regards
esodot

You don’t need to worry about that.
That amount has decimals. For example, Eth has 18 decimals.
So if user bet 1.22ETH, _amount will be 1,220,000,000,000,000,000.
If you want to make people to bet only unit amount(1ETH, 2ETH…), you can use modulo operator.
But I dont think it’s needed.

1 Like

I believe you are missing some info on two subjects here:

  1. Understanding how units in Ethereum work
  2. Understanding how “money” is transferred in a smart contract

Units
On the blockchain, there are no fractions. Everything is an integer, meaning a whole number, with no decimal point.

How do we represent 1.1 ETH then?
The actual unit-of-account on Ethereum is called “Wei”, and every ETH is actually 10^18 Wei, so:
1 ETH == 1,000,000,000,000,000,000 Wei

If you want to represent 1.1 ETH, you denominate it in Wei:
1.1 ETH == 1,100,000,000,000,000,000 Wei

Solidity provides several convenience shorthand units, for example ether.
Instead of writing uint amount = 1000000000000000000, you can write uint amount = 1 ether.

Check out the docs to see what shorthand units Solidity provides.

Hope this helps with understanding units.

How to transfer money
If you want to be able to accept money inside a smart contract function, you need to mark it payable:

function takePartInRaffle(uint _amount) external activeContract payable {
...
}

And to access the transferred amount, you don’t use a function parameter, you use a special variable: msg.value. This variable contains the amount of Wei transferred along with the transaction.
So if you want, for example, to require someone to send at least 1 ETH, you would use:

require(msg.value > 1000000000000000000);

Or, using the Solidity units:

require(msg.value > 1 ether);

These two are the same.

Check out this StackExchange answer for more info.

Hope this helps.

1 Like

Thank you very much for the great explanation. I really was not aware how the decimals and fractions are handled. Make sense now! :+1: