Smart Contract

Inbox

A.de7a5daf9df48c65.Inbox

Valid From

85,739,540

Deployed

2d ago
Feb 25, 2026, 07:56:08 AM UTC

Dependents

56 imports
1import NonFungibleToken from 0x1d7e57aa55817448
2import Pack from 0xde7a5daf9df48c65
3
4// Purpose: This Inbox contract allows the admin to send pack NFTs to a centralized inbox held by the admin.
5// This allows the recipients to claim their packs at any time.
6
7pub contract Inbox {
8    
9    // -----------------------------------------------------------------------
10    // Inbox Events
11    // -----------------------------------------------------------------------
12    pub event MailClaimed(address: Address, packID: UInt64)
13    pub event PackMailCreated(address: Address, packIDs: [UInt64])
14    pub event MailAdminClaimed(wallet: Address, packID: UInt64)
15
16    // -----------------------------------------------------------------------
17    // Named Paths
18    // -----------------------------------------------------------------------
19    pub let CentralizedInboxStoragePath: StoragePath
20    pub let CentralizedInboxPrivatePath: PrivatePath
21    pub let CentralizedInboxPublicPath: PublicPath
22
23    // -----------------------------------------------------------------------
24    // Inbox Fields
25    // -----------------------------------------------------------------------
26
27    pub resource interface Public {
28        pub fun getAddresses(): [Address]
29        pub fun getIDs(wallet: Address): [UInt64]?
30        pub fun borrowPack(wallet: Address, id: UInt64): &Pack.NFT{Pack.Public}?
31        pub fun claimMail(recipient: &{NonFungibleToken.Receiver}, id: UInt64)
32        pub fun getMailsLength(): Int
33    }
34
35    pub resource CentralizedInbox: Public {
36        access(self) var mails: @{Address: Pack.Collection}
37
38        init() {
39            self.mails <- {}
40        }
41
42        pub fun getAddresses(): [Address] {
43            return self.mails.keys
44        }
45
46        pub fun getIDs(wallet: Address): [UInt64]? {
47            if(self.mails[wallet] != nil) {
48                let collectionRef = (&self.mails[wallet] as auth &Pack.Collection?)!
49                return collectionRef.getIDs()
50            } else {
51                return nil
52            }
53        }
54
55        pub fun borrowPack(wallet: Address, id: UInt64): &Pack.NFT{Pack.Public}? {
56            let collectionRef = (&self.mails[wallet] as auth &Pack.Collection?)!
57            return collectionRef.borrowPack(id: id)
58        }
59
60        pub fun claimMail(recipient: &{NonFungibleToken.Receiver}, id: UInt64) {
61            let wallet = recipient.owner!.address
62
63            if(self.mails[wallet] != nil) {
64                let collectionRef = (&self.mails[wallet] as auth &Pack.Collection?)!
65                recipient.deposit(token: <-collectionRef.withdraw(withdrawID: id))
66            }
67
68            emit MailClaimed(address: wallet, packID: id)
69
70        }
71
72        pub fun getMailsLength(): Int {
73            return self.mails.length
74        }
75
76        pub fun createPackMail(wallet: Address, packs: @Pack.Collection) {
77            let IDs = packs.getIDs()
78
79            if (self.mails[wallet] == nil) {
80                self.mails[wallet] <-! Pack.createEmptyCollection() as! @Pack.Collection
81            }
82
83            let collectionRef = (&self.mails[wallet] as auth &Pack.Collection?)!
84
85            for id in IDs {
86                collectionRef.deposit(token: <- packs.withdraw(withdrawID: id))
87            }
88
89            destroy packs
90
91            emit PackMailCreated(address: wallet, packIDs: IDs)
92
93        }
94
95        pub fun adminClaimMail(wallet: Address, recipient: &{NonFungibleToken.Receiver}, id: UInt64) {
96            if(self.mails[wallet] != nil) {
97                let collectionRef = (&self.mails[wallet] as auth &Pack.Collection?)!
98                recipient.deposit(token: <-collectionRef.withdraw(withdrawID: id))
99            }
100
101            emit MailAdminClaimed(wallet: wallet, packID: id)
102
103        }
104
105        pub fun moveMail(wallet: Address): @Pack.Collection {
106            
107            let packs <- Pack.createEmptyCollection() as! @Pack.Collection
108
109            if(self.mails[wallet] != nil) {
110                let collectionRef = (&self.mails[wallet] as auth &Pack.Collection?)!
111
112                let IDs = collectionRef.getIDs()
113
114                for id in IDs {
115                    packs.deposit(token: <-collectionRef.withdraw(withdrawID: id))
116                }
117                
118            }
119
120            return <-packs
121        }
122
123        pub fun createNewCentralizedInbox(): @CentralizedInbox {
124            return <-create CentralizedInbox()
125        }
126
127        destroy() {
128            pre {
129                self.mails.length == 0: "Can't destroy: mails are left in the inbox"
130            }
131            destroy self.mails
132        }
133    }
134
135    init() {
136        // Set named paths
137        self.CentralizedInboxStoragePath = /storage/BasicBeastsCentralizedInbox
138        self.CentralizedInboxPrivatePath = /private/BasicBeastsCentralizedInboxUpgrade
139        self.CentralizedInboxPublicPath = /public/BasicBeastsCentralizedInbox
140
141        // Put CentralizedInbox in storage
142        self.account.save(<-create CentralizedInbox(), to: self.CentralizedInboxStoragePath)
143
144        self.account.link<&Inbox.CentralizedInbox>(self.CentralizedInboxPrivatePath, target: self.CentralizedInboxStoragePath) 
145                                                ?? panic("Could not get a capability to the Centralized Inbox")
146
147        self.account.link<&Inbox.CentralizedInbox{Public}>(self.CentralizedInboxPublicPath, target: self.CentralizedInboxStoragePath) 
148                                                ?? panic("Could not get a capability to the Centralized Inbox")
149    }
150}
151