Skip to content

Commit

Permalink
TestDrawRaffle (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: ash <[email protected]>
  • Loading branch information
web3ashlee and ash authored Dec 21, 2023
1 parent 9f473d5 commit b429568
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
11 changes: 11 additions & 0 deletions scripts/raffle/get_raffle_entries.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "FlowtyRaffles"
pub fun main(addr: Address, id: UInt64): [AnyStruct] {
let acct = getAuthAccount(addr)
let manager = acct.borrow<&FlowtyRaffles.Manager{FlowtyRaffles.ManagerPublic}>(from: FlowtyRaffles.ManagerStoragePath)
?? panic("raffles manager not found")
let raffle = manager.borrowRafflePublic(id: id)
?? panic("raffle not found")
let source: &AnyResource{FlowtyRaffles.RaffleSourcePublic} = raffle.borrowSourcePublic() ?? panic("source is invalid")

return source.getEntries()
}
41 changes: 38 additions & 3 deletions test/FlowtyWrapped_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "test_helpers.cdc"
import "FlowtyWrapped"
import "MetadataViews"
import "WrappedEditions"
import "FlowtyRaffles"

pub let rafflesAcct = Test.getAccount(Address(0x0000000000000007))
pub let minterAccount = Test.getAccount(Address(0x0000000000000007))
Expand All @@ -29,8 +30,8 @@ pub fun setup() {

// register a test wrapped edition
let removeAfterReveal: Bool = true
let start: UInt64 = 0
let end: UInt64 = 100
let start: UInt64? = nil
let end: UInt64? = nil
let baseImageUrl: String = "https://example.com/image/"
let baseHtmlUrl: String = "QmVZv2s6sozWWb4dEcANaszqKWLQbieYJLysK7NGq3RGdJ"
registerEdition(rafflesAcct: Test.getAccount(Address(0x0000000000000007)), removeAfterReveal: removeAfterReveal, start: start, end: end, baseImageUrl: baseImageUrl, baseHtmlUrl: baseHtmlUrl)
Expand Down Expand Up @@ -203,7 +204,34 @@ pub fun testIpfsUrlNoName() {
assert(ipfsUrl == "ipfs://QmVZv2s6sozWWb4dEcANaszqKWLQbieYJLysK7NGq3RGdJ?username=".concat(acct.address.toString()).concat("&raffleTickets=1"), message: "unexpected ipfs url")
}

pub fun registerEdition(rafflesAcct: Test.Account, removeAfterReveal: Bool, start: UInt64, end: UInt64, baseImageUrl: String, baseHtmlUrl: String) {
pub fun testDrawRaffle() {
let acct = Test.createAccount()
let username: String = "user1"

let editionName = "Flowty Wrapped 2023"
let createEvent = (Test.eventsOfType(Type<FlowtyRaffles.RaffleCreated>()).removeLast() as! FlowtyRaffles.RaffleCreated)

setupForMint(acct: acct, name: username)
let entries: AnyStruct = scriptExecutor("raffle/get_raffle_entries.cdc", [minterAccount.address, createEvent.raffleID])
let castedEntries = entries! as! [AnyStruct]

assert(castedEntries.length >= 1, message: "no entries")
assert(castedEntries.removeLast() as! Address == acct.address)

let drawing = drawFromRaffle(rafflesAcct, createEvent.raffleID)

var winnerIsFromEntryPool = false
for e in castedEntries {
let c = (e as! Address).toString()
if c == drawing{
winnerIsFromEntryPool = true
break
}
}
assert(winnerIsFromEntryPool)
}

pub fun registerEdition(rafflesAcct: Test.Account, removeAfterReveal: Bool, start: UInt64?, end: UInt64?, baseImageUrl: String, baseHtmlUrl: String) {
txExecutor("register_edition.cdc", [rafflesAcct], [removeAfterReveal, start, end, baseImageUrl, baseHtmlUrl], nil)
}

Expand Down Expand Up @@ -231,4 +259,11 @@ pub fun setupForMint(acct: Test.Account, name: String) {
let collections: [String] = [""]

txExecutor("mint_flowty_wrapped.cdc", [minterAccount], [acct.address, name, ticket, totalNftsOwned, floatCount, favoriteCollections, collections], nil)
}

pub fun drawFromRaffle(_ signer: Test.Account, _ id: UInt64): String {
txExecutor("raffle/draw_from_raffle.cdc", [signer], [id], nil)

let drawingEvent = Test.eventsOfType(Type<FlowtyRaffles.RaffleReceiptRevealed>()).removeLast() as! FlowtyRaffles.RaffleReceiptRevealed
return drawingEvent.value ?? ""
}
12 changes: 12 additions & 0 deletions transactions/raffle/draw_from_raffle.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "FlowtyRaffles"

transaction(id: UInt64) {
prepare(acct: AuthAccount) {
let manager = acct.borrow<&FlowtyRaffles.Manager>(from: FlowtyRaffles.ManagerStoragePath)
?? panic("raffles manager not found")
let receiptID = manager.commitDrawing(raffleID: id)

let ref = manager as &FlowtyRaffles.Manager{FlowtyRaffles.ManagerPublic}
manager.revealDrawing(manager: ref, raffleID: id, receiptID: receiptID)
}
}
4 changes: 2 additions & 2 deletions transactions/register_edition.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import "FlowtyRaffles"
import "FlowtyRaffleSource"

// flow transactions send ./transactions/register_edition.cdc false 1703035065 1705195052 "https://storage.googleapis.com/flowty-wrapped-2023-testnet/" QmcvXz8zZ8hZwgH95zVQ8ZEJUZ92oR9MVjCFVYePyCuvxB -n testnet --signer wrapped-testnet
transaction(removeAfterReveal: Bool, start: UInt64, end: UInt64, baseImageUrl: String, baseHtmlUrl: String) {
transaction(removeAfterReveal: Bool, start: UInt64?, end: UInt64?, baseImageUrl: String, baseHtmlUrl: String) {
prepare(acct: AuthAccount) {
let raffleManager = acct.borrow<&FlowtyRaffles.Manager>(from: FlowtyRaffles.ManagerStoragePath)!

// make a raffle source which is based on addresses
let source <- FlowtyRaffleSource.createRaffleSource(entryType: Type<Address>(), removeAfterReveal: removeAfterReveal)

// fill out raffle details. All we really care about is the start and end here
let details = FlowtyRaffles.Details(start: start, end: end, display: nil, externalURL: nil, commitBlocksAhead: 1)
let details = FlowtyRaffles.Details(start: start, end: end, display: nil, externalURL: nil, commitBlocksAhead: 0)

// make the raffle, get its id to pass on to the edition
let raffleID = raffleManager.createRaffle(source: <- source, details: details, revealers: [])
Expand Down

0 comments on commit b429568

Please sign in to comment.