-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
} | ||
|
||
Comment on lines
+48
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( |
||
|
||
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) | ||
} | ||
Comment on lines
+163
to
+173
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 <- {} | ||
} | ||
|
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) | ||
} |
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() | ||
} |
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) | ||
} | ||
} |
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) | ||
} | ||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
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