Smart Contract
BalloonSampleNFT
A.500f525ae1d02c5c.BalloonSampleNFT
1import NonFungibleToken from 0x1d7e57aa55817448
2
3pub contract BalloonSampleNFT: NonFungibleToken {
4
5 /// Total supply of BalloonSampleNFT in existence
6 pub var totalSupply: UInt64
7
8 /// The event that is emitted when the contract is created
9 pub event ContractInitialized()
10
11 /// The event that is emitted when an NFT is withdrawn from a Collection
12 pub event Withdraw(id: UInt64, from: Address?)
13
14 /// The event that is emitted when an NFT is deposited to a Collection
15 pub event Deposit(id: UInt64, to: Address?)
16
17 /// Storage and Public Paths
18 pub let CollectionStoragePath: StoragePath
19 pub let CollectionPublicPath: PublicPath
20
21 /// The core resource that represents a Non Fungible Token.
22 /// and stored in the Collection resource
23 ///
24 pub resource NFT: NonFungibleToken.INFT {
25
26 /// The unique ID that each NFT has
27 pub let id: UInt64
28
29 /// Metadata fields
30 pub let name: String
31 pub let rgbColor: String
32 pub let inflation: UInt64
33 access(self) let metadata: {String: AnyStruct}
34
35 init(
36 id: UInt64,
37 name: String,
38 rgbColor: String,
39 inflation: UInt64,
40 metadata: {String: AnyStruct},
41 ) {
42 self.id = id
43 self.name = name
44 self.rgbColor = rgbColor
45 self.inflation = inflation
46 self.metadata = metadata
47 }
48 }
49
50 pub resource interface BalloonSampleNFTCollectionPublic {
51 pub fun deposit(token: @NonFungibleToken.NFT)
52 pub fun getIDs(): [UInt64]
53 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
54 pub fun borrowBalloonSampleNFT(id: UInt64): &BalloonSampleNFT.NFT? {
55 post {
56 (result == nil) || (result?.id == id):
57 "Cannot borrow BalloonSampleNFT reference: the ID of the returned reference is incorrect"
58 }
59 }
60 }
61
62 /// The resource that will be holding the NFTs inside any account.
63 /// In order to be able to manage NFTs any account will need to create
64 /// an empty collection first
65 ///
66 pub resource Collection: BalloonSampleNFTCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic {
67 // dictionary of NFT conforming tokens
68 // NFT is a resource type with an `UInt64` ID field
69 pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
70
71 init () {
72 self.ownedNFTs <- {}
73 }
74
75
76 pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
77 let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
78
79 emit Withdraw(id: token.id, from: self.owner?.address)
80
81 return <-token
82 }
83
84
85 pub fun deposit(token: @NonFungibleToken.NFT) {
86 let token <- token as! @BalloonSampleNFT.NFT
87
88 let id: UInt64 = token.id
89
90 // add the new token to the dictionary which removes the old one
91 let oldToken <- self.ownedNFTs[id] <- token
92
93 emit Deposit(id: id, to: self.owner?.address)
94
95 destroy oldToken
96 }
97
98
99 pub fun getIDs(): [UInt64] {
100 return self.ownedNFTs.keys
101 }
102
103
104 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
105 return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
106 }
107
108
109 pub fun borrowBalloonSampleNFT(id: UInt64): &BalloonSampleNFT.NFT? {
110 if self.ownedNFTs[id] != nil {
111 // Create an authorized reference to allow downcasting
112 let ref = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
113 return ref as! &BalloonSampleNFT.NFT
114 }
115
116 return nil
117 }
118
119 destroy() {
120 destroy self.ownedNFTs
121 }
122 }
123
124
125 pub fun createEmptyCollection(): @NonFungibleToken.Collection {
126 return <- create Collection()
127 }
128
129 pub fun mintNFT(
130 recipient: &{NonFungibleToken.CollectionPublic},
131 name: String,
132 rgbColor: String,
133 inflation: UInt64
134 ) {
135 let metadata: {String: AnyStruct} = {}
136 let currentBlock = getCurrentBlock()
137 metadata["mintedBlock"] = currentBlock.height
138 metadata["mintedTime"] = currentBlock.timestamp
139 metadata["minter"] = recipient.owner!.address
140
141
142 var newNFT <- create NFT(
143 id: BalloonSampleNFT.totalSupply,
144 name: name,
145 rgbColor: rgbColor,
146 inflation: inflation,
147 metadata: metadata,
148 )
149 recipient.deposit(token: <-newNFT)
150
151 BalloonSampleNFT.totalSupply = BalloonSampleNFT.totalSupply + UInt64(1)
152 }
153
154 init() {
155 // Initialize the total supply
156 self.totalSupply = 0
157
158 // Set the named paths
159 self.CollectionStoragePath = /storage/balloonSampleNFTCollection
160 self.CollectionPublicPath = /public/balloonSampleNFTCollection
161
162 // Create a Collection resource and save it to storage
163 let collection <- create Collection()
164 self.account.save(<-collection, to: self.CollectionStoragePath)
165
166 // create a public capability for the collection
167 self.account.link<&BalloonSampleNFT.Collection{NonFungibleToken.CollectionPublic, BalloonSampleNFT.BalloonSampleNFTCollectionPublic}>(
168 self.CollectionPublicPath,
169 target: self.CollectionStoragePath
170 )
171
172 emit ContractInitialized()
173 }
174}