Skip to content

Commit

Permalink
Add tests to flowty wrapped and fix some transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Ribeiro authored and Felipe Ribeiro committed Mar 12, 2024
1 parent bf81963 commit 03efcc1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 51 deletions.
2 changes: 1 addition & 1 deletion contracts/FlowtyWrapped.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ access(all) contract FlowtyWrapped: NonFungibleToken, ViewResolver {
pre {
self.editions[name] != nil: "no edition found with given name"
}
return (&self.editions[name] as &{WrappedEdition}?)!
return &self.editions[name]!
}

access(all) fun getEdition(_ name: String): {WrappedEdition} {
Expand Down
12 changes: 6 additions & 6 deletions contracts/WrappedEditions.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ access(all) contract WrappedEditions {

access(all) let totalNftsOwned: Int
access(all) let floatCount: Int
access(all) let favoriteCollections: [String] // type identifier of each collection
access(all) let collections: [String] // type identifier of each collection
access(all) let favoriteCollections: [String]
access(all) let collections: [String]

access(all) fun toTraits(): MetadataViews.Traits {
let traits: [MetadataViews.Trait] = [
Expand Down Expand Up @@ -98,17 +98,17 @@ access(all) contract WrappedEditions {
let nft <- FlowtyWrapped.mint(id: FlowtyWrapped.totalSupply, serial: self.supply, editionName: self.name, address: address, data: data)

// allocate raffle tickets
// let manager = FlowtyWrapped.getRaffleManager()
// let raffle = manager.borrowRaffle(id: self.raffleID)
// ?? panic("raffle not found in manager")
let manager = FlowtyWrapped.getRaffleManager()
let raffle = manager.borrowRaffle(id: self.raffleID)
?? panic("raffle not found in manager")

let entries: [Address] = []
var count = 0
while count < casted.tickets {
entries.append(address)
count = count + 1
}
// raffle.addEntries(entries)
raffle.addEntries(entries)

self.mintedAddresses[address] = true
return <- nft
Expand Down
2 changes: 1 addition & 1 deletion contracts/raffle/FlowtyRaffles.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ access(all) contract FlowtyRaffles {
// return prg.nextUInt64()
// TODO: use commented-out implementation once we can test using the randomness beacon in the cadence testing framework
return 1
return revertibleRandom<UInt64>()
}

access(all) fun extractString(_ value: AnyStruct?): String? {
Expand Down
52 changes: 26 additions & 26 deletions test/FlowtyWrapped_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import "MetadataViews"
import "WrappedEditions"
import "FlowtyRaffles"

pub let rafflesAcct = Test.getAccount(Address(0x0000000000000007))
pub let minterAccount = Test.getAccount(Address(0x0000000000000007))
access(all) let rafflesAcct = Test.getAccount(Address(0x0000000000000007))
access(all) let minterAccount = Test.getAccount(Address(0x0000000000000007))

pub fun setup() {
access(all) fun setup() {
var err = Test.deployContract(name: "FlowtyRaffles", path: "../contracts/raffle/FlowtyRaffles.cdc", arguments: [])
Test.expect(err, Test.beNil())

Expand Down Expand Up @@ -38,16 +38,16 @@ pub fun setup() {

}

pub fun testSetupManager() {
access(all) fun testSetupManager() {
let acct = Test.createAccount()
txExecutor("setup_flowty_wrapped.cdc", [acct], [], nil)
}

pub fun testGetRaffleManager() {
access(all) fun testGetRaffleManager() {
scriptExecutor("raffle/borrow_raffle_manager.cdc", [rafflesAcct.address])
}

pub fun testSetCollectionExternalUrl() {
access(all) fun testSetCollectionExternalUrl() {
let baseHtmlUrl: String = "https://flowty.io/asset/0x0000000000000007/FlowtyWrappedTEST"

txExecutor("set_collection_external_url.cdc", [rafflesAcct], [baseHtmlUrl], nil)
Expand All @@ -57,13 +57,13 @@ pub fun testSetCollectionExternalUrl() {
assert(castedResult == baseHtmlUrl, message: "baseHtmlUrl does not match expected")
}

pub fun testMint() {
access(all) fun testMint() {
let acct = Test.createAccount()
let username: String = "user1"
setupForMint(acct: acct, name: username)
}

pub fun testGetEditions() {
access(all) fun testGetEditions() {
let acct = Test.createAccount()
let username: String = "user1"
setupForMint(acct: acct, name: username)
Expand All @@ -75,7 +75,7 @@ pub fun testGetEditions() {
scriptExecutor("get_editions_flowty_wrapped.cdc", [acct.address, nftID1])
}

pub fun testEditionResolveView() {
access(all) fun testEditionResolveView() {
let acct = Test.createAccount()

let currentEditionNumber = getEditionNumber()
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fun testEditionResolveView() {
assert(max == expectedEditionMax, message: "max should be nil")
}

pub fun testDepositToWrongAddressFails() {
access(all) fun testDepositToWrongAddressFails() {
let acct = Test.createAccount()
let wrongAccount = Test.createAccount()

Expand All @@ -127,7 +127,7 @@ pub fun testDepositToWrongAddressFails() {



pub fun testBorrowNFT() {
access(all) fun testBorrowNFT() {
let acct = Test.createAccount()
let username: String = "user1"
setupForMint(acct: acct, name: username)
Expand All @@ -140,7 +140,7 @@ pub fun testBorrowNFT() {
scriptExecutor("borrow_nft.cdc", [acct.address, nftID1])
}

pub fun testSingleMint() {
access(all) fun testSingleMint() {
let acct = Test.createAccount()

txExecutor("setup_flowty_wrapped.cdc", [acct], [], nil)
Expand All @@ -158,7 +158,7 @@ pub fun testSingleMint() {
txExecutor("mint_flowty_wrapped.cdc", [minterAccount], [acct.address, username, ticket, totalNftsOwned, floatCount, favoriteCollections, collections], "address has already been minted")
}

pub fun testWithdrawFails() {
access(all) fun testWithdrawFails() {
let acct = Test.createAccount()
let acct2 = Test.createAccount()
let username: String = "user1"
Expand All @@ -172,7 +172,7 @@ pub fun testWithdrawFails() {
txExecutor("withdraw_nft.cdc", [acct], [acct.address, acct2.address, nftID1], "Flowty Wrapped is not transferrable")
}

pub fun testMediasIpfsUrl() {
access(all) fun testMediasIpfsUrl() {
let acct = Test.createAccount()
let username: String = "user1"
setupForMint(acct: acct, name: username)
Expand All @@ -185,10 +185,10 @@ pub fun testMediasIpfsUrl() {

let ipfsMedia = medias.items[0]
let ipfsUrl = ipfsMedia.file.uri()
assert(ipfsUrl == "ipfs://QmRfVR98oe6qxeWFcnY9tfM2CLUJg3rvxbBPS5LjYwp69Z?username=user1&raffleTickets=1", message: "unexpected ipfs url")
assert(ipfsUrl == "ipfs://QmfPkn13gbBNVK6bKtdqyUEa92bmqDy8aVQqGj3pByyyoP?username=user1&raffleTickets=1", message: "unexpected ipfs url")
}

pub fun testIpfsUrlNoName() {
access(all) fun testIpfsUrlNoName() {
let acct = Test.createAccount()
let username: String = ""
setupForMint(acct: acct, name: username)
Expand All @@ -201,22 +201,22 @@ pub fun testIpfsUrlNoName() {

let ipfsMedia = medias.items[0]
let ipfsUrl = ipfsMedia.file.uri()
assert(ipfsUrl == "ipfs://QmRfVR98oe6qxeWFcnY9tfM2CLUJg3rvxbBPS5LjYwp69Z?username=".concat(acct.address.toString()).concat("&raffleTickets=1"), message: "unexpected ipfs url")
assert(ipfsUrl == "ipfs://QmfPkn13gbBNVK6bKtdqyUEa92bmqDy8aVQqGj3pByyyoP?username=".concat(acct.address.toString()).concat("&raffleTickets=1"), message: "unexpected ipfs url")
}

pub fun testDrawRaffle() {
access(all) fun testDrawRaffle() {
let acct = Test.createAccount()
let username: String = "user1"

let editionName = "Flowty Wrapped 2023"
let createEvent = (Test.eventsOfType(Type<FlowtyRaffles.RaffleCreated>()).removeLast() as! FlowtyRaffles.RaffleCreated)

setupForMint(acct: acct, name: username)
let entries: AnyStruct = scriptExecutor("raffle/get_raffle_entries.cdc", [minterAccount.address, createEvent.raffleID])!
let castedEntries = entries as! [AnyStruct]
let entries: AnyStruct = scriptExecutor("raffle/get_raffle_entries.cdc", [minterAccount.address, createEvent.raffleID])
let castedEntries = entries! as! [AnyStruct]

assert(castedEntries.length >= 1, message: "no entries")
assert(castedEntries[castedEntries.length - 1] as! Address == acct.address)
assert(castedEntries.removeLast() as! Address == acct.address)

let drawing = drawFromRaffle(rafflesAcct, createEvent.raffleID)

Expand All @@ -231,15 +231,15 @@ pub fun testDrawRaffle() {
assert(winnerIsFromEntryPool)
}

pub fun registerEdition(rafflesAcct: Test.Account, removeAfterReveal: Bool, start: UInt64?, end: UInt64?, baseImageUrl: String, baseHtmlUrl: String) {
access(all) fun registerEdition(rafflesAcct: Test.TestAccount, removeAfterReveal: Bool, start: UInt64?, end: UInt64?, baseImageUrl: String, baseHtmlUrl: String) {
txExecutor("register_edition.cdc", [rafflesAcct], [removeAfterReveal, start, end, baseImageUrl, baseHtmlUrl], nil)
}

pub fun getMedias(addr: Address, nftID: UInt64): MetadataViews.Medias {
access(all) fun getMedias(addr: Address, nftID: UInt64): MetadataViews.Medias {
return scriptExecutor("get_medias.cdc", [addr, nftID])! as! MetadataViews.Medias
}

pub fun getEditionNumber(): UInt64{
access(all) fun getEditionNumber(): UInt64{
let editionName = "Flowty Wrapped 2023"
let res = scriptExecutor("get_total_edition_supply.cdc", [minterAccount.address, editionName])

Expand All @@ -248,7 +248,7 @@ pub fun getEditionNumber(): UInt64{

}

pub fun setupForMint(acct: Test.Account, name: String) {
access(all) fun setupForMint(acct: Test.TestAccount, name: String) {

txExecutor("setup_flowty_wrapped.cdc", [acct], [], nil)

Expand All @@ -261,7 +261,7 @@ pub fun setupForMint(acct: Test.Account, name: String) {
txExecutor("mint_flowty_wrapped.cdc", [minterAccount], [acct.address, name, ticket, totalNftsOwned, floatCount, favoriteCollections, collections], nil)
}

pub fun drawFromRaffle(_ signer: Test.Account, _ id: UInt64): String {
access(all) fun drawFromRaffle(_ signer: Test.TestAccount, _ id: UInt64): String {
txExecutor("raffle/draw_from_raffle.cdc", [signer], [id], nil)

let drawingEvent = Test.eventsOfType(Type<FlowtyRaffles.RaffleReceiptRevealed>()).removeLast() as! FlowtyRaffles.RaffleReceiptRevealed
Expand Down
4 changes: 2 additions & 2 deletions test/test_helpers.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import "NonFungibleToken"
import "ExampleNFT"
import "ExampleNFT2"
import "ExampleToken"
// import "ExampleTokenUnsupported"
import "ExampleTokenUnsupported"
import "FlowToken"
// import "NFTStorefrontV2"
import "NFTStorefrontV2"

// Helper functions. All of the following were taken from
// https://github.com/onflow/Offers/blob/fd380659f0836e5ce401aa99a2975166b2da5cb0/lib/cadence/test/Offers.cdc
Expand Down
8 changes: 4 additions & 4 deletions transactions/fail_mint_to_wrong_account.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ transaction(address: Address, wrongAccount:Address, username: String, ticket: In
let wrapped2023Data = WrappedEditions.Wrapped2023Data(
username,
ticket,
totalNftsOwned,
floatCount,
favoriteCollections,
collections
totalNftsOwned: totalNftsOwned,
floatCount: floatCount,
favoriteCollections: favoriteCollections,
collections: collections
)
let data: {String: AnyStruct} = {
"wrapped": wrapped2023Data
Expand Down
8 changes: 4 additions & 4 deletions transactions/mint_flowty_wrapped.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ transaction(address: Address, username: String, ticket: Int, totalNftsOwned: Int
let wrapped2023Data = WrappedEditions.Wrapped2023Data(
username,
ticket,
totalNftsOwned,
floatCount,
favoriteCollections,
collections
totalNftsOwned: totalNftsOwned,
floatCount: floatCount,
favoriteCollections: favoriteCollections,
collections: collections
)
let data: {String: AnyStruct} = {
"wrapped": wrapped2023Data
Expand Down
8 changes: 2 additions & 6 deletions transactions/publish_admin.cdc
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import "FlowtyWrapped"

transaction(receiver: Address) {
prepare(acct: AuthAccount) {
let identifier = "FlowtyWrapped_Admin_".concat(receiver.toString())
let p = PrivatePath(identifier: identifier)!
prepare(acct: auth(Storage, Capabilities, Inbox) &Account) {

acct.unlink(p)
let cap = acct.link<&FlowtyWrapped.Admin>(p, target: FlowtyWrapped.AdminStoragePath)
?? panic("failed to link admin capability")
let cap = acct.capabilities.storage.issue<&FlowtyWrapped.Admin>(FlowtyWrapped.AdminStoragePath)

acct.inbox.publish(cap, name: "flowty-wrapped-minter", recipient: receiver)
}
Expand Down
2 changes: 1 addition & 1 deletion transactions/withdraw_nft.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ transaction(owner: Address, receiver: Address, withdrawID: UInt64) {

recipient.deposit(token: <-nft)
}
}
}

0 comments on commit 03efcc1

Please sign in to comment.