Skip to content

Commit

Permalink
raffle source for FLOAT events (#13)
Browse files Browse the repository at this point in the history
* raffle source for FLOAT events

* add some missing methods to the float raffle source

* add comments
  • Loading branch information
austinkline authored Dec 18, 2023
1 parent 791cf14 commit 2758355
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
53 changes: 53 additions & 0 deletions contracts/FLOATRaffleSource.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import "FlowtyRaffles"
import "FLOAT"

/*
FLOATRaffleSource - A Raffle source implementation which uses the claims on a FLOAT as the source of a drawing.
*/
pub contract FLOATRaffleSource {
pub resource RaffleSource {
pub let eventCap: Capability<&FLOAT.FLOATEvents{FLOAT.FLOATEventsPublic}>
pub let eventId: UInt64

pub fun getEntries(): [AnyStruct] {
return self.borrowEvent().getClaims().values
}

pub fun getEntryCount(): Int {
return self.borrowEvent().getClaims().length
}

pub fun getEntryAt(index: Int): AnyStruct {
return self.borrowEvent().getClaims()[UInt64(index)]
}

pub fun revealCallback(drawingResult: FlowtyRaffles.DrawingResult) {
return
}

pub fun addEntry(_ v: AnyStruct) {
panic("addEntry is not supported on FLOATRaffleSource")
}

pub fun addEntries(_ v: [AnyStruct]) {
panic("addEntries is not supported on FLOATRaffleSource")
}

pub fun borrowEvent(): &FLOAT.FLOATEvent{FLOAT.FLOATEventPublic} {
let cap = self.eventCap.borrow() ?? panic("eventCap is not valid")
return cap.borrowPublicEventRef(eventId: self.eventId) ?? panic("invalid event id")
}

init(eventCap: Capability<&FLOAT.FLOATEvents{FLOAT.FLOATEventsPublic}>, eventId: UInt64) {
self.eventCap = eventCap
self.eventId = eventId

// ensure we can borrow the event. This will panic if the we aren't able to
self.borrowEvent()
}
}

pub fun createRaffleSource(eventCap: Capability<&FLOAT.FLOATEvents{FLOAT.FLOATEventsPublic}>, eventId: UInt64): @RaffleSource {
return <- create RaffleSource(eventCap: eventCap, eventId: eventId)
}
}
29 changes: 21 additions & 8 deletions contracts/FlowtyRaffleSource.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import "FlowtyRaffles"

/*
FlowtyRaffleSource - Contains a basic implementation of RaffleSource which can be used for all `AnyStruct`
types. For example, if a consumer of this resource wanted to make a raffle that uses an array of Addresses as the pool
to draw from, they could use the AnyStructRaffleSource with an entry type of Type<Address>() and would be guaranteed to only
be able to put addresses in their array of entries.
This is enforced so that consumers of that source can have safety when reading entries from the array in case they want to handle any additional
logic alongside the raffle itself, such as distributing a prize when a raffle is drawn
In addition to entryType, a field called `removeAfterReveal` is also provided, which, if enabled, will remove an entry
from the entries array any time a reveal is performed. This is useful for cases where you don't want the same entry to be able to be drawn
multiple times.
*/
pub contract FlowtyRaffleSource {
pub resource AnyStructRaffleSource: FlowtyRaffles.RaffleSourcePublic, FlowtyRaffles.RaffleSourcePrivate {
pub let entries: [AnyStruct]
Expand All @@ -14,6 +27,14 @@ pub contract FlowtyRaffleSource {
return self.entries[index]
}

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

pub fun getEntryCount(): Int {
return self.entries.length
}

pub fun addEntry(_ v: AnyStruct) {
pre {
v.getType() == self.entryType: "incorrect entry type"
Expand All @@ -38,14 +59,6 @@ pub contract FlowtyRaffleSource {
self.entries.remove(at: drawingResult.index)
}

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

pub fun getEntryCount(): Int {
return self.entries.length
}

init(entryType: Type, removeAfterReveal: Bool) {
self.entries = []
self.entryType = entryType
Expand Down
2 changes: 1 addition & 1 deletion contracts/FlowtyRaffles.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub contract FlowtyRaffles {
*/
access(contract) fun reveal(_ source: &{RaffleSourcePublic, RaffleSourcePrivate}): DrawingResult {
pre {
self.commitBlock == nil || self.commitBlock! <= getCurrentBlock().height: "receipt cannot be revealed yet"
self.commitBlock! <= getCurrentBlock().height: "receipt cannot be revealed yet"
self.result == nil: "receipt has already been revealed"
}

Expand Down

0 comments on commit 2758355

Please sign in to comment.