Skip to content

Instantly share code, notes, and snippets.

@AlmostEfficient
Last active October 11, 2022 14:29
Show Gist options
  • Save AlmostEfficient/28d2fa6bdf8c920cbbc03f1b9f770b5f to your computer and use it in GitHub Desktop.
Save AlmostEfficient/28d2fa6bdf8c920cbbc03f1b9f770b5f to your computer and use it in GitHub Desktop.
Publicly mintable Flow NFT contract that conforms to the latest standards.
import NonFungibleToken from 0x631e88ae7f1d7c20;
import MetadataViews from 0x631e88ae7f1d7c20;
pub contract CatMoji: NonFungibleToken {
pub var totalSupply: UInt64
pub event ContractInitialized()
pub event Withdraw(id: UInt64, from: Address?)
pub event Deposit(id: UInt64, to: Address?)
pub let CollectionStoragePath: StoragePath
pub let CollectionPublicPath: PublicPath
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
pub let id: UInt64
pub let name: String
pub let description: String
pub let thumbnail: String
init(
id: UInt64,
name: String,
description: String,
thumbnail: String,
) {
self.id = id
self.name = name
self.description = description
self.thumbnail = thumbnail
}
pub fun getViews(): [Type] {
return [
Type<MetadataViews.Display>()
]
}
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Display>():
return MetadataViews.Display(
name: self.name,
description: self.description,
thumbnail: MetadataViews.HTTPFile(
url: self.thumbnail
)
)
}
return nil
}
}
pub resource interface CatMojiCollectionPublic {
pub fun deposit(token: @NonFungibleToken.NFT)
pub fun getIDs(): [UInt64]
pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
}
pub resource Collection: CatMojiCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
init () {
self.ownedNFTs <- {}
}
pub fun getIDs(): [UInt64] {
return self.ownedNFTs.keys
}
pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
emit Withdraw(id: token.id, from: self.owner?.address)
return <-token
}
pub fun deposit(token: @NonFungibleToken.NFT) {
let token <- token as! @CatMoji.NFT
let id: UInt64 = token.id
let oldToken <- self.ownedNFTs[id] <- token
emit Deposit(id: id, to: self.owner?.address)
destroy oldToken
}
pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
}
pub fun borrowViewResolver(id: UInt64): &AnyResource{MetadataViews.Resolver} {
let nft = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
let CatMoji = nft as! &CatMoji.NFT
return CatMoji as &AnyResource{MetadataViews.Resolver}
}
destroy() {
destroy self.ownedNFTs
}
}
pub fun createEmptyCollection(): @NonFungibleToken.Collection {
return <- create Collection()
}
pub fun mintNFT(
recipient: &{NonFungibleToken.CollectionPublic},
name: String,
description: String,
thumbnail: String,
) {
var newNFT <- create NFT(
id: CatMoji.totalSupply,
name: name,
description: description,
thumbnail: thumbnail
)
recipient.deposit(token: <-newNFT)
CatMoji.totalSupply = CatMoji.totalSupply + UInt64(1)
}
init() {
self.totalSupply = 0
self.CollectionStoragePath = /storage/CatMojiCollection
self.CollectionPublicPath = /public/CatMojiCollection
let collection <- create Collection()
self.account.save(<-collection, to: self.CollectionStoragePath)
self.account.link<&CatMoji.Collection{NonFungibleToken.CollectionPublic, CatMoji.CatMojiCollectionPublic, MetadataViews.ResolverCollection}>(
self.CollectionPublicPath,
target: self.CollectionStoragePath
)
emit ContractInitialized()
}
}
@BlockFather
Copy link

very nice good ser.

@vvronskyFX
Copy link

Nice this is awesome!

@crazydevlegend
Copy link

Thanks, @AlmostEfficient
BTW, I have a question, on line 99, I guess it's redundant to cast to AnyResource. (I'm new to Flow, and just wanna have your inputs)
What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment