Skip to content

Commit

Permalink
Playing with Raffles contract and adding some functions and transacti…
Browse files Browse the repository at this point in the history
…ons to test it
  • Loading branch information
Felipe Ribeiro authored and Felipe Ribeiro committed Dec 15, 2023
1 parent 0e2ed0a commit beb9012
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
67 changes: 67 additions & 0 deletions contracts/Raffles.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,23 @@ pub contract Raffles {
pub let source: @{RaffleSource}
pub let details: Details

pub fun getSourceType(): Type {
return self.source.getType()
}

pub fun getAddressSource(): &AddressRaffleSource {
let source = &self.source as auth &{RaffleSource}
return source as! &AddressRaffleSource
}

pub fun draw(): Int {
return self.source.draw()
}

pub fun getAddresses(): [Address] {
return self.getAddressSource().getAddresses()
}

pub fun getEntryAt(index: Int): AnyStruct {
return self.source.getEntryAt(index: index)
}
Expand Down Expand Up @@ -85,18 +98,36 @@ pub contract Raffles {
return self.addresses[index]
}

pub fun getAddresses(): [Address] {
return self.addresses
}

pub fun addAddress(address: Address) {
self.addresses.append(address)
}

init() {
self.addresses = []
}
}

pub resource interface ManagerPublic {
pub fun borrowRafflePublic(id: UInt64): &{RafflePublic}?
pub fun getIDs(): [UInt64]
pub fun getRaffleAddresses(id: UInt64): [Address]
}

pub resource Manager: ManagerPublic {
access(self) let raffles: @{UInt64: Raffle}

pub fun getIDs(): [UInt64] {
return self.raffles.keys
}

pub fun getRaffleAddresses(id: UInt64): [Address] {
return (&self.raffles[id] as &Raffle?)!.getAddresses()
}

pub fun borrowRafflePublic(id: UInt64): &{RafflePublic}? {
if self.raffles[id] == nil {
return nil
Expand All @@ -105,6 +136,42 @@ pub contract Raffles {
return &self.raffles[id] as &Raffle?
}

pub fun addAddressToRaffle(id: UInt64, address: Address) {
let raffle = &self.raffles[id] as &Raffle?
if raffle == nil {
panic("raffle with id ".concat(id.toString()).concat(" does not exist"))
}

let source = raffle!.getAddressSource()
source.addAddress(address: address)
}

pub fun createAddressRaffleSource(): @AddressRaffleSource {
return <- create Raffles.AddressRaffleSource()
}

pub fun createRaffle(source: @{RaffleSource}, details: Details) {
let id = UInt64(self.raffles.length)
let sourceType = source.getType()

let raffle <- create Raffle(source: <- source, details: details)
self.raffles[id] <-! raffle

emit RaffleCreated(address: self.owner!.address, raffleID: id, sourceType: sourceType)
}

pub fun draw(id: UInt64) {
let raffle: &Raffles.Raffle? = &self.raffles[id] as &Raffle?
if raffle == nil {
panic("raffle with id ".concat(id.toString()).concat(" does not exist"))
}

let index = raffle!.draw()
let value = raffle!.getEntryAt(index: index)

Raffles.emitDrawing(address: self.owner!.address, raffleID: id, sourceType: raffle!.getSourceType() ,index: index, value: value)
}

init() {
self.raffles <- {}
}
Expand Down
7 changes: 7 additions & 0 deletions scripts/get_raffle_addresses.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "Raffles"

pub fun main(addr: Address, raffleId: UInt64): [Address] {
let manager = getAccount(addr).getCapability<&Raffles.Manager{Raffles.ManagerPublic}>(Raffles.ManagerPublicPath).borrow()
?? panic("unable to borrow manager")
return manager.getRaffleAddresses(id: raffleId)
}
7 changes: 7 additions & 0 deletions scripts/get_raffle_ids.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "Raffles"

pub fun main(addr: Address): [UInt64] {
let manager = getAccount(addr).getCapability<&Raffles.Manager{Raffles.ManagerPublic}>(Raffles.ManagerPublicPath).borrow()
?? panic("unable to borrow manager")
return manager.getIDs()
}
10 changes: 10 additions & 0 deletions transactions/add_address_raffle.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "Raffles"

transaction(raffleId: UInt64, addressToAdd: Address) {
prepare(acct: AuthAccount) {
let manager = acct.borrow<&Raffles.Manager>(from: Raffles.ManagerStoragePath)
?? panic("Could not borrow a reference to the Manager")

manager.addAddressToRaffle(id: raffleId, address: addressToAdd)
}
}
25 changes: 25 additions & 0 deletions transactions/create_raffle.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "Raffles"
import "MetadataViews"

transaction(raffleStart: UInt64?, raffleEnd: UInt64?, raffleTitle: String, raffleDescription: String, raffleImageUrl: String) {
prepare(acct: AuthAccount) {
let manager = acct.borrow<&Raffles.Manager>(from: Raffles.ManagerStoragePath)
?? panic("Could not borrow a reference to the Manager")

let addressRaffleSource <- manager.createAddressRaffleSource()

let details = Raffles.Details(
start: raffleStart,
end: raffleEnd!,
display: MetadataViews.Display(
title: raffleTitle,
description: raffleDescription,
media: MetadataViews.HTTPFile(
url: raffleImageUrl
)
)
)

manager.createRaffle(source: <- addressRaffleSource, details: details)
}
}
10 changes: 10 additions & 0 deletions transactions/raffle_draw.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "Raffles"

transaction(raffleId: UInt64) {
prepare(acct: AuthAccount) {
let manager = acct.borrow<&Raffles.Manager>(from: Raffles.ManagerStoragePath)
?? panic("Could not borrow a reference to the Manager")

manager.draw(id: raffleId)
}
}

0 comments on commit beb9012

Please sign in to comment.