Smart Contract
StorageRent
A.707adbad1428c624.StorageRent
1
2import FungibleToken from 0xf233dcee88fe0abe
3import FlowStorageFees from 0xe467b9dd11fa00df
4import FlowToken from 0x1654653399040a61
5
6access(all)
7contract StorageRent{
8 access(all)
9 let StorageRentAdminStoragePath: StoragePath
10
11 access(contract)
12 var StorageRentRefillThreshold: UInt64
13
14
15
16 access(contract)
17 var isOpen: Bool
18
19 access(all) event Refilled(address: Address, amount: UFix64)
20
21
22 access(all)
23 view fun getStorageRentRefillThreshold(): UInt64{
24 return self.StorageRentRefillThreshold
25 }
26
27 access(all)
28 view fun getIsOpen(): Bool{
29 return self.isOpen
30 }
31
32 access(all)
33 fun tryRefill(_ address: Address){
34 if self.isOpen == false {
35 return
36 }
37
38 let recipient = getAccount(address)
39 let receiverRef = recipient.capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)!.borrow()
40 if receiverRef == nil || (receiverRef!).owner == nil{
41 return
42 }
43
44 var low: UInt64 = recipient.storage.used
45 var high: UInt64 = recipient.storage.capacity
46 if high < low{
47 high <-> low
48 }
49 if high - low < self.StorageRentRefillThreshold {
50 let vaultRef = self.account.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
51 ?? panic("Could not borrow reference to the owner's Vault!")
52
53 let requiredAmount = FlowStorageFees.storageCapacityToFlow(FlowStorageFees.convertUInt64StorageBytesToUFix64Megabytes(self.StorageRentRefillThreshold * 2))
54 (receiverRef!).deposit(from: <-(vaultRef!).withdraw(amount: requiredAmount))
55
56 emit Refilled(address: address, amount: requiredAmount)
57 }
58 }
59
60
61 access(all)
62 resource Admin{
63 access(all)
64 fun setStorageRentRefillThreshold(_ threshold: UInt64){
65 StorageRent.StorageRentRefillThreshold = threshold
66 }
67
68 access(all)
69 fun setIsOpen(_ flag: Bool){
70 StorageRent.isOpen = flag
71 }
72 }
73
74 init(){
75 self.StorageRentAdminStoragePath = /storage/StorageRentAdmin
76 self.StorageRentRefillThreshold = 5000
77 self.isOpen = true
78 let admin <- create Admin()
79 self.account.storage.save(<-admin, to: /storage/StorageRentAdmin)
80 }
81}