Hi I am in the middle of looking into creating an ETH casino for fun and to learn smart contracts and what not.

So say I have a website where you deposit 1 ETH and then you gamble it and you win, so now you have 2 ETH and you decide to withdraw.

How would the flow look like?

Like when a user registers they must be assigned an address to deposit to that is unique to that user.

Then the user has a balance of 1 ETH and he gambles it and it turns into 2 ETH.

How would the withdraw procedure look like for this? When he withdraws it would send from that deposit address and from the "main" address?

So my questions are:

Would this be possible to do through smart contracts?

How would you generate these deposit addresses and control them to withdraw and such?

Thank you very much.

1 Answer 1


You use each user address and define a mapping with users balances.

mapping (address => uint) balance;

So when they make a deposit their balance will increase

function deposit() public payable {
    balance[msg.sender] += msg.value;

When a bet is done you debit from their balance

function bet(uint amount) public {
    require(balance[msg.sender] >= amount);
    balance[msg.sender] -= amount;
    // Process bet ...

And when someone want to withdraw their balance they will call this

function withdraw(uint amount) public {
    if (amount > balance[msg.sender]) {
        amount = balance[msg.sender];
    balance[msg.sender] -= amount;

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