Skip to content

Commit

Permalink
refactor to support commit-reveal (#12)
Browse files Browse the repository at this point in the history
* refactor to support commit-reveal

* get tests working without randomness beacon

* allow raffle creators to restrict who can draw for them
  • Loading branch information
austinkline authored Dec 18, 2023
1 parent 3420349 commit 791cf14
Show file tree
Hide file tree
Showing 12 changed files with 608 additions and 178 deletions.
33 changes: 19 additions & 14 deletions contracts/FlowtyRaffleSource.cdc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import "FlowtyRaffles"

pub contract FlowtyRaffleSource {
pub resource GenericRaffleSource: FlowtyRaffles.RaffleSource {
pub resource AnyStructRaffleSource: FlowtyRaffles.RaffleSourcePublic, FlowtyRaffles.RaffleSourcePrivate {
pub let entries: [AnyStruct]
pub let entryType: Type
pub let removeAfterReveal: Bool

pub fun getEntryType(): Type {
return self.entryType
Expand All @@ -22,37 +23,41 @@ pub contract FlowtyRaffleSource {
}

pub fun addEntries(_ v: [AnyStruct]) {
pre {
pre {
VariableSizedArrayType(self.entryType) == v.getType(): "incorrect array type"
}

self.entries.appendAll(v)
}

pub fun getNumEntries(): Int {
return self.entries.length
pub fun revealCallback(drawingResult: FlowtyRaffles.DrawingResult) {
if !self.removeAfterReveal {
return
}

self.entries.remove(at: drawingResult.index)
}

pub fun getEntries(): [AnyStruct] {
return self.entries
}

pub fun draw(): FlowtyRaffles.DrawingSelection {
let numEntries = self.entries.length
let r = revertibleRandom()
let index = Int(r % UInt64(numEntries))
let value = self.entries[index]

return FlowtyRaffles.DrawingSelection(index, value)
pub fun getEntryCount(): Int {
return self.entries.length
}

init(_ entryType: Type) {
init(entryType: Type, removeAfterReveal: Bool) {
self.entries = []
self.entryType = entryType
self.removeAfterReveal = removeAfterReveal
}
}

pub fun createRaffleSource(_ type: Type): @GenericRaffleSource {
return <- create GenericRaffleSource(type)
pub fun createRaffleSource(entryType: Type, removeAfterReveal: Bool): @AnyStructRaffleSource {
pre {
entryType.isSubtype(of: Type<AnyStruct>()): "entry type must be a subtype of AnyStruct"
}

return <- create AnyStructRaffleSource(entryType: entryType, removeAfterReveal: removeAfterReveal)
}
}
Loading

0 comments on commit 791cf14

Please sign in to comment.