diff --git a/contracts/Raffles.cdc b/contracts/Raffles.cdc index d47cce3..38fecf6 100644 --- a/contracts/Raffles.cdc +++ b/contracts/Raffles.cdc @@ -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) } @@ -85,6 +98,14 @@ 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 = [] } @@ -92,11 +113,21 @@ pub contract Raffles { 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 @@ -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 <- {} } diff --git a/scripts/get_raffle_addresses.cdc b/scripts/get_raffle_addresses.cdc new file mode 100644 index 0000000..6719f37 --- /dev/null +++ b/scripts/get_raffle_addresses.cdc @@ -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) +} \ No newline at end of file diff --git a/scripts/get_raffle_ids.cdc b/scripts/get_raffle_ids.cdc new file mode 100644 index 0000000..84468e6 --- /dev/null +++ b/scripts/get_raffle_ids.cdc @@ -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() +} \ No newline at end of file diff --git a/transactions/add_address_raffle.cdc b/transactions/add_address_raffle.cdc new file mode 100644 index 0000000..2b64fbe --- /dev/null +++ b/transactions/add_address_raffle.cdc @@ -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) + } +} \ No newline at end of file diff --git a/transactions/create_raffle.cdc b/transactions/create_raffle.cdc new file mode 100644 index 0000000..8c8a16a --- /dev/null +++ b/transactions/create_raffle.cdc @@ -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) + } +} \ No newline at end of file diff --git a/transactions/raffle_draw.cdc b/transactions/raffle_draw.cdc new file mode 100644 index 0000000..ccfecd5 --- /dev/null +++ b/transactions/raffle_draw.cdc @@ -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) + } +} \ No newline at end of file