Skip to content

Commit

Permalink
Linting tx
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 10, 2024
1 parent 6aad013 commit f8f07aa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 84 deletions.
65 changes: 13 additions & 52 deletions cadence/contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,95 +8,60 @@
access(all) contract ExampleNFT {

// Declare Path constants so paths do not have to be hardcoded
// in transactions and scripts
// Declare Path constants
access(all) let CollectionStoragePath: StoragePath
access(all) let CollectionPublicPath: PublicPath
access(all) let MinterStoragePath: StoragePath

// Tracks the unique IDs of the NFTs
access(all) var idCount: UInt64

// Declare the NFT resource type
access(all) resource NFT {
// The unique ID that differentiates each NFT
access(all) let id: UInt64

// Initialize both fields in the initializer
init(initID: UInt64) {
self.id = initID
}
}

access(all) entitlement Withdraw

// The definition of the Collection resource that
// holds the NFTs that a user owns
access(all) resource Collection {
// dictionary of NFT conforming tokens
// NFT is a resource type with an `UInt64` ID field
// Interface defining required methods for the Collection
access(all) resource interface CollectionInterface {
access(all) fun deposit(token: @NFT)
access(Withdraw) fun withdraw(withdrawID: UInt64): @NFT
access(all) view fun getIDs(): [UInt64]
}

// Collection resource conforming to CollectionInterface
access(all) resource Collection: CollectionInterface {
access(all) var ownedNFTs: @{UInt64: NFT}

// Initialize the NFTs field to an empty collection
init () {
self.ownedNFTs <- {}
}

// withdraw
//
// Function that removes an NFT from the collection
// and moves it to the calling context
access(Withdraw) fun withdraw(withdrawID: UInt64): @NFT {
// If the NFT isn't found, the transaction panics and reverts
let token <- self.ownedNFTs.remove(key: withdrawID)
?? panic("Could not withdraw an ExampleNFT.NFT with id="
.concat(withdrawID.toString())
.concat("Verify that the collection owns the NFT ")
.concat("with the specified ID first before withdrawing it."))

?? panic("Could not withdraw NFT with id=".concat(withdrawID.toString()))
return <-token
}

// deposit
//
// Function that takes a NFT as an argument and
// adds it to the collections dictionary
access(all) fun deposit(token: @NFT) {
// add the new token to the dictionary with a force assignment
// if there is already a value at that key, it will fail and revert
self.ownedNFTs[token.id] <-! token
}

// idExists checks to see if a NFT
// with the given ID exists in the collection
access(all) view fun idExists(id: UInt64): Bool {
return self.ownedNFTs[id] != nil
}

// getIDs returns an array of the IDs that are in the collection
access(all) view fun getIDs(): [UInt64] {
return self.ownedNFTs.keys
}
}

// creates a new empty Collection resource and returns it
access(all) fun createEmptyCollection(): @Collection {
return <- create Collection()
}

// mintNFT
//
// Function that mints a new NFT with a new ID
// and returns it to the caller
access(all) fun mintNFT(): @NFT {

// create a new NFT
var newNFT <- create NFT(initID: self.idCount)

// change the id so that each ID is unique
let newNFT <- create NFT(initID: self.idCount)
self.idCount = self.idCount + 1

return <-newNFT
}

Expand All @@ -105,14 +70,10 @@ access(all) contract ExampleNFT {
self.CollectionPublicPath = /public/nftTutorialCollection
self.MinterStoragePath = /storage/nftTutorialMinter

// initialize the ID count to one
self.idCount = 1

// store an empty NFT Collection in account storage
self.account.storage.save(<-self.createEmptyCollection(), to: self.CollectionStoragePath)

// publish a capability to the Collection in storage
let cap = self.account.capabilities.storage.issue<&Collection>(self.CollectionStoragePath)
self.account.capabilities.publish(cap, at: self.CollectionPublicPath)
}
}
}
72 changes: 40 additions & 32 deletions cadence/transactions/create_nft_listing.cdc
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
import "NFTStorefront"
import "NonFungibleToken"
import "ExampleNFT"
import "FungibleToken"
import "ExampleNFT"

transaction {

// Reference to the NFTStorefront.Storefront resource.
let storefront: &NFTStorefront.Storefront

// Capability for the ExampleNFT.Collection, allowing NFT interactions.
let exampleNFTProvider: Capability<&ExampleNFT.Collection>

// Capability for the FungibleToken.Vault, allowing token interactions.
let tokenReceiver: Capability<&FungibleToken.Vault>
transaction {
let storefront: auth(NFTStorefront.CreateListing) &NFTStorefront.Storefront
let exampleNFTProvider: Capability<&{ExampleNFT.CollectionInterface}>
let tokenReceiver: Capability<&{FungibleToken.Receiver}>

prepare(signer: auth(Storage, Capabilities) &Account) {
// Borrow the storefront reference.
self.storefront = signer.storage.borrow<&NFTStorefront.Storefront>(at: NFTStorefront.StorefrontStoragePath)
?? panic("Cannot borrow storefront")

// Check and link the ExampleNFT.Collection capability if it doesn't exist.
if signer.capabilities.storage.get<&ExampleNFT.Collection>(at: ExampleNFT.CollectionPrivatePath).check() == false {
let cap = signer.capabilities.storage.issue<&ExampleNFT.Collection>(ExampleNFT.CollectionPrivatePath, target: ExampleNFT.CollectionStoragePath)
signer.capabilities.publish(cap, at: ExampleNFT.CollectionPublicPath)
// Retrieve the storefront capability
let storefrontCap = signer.capabilities.get<&NFTStorefront.Storefront>(
NFTStorefront.StorefrontStoragePath
)

// Borrow the resource from the capability
self.storefront = storefrontCap.borrow()
?? panic("Cannot borrow storefront resource")

// Ensure the ExampleNFT Collection capability is published
if signer.capabilities.get<&{ExampleNFT.CollectionInterface}>(
ExampleNFT.CollectionPublicPath
) == nil {
let issuedCapability = signer.capabilities.storage.issue<&ExampleNFT.Collection>(
ExampleNFT.CollectionStoragePath
)
signer.capabilities.publish(issuedCapability, at: ExampleNFT.CollectionPublicPath)
}

// Retrieve the ExampleNFT.Collection capability.
self.exampleNFTProvider = signer.capabilities.storage.get<&ExampleNFT.Collection>(at: ExampleNFT.CollectionPrivatePath)!
assert(self.exampleNFTProvider.borrow() != nil, message: "Missing or mis-typed ExampleNFT.Collection provider")
// Retrieve and verify the ExampleNFT Collection capability
let nftProviderCap = signer.capabilities.get<&{ExampleNFT.CollectionInterface}>(
ExampleNFT.CollectionPublicPath
)
self.exampleNFTProvider = nftProviderCap

// Retrieve the FungibleToken.Vault receiver capability.
self.tokenReceiver = signer.capabilities.get<&FungibleToken.Vault>(/public/MainReceiver)!
assert(self.tokenReceiver.borrow() != nil, message: "Missing or mis-typed FlowToken receiver")
// Retrieve and verify the FungibleToken receiver capability
let tokenReceiverCap = signer.capabilities.get<&{FungibleToken.Receiver}>(
/public/MainReceiver
)
self.tokenReceiver = tokenReceiverCap

// Define a SaleCut with the token receiver and amount.
// Create a sale cut
let saleCut = NFTStorefront.SaleCut(
receiver: self.tokenReceiver,
amount: 10.0
)

// Create a new listing in the storefront.
self.storefront.createListing(
nftProviderCapability: self.exampleNFTProvider,
nftType: Type<@NonFungibleToken.NFT>(),
nftID: 0,
salePaymentVaultType: Type<@FungibleToken.Vault>(),
// Create the listing
let listingID = self.storefront.createListing(
nftProviderCapability: self.exampleNFTProvider,
nftType: Type<@ExampleNFT.NFT>(),
nftID: 1,
salePaymentVaultType: Type<@FungibleToken.Vault>(),
saleCuts: [saleCut]
)

log("Storefront listing created")
log("Storefront listing created with ID: ".concat(listingID.toString()))
}
}

0 comments on commit f8f07aa

Please sign in to comment.