Skip to content

Commit e9ae751

Browse files
committed
add simple_weather_oracle code
1 parent 3c334e6 commit e9ae751

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# @generated by Move, please check-in and do not edit manually.
2+
3+
[move]
4+
version = 2
5+
manifest_digest = "BF831D32EE16AD916FC6343DB6E78FF816EA86F39D18A80EDEBFAC01B76CC321"
6+
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
7+
dependencies = [
8+
{ name = "Sui" },
9+
]
10+
11+
[[move.package]]
12+
name = "MoveStdlib"
13+
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" }
14+
15+
[[move.package]]
16+
name = "Sui"
17+
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }
18+
19+
dependencies = [
20+
{ name = "MoveStdlib" },
21+
]
22+
23+
[move.toolchain-version]
24+
compiler-version = "1.33.0"
25+
edition = "2024.beta"
26+
flavor = "sui"
27+
28+
[env]
29+
30+
[env.devnet]
31+
chain-id = "6b9b3fe9"
32+
original-published-id = "0x95706c5b5adac5541bcb4398996f9d3662f0350e67703b22aa5f894b498d34df"
33+
latest-published-id = "0x95706c5b5adac5541bcb4398996f9d3662f0350e67703b22aa5f894b498d34df"
34+
published-version = "1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "simple_weather_oracle"
3+
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
4+
version = "1.0.0"
5+
published-at = "0x95706c5b5adac5541bcb4398996f9d3662f0350e67703b22aa5f894b498d34df"
6+
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
7+
# authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]
8+
9+
[dependencies]
10+
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }
11+
12+
# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
13+
# Revision can be a branch, a tag, and a commit hash.
14+
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }
15+
16+
# For local dependencies use `local = path`. Path is relative to the package root
17+
# Local = { local = "../path/to" }
18+
19+
# To resolve a version conflict and force a specific version for dependency
20+
# override use `override = true`
21+
# Override = { local = "../conflicting/version", override = true }
22+
23+
[addresses]
24+
oracle = "0x95706c5b5adac5541bcb4398996f9d3662f0350e67703b22aa5f894b498d34df"
25+
26+
# Named addresses will be accessible in Move as `@name`. They're also exported:
27+
# for example, `std = "0x1"` is exported by the Standard Library.
28+
# alice = "0xA11CE"
29+
30+
[dev-dependencies]
31+
# The dev-dependencies section allows overriding dependencies for `--test` and
32+
# `--dev` modes. You can introduce test-only dependencies here.
33+
# Local = { local = "../path/to/dev-build" }
34+
35+
[dev-addresses]
36+
# The dev-addresses section allows overwriting named addresses for the `--test`
37+
# and `--dev` modes.
38+
# alice = "0xB0B"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
module oracle::simple_weather {
2+
use std::string::{Self, String};
3+
use sui::dynamic_object_field as dof;
4+
use sui::package;
5+
6+
/// Define a capability for the admin of the oracle.
7+
public struct AdminCap has key, store { id: UID }
8+
9+
/// // Define a one-time witness to create the `Publisher` of the oracle.
10+
public struct SIMPLE_WEATHER has drop {}
11+
12+
// Define a struct for the weather oracle
13+
public struct WeatherOracle has key {
14+
id: UID,
15+
/// The address of the oracle.
16+
address: address,
17+
/// The name of the oracle.
18+
name: String,
19+
/// The description of the oracle.
20+
description: String,
21+
}
22+
23+
// Define a struct for each city that the oracle covers
24+
public struct CityWeatherOracle has key, store {
25+
id: UID,
26+
geoname_id: u32, // The unique identifier of the city
27+
name: String, // The name of the city
28+
country: String, // The country of the city
29+
pressure: u32, // The atmospheric pressure in hPa
30+
// ...
31+
}
32+
33+
/// Module initializer. Uses One Time Witness to create Publisher and transfer it to sender.
34+
fun init(otw: SIMPLE_WEATHER, ctx: &mut TxContext) {
35+
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+
public fun add_city(
49+
_: &AdminCap, // The admin capability
50+
oracle: &mut WeatherOracle, // 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: &mut TxContext // 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
68+
public fun remove_city(_: &AdminCap, oracle: &mut WeatherOracle, geoname_id: u32) {
69+
let CityWeatherOracle { id, geoname_id: _, name: _, country: _, pressure: _} = dof::remove(&mut oracle.id, geoname_id);
70+
object::delete(id);
71+
}
72+
73+
// Public function for updating the weather conditions of a city
74+
public fun update(
75+
_: &AdminCap,
76+
oracle: &mut WeatherOracle,
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+
public fun city_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.
96+
public fun update_name(_: &AdminCap, weather_oracle: &mut WeatherOracle, name: String) {
97+
weather_oracle.name = name;
98+
}
99+
100+
// This function updates the description of a weather oracle contract.
101+
// It takes an admin capability, a mutable reference to the weather oracle, and a new description as arguments.
102+
// It assigns the new description to the weather oracle's description field.
103+
public fun update_description(_: &AdminCap, weather_oracle: &mut WeatherOracle, description: String) {
104+
weather_oracle.description = description;
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
#[test_only]
3+
module simple_weather_oracle::simple_weather_oracle_tests {
4+
// uncomment this line to import the module
5+
// use simple_weather_oracle::simple_weather_oracle;
6+
7+
const ENotImplemented: u64 = 0;
8+
9+
#[test]
10+
fun test_simple_weather_oracle() {
11+
// pass
12+
}
13+
14+
#[test, expected_failure(abort_code = ::simple_weather_oracle::simple_weather_oracle_tests::ENotImplemented)]
15+
fun test_simple_weather_oracle_fail() {
16+
abort ENotImplemented
17+
}
18+
}
19+
*/

0 commit comments

Comments
 (0)