Skip to content

Commit

Permalink
Debug tx
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 11, 2024
1 parent 962a59a commit 41711e3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
18 changes: 8 additions & 10 deletions cadence/contracts/Recipe.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ access(all) contract ExampleNFT: NonFungibleToken {
let traitsView = MetadataViews.dictToTraits(dict: self.metadata, excludedNames: excludedTraits)

// mintedTime is a unix timestamp, we should mark it with a displayType so platforms know how to show it.
let mintedTimeTrait = MetadataViews.Trait(name: "mintedTime", value: self.metadata["mintedTime"]!, displayType: "Date", rarity: nil)
traitsView.addTrait(mintedTimeTrait)
if let mintedTime = self.metadata["mintedTime"] as? String {
let mintedTimeTrait = MetadataViews.Trait(name: "mintedTime", value: mintedTime, displayType: "Date", rarity: nil)
traitsView.addTrait(mintedTimeTrait)
}

// foo is a trait with its own rarity
let fooTraitRarity = MetadataViews.Rarity(score: 10.0, max: 100.0, description: "Common")
Expand Down Expand Up @@ -325,16 +327,12 @@ access(all) contract ExampleNFT: NonFungibleToken {
name: String,
description: String,
thumbnail: String,
royalties: [MetadataViews.Royalty]
royalties: [MetadataViews.Royalty],
metadata: {String: String},
): @ExampleNFT.NFT {

let metadata: {String: AnyStruct} = {}
//let metadata: {String: AnyStruct} = {}
let currentBlock = getCurrentBlock()
metadata["mintedBlock"] = currentBlock.height
metadata["mintedTime"] = currentBlock.timestamp

// this piece of metadata will be used to show embedding rarity into a trait
metadata["foo"] = "bar"

// create a new NFT
var newNFT <- create NFT(
Expand Down Expand Up @@ -365,7 +363,7 @@ access(all) contract ExampleNFT: NonFungibleToken {
self.account.capabilities.publish(collectionCap, at: self.CollectionPublicPath)

// Create a Minter resource and save it to storage
let minter <- create NFTMinter()
let minter: @ExampleNFT.NFTMinter <- create NFTMinter()
self.account.storage.save(<-minter, to: self.MinterStoragePath)
}
}
80 changes: 51 additions & 29 deletions cadence/transactions/combine_views.cdc
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
import "MetadataViews"
import "ExampleNFT"

access(all)
fun main(): AnyStruct {
let address: Address = 0x02
let id: UInt64 = 0

let account = getAccount(address)

// Borrow the collection's ResolverCollection capability
let collection = account.capabilities.borrow<&{MetadataViews.ResolverCollection}>(
/public/exampleNFTCollection
) ?? panic("Could not borrow a reference to the collection at /public/exampleNFTCollection")

// Borrow the NFT's Resolver reference
let nft = collection.borrowViewResolver(id: id)
?? panic("Could not resolve NFT with ID \(id) in the collection")

// Get the Traits view for the NFT
let traitsView = nft.resolveView(Type<MetadataViews.Traits>())
?? panic("Traits view not found for NFT with ID \(id)")

// Get the Display view for the NFT
let displayView = nft.resolveView(Type<MetadataViews.Display>())
?? panic("Display view not found for NFT with ID \(id)")

// Combine the views into a dictionary
let object = {"Traits": traitsView, "Display": displayView}

return object
}
transaction {

prepare(signer: auth(Storage, Capabilities) &Account) {
// Use the caller's address
let address: Address = signer.address

// Borrow the NFTMinter from the caller's storage
let minter = signer.storage.borrow<&ExampleNFT.NFTMinter>(
from: /storage/exampleNFTMinter
) ?? panic("Could not borrow the NFT minter reference.")

// Mint a new NFT
let nft <- minter.mintNFT(
name: "Example NFT",
description: "Minting a sample NFT",
thumbnail: "https://example.com/thumbnail.png",
royalties: [],
metadata: {
"Power": "100",
"Will": "Strong",
"Determination": "Unyielding"
},

)

let id = nft.id

// Borrow the collection capability to deposit the minted NFT
let collection = signer.capabilities.borrow<&ExampleNFT.Collection>(
/public/exampleNFTCollection
) ?? panic("Could not borrow the collection reference at /public/exampleNFTCollection.")

// Deposit the minted NFT into the collection
collection.deposit(token: <-nft)

// Borrow the ViewResolver for the given NFT ID
let resolver = collection.borrowViewResolver(id: id)
?? panic("Could not borrow the ViewResolver for the NFT ID.")

// Get the Traits view for the NFT
let traitsView = resolver.resolveView(Type<MetadataViews.Traits>())
?? panic("Traits view not found for NFT ID.")

// Get the Display view for the NFT
let displayView = resolver.resolveView(Type<MetadataViews.Display>())
?? panic("Display view not found for NFT ID.")

let object = {"Traits": traitsView, "Display": displayView}
log(object)
}
}

0 comments on commit 41711e3

Please sign in to comment.