Skip to content

Commit

Permalink
add a test to borrow active phases (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkline authored Apr 10, 2024
1 parent 72a41ad commit 6d42346
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
19 changes: 16 additions & 3 deletions contracts/FlowtyDrops.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub contract FlowtyDrops {

pub event DropAdded(address: Address, id: UInt64, name: String, description: String, imageUrl: String, start: UInt64?, end: UInt64?)
pub event Minted(address: Address, dropID: UInt64, phaseID: UInt64, nftID: UInt64, nftType: String)
pub event PhaseAdded(dropID: UInt64, dropAddress: Address, id: UInt64, index: Int, switcherType: String, pricerType: String, addressVerifierType: String)
pub event PhaseRemoved(dropID: UInt64, dropAddress: Address, id: UInt64)

// Interface to expose all the components necessary to participate in a drop
// and to ask questions about a drop.
Expand Down Expand Up @@ -51,7 +53,7 @@ pub contract FlowtyDrops {
self.phases.length > phaseIndex: "phase index is too high"
receiverCap.check(): "receiver capability is not valid"
}

// validate the payment vault amount and type
let phase = &self.phases[phaseIndex] as! &Phase
assert(
Expand Down Expand Up @@ -134,7 +136,15 @@ pub contract FlowtyDrops {
}

pub fun addPhase(_ phase: @Phase) {
// TODO: phase added event
emit PhaseAdded(
dropID: self.uuid,
dropAddress: self.owner!.address,
id: phase.uuid,
index: self.phases.length,
switcherType: phase.details.switch.getType().identifier,
pricerType: phase.details.pricer.getType().identifier,
addressVerifierType: phase.details.addressVerifier.getType().identifier
)
self.phases.append(<-phase)
}

Expand All @@ -143,7 +153,10 @@ pub contract FlowtyDrops {
self.phases.length > index: "index is greater than length of phases"
}

return <- self.phases.remove(at: index)
let phase <- self.phases.remove(at: index)
emit PhaseRemoved(dropID: self.uuid, dropAddress: self.owner!.address, id: phase.uuid)

return <- phase
}

pub fun getDetails(): DropDetails {
Expand Down
23 changes: 23 additions & 0 deletions scripts/get_active_phases.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import "FlowtyDrops"
import "ViewResolver"

pub fun main(contractAddress: Address, contractName: String, dropID: UInt64): [UInt64] {
let resolver = getAccount(contractAddress).contracts.borrow<&ViewResolver>(name: contractName)
?? panic("contract does not implement ViewResolver interface")

let dropResolver = resolver.resolveView(Type<FlowtyDrops.DropResolver>())! as! FlowtyDrops.DropResolver

let container = dropResolver.borrowContainer()
?? panic("drop container not found")

let drop = container.borrowDropPublic(id: dropID)
?? panic("drop not found")

let phases = drop.borrowActivePhases()
let ids: [UInt64] = []
for p in phases {
ids.append(p.uuid)
}

return ids
}
20 changes: 20 additions & 0 deletions tests/FlowtyDrops_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ pub fun test_OpenEditionNFT_EditMaxPerMint() {
Test.assertEqual(true, canMintAtPhase(contractAddress: openEditionAccount.address, contractName: "OpenEditionNFT", dropID: dropID, phaseIndex: 0, minter: openEditionAccount.address, numToMint: 5, totalMinted: 0, paymentIdentifier: exampleTokenIdentifier()))
}

pub fun test_OpenEditionNFT_GetActivePhases() {
let dropID = createDefaultTimeBasedOpenEditionDrop()
let activePhaseIDs = scriptExecutor("get_active_phases.cdc", [openEditionAccount.address, "OpenEditionNFT", dropID])! as! [UInt64]
Test.assert(activePhaseIDs.length == 1, message: "unexpected active phase length")
}

pub fun test_OpenEditionNFT_addPhase() {
let dropID = createDefaultTimeBasedOpenEditionDrop()

txExecutor("drops/add_free_phase.cdc", [openEditionAccount], [dropID, nil, nil], nil, nil)
let phaseEvent = Test.eventsOfType(Type<FlowtyDrops.PhaseAdded>()).removeLast() as! FlowtyDrops.PhaseAdded

var activePhaseIDs = scriptExecutor("get_active_phases.cdc", [openEditionAccount.address, "OpenEditionNFT", dropID])! as! [UInt64]
Test.assert(activePhaseIDs.contains(phaseEvent.id), message: "unexpected active phase length")

txExecutor("drops/remove_last_phase.cdc", [openEditionAccount], [dropID, nil, nil], nil, nil)
activePhaseIDs = scriptExecutor("get_active_phases.cdc", [openEditionAccount.address, "OpenEditionNFT", dropID])! as! [UInt64]
Test.assert(!activePhaseIDs.contains(phaseEvent.id), message: "unexpected active phase length")
}

// ------------------------------------------------------------------------
// Helper functions section
Expand Down
16 changes: 16 additions & 0 deletions transactions/drops/add_free_phase.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "FlowtyDrops"
import "FlowtyPricers"

transaction(dropID: UInt64, start: UInt64?, end: UInt64?) {
prepare(acct: AuthAccount) {
let container = acct.borrow<&FlowtyDrops.Container>(from: FlowtyDrops.ContainerStoragePath)
?? panic("container not found")
let drop = container.borrowDrop(id: dropID) ?? panic("drop not found")
let firstPhase = drop.borrowPhase(index: 0)

let details = FlowtyDrops.PhaseDetails(switch: firstPhase.details.switch, display: firstPhase.details.display, pricer: FlowtyPricers.Free(), addressVerifier: firstPhase.details.addressVerifier)
let phase <- FlowtyDrops.createPhase(details: details)

drop.addPhase(<- phase)
}
}
16 changes: 16 additions & 0 deletions transactions/drops/remove_last_phase.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import "FlowtyDrops"
import "FlowtyPricers"

transaction(dropID: UInt64, start: UInt64?, end: UInt64?) {
prepare(acct: AuthAccount) {
let container = acct.borrow<&FlowtyDrops.Container>(from: FlowtyDrops.ContainerStoragePath)
?? panic("container not found")
let drop = container.borrowDrop(id: dropID) ?? panic("drop not found")
let firstPhase = drop.borrowPhase(index: 0)

let details = FlowtyDrops.PhaseDetails(switch: firstPhase.details.switch, display: firstPhase.details.display, pricer: FlowtyPricers.Free(), addressVerifier: firstPhase.details.addressVerifier)

let phases = drop.borrowAllPhases()
destroy drop.removePhase(index: phases.length - 1)
}
}

0 comments on commit 6d42346

Please sign in to comment.