Smart Contract
BasicBeastsInbox
A.de7a5daf9df48c65.BasicBeastsInbox
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.
6access(all) contract BasicBeastsInbox {
7
8 access(all) entitlement InboxOwner
9
10 // -----------------------------------------------------------------------
11 // Inbox Events
12 // -----------------------------------------------------------------------
13 access(all) event MailClaimed(address: Address, packID: UInt64)
14 access(all) event PackMailCreated(address: Address, packIDs: [UInt64])
15 access(all) event MailAdminClaimed(wallet: Address, packID: UInt64)
16
17 // -----------------------------------------------------------------------
18 // Named Paths
19 // -----------------------------------------------------------------------
20 access(all) let CentralizedInboxStoragePath: StoragePath
21 access(all) let CentralizedInboxPrivatePath: PrivatePath
22 access(all) let CentralizedInboxPublicPath: PublicPath
23
24 // -----------------------------------------------------------------------
25 // Inbox Fields
26 // -----------------------------------------------------------------------
27 access(all) resource interface Public{
28 access(all) view fun getAddresses(): [Address]
29 access(all) view fun getIDs(wallet: Address): [UInt64]?
30 access(all) view fun borrowPack(wallet: Address, id: UInt64): &Pack.NFT?
31 access(all) fun claimMail(recipient: &{NonFungibleToken.Receiver}, id: UInt64)
32 access(all) view fun getMailsLength(): Int
33 }
34
35 access(all) resource CentralizedInbox: Public {
36 access(self) var mails: @{Address: Pack.Collection}
37
38 init(){
39 self.mails <-{}
40 }
41
42 access(all) view fun getAddresses(): [Address] {
43 return self.mails.keys
44 }
45
46 access(all) view fun getIDs(wallet: Address): [UInt64]? {
47 if self.mails[wallet] != nil{
48 let collectionRef = (&self.mails[wallet] as &Pack.Collection?)!
49 return collectionRef.getIDs()
50 } else{
51 return nil
52 }
53 }
54
55 access(all) view fun borrowPack(wallet: Address, id: UInt64): &Pack.NFT?{
56 let collectionRef = (&self.mails[wallet] as &Pack.Collection?)!
57 return collectionRef.borrowPack(id: id)
58 }
59
60 access(all) fun claimMail(recipient: &{NonFungibleToken.Receiver}, id: UInt64) {
61 let wallet = (recipient.owner!).address
62 if self.mails[wallet] != nil{
63 let collectionRef = (&self.mails[wallet] as auth(NonFungibleToken.Withdraw) &Pack.Collection?)!
64 recipient.deposit(token: <-collectionRef.withdraw(withdrawID: id))
65 }
66 emit MailClaimed(address: wallet, packID: id)
67 }
68
69 access(all) view fun getMailsLength(): Int{
70 return self.mails.length
71 }
72
73 access(InboxOwner) fun createPackMail(wallet: Address, packs: @Pack.Collection){
74 let IDs = packs.getIDs()
75 if self.mails[wallet] == nil{
76 self.mails[wallet] <-! Pack.createEmptyCollection(nftType: Type<@Pack.NFT>()) as! @Pack.Collection
77 }
78 let collectionRef = (&self.mails[wallet] as &Pack.Collection?)!
79 for id in IDs{
80 collectionRef.deposit(token: <-packs.withdraw(withdrawID: id))
81 }
82 destroy packs
83 emit PackMailCreated(address: wallet, packIDs: IDs)
84 }
85
86 access(InboxOwner) fun adminClaimMail(wallet: Address, recipient: &{NonFungibleToken.Receiver}, id: UInt64){
87 if self.mails[wallet] != nil{
88 let collectionRef = (&self.mails[wallet] as auth(NonFungibleToken.Withdraw) &Pack.Collection?)!
89 recipient.deposit(token: <-collectionRef.withdraw(withdrawID: id))
90 }
91 emit MailAdminClaimed(wallet: wallet, packID: id)
92 }
93
94 access(all) fun createNewCentralizedInbox(): @CentralizedInbox{
95 return <-create CentralizedInbox()
96 }
97 }
98
99 init(){
100 // Set named paths
101 self.CentralizedInboxStoragePath = /storage/BasicBeastsCentralizedInbox_2
102 self.CentralizedInboxPrivatePath = /private/BasicBeastsCentralizedInboxUpgrade_2
103 self.CentralizedInboxPublicPath = /public/BasicBeastsCentralizedInbox_2
104
105 // Put CentralizedInbox in storage
106 self.account.storage.save(<-create CentralizedInbox(), to: self.CentralizedInboxStoragePath)
107 }
108}
109