1

I want to create a smart contract that has zero decimal places, i.e. whole integer balances only. The idea is that 1 token represents a ticket. It would be possible for people to buy multiple tickets. So basically an ERC20 with no decimal places, but with one extra function.

I want to be able to randomly select a winning address; getWinner(r), but the winner should be in proportion to the tickets an address owns. So for example; if A owns 1 ticket, B owns 3 tickets; then B is 3 times more likely to win. The problem I'm having is how to do this for a large number of addresses, I don't want to iterate. I guess I could store the address for every ticket in an array; [ A, B, B, B ] but that seems kind of inefficient, also I would have to move things around to plug holes on balance changes and deletes.

Is there a better way?

2
  • whole integer balances only - that's the case whether you like it or not. There are no floating-point types in Solidity. You might be confused with the facts that some dapps display the balance in units of 10**18 instead of in units of 1 (so they divide the balance by 10**18 before displaying it). Jun 26 '19 at 10:31
  • Yes, I just meant that I will set the decimals public field to 0 in the ERC20. Perhaps I should leave it at ^18 and just prevent them owning "fractions", I don't think this really changes the underlying challenge tho.
    – TurbanMan
    Jun 26 '19 at 10:40
0

Maybe you could generate the random number in the contract (on-chain), so everyone can see it was fairly made (ie. not just made up by you), then do the selection of the winning address off-chain, but in a way that is understood and predictable according to the on-chain data -- after which you pass the selected address back in... that way could could do expensive iteration to select the winning address off-chain, ie. for free.

1
  • thx, had not considered that approach.
    – TurbanMan
    Jun 27 '19 at 8:38

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.