Skip to content

Commit

Permalink
Update to Cadence v1.0 (#6)
Browse files Browse the repository at this point in the history
* udpate to use dependency manager
* add previewnet config as a workaround for onflow/flowkit#63
* update dependencies to point previewnet
* run flow deps install to update flow.json
* update cadence source code
* update flow.json
* update readme
* update cadence scripts and readme
  • Loading branch information
bartolomej authored Sep 9, 2024
1 parent 624ffd7 commit ef9437c
Show file tree
Hide file tree
Showing 10 changed files with 425 additions and 388 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Cache directory used by Cadence dependency manager
imports
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ and then start the development command by running:
```
After the command is started it will automatically watch any changes you make to Cadence files and make sure to continiously sync those changes on the emulator network. If you make any mistakes it will report the errors as well. Read more [about the command here](https://developers.flow.com/tools/flow-cli/super-commands)

**Using external dependencies**

If you wanna use external contract dependencies (like NonFungibleToken, FlowToken, FungibleToken,..) you can install them using Cadence dependency manager: https://developers.flow.com/tools/flow-cli/dependency-manager

Use [ContractBrowser](https://contractbrowser.com/) to explore available 3rd party contracts in the Flow ecosystem.

## 🏎️ Interacting with ExampleNFT

### Initializing an Account
Expand All @@ -62,10 +68,16 @@ flow transactions send \

With `user1` account initialized to receive `ExampleNFT`, we can now mint into the `user1` account. The minting transaction should be signed by the account that's storing the `ExampleNFT` contract (admin). We need to pass the recipient account's address to the mint transaction. You can grab it from the initialization step above, or `flow.json`.

```
```bash
flow transactions send \
cadence/transactions/mint.cdc \
0xUser1Address \
0xe03daebed8ca0615 \
"Hello World" \
"Hello from Cadence" \
"https://example.com/my-nft-thumbnail.png" \
"[]" \
"[]" \
"[]" \
--signer exampleNFT
```

Expand All @@ -76,7 +88,8 @@ Fetch the `id`s of `ExampleNFT` NFTs stored in a given account:
```
flow scripts execute \
cadence/scripts/get_nft_ids.cdc \
0xUser1Address
0xUser1Address \
/public/exampleNFTCollection
```

### Get NFT Metadata
Expand All @@ -87,7 +100,7 @@ Fetch metadata for given `ExampleNFT` id in an account:
flow scripts execute \
cadence/scripts/get_nft_metadata.cdc \
0xUser1Address \
nft_id
your_nft_id
```

### Tranferring NFT to Another Account
Expand All @@ -104,8 +117,10 @@ Transfer `ExampleNFT` with given `nft_id` to `user2`:
```
flow transactions send \
cadence/transactions/transfer.cdc \
0xUser2Address \
nft_id \
your_contract_address \
ExampleNFT \
user2_address \
your_nft_id \
--signer user1
```

Expand Down
415 changes: 191 additions & 224 deletions cadence/contracts/exampleNFT/ExampleNFT.cdc

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions cadence/contracts/standards/ViewResolver.cdc

This file was deleted.

13 changes: 6 additions & 7 deletions cadence/scripts/get_nft_ids.cdc
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/// Script to get NFT IDs in an account's collection
import "NonFungibleToken"
import "ExampleNFT"

/// Script to get NFT IDs in an account's collection
///
pub fun main(address: Address): [UInt64] {
access(all) fun main(address: Address, collectionPublicPath: PublicPath): [UInt64] {
let account = getAccount(address)

let collectionRef = account
.getCapability(ExampleNFT.CollectionPublicPath)
.borrow<&{NonFungibleToken.CollectionPublic}>()
?? panic("Could not borrow capability from public collection at specified path")
let collectionRef = account.capabilities.borrow<&{NonFungibleToken.Collection}>(
collectionPublicPath
) ?? panic("Could not borrow capability from collection at specified path")

return collectionRef.getIDs()
}
110 changes: 60 additions & 50 deletions cadence/scripts/get_nft_metadata.cdc
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import "MetadataViews"
import "ExampleNFT"

/// This script gets all the view-based metadata associated with the specified NFT
/// and returns it as a single struct
pub struct NFT {
pub let name: String
pub let description: String
pub let thumbnail: String
pub let owner: Address
pub let type: String
pub let royalties: [MetadataViews.Royalty]
pub let externalURL: String
pub let serialNumber: UInt64
pub let collectionPublicPath: PublicPath
pub let collectionStoragePath: StoragePath
pub let collectionProviderPath: PrivatePath
pub let collectionPublic: String
pub let collectionPublicLinkedType: String
pub let collectionProviderLinkedType: String
pub let collectionName: String
pub let collectionDescription: String
pub let collectionExternalURL: String
pub let collectionSquareImage: String
pub let collectionBannerImage: String
pub let collectionSocials: {String: String}
pub let edition: MetadataViews.Edition
pub let traits: MetadataViews.Traits
pub let medias: MetadataViews.Medias?
pub let license: MetadataViews.License?
import "ExampleNFT"
import "MetadataViews"

access(all) struct NFT {
access(all) let name: String
access(all) let description: String
access(all) let thumbnail: String
access(all) let owner: Address
access(all) let type: String
access(all) let royalties: [MetadataViews.Royalty]
access(all) let externalURL: String
access(all) let serialNumber: UInt64
access(all) let collectionPublicPath: PublicPath
access(all) let collectionStoragePath: StoragePath
access(all) let collectionPublic: String
access(all) let collectionPublicLinkedType: String
access(all) let collectionName: String
access(all) let collectionDescription: String
access(all) let collectionExternalURL: String
access(all) let collectionSquareImage: String
access(all) let collectionBannerImage: String
access(all) let collectionSocials: {String: String}
access(all) let edition: MetadataViews.Edition
access(all) let traits: MetadataViews.Traits
access(all) let medias: MetadataViews.Medias?
access(all) let license: MetadataViews.License?
access(all) let bridgedName: String
access(all) let symbol: String
access(all) let tokenURI: String

init(
name: String,
Expand All @@ -40,10 +42,8 @@ pub struct NFT {
serialNumber: UInt64,
collectionPublicPath: PublicPath,
collectionStoragePath: StoragePath,
collectionProviderPath: PrivatePath,
collectionPublic: String,
collectionPublicLinkedType: String,
collectionProviderLinkedType: String,
collectionName: String,
collectionDescription: String,
collectionExternalURL: String,
Expand All @@ -52,8 +52,11 @@ pub struct NFT {
collectionSocials: {String: String},
edition: MetadataViews.Edition,
traits: MetadataViews.Traits,
medias:MetadataViews.Medias?,
license:MetadataViews.License?
medias:MetadataViews.Medias?,
license:MetadataViews.License?,
bridgedName: String,
symbol: String,
tokenURI: String
) {
self.name = name
self.description = description
Expand All @@ -65,10 +68,8 @@ pub struct NFT {
self.serialNumber = serialNumber
self.collectionPublicPath = collectionPublicPath
self.collectionStoragePath = collectionStoragePath
self.collectionProviderPath = collectionProviderPath
self.collectionPublic = collectionPublic
self.collectionPublicLinkedType = collectionPublicLinkedType
self.collectionProviderLinkedType = collectionProviderLinkedType
self.collectionName = collectionName
self.collectionDescription = collectionDescription
self.collectionExternalURL = collectionExternalURL
Expand All @@ -77,20 +78,26 @@ pub struct NFT {
self.collectionSocials = collectionSocials
self.edition = edition
self.traits = traits
self.medias=medias
self.license=license
self.medias = medias
self.license = license
self.bridgedName = bridgedName
self.symbol = symbol
self.tokenURI = tokenURI
}
}

pub fun main(address: Address, id: UInt64): NFT {
access(all) fun main(address: Address, id: UInt64): NFT {
let account = getAccount(address)

let collection = account
.getCapability(ExampleNFT.CollectionPublicPath)
.borrow<&{ExampleNFT.ExampleNFTCollectionPublic}>()
?? panic("Could not borrow a reference to the collection")
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData?
?? panic("ViewResolver does not resolve NFTCollectionData view")

let collection = account.capabilities.borrow<&ExampleNFT.Collection>(
collectionData.publicPath
) ?? panic("Could not borrow a reference to the collection")

let nft = collection.borrowExampleNFT(id: id)!
let nft = collection.borrowNFT(id)
?? panic("Could not borrow a reference to an NFT with the given ID")

// Get the basic display information for this NFT
let display = MetadataViews.getDisplay(nft)!
Expand All @@ -114,10 +121,12 @@ pub fun main(address: Address, id: UInt64): NFT {
collectionSocials[key] = collectionDisplay.socials[key]!.url
}

let traits = MetadataViews.getTraits(nft)!
let traits = MetadataViews.getTraits(nft)!

let medias=MetadataViews.getMedias(nft)
let license=MetadataViews.getLicense(nft)
let medias = MetadataViews.getMedias(nft)
let license = MetadataViews.getLicense(nft)

let bridgedMetadata = MetadataViews.getEVMBridgedMetadata(nft)!

return NFT(
name: display.name,
Expand All @@ -130,10 +139,8 @@ pub fun main(address: Address, id: UInt64): NFT {
serialNumber: serialNumberView.number,
collectionPublicPath: nftCollectionView.publicPath,
collectionStoragePath: nftCollectionView.storagePath,
collectionProviderPath: nftCollectionView.providerPath,
collectionPublic: nftCollectionView.publicCollection.identifier,
collectionPublicLinkedType: nftCollectionView.publicLinkedType.identifier,
collectionProviderLinkedType: nftCollectionView.providerLinkedType.identifier,
collectionName: collectionDisplay.name,
collectionDescription: collectionDisplay.description,
collectionExternalURL: collectionDisplay.externalURL.url,
Expand All @@ -142,7 +149,10 @@ pub fun main(address: Address, id: UInt64): NFT {
collectionSocials: collectionSocials,
edition: nftEditionView.infoList[0],
traits: traits,
medias:medias,
license:license
medias: medias,
license: license,
bridgedName: bridgedMetadata.name,
symbol: bridgedMetadata.symbol,
tokenURI: bridgedMetadata.uri.uri()
)
}
}
27 changes: 15 additions & 12 deletions cadence/transactions/init.cdc
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import "NonFungibleToken"
import "MetadataViews"
import "ExampleNFT"

/// This transaction is what an account would run
/// to set itself up to receive NFTs
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"

transaction {

prepare(signer: AuthAccount) {
prepare(signer: auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability) &Account) {

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData?
?? panic("ViewResolver does not resolve NFTCollectionData view")

// Return early if the account already has a collection
if signer.borrow<&ExampleNFT.Collection>(from: ExampleNFT.CollectionStoragePath) != nil {
if signer.storage.borrow<&ExampleNFT.Collection>(from: collectionData.storagePath) != nil {
return
}

// Create a new empty collection
let collection <- ExampleNFT.createEmptyCollection()
let collection <- ExampleNFT.createEmptyCollection(nftType: Type<@ExampleNFT.NFT>())

// save it to the account
signer.save(<-collection, to: ExampleNFT.CollectionStoragePath)
signer.storage.save(<-collection, to: collectionData.storagePath)

// create a public capability for the collection
signer.link<&{NonFungibleToken.CollectionPublic, ExampleNFT.ExampleNFTCollectionPublic, MetadataViews.ResolverCollection}>(
ExampleNFT.CollectionPublicPath,
target: ExampleNFT.CollectionStoragePath
)
signer.capabilities.unpublish(collectionData.publicPath)
let collectionCap = signer.capabilities.storage.issue<&ExampleNFT.Collection>(collectionData.storagePath)
signer.capabilities.publish(collectionCap, at: collectionData.publicPath)
}
}
Loading

0 comments on commit ef9437c

Please sign in to comment.