Smart Contract
TreasureChestFUSDReward
A.117396d8a72ad372.TreasureChestFUSDReward
1// FUSD Reward for claiming The Inspected Treasure Chest
2import NonFungibleToken from 0x1d7e57aa55817448
3import FungibleToken from 0xf233dcee88fe0abe
4import FUSD from 0x3c5959b568896393
5import NFTDayTreasureChest from 0x117396d8a72ad372
6
7access(all)
8contract TreasureChestFUSDReward{
9
10 access(all) entitlement CentralizedInboxOwner
11
12 // -----------------------------------------------------------------------
13 // TreasureChestFUSDReward Events
14 // -----------------------------------------------------------------------
15 access(all)
16 event BonusAdded(wallet: Address, amount: UFix64, bonus: String)
17
18 access(all)
19 event RewardClaimed(wallet: Address, reward: UFix64)
20
21 access(all)
22 event AdminRewardReclaim(chestID: UInt64, rewardAmount: UFix64)
23
24 access(all)
25 event RewardCreated(chestID: UInt64)
26
27 // -----------------------------------------------------------------------
28 // Named Paths
29 // -----------------------------------------------------------------------
30 access(all)
31 let CentralizedInboxStoragePath: StoragePath
32
33 access(all)
34 let CentralizedInboxPrivatePath: PrivatePath
35
36 access(all)
37 let CentralizedInboxPublicPath: PublicPath
38
39 // -----------------------------------------------------------------------
40 // TreasureChestFUSDReward Fields
41 // -----------------------------------------------------------------------
42 access(all)
43 resource interface Public{
44 access(all) view
45 fun getClaimed():{ UInt64: Address}
46
47 access(all) view
48 fun getChestIDs(): [UInt64]
49
50 access(all) view
51 fun getBonusRewards():{ Address: String}
52
53 access(all)
54 fun addBonus(wallet: Address, amount: UFix64)
55
56 access(all)
57 fun claimReward(
58 recipient: &FUSD.Vault,
59 chest: @NFTDayTreasureChest.NFT
60 ): @NFTDayTreasureChest.NFT
61 }
62
63 access(all)
64 resource CentralizedInbox: Public{
65 // List of claimed chests and which address claimed it
66 access(self)
67 var claimed:{ UInt64: Address}
68
69 // The chest and the vault with the reward amount
70 access(self)
71 var rewards: @{UInt64: FUSD.Vault}
72
73 // List of addresses and their bonus rewards
74 access(self)
75 var bonusRewards:{ Address: String}
76
77 init(){
78 self.claimed ={}
79 self.rewards <-{}
80 self.bonusRewards ={}
81 }
82
83 access(all) view
84 fun getClaimed():{ UInt64: Address}{
85 return self.claimed
86 }
87
88 access(all) view
89 fun getChestIDs(): [UInt64]{
90 return self.rewards.keys
91 }
92
93 access(all) view
94 fun getBonusRewards():{ Address: String}{
95 return self.bonusRewards
96 }
97
98 access(all)
99 fun addBonus(wallet: Address, amount: UFix64){
100 pre{
101 self.bonusRewards[wallet] == nil:
102 "Cannot add bonus: Bonus has already been added"
103 }
104 var bonus = "Starter Pack"
105 if amount == 6.9{
106 bonus = "Saber Merch"
107 }
108 if amount == 69.00{
109 bonus = "Cursed Black Pack"
110 }
111 self.bonusRewards[wallet] = bonus
112 emit BonusAdded(wallet: wallet, amount: amount, bonus: bonus)
113 }
114
115 access(all)
116 fun claimReward(recipient: &FUSD.Vault, chest: @NFTDayTreasureChest.NFT): @NFTDayTreasureChest.NFT{
117 pre{
118 self.rewards[chest.id] != nil:
119 "Can't claim reward: Chest doesn't have a reward to claim"
120 !self.claimed.keys.contains(chest.id):
121 "Can't claim reward: Reward from chest has already been claimed"
122 }
123 let wallet = (recipient.owner!).address
124 let vaultRef = (&self.rewards[chest.id] as auth(FungibleToken.Withdraw) &FUSD.Vault?)!
125 let amount = vaultRef.balance
126 recipient.deposit(from: <-vaultRef.withdraw(amount: vaultRef.balance))
127
128 // Add to claimed list
129 self.claimed[chest.id] = wallet
130 emit RewardClaimed(wallet: wallet, reward: amount)
131 return <-chest
132 }
133
134 // -----------------------------------------------------------------------
135 // Admin Functions
136 // -----------------------------------------------------------------------
137 access(all) view
138 fun getBalance(chestID: UInt64): UFix64?{
139 if self.rewards[chestID] != nil{
140 let vaultRef: &FUSD.Vault = (&self.rewards[chestID] as &FUSD.Vault?)!
141 return vaultRef.balance
142 } else{
143 return nil
144 }
145 }
146
147 access(CentralizedInboxOwner)
148 fun adminReclaimReward(chestID: UInt64, recipient: &FUSD.Vault){
149 pre{
150 self.rewards[chestID] != nil:
151 "Can't reclaim reward: Chest doesn't have a reward to reclaim"
152 }
153 let vaultRef = (&self.rewards[chestID] as auth(FungibleToken.Withdraw) &FUSD.Vault?)!
154 let amount = vaultRef.balance
155 recipient.deposit(from: <-vaultRef.withdraw(amount: vaultRef.balance))
156 emit AdminRewardReclaim(chestID: chestID, rewardAmount: amount)
157 }
158
159 access(CentralizedInboxOwner)
160 fun createReward(chestID: UInt64, reward: @FUSD.Vault){
161 pre{
162 self.rewards[chestID] == nil:
163 "Can't create rewards: Reward has already been created"
164 }
165 self.rewards[chestID] <-! reward
166 emit RewardCreated(chestID: chestID)
167 }
168
169 access(all)
170 fun createNewCentralizedInbox(): @CentralizedInbox{
171 return <-create CentralizedInbox()
172 }
173 }
174
175 init(){
176 // Set named paths
177 self.CentralizedInboxStoragePath = /storage/BasicBeastsTreasureChestFUSDReward
178 self.CentralizedInboxPrivatePath = /private/BasicBeastsTreasureChestFUSDRewardUpgrade
179 self.CentralizedInboxPublicPath = /public/BasicBeastsTreasureChestFUSDReward
180
181 // Put CentralizedInbox in storage
182 self.account.storage.save(<-create CentralizedInbox(), to: self.CentralizedInboxStoragePath)
183 var capability_1 =
184 self.account.capabilities.storage.issue<&TreasureChestFUSDReward.CentralizedInbox>(
185 self.CentralizedInboxStoragePath
186 )
187 self.account.capabilities.publish(capability_1, at: self.CentralizedInboxPublicPath)
188 }
189}
190