Skip to main content

xrpl_common_stdlib/objects/generated/
oracle.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3use crate::host::Result;
4use crate::objects::traits::CurrentLedgerObjectCommonFields;
5use crate::objects::traits::LedgerObjectCommonFields;
6use crate::objects::{current_ledger_object, ledger_object};
7use crate::sfield;
8use crate::types::account_id::AccountID;
9use crate::types::blob::{StandardBlob, UriBlob};
10use crate::types::uint::Hash256;
11
12/// Trait providing access to fields specific to Oracle objects in any ledger.
13pub trait OracleFields: LedgerObjectCommonFields {
14    /// The account with update and delete privileges for the oracle. It's recommended to set up
15    /// multi-signing on this account.
16    fn owner(&self) -> Result<AccountID> {
17        ledger_object::get_field(self.get_slot_num(), sfield::Owner)
18    }
19
20    /// The OracleDocumentID field (Optional).
21    fn oracle_document_id(&self) -> Result<Option<u32>> {
22        ledger_object::get_field_optional(self.get_slot_num(), sfield::OracleDocumentID)
23    }
24
25    /// An arbitrary value that identifies an oracle provider, such as Chainlink, Band, or DIA. This
26    /// field is a string, up to 256 ASCII hex encoded characters (`0x20`-`0x7E`).
27    fn provider(&self) -> Result<StandardBlob> {
28        ledger_object::get_field(self.get_slot_num(), sfield::Provider)
29    }
30
31    /// Arbitrary string to describe the type of asset, such as _currency_, _commodity_, or _index_.
32    /// Must be formatted as hexadecimal representing ASCII characters (`0x20`-`0x7E`), maximum 16
33    /// bytes.
34    fn asset_class(&self) -> Result<StandardBlob> {
35        ledger_object::get_field(self.get_slot_num(), sfield::AssetClass)
36    }
37
38    /// The time the data was last updated, represented in Unix time. (Note: Unlike many other time
39    /// values on the XRP Ledger, this value does not use the Ripple Epoch.)
40    fn last_update_time(&self) -> Result<u32> {
41        ledger_object::get_field(self.get_slot_num(), sfield::LastUpdateTime)
42    }
43
44    /// An optional Universal Resource Identifier to reference price data off-chain. This field is
45    /// limited to 256 bytes.
46    fn uri(&self) -> Result<Option<UriBlob>> {
47        ledger_object::get_field_optional(self.get_slot_num(), sfield::URI)
48    }
49
50    /// A hint indicating which page of the oracle owner's owner directory links to this entry, in
51    /// case the directory consists of multiple pages.
52    fn owner_node(&self) -> Result<u64> {
53        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
54    }
55
56    /// The hash of the previous transaction that modified this entry.
57    fn previous_txn_id(&self) -> Result<Hash256> {
58        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
59    }
60
61    /// The ledger index that this object was most recently modified or created in.
62    fn previous_txn_lgr_seq(&self) -> Result<u32> {
63        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
64    }
65}
66
67/// Trait providing access to fields specific to the current Oracle object.
68pub trait CurrentOracleFields: CurrentLedgerObjectCommonFields {
69    /// The account with update and delete privileges for the oracle. It's recommended to set up
70    /// multi-signing on this account.
71    fn owner(&self) -> Result<AccountID> {
72        current_ledger_object::get_field(sfield::Owner)
73    }
74
75    /// The OracleDocumentID field (Optional).
76    fn oracle_document_id(&self) -> Result<Option<u32>> {
77        current_ledger_object::get_field_optional(sfield::OracleDocumentID)
78    }
79
80    /// An arbitrary value that identifies an oracle provider, such as Chainlink, Band, or DIA. This
81    /// field is a string, up to 256 ASCII hex encoded characters (`0x20`-`0x7E`).
82    fn provider(&self) -> Result<StandardBlob> {
83        current_ledger_object::get_field(sfield::Provider)
84    }
85
86    /// Arbitrary string to describe the type of asset, such as _currency_, _commodity_, or _index_.
87    /// Must be formatted as hexadecimal representing ASCII characters (`0x20`-`0x7E`), maximum 16
88    /// bytes.
89    fn asset_class(&self) -> Result<StandardBlob> {
90        current_ledger_object::get_field(sfield::AssetClass)
91    }
92
93    /// The time the data was last updated, represented in Unix time. (Note: Unlike many other time
94    /// values on the XRP Ledger, this value does not use the Ripple Epoch.)
95    fn last_update_time(&self) -> Result<u32> {
96        current_ledger_object::get_field(sfield::LastUpdateTime)
97    }
98
99    /// An optional Universal Resource Identifier to reference price data off-chain. This field is
100    /// limited to 256 bytes.
101    fn uri(&self) -> Result<Option<UriBlob>> {
102        current_ledger_object::get_field_optional(sfield::URI)
103    }
104
105    /// A hint indicating which page of the oracle owner's owner directory links to this entry, in
106    /// case the directory consists of multiple pages.
107    fn owner_node(&self) -> Result<u64> {
108        current_ledger_object::get_field(sfield::OwnerNode)
109    }
110
111    /// The hash of the previous transaction that modified this entry.
112    fn previous_txn_id(&self) -> Result<Hash256> {
113        current_ledger_object::get_field(sfield::PreviousTxnID)
114    }
115
116    /// The ledger index that this object was most recently modified or created in.
117    fn previous_txn_lgr_seq(&self) -> Result<u32> {
118        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
119    }
120}
121
122#[derive(Debug, Clone, Copy, Eq, PartialEq)]
123pub struct Oracle {
124    pub(crate) slot_num: i32,
125}
126
127impl Oracle {
128    /// Binds this handle to a host-managed slot holding an Oracle ledger object.
129    pub fn new(slot_num: i32) -> Self {
130        Self { slot_num }
131    }
132}
133
134impl LedgerObjectCommonFields for Oracle {
135    fn get_slot_num(&self) -> i32 {
136        self.slot_num
137    }
138}
139
140impl OracleFields for Oracle {}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145    use crate::host::host_bindings_trait::MockHostBindings;
146    use crate::host::setup_mock;
147    use crate::objects::test_utils::*;
148
149    #[test]
150    fn read_all_fields() {
151        let mut mock = MockHostBindings::new();
152        mock_all_fields_present(&mut mock);
153        let _guard = setup_mock(mock);
154
155        let obj = Oracle::new(0);
156
157        assert!(obj.owner().is_ok());
158        assert!(obj.provider().is_ok());
159        assert!(obj.asset_class().is_ok());
160        assert!(obj.last_update_time().is_ok());
161        assert!(obj.owner_node().is_ok());
162        assert!(obj.previous_txn_id().is_ok());
163        assert!(obj.previous_txn_lgr_seq().is_ok());
164        assert!(obj.oracle_document_id().is_ok());
165        assert!(obj.uri().is_ok());
166    }
167
168    #[test]
169    fn optional_fields_none() {
170        let mut mock = MockHostBindings::new();
171        mock_all_fields_not_found(&mut mock);
172        let _guard = setup_mock(mock);
173
174        let obj = Oracle::new(0);
175
176        assert!(obj.oracle_document_id().unwrap().is_none());
177    }
178}