Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding a lottery to examples #1850

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

kazai777
Copy link
Contributor

Adding a gnolotto lottery to the examples r/demo/gnolotto_factory and p/demo/gnolotto, with a README as tutorial in the realm

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@kazai777 kazai777 requested review from a team as code owners March 27, 2024 12:26
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Mar 27, 2024
Copy link

codecov bot commented Mar 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.43%. Comparing base (8ec556e) to head (63d4d90).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1850      +/-   ##
==========================================
+ Coverage   63.30%   63.43%   +0.12%     
==========================================
  Files         548      535      -13     
  Lines       78511    84991    +6480     
==========================================
+ Hits        49704    53916    +4212     
- Misses      25452    27548    +2096     
- Partials     3355     3527     +172     
Flag Coverage Δ
contribs/gnodev 59.94% <ø> (-0.63%) ⬇️
contribs/gnofaucet 14.82% <ø> (ø)
contribs/gnokeykc ?
contribs/gnomd ?
gno.land 67.18% <ø> (ø)
gnovm 67.88% <ø> (ø)
misc/autocounterd ?
misc/genproto ?
misc/genstd 79.72% <ø> (ø)
misc/goscan ?
misc/loop ?
tm2 62.42% <ø> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution and tutorial!

I've left comments on which parts can be improved. I suggest that for each conversation, you make a change, and then commit it. This way, reviewers can exactly see where you changed code to match their suggestion. You can copy commit links and paste them into the conversations. Please ping me on Signal when you finish implementing the feedback 🙏

examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
examples/gno.land/r/demo/gnolotto_factory/README.md Outdated Show resolved Hide resolved
examples/gno.land/r/demo/gnolotto_factory/README.md Outdated Show resolved Hide resolved
@notJoon
Copy link
Member

notJoon commented Apr 5, 2024

The tidy CI has failed; please consider running the following commands:

cd examples
make tidy

Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I left a second round of reviews. Thanks for working on this.

Now for the hardest part - please try to write tests for the package and the realm. This will make sure that the realm is working correctly, and you will most likely discover some issues while writing the tests.

examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/gnolotto/gnolotto.gno Outdated Show resolved Hide resolved
Comment on lines +84 to +92
l, _ := lotteryRaw.(*gnolotto.Lottery)

if time.Now().Unix() > l.DrawTime.Unix() {
panic("This lottery has already ended")
}

if len(numbers) != gnolotto.MaxLottoNumbers {
panic("You must select exactly 5 numbers")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we want to be careful with how much computation we do in Gno, I suggest you move these simple checks before more complex ones, since there is a chance to exit the transaction early and spend less gas that way.

For example, why would you check if the numbers are unique (costs more) before checking if the number of numbers is correct (costs less)?

@zivkovicmilos
Copy link
Member

Hey @kazai777, have you had a chance to look at the comments?

@kazai777
Copy link
Contributor Author

Hey @kazai777, have you had a chance to look at the comments?

Hi @zivkovicmilos , I hadn't seen the comments, I'll have a look :)

@kazai777 kazai777 requested a review from leohhhn August 30, 2024 06:38
@Kouteki Kouteki added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 8, 2024
@jefft0 jefft0 added review/triage-pending PRs opened by external contributors that are waiting for the 1st review and removed review/triage-pending PRs opened by external contributors that are waiting for the 1st review labels Oct 8, 2024
@thehowl
Copy link
Member

thehowl commented Oct 31, 2024

hey @leohhhn, can you take another look?

Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I left some more comments. Please, do not resolve conversations if you haven't implemented the requested change (ie, this comment). Also, the tests are failing.

Comment on lines +111 to +117
func Draw(lotteryIDStr string) string {
lotteryID, _ := seqid.FromString(lotteryIDStr)
id := lotteryID.Binary()

if std.PrevRealm().Addr() != admin {
panic("Only the admin can draw the winning numbers")
}
Copy link
Contributor

@leohhhn leohhhn Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit early if the caller is not the admin; save some gas

}

lotteryRaw, exists := lotteries.Get(id)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


const MaxLottoNumbers = 5

// Adds a new ticket to the lottery
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you follow godoc? Start the comment with the func name. Applies for all comments

@@ -0,0 +1,113 @@
package gnolotto
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a short godoc comment about the package to give a brief intro as to what it's about, what it can be used for?

Comment on lines +31 to +45
sentCoins := std.GetOrigSend()
amount := sentCoins.AmountOf("ugnot")

if prizePool != amount {
panic("Prize pool must match the transaction value")
}

if drawTime < time.Now().Unix() {
panic("Invalid draw time")
}

if std.PrevRealm().Addr() != admin {
panic("Only the admin can create a lottery")
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First do checks, then everything else. Check for admin first.

)

// Replace this address with your address
var admin std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use p/demo/ownable

Comment on lines +25 to +27
func init() {
lotteries = avl.NewTree()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's initialize this tree at the declaration, outside of init.


lottery := gnolotto.NewLottery(time.Unix(drawTime, 0), prizePool)

lotteries.Set(lotteryID.Binary(), lottery)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .Binary()? Use .String()

Comment on lines +39 to +59
func (l *Lottery) Draw() {
var blockHeight int64 = std.GetHeight()

l.WinningNumbers = nil
numbersMap := make(map[int]bool)

// Add variability to the pseudo-random number generation
var variabilityFactor int64 = 1

for len(l.WinningNumbers) < MaxLottoNumbers {
simpleSeed := (blockHeight + variabilityFactor*251) % 233280
number := int(simpleSeed%15) + 1 // Ensure number is between 1 and 15

if !numbersMap[number] {
l.WinningNumbers = append(l.WinningNumbers, number)
numbersMap[number] = true
}

variabilityFactor += 13 // Adjusts for increased variability
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have p/demo/entropy, let's use that. The main reason someone might check this package out in the examples folder is to check how randomness is done (lottery = random).

return numbers, nil
}

func AreNumberMatching(ticketNumbers, winningNumbers []int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a private func

Suggested change
func AreNumberMatching(ticketNumbers, winningNumbers []int) bool {
func areNumberMatching(ticketNumbers, winningNumbers []int) bool {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages. review/triage-pending PRs opened by external contributors that are waiting for the 1st review
Projects
Status: DevRel Review Pile
Status: In Progress
Status: No status
Status: In Review
Development

Successfully merging this pull request may close these issues.

7 participants