Skip to main content

xrpl_common_stdlib/objects/generated/
bridge.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3/// Placeholder buffer size for fields whose XRPL wire type has no genuine Rust
4/// mapping yet (VECTOR256, XCHAIN_BRIDGE, PATHSET, ...). Such getters return
5/// raw, unparsed bytes; see the summary at the top of `generated/mod.rs`.
6const RAW_UNMAPPED_FIELD_SIZE: usize = 512;
7
8use crate::host::Result;
9use crate::host::error_codes::match_result_code;
10use crate::host::home_le_field;
11use crate::host::le_field;
12use crate::objects::traits::CurrentLedgerObjectCommonFields;
13use crate::objects::traits::LedgerObjectCommonFields;
14use crate::objects::{current_ledger_object, ledger_object};
15use crate::sfield;
16use crate::types::account_id::AccountID;
17use crate::types::amount::Amount;
18use crate::types::uint::Hash256;
19
20/// Trait providing access to fields specific to Bridge objects in any ledger.
21pub trait BridgeFields: LedgerObjectCommonFields {
22    /// The account that submitted the `XChainCreateBridge` transaction on the blockchain.
23    fn account(&self) -> Result<AccountID> {
24        ledger_object::get_field(self.get_slot_num(), sfield::Account)
25    }
26
27    /// The total amount, in XRP, to be rewarded for providing a signature for cross-chain transfer
28    /// or for signing for the cross-chain reward. This amount will be split among the signers.
29    fn signature_reward(&self) -> Result<Amount> {
30        ledger_object::get_field(self.get_slot_num(), sfield::SignatureReward)
31    }
32
33    /// The minimum amount, in XRP, required for an `XChainAccountCreateCommit` transaction. If this
34    /// isn't present, the `XChainAccountCreateCommit` transaction will fail. This field can only be
35    /// present on XRP-XRP bridges.
36    fn min_account_create_amount(&self) -> Result<Option<Amount>> {
37        ledger_object::get_field_optional(self.get_slot_num(), sfield::MinAccountCreateAmount)
38    }
39
40    /// The door accounts and assets of the bridge this object correlates to.
41    /// Raw bytes; XCHAIN_BRIDGE is not yet typed in Rust.
42    fn xchain_bridge(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
43        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
44        let result_code = unsafe {
45            le_field(
46                self.get_slot_num(),
47                sfield::XChainBridge.into(),
48                buffer.as_mut_ptr(),
49                buffer.len(),
50            )
51        };
52        match_result_code(result_code, || buffer)
53    }
54
55    /// The value of the next `XChainClaimID` to be created.
56    fn xchain_claim_id(&self) -> Result<u64> {
57        ledger_object::get_field(self.get_slot_num(), sfield::XChainClaimID)
58    }
59
60    /// A counter used to order the execution of account create transactions. It is incremented
61    /// every time a successful `XChainAccountCreateCommit` transaction is run for the source chain.
62    fn xchain_account_create_count(&self) -> Result<u64> {
63        ledger_object::get_field(self.get_slot_num(), sfield::XChainAccountCreateCount)
64    }
65
66    /// A counter used to order the execution of account create transactions. It is incremented
67    /// every time a `XChainAccountCreateCommit` transaction is "claimed" on the destination chain.
68    /// When the "claim" transaction is run on the destination chain, the `XChainAccountClaimCount`
69    /// must match the value that the `XChainAccountCreateCount` had at the time the
70    /// `XChainAccountClaimCount` was run on the source chain. This orders the claims so that they
71    /// run in the same order that the `XChainAccountCreateCommit` transactions ran on the source
72    /// chain, to prevent transaction replay.
73    fn xchain_account_claim_count(&self) -> Result<u64> {
74        ledger_object::get_field(self.get_slot_num(), sfield::XChainAccountClaimCount)
75    }
76
77    /// The OwnerNode field (Required).
78    fn owner_node(&self) -> Result<u64> {
79        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
80    }
81
82    /// The PreviousTxnID field (Required).
83    fn previous_txn_id(&self) -> Result<Hash256> {
84        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
85    }
86
87    /// The PreviousTxnLgrSeq field (Required).
88    fn previous_txn_lgr_seq(&self) -> Result<u32> {
89        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
90    }
91}
92
93/// Trait providing access to fields specific to the current Bridge object.
94pub trait CurrentBridgeFields: CurrentLedgerObjectCommonFields {
95    /// The account that submitted the `XChainCreateBridge` transaction on the blockchain.
96    fn account(&self) -> Result<AccountID> {
97        current_ledger_object::get_field(sfield::Account)
98    }
99
100    /// The total amount, in XRP, to be rewarded for providing a signature for cross-chain transfer
101    /// or for signing for the cross-chain reward. This amount will be split among the signers.
102    fn signature_reward(&self) -> Result<Amount> {
103        current_ledger_object::get_field(sfield::SignatureReward)
104    }
105
106    /// The minimum amount, in XRP, required for an `XChainAccountCreateCommit` transaction. If this
107    /// isn't present, the `XChainAccountCreateCommit` transaction will fail. This field can only be
108    /// present on XRP-XRP bridges.
109    fn min_account_create_amount(&self) -> Result<Option<Amount>> {
110        current_ledger_object::get_field_optional(sfield::MinAccountCreateAmount)
111    }
112
113    /// The door accounts and assets of the bridge this object correlates to.
114    /// Raw bytes; XCHAIN_BRIDGE is not yet typed in Rust.
115    fn xchain_bridge(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
116        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
117        let result_code = unsafe {
118            home_le_field(
119                sfield::XChainBridge.into(),
120                buffer.as_mut_ptr(),
121                buffer.len(),
122            )
123        };
124        match_result_code(result_code, || buffer)
125    }
126
127    /// The value of the next `XChainClaimID` to be created.
128    fn xchain_claim_id(&self) -> Result<u64> {
129        current_ledger_object::get_field(sfield::XChainClaimID)
130    }
131
132    /// A counter used to order the execution of account create transactions. It is incremented
133    /// every time a successful `XChainAccountCreateCommit` transaction is run for the source chain.
134    fn xchain_account_create_count(&self) -> Result<u64> {
135        current_ledger_object::get_field(sfield::XChainAccountCreateCount)
136    }
137
138    /// A counter used to order the execution of account create transactions. It is incremented
139    /// every time a `XChainAccountCreateCommit` transaction is "claimed" on the destination chain.
140    /// When the "claim" transaction is run on the destination chain, the `XChainAccountClaimCount`
141    /// must match the value that the `XChainAccountCreateCount` had at the time the
142    /// `XChainAccountClaimCount` was run on the source chain. This orders the claims so that they
143    /// run in the same order that the `XChainAccountCreateCommit` transactions ran on the source
144    /// chain, to prevent transaction replay.
145    fn xchain_account_claim_count(&self) -> Result<u64> {
146        current_ledger_object::get_field(sfield::XChainAccountClaimCount)
147    }
148
149    /// The OwnerNode field (Required).
150    fn owner_node(&self) -> Result<u64> {
151        current_ledger_object::get_field(sfield::OwnerNode)
152    }
153
154    /// The PreviousTxnID field (Required).
155    fn previous_txn_id(&self) -> Result<Hash256> {
156        current_ledger_object::get_field(sfield::PreviousTxnID)
157    }
158
159    /// The PreviousTxnLgrSeq field (Required).
160    fn previous_txn_lgr_seq(&self) -> Result<u32> {
161        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
162    }
163}
164
165#[derive(Debug, Clone, Copy, Eq, PartialEq)]
166pub struct Bridge {
167    pub(crate) slot_num: i32,
168}
169
170impl Bridge {
171    /// Binds this handle to a host-managed slot holding a Bridge ledger object.
172    pub fn new(slot_num: i32) -> Self {
173        Self { slot_num }
174    }
175}
176
177impl LedgerObjectCommonFields for Bridge {
178    fn get_slot_num(&self) -> i32 {
179        self.slot_num
180    }
181}
182
183impl BridgeFields for Bridge {}
184
185#[cfg(test)]
186mod tests {
187    use super::*;
188    use crate::host::host_bindings_trait::MockHostBindings;
189    use crate::host::setup_mock;
190    use crate::objects::test_utils::*;
191
192    #[test]
193    fn read_all_fields() {
194        let mut mock = MockHostBindings::new();
195        mock_all_fields_present(&mut mock);
196        let _guard = setup_mock(mock);
197
198        let obj = Bridge::new(0);
199
200        assert!(obj.account().is_ok());
201        assert!(obj.signature_reward().is_ok());
202        assert!(obj.xchain_bridge().is_ok());
203        assert!(obj.xchain_claim_id().is_ok());
204        assert!(obj.xchain_account_create_count().is_ok());
205        assert!(obj.xchain_account_claim_count().is_ok());
206        assert!(obj.owner_node().is_ok());
207        assert!(obj.previous_txn_id().is_ok());
208        assert!(obj.previous_txn_lgr_seq().is_ok());
209        assert!(obj.min_account_create_amount().is_ok());
210    }
211}