af_oracle/
lib.rs

1#![cfg_attr(all(doc, not(doctest)), feature(doc_auto_cfg))]
2
3//! Move types for Aftermath's `AfOracle` package
4
5use af_sui_types::Address;
6use af_utilities::types::IFixed;
7use sui_framework_sdk::object::{ID, UID};
8use sui_framework_sdk::{Field, FieldTypeTag};
9
10pub mod errors;
11pub mod event_ext;
12pub mod event_instance;
13#[cfg(feature = "graphql")]
14pub mod graphql;
15
16// Main package types
17pub use self::oracle::{PriceFeed, PriceFeedStorage, PriceFeedStorageTypeTag, PriceFeedTypeTag};
18
19/// Dynamic field storing a [`PriceFeed`].
20pub type PriceFeedDf = Field<keys::PriceFeedForSource, PriceFeed>;
21
22af_sui_pkg_sdk::sui_pkg_sdk!(af_oracle {
23    module oracle {
24        // =========================================================================
25        //  Objects
26        // =========================================================================
27
28        /// Capability object required to create/remove price feeds.
29        ///
30        /// Created as a single-writer object, unique.
31        struct AuthorityCap has key, store {
32            id: UID,
33        }
34
35        /// Object that stores all the price feeds for this aggregation.
36        struct PriceFeedStorage has key, store {
37            id: UID,
38            /// Symbol for this price feed storage
39            symbol: String,
40            /// Amount of decimals for this price feed storage.
41            decimals: u64,
42            /// List of source wrapper ids having a feed in this storage.
43            /// Each ID is used as key for the dynamic field containing the price feed
44            source_wrapper_ids: vector<ID>,
45        }
46
47        /// Object that identifies a price feed for a single source.
48        struct PriceFeed has key, store {
49            id: UID,
50            /// Price value.
51            price: IFixed,
52            /// Price timestamp.
53            timestamp: u64,
54            /// Allowed gap between current
55            /// and provided timestamp.
56            time_tolerance: u64,
57        }
58
59        /// Signals a package's capability to act as a price feed source.
60        struct SourceCap has store {}
61    }
62
63    module keys {
64        /// Key type for accessing price feed for particular source.
65        struct PriceFeedForSource has copy, drop, store {
66            source_wrapper_id: ID,
67        }
68
69        /// For attaching a capability to an app.
70        ///
71        /// This ensures only this package has access to this dynamic field since the key can only be
72        /// constructed here.
73        struct Authorization has copy, drop, store {}
74    }
75
76    module events {
77        struct CreatedPriceFeedStorage has copy, drop {
78            price_feed_storage_id: ID,
79            symbol: String,
80            decimals: u64
81        }
82
83        struct AddedAuthorization has copy, drop {
84            source_wrapper_id: ID,
85        }
86
87        struct RemovedAuthorization has copy, drop {
88            source_wrapper_id: ID,
89        }
90
91        struct CreatedPriceFeed has copy, drop {
92            price_feed_storage_id: ID,
93            source_wrapper_id: ID,
94            price: IFixed,
95            timestamp: u64,
96            time_tolerance: u64,
97        }
98
99        struct RemovedPriceFeed has copy, drop {
100            price_feed_storage_id: ID,
101            source_wrapper_id: ID,
102        }
103
104        struct UpdatedPriceFeed has copy, drop {
105            price_feed_storage_id: ID,
106            source_wrapper_id: ID,
107            old_price: IFixed,
108            old_timestamp: u64,
109            new_price: IFixed,
110            new_timestamp: u64,
111        }
112
113        struct UpdatedPriceFeedTimeTolerance has copy, drop {
114            price_feed_storage_id: ID,
115            source_wrapper_id: ID,
116            old_time_tolerance: u64,
117            new_time_tolerance: u64,
118        }
119    }
120});
121
122impl PriceFeedStorage {
123    /// Convenience function to build the type of a [`PriceFeedDf`].
124    pub fn price_feed_df_type(
125        package: Address,
126    ) -> FieldTypeTag<self::keys::PriceFeedForSource, PriceFeed> {
127        Field::type_(
128            self::keys::PriceFeedForSource::type_(package),
129            PriceFeed::type_(package),
130        )
131    }
132}