Smart Contract
HotspotRegistry
A.efc9bea2fda54f34.HotspotRegistry
1// HotspotRegistry.cdc
2//
3// This contract manages the registration and management of hotspots in the network.
4// It allows users to register hotspots and admins to update their status, and retrieve information about them.
5
6import HotspotOperatorNFT from 0xefc9bea2fda54f34
7
8access(all) contract HotspotRegistry {
9
10 // Events
11 access(all) event ContractInitialized()
12 access(all) event HotspotRegistered(id: UInt64, owner: Address)
13 access(all) event HotspotStatusChanged(id: UInt64, online: Bool)
14 access(all) event HotspotLocationUpdated(id: UInt64, lat: Fix64, lng: Fix64)
15
16 // Named paths
17 access(all) let AdminStoragePath: StoragePath
18
19 // Registry of all hotspots
20 // Hotspot ID -> Hotspot Struct
21 access(all) var hotspots: {UInt64: Hotspot}
22
23 // Represents a 5G hotspot in the network
24 access(all) struct Hotspot {
25 access(all) let id: UInt64
26 access(all) let owner: Address
27 access(all) var lat: Fix64?
28 access(all) var lng: Fix64?
29 access(all) var online: Bool
30 access(all) var lastUpdated: UFix64
31 access(all) var totalUptime: UFix64
32
33 init(id: UInt64, owner: Address) {
34 self.id = id
35 self.owner = owner
36 self.lat = nil
37 self.lng = nil
38 self.online = true
39 self.lastUpdated = getCurrentBlock().timestamp
40 self.totalUptime = 0.0
41 }
42
43 // Setter methods
44 access(all) fun setLatLng(lat: Fix64, lng: Fix64) {
45 self.lat = lat
46 self.lng = lng
47 }
48
49 access(all) fun setOnlineStatus(online: Bool, currentTime: UFix64) {
50 // If going from online to offline, add to total uptime
51 if self.online && !online {
52 self.totalUptime = self.totalUptime + (currentTime - self.lastUpdated)
53 }
54
55 self.online = online
56 self.lastUpdated = currentTime
57 }
58
59 access(all) fun addUptime(amount: UFix64) {
60 self.totalUptime = self.totalUptime + amount
61 }
62 }
63
64 // Register a new hotspot
65 access(all) fun registerHotspot(owner: Address, hotspotOperatorNFT: @HotspotOperatorNFT.NFT): @HotspotOperatorNFT.NFT {
66
67 let newHotspotID = hotspotOperatorNFT.uuid
68
69 let newHotspot = Hotspot(
70 id: newHotspotID,
71 owner: owner
72 )
73
74 HotspotRegistry.hotspots[newHotspotID] = newHotspot
75
76 emit HotspotRegistered(id: newHotspot.id, owner: owner)
77
78 return <-hotspotOperatorNFT
79 }
80
81 access(all) resource Admin {
82 access(all) fun updateHotspotStatus(id: UInt64, online: Bool) {
83 if var hotspot = &HotspotRegistry.hotspots[id] as &Hotspot? {
84 let now = getCurrentBlock().timestamp
85
86 // Update the hotspot status using the setter method
87 hotspot.setOnlineStatus(online: online, currentTime: now)
88
89 emit HotspotStatusChanged(id: id, online: online)
90 }
91 }
92
93 // Update hotspot location
94 access(all) fun updateHotspotLocation(id: UInt64, lat: Fix64, lng: Fix64) {
95 if var hotspot = &HotspotRegistry.hotspots[id] as &Hotspot? {
96 // Update location using the setter method
97 hotspot.setLatLng(lat: lat, lng: lng)
98
99 emit HotspotLocationUpdated(id: id, lat: lat, lng: lng)
100 }
101 }
102 }
103
104 access(all) view fun getHotspotByID(id: UInt64): Hotspot? {
105 return HotspotRegistry.hotspots[id]
106 }
107
108 access(all) fun getHotspotsByOwner(owner: Address): [Hotspot] {
109 let result: [Hotspot] = []
110
111 for hotspot in HotspotRegistry.hotspots.values {
112 if hotspot.owner == owner {
113 result.append(hotspot)
114 }
115 }
116
117 return result
118 }
119
120 access(all) view fun getAllHotspots(): [Hotspot] {
121 return HotspotRegistry.hotspots.values
122 }
123
124 init() {
125 self.hotspots = {}
126
127 self.AdminStoragePath = /storage/HotspotRegistryAdmin_1
128
129 let admin <- create Admin()
130 self.account.storage.save(<-admin, to: self.AdminStoragePath)
131
132 emit ContractInitialized()
133 }
134
135}