Smart Contract

EternalShardedCollection

A.c38aea683c0c4d38.EternalShardedCollection

Deployed

2h ago
Mar 01, 2026, 05:18:34 PM UTC

Dependents

0 imports
1/*
2    Copied from the EternalShardedCollection contract except with
3    names changed.
4*/
5
6import NonFungibleToken from 0x1d7e57aa55817448
7import Eternal from 0xc38aea683c0c4d38
8
9pub contract EternalShardedCollection {
10
11    // ShardedCollection stores a dictionary of Eternal Collections
12    // A Moment is stored in the field that corresponds to its id % numBuckets
13    pub resource ShardedCollection: Eternal.MomentCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic { 
14        
15        // Dictionary of Eternal collections
16        pub var collections: @{UInt64: Eternal.Collection}
17
18        // The number of buckets to split Moments into
19        // This makes storage more efficient and performant
20        pub let numBuckets: UInt64
21
22        init(numBuckets: UInt64) {
23            self.collections <- {}
24            self.numBuckets = numBuckets
25
26            // Create a new empty collection for each bucket
27            var i: UInt64 = 0
28            while i < numBuckets {
29
30                self.collections[i] <-! Eternal.createEmptyCollection() as! @Eternal.Collection
31
32                i = i + UInt64(1)
33            }
34        }
35
36        // withdraw removes a Moment from one of the Collections 
37        // and moves it to the caller
38        pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
39            post {
40                result.id == withdrawID: "The ID of the withdrawn NFT is incorrect"
41            }
42            // Find the bucket it should be withdrawn from
43            let bucket = withdrawID % self.numBuckets
44
45            // Withdraw the moment
46            let token <- self.collections[bucket]?.withdraw(withdrawID: withdrawID)!
47            
48            return <-token
49        }
50
51        // batchWithdraw withdraws multiple tokens and returns them as a Collection
52        //
53        // Parameters: ids: an array of the IDs to be withdrawn from the Collection
54        //
55        // Returns: @NonFungibleToken.Collection a Collection containing the moments
56        //          that were withdrawn
57        pub fun batchWithdraw(ids: [UInt64]): @NonFungibleToken.Collection {
58            var batchCollection <- Eternal.createEmptyCollection()
59            
60            // Iterate through the ids and withdraw them from the Collection
61            for id in ids {
62                batchCollection.deposit(token: <-self.withdraw(withdrawID: id))
63            }
64            return <-batchCollection
65        }
66
67        // deposit takes a Moment and adds it to the Collections dictionary
68        pub fun deposit(token: @NonFungibleToken.NFT) {
69
70            // Find the bucket this corresponds to
71            let bucket = token.id % self.numBuckets
72
73            // Remove the collection
74            let collection <- self.collections.remove(key: bucket)!
75
76            // Deposit the nft into the bucket
77            collection.deposit(token: <-token)
78
79            // Put the Collection back in storage
80            self.collections[bucket] <-! collection
81        }
82
83        // batchDeposit takes a Collection object as an argument
84        // and deposits each contained NFT into this Collection
85        pub fun batchDeposit(tokens: @NonFungibleToken.Collection) {
86            let keys = tokens.getIDs()
87
88            // Iterate through the keys in the Collection and deposit each one
89            for key in keys {
90                self.deposit(token: <-tokens.withdraw(withdrawID: key))
91            }
92            destroy tokens
93        }
94
95        // getIDs returns an array of the IDs that are in the Collection
96        pub fun getIDs(): [UInt64] {
97
98            var ids: [UInt64] = []
99            // Concatenate IDs in all the Collections
100            for key in self.collections.keys {
101                for id in self.collections[key]?.getIDs() ?? [] {
102                    ids.append(id)
103                }
104            }
105            return ids
106        }
107
108        // borrowNFT Returns a borrowed reference to a Moment in the Collection
109        // so that the caller can read data and call methods from it
110        pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
111            post {
112                result.id == id: "The ID of the reference is incorrect"
113            }
114
115            // Get the bucket of the nft to be borrowed
116            let bucket = id % self.numBuckets
117
118            // Find NFT in the collections and borrow a reference
119            return self.collections[bucket]?.borrowNFT(id: id)!
120        }
121
122        // borrowMoment Returns a borrowed reference to a Moment in the Collection
123        // so that the caller can read data and call methods from it
124        // They can use this to read its setID, playID, serialNumber,
125        // or any of the setData or Play Data associated with it by
126        // getting the setID or playID and reading those fields from
127        // the smart contract
128        //
129        // Parameters: id: The ID of the NFT to get the reference for
130        //
131        // Returns: A reference to the NFT
132        pub fun borrowMoment(id: UInt64): &Eternal.NFT? {
133
134            // Get the bucket of the nft to be borrowed
135            let bucket = id % self.numBuckets
136
137            return self.collections[bucket]?.borrowMoment(id: id) ?? nil
138        }
139
140        // If a transaction destroys the Collection object,
141        // All the NFTs contained within are also destroyed
142        destroy() {
143            destroy self.collections
144        }
145    }
146
147    // Creates an empty ShardedCollection and returns it to the caller
148    pub fun createEmptyCollection(numBuckets: UInt64): @ShardedCollection {
149        return <-create ShardedCollection(numBuckets: numBuckets)
150    }
151}
152