Smart Contract
BackpackMinter
A.807c3d470888cc48.BackpackMinter
1// SPDX-License-Identifier: UNLICENSED
2import Flunks from 0x807c3d470888cc48
3import Backpack from 0x807c3d470888cc48
4import NonFungibleToken from 0x1d7e57aa55817448
5import MetadataViews from 0x1d7e57aa55817448
6import HybridCustodyHelper from 0x807c3d470888cc48
7
8access(all)
9contract BackpackMinter{
10 access(all)
11 event ContractInitialized()
12
13 access(all)
14 event BackpackClaimed(FlunkTokenID: UInt64, BackpackTokenID: UInt64, signer: Address)
15
16 access(all)
17 let AdminStoragePath: StoragePath
18
19 access(self)
20 var backpackClaimedPerFlunkTokenID:{ UInt64: UInt64} // Flunk token ID: backpack token ID
21
22
23 access(self)
24 var backpackClaimedPerFlunkTemplate:{ UInt64: UInt64} // Flunks template ID: backpack token ID
25
26
27 access(all)
28 fun claimBackPack(tokenID: UInt64, signer: auth(SaveValue, Capabilities, Storage, BorrowValue) &Account, setID: UInt64){
29 // verify that the token is not already claimed
30 pre{
31 tokenID >= 0 && tokenID <= 9998:
32 "Invalid Flunk token ID"
33 !BackpackMinter.backpackClaimedPerFlunkTokenID.containsKey(tokenID):
34 "Token ID already claimed"
35 }
36
37 // verify Flunk ownership
38 let ownerCollectionTokenIds =
39 HybridCustodyHelper.getFlunksTokenIDsFromAllLinkedAccounts(ownerAddress: signer.address)
40 if !ownerCollectionTokenIds.contains(tokenID){
41 panic("signer is not owner of Flunk")
42 }
43
44 // HybridCustodyHelper to force re-linking collections
45 HybridCustodyHelper.forceRelinkCollections(signer: signer)
46
47 // Get recipient receiver capoatility
48 let recipient = getAccount(signer.address)
49 let backpackReceiver =
50 recipient.capabilities.get<&{NonFungibleToken.CollectionPublic}>(
51 Backpack.CollectionPublicPath
52 ).borrow()
53 ?? panic("Could not get receiver reference to the Backpack NFT Collection")
54
55 // mint backpack to signer
56 let admin =
57 self.account.storage.borrow<&Backpack.Admin>(from: Backpack.AdminStoragePath)
58 ?? panic("Could not borrow a reference to the Backpack Admin")
59 let backpackSet = admin.borrowSet(setID: setID)
60 let backpackNFT <- backpackSet.mintNFT()
61 let backpackTokenID = backpackNFT.id
62 emit BackpackClaimed(
63 FlunkTokenID: tokenID,
64 BackpackTokenID: backpackNFT.id,
65 signer: signer.address
66 )
67 backpackReceiver.deposit(token: <-backpackNFT)
68 BackpackMinter.backpackClaimedPerFlunkTokenID[tokenID] = backpackTokenID
69 let trueOwnerAddress =
70 HybridCustodyHelper.getChildAccountAddressHoldingFlunksTokenId(
71 ownerAddress: signer.address,
72 tokenID: tokenID
73 )
74 ?? panic("Could not find true owner for specified tokenID")
75 let trueOwnercollection =
76 getAccount(trueOwnerAddress).capabilities.get<&Flunks.Collection>(
77 Flunks.CollectionPublicPath
78 ).borrow()
79 ?? panic("Could not borrow a reference to the account's NFT collection")
80 let item = trueOwnercollection.borrowNFT(tokenID)
81 let serialView =
82 item?.resolveView(Type<MetadataViews.Serial>())
83 ?? panic("Could not get the item's serial view")
84 let serial = serialView as! MetadataViews.Serial?
85 let templateID = serial?.number ?? 0
86 BackpackMinter.backpackClaimedPerFlunkTemplate[templateID] = backpackTokenID
87 }
88
89 access(all)
90 fun getClaimedBackPacksPerFlunkTokenID():{ UInt64: UInt64}{
91 return BackpackMinter.backpackClaimedPerFlunkTokenID
92 }
93
94 access(all)
95 fun getClaimedBackPacksPerFlunkTemplateID():{ UInt64: UInt64}{
96 return BackpackMinter.backpackClaimedPerFlunkTemplate
97 }
98
99 init(){
100 self.AdminStoragePath = /storage/BackpackMinterAdmin
101 self.backpackClaimedPerFlunkTokenID ={}
102 self.backpackClaimedPerFlunkTemplate ={}
103 emit ContractInitialized()
104 }
105}
106