Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Felipe tests 2 #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +39 to +42
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be added here, unfortunately. The Raffle itself should have no knowledge of the "source" it is using

What we likely have to do is permit returning an auth reference source to the owner of the raffle so that they can cast it to the original type at will


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

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

Comment on lines +48 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this, unfortunately can't be included

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()
}
Comment on lines +127 to +129
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally with method like this, they should exist in the underlying resource you are calling, and a borrow method should be used to get to it (borrowRafflePublic in this case)


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)
}
Comment on lines +163 to +173
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here on borrowing, the Raffle resource itself can encapsulate this


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)
}
}