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
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3d20ae0
add realm and README
kazai777 Mar 27, 2024
f0a477d
add package gnolotto
kazai777 Mar 27, 2024
26a2dd2
Merge branch 'master' into add_kazai_gnolotto
kazai777 Mar 27, 2024
e56ea56
replace comment random by pseudo-random
kazai777 Apr 2, 2024
f9f4993
comment modification, replacing random by pesudo-random
kazai777 Apr 2, 2024
4d095c8
Merge branch 'add_kazai_gnolotto' of https://github.com/kazai777/gno …
kazai777 Apr 2, 2024
793f749
Add constant for max lottery numbers
kazai777 Apr 2, 2024
303dfd4
Correct typo for `iterate`
kazai777 Apr 2, 2024
f6b51f2
replace winners by winningOwners
kazai777 Apr 2, 2024
ca1398d
remove casting to int64
kazai777 Apr 2, 2024
8780052
remove refund manually and add panic
kazai777 Apr 2, 2024
88e2171
remove refund manually and add panic
kazai777 Apr 2, 2024
0832c33
remove refund manually and add panic
kazai777 Apr 2, 2024
add8778
remove refund manually and add panic
kazai777 Apr 2, 2024
769d870
remove refund manually and add panic and modify condition
kazai777 Apr 2, 2024
92caa54
remove refund manually and add panic
kazai777 Apr 2, 2024
a006915
replace return by panic
kazai777 Apr 2, 2024
e54628a
replace return by panic
kazai777 Apr 2, 2024
3b683a7
replace return by panic
kazai777 Apr 2, 2024
444b5ef
Modify ID lottery by seqid
kazai777 Apr 2, 2024
b0bccca
add const TICKET_PRICE
kazai777 Apr 2, 2024
bba81f5
separation of string to int slice conversion function
kazai777 Apr 2, 2024
1cdf875
formatting gnokey commands
kazai777 Apr 2, 2024
4c649fd
refactoring README
kazai777 Apr 2, 2024
58e0675
delete import not used in realm
kazai777 Apr 2, 2024
7fbf161
make tidy
kazai777 Apr 5, 2024
3c2409b
modification CheckWinners()
kazai777 Apr 17, 2024
5ea48cc
Merge branch 'gnolang:master' into add_kazai_gnolotto
kazai777 Aug 29, 2024
a4a5f38
add test file for realm and fix code
kazai777 Aug 30, 2024
1fb9689
add testfile for package and fix code
kazai777 Aug 30, 2024
4805e22
fix commit
kazai777 Aug 30, 2024
db4c8c4
Merge branch 'master' into add_kazai_gnolotto
kazai777 Aug 31, 2024
63d4d90
Merge branch 'master' into add_kazai_gnolotto
thehowl Oct 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/gnolotto/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/gnolotto
113 changes: 113 additions & 0 deletions examples/gno.land/p/demo/gnolotto/gnolotto.gno
Original file line number Diff line number Diff line change
@@ -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?


import (
"std"
"strconv"
"strings"
"time"
)

type Ticket struct {
Numbers []int // Holds the selected numbers for the lottery ticket
Owner std.Address // Address of the ticket owner
}

type Lottery struct {
Tickets []Ticket // All tickets in the lottery
WinningNumbers []int // Winning numbers after the draw
DrawTime time.Time // Time of the draw
PrizePool int64 // Total prize pool amount
}

// Intializes a new lottery instance with a specified draw time and prize pool
func NewLottery(drawTime time.Time, prizePool int64) *Lottery {
return &Lottery{
DrawTime: drawTime,
PrizePool: prizePool,
Tickets: make([]Ticket, 0),
}
}

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

func (l *Lottery) AddTicket(numbers []int, owner std.Address) {
l.Tickets = append(l.Tickets, Ticket{Numbers: numbers, Owner: owner})
}

// Conducts the draw by generating 5 pseudo-random numbers between 1 and 15 inclusive
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
}
}
Comment on lines +39 to +59
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).


// Iterate over all tickets to identify and return the addresses of the winningOwners
func (l *Lottery) CheckWinners() []std.Address {
var winningOwners []std.Address
kazai777 marked this conversation as resolved.
Show resolved Hide resolved

for _, ticket := range l.Tickets {
isWinner := true

for i := 0; i < MaxLottoNumbers; i++ {
if ticket.Numbers[i] != l.WinningNumbers[i] {
isWinner = false
break
}
}

if isWinner {
winningOwners = append(winningOwners, ticket.Owner)
}
}

return winningOwners
}

// Distributes the prize pool equally among the winning ticket owners
func (l *Lottery) PayWinners(winningOwners []std.Address) {
kazai777 marked this conversation as resolved.
Show resolved Hide resolved
if len(winningOwners) == 0 {
return
} else {
kazai777 marked this conversation as resolved.
Show resolved Hide resolved
// Calculate reward per winner
var reward int64 = l.PrizePool / int64(len(winningOwners))

banker := std.GetBanker(std.BankerTypeRealmSend)

for _, owner := range winningOwners {
send := std.Coins{{"ugnot", reward}}
banker.SendCoins(std.GetOrigPkgAddr(), owner, send) // Send reward to each winner
}

l.PrizePool = 0 // Reset the prize pool after distribution
}
}

func StringToIntSlice(numbersStr string) ([]int, error) {
numbersSlice := strings.Split(numbersStr, ",")
numbers := make([]int, len(numbersSlice))
for i, numStr := range numbersSlice {
num, err := strconv.Atoi(strings.TrimSpace(numStr))
if err != nil {
return nil, err
}
numbers[i] = num
}
return numbers, nil
}
Loading
Loading