Smart Contract

StorageHelper

A.8f9231920da9af6d.StorageHelper

Deployed

1d ago
Feb 26, 2026, 10:18:21 PM UTC

Dependents

0 imports
1import FungibleToken from 0xf233dcee88fe0abe 
2import FlowToken from 0x1654653399040a61 
3
4access(all) contract StorageHelper {
5
6    access(all) var topUpAmount: UFix64
7    access(all) var topUpThreshold: UInt64
8
9    access(all) event AccountToppedUp(address: Address, amount: UFix64)
10
11    access(all) view fun availableAccountStorage(address: Address): UInt64 {
12        return getAccount(address).storage.capacity - getAccount(address).storage.used
13    }
14
15    access(account) fun topUpAccount(address: Address) {
16        let topUpAmount: UFix64 = self.getTopUpAmount()
17        let topUpThreshold: UInt64 = self.getTopUpThreshold()
18        
19        if (StorageHelper.availableAccountStorage(address: address) > topUpThreshold) {
20            return
21        }
22
23        let vaultRef: auth(FungibleToken.Withdraw) &FlowToken.Vault = self.account.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
24			?? panic("Could not borrow reference to the owner's Vault!")
25
26        let topUpFunds: @{FungibleToken.Vault} <- vaultRef.withdraw(amount: topUpAmount)
27        
28        let receiverRef: &{FungibleToken.Receiver} = getAccount(address)
29            .capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
30            .borrow()
31			?? panic("Could not borrow receiver reference to the recipient's Vault")
32
33        receiverRef.deposit(from: <-topUpFunds)
34
35        emit AccountToppedUp(address: address, amount: topUpAmount)
36    }
37
38    access(account) fun updateTopUpAmount(amount: UFix64) {
39        self.topUpAmount = amount
40    }
41
42    access(account) fun updateTopUpThreshold(threshold: UInt64) {
43        self.topUpThreshold = threshold
44    }
45
46    access(account) view fun getTopUpAmount(): UFix64 {
47        return 0.000012 // self.topUpAmount //
48    }
49
50    access(account) view fun getTopUpThreshold(): UInt64 {
51        return 1200 // self.topUpThreshold
52    }
53
54    init() {
55        self.topUpAmount = 0.000012
56        self.topUpThreshold = 1200
57    }
58 }