Naive Lottery Smart Contract Implemented In Plutus

Besides enjoying in the holiday cheer I’ve been spending most of my free time over the past week reading through all of the source code for Plutus and getting the hang of how the Plutus Platform is suppose to work.

Given that there’s only a handful of example SCs, and only 2 that are reasonably simple enough to skim over without really knowing what’s going on, I’ve decided to start releasing some more basic contracts.

This first one is an implementation of a simple lottery game. It’s not too bad to follow along with if you’re use to haskell, but just be a bit wary when trying to edit around in the ValidatorScript/smart contract itself. Plutus Playground has some bugs, one of which being it won’t display many compiler error messages (seems to have to do with the GHC plugin for converting to Plutus Core having broken error messages on PP) so unless you clone the repo and compile the contract locally, you’ll be shooting in the dark. Even if you’re not too technical you can still run this contract on Plutus Playground and play around with the UI.

Once IOHK starts to finalise the Prelude, fix some bugs, and just in general patch a lot of the holes in the IDE and in Plutus itself, I intend to begin writing some tutorials for the community. Have fun hacking around, and hope the holidays have been treating everyone well :+1:

26 Likes

Thank you!

1 Like

great work for the non tech guy.

1 Like

Great work! Also, thank you for reporting the bugs through GitHub. That is very helpful!

5 Likes

It’s my pleasure. Fixing a lot of the front-facing bugs that beginners will run into is important for adoption, so the quicker they can get solved the better.

3 Likes

Huge respect!

I have re-started learning Haskell a couple of days ago via learnyouahaskell.com. Will see how it goes, but looking forward to some practical applications of the language in blockchain space.

4 Likes

@bobert Thanks again for providing this and the Jelly Bean Guessing Game example.

I hate to ask this since you’ve already been so helpful. But, if you get five mins, it would be super helpful if you could explain in layman’s terms what’s going on in the on-chain code with this one (i.e. what each part of the on-chain code is doing). The second half of the on-chain code seems a little more straight forward. But, a quick breakdown of the first half would be really helpful.

Sure thing, though I don’t have the time to explain folds in laymans terms so that’s a bit of homework on you.

isNumWinner is a function which uses a right fold to go through the list of winning numbers and check that it is one of them, or return False.

isWinner then is a right fold that takes every number in the provided ticket and then calls isNumWinner on them to verify each numbers returns true. If they all return true then it returns true as well.

Check out folding in haskell and it should make a lot more sense to you: https://wiki.haskell.org/Foldr_Foldl_Foldl

1 Like

@bobert Many thanks! Very helpful!

For anyone learning this at the same time as me, this video has been super helpful and covers foldr and foldl at 24:50:

3 Likes