You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package::claim_and_keep(otw, ctx); // Claim ownership of the one-time witness and keep it
36
+
37
+
let cap = AdminCap { id: object::new(ctx) }; // Create a new admin capability object
38
+
transfer::share_object(WeatherOracle {
39
+
id: object::new(ctx),
40
+
address: tx_context::sender(ctx),
41
+
name: string::utf8(b"SuiMeteo"),
42
+
description: string::utf8(b"A weather oracle for posting weather updates (temperature, pressure, humidity, visibility, wind metrics and cloud state) for major cities around the world. Currently the data is fetched from https://openweathermap.org. SuiMeteo provides the best available information, but it does not guarantee its accuracy, completeness, reliability, suitability, or availability. Use it at your own risk and discretion."),
43
+
});
44
+
transfer::public_transfer(cap, tx_context::sender(ctx)); // Transfer the admin capability to the sender.
45
+
}
46
+
47
+
// Public function for adding a new city to the oracle
48
+
publicfunadd_city(
49
+
_: &AdminCap, // The admin capability
50
+
oracle: &mutWeatherOracle, // A mutable reference to the oracle object
51
+
geoname_id: u32, // The unique identifier of the city
52
+
name: String, // The name of the city
53
+
country: String, // The country of the city
54
+
ctx: &mutTxContext// A mutable reference to the transaction context
55
+
) {
56
+
dof::add(&mut oracle.id, geoname_id, // Add a new dynamic object field to the oracle object with the geoname ID as the key and a new city weather oracle object as the value.
57
+
CityWeatherOracle {
58
+
id: object::new(ctx), // Assign a unique ID to the city weather oracle object
59
+
geoname_id, // Set the geoname ID of the city weather oracle object
60
+
name, // Set the name of the city weather oracle object
61
+
country, // Set the country of the city weather oracle object
62
+
pressure: 0, // Initialize the pressure to be zero
63
+
}
64
+
);
65
+
}
66
+
67
+
// Public function for removing an existing city from the oracle
// Public function for updating the weather conditions of a city
74
+
publicfunupdate(
75
+
_: &AdminCap,
76
+
oracle: &mutWeatherOracle,
77
+
geoname_id: u32,
78
+
pressure: u32,
79
+
) {
80
+
let city_weather_oracle_mut = dof::borrow_mut<u32, CityWeatherOracle>(&mut oracle.id, geoname_id); // Borrow a mutable reference to the city weather oracle object with the geoname ID as the key
81
+
city_weather_oracle_mut.pressure = pressure;
82
+
}
83
+
84
+
/// Returns the `pressure` of the `CityWeatherOracle` with the given `geoname_id`.
85
+
publicfuncity_weather_oracle_pressure(
86
+
weather_oracle: &WeatherOracle,
87
+
geoname_id: u32
88
+
): u32 {
89
+
let city_weather_oracle = dof::borrow<u32, CityWeatherOracle>(&weather_oracle.id, geoname_id);
90
+
city_weather_oracle.pressure
91
+
}
92
+
93
+
// This function updates the name of a weather oracle contract.
94
+
// It takes an admin capability, a mutable reference to the weather oracle, and a new name as arguments.
95
+
// It assigns the new name to the weather oracle's name field.
0 commit comments