xrpl_common_stdlib/objects/generated/pay_channel.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::amount::Amount;
10use crate::types::blob::PublicKeyBlob;
11use crate::types::uint::Hash256;
12
13/// Trait providing access to fields specific to PayChannel objects in any ledger.
14pub trait PayChannelFields: LedgerObjectCommonFields {
15 /// The source address that owns this payment channel. This comes from the sending address of
16 /// the transaction that created the channel.
17 fn account(&self) -> Result<AccountID> {
18 ledger_object::get_field(self.get_slot_num(), sfield::Account)
19 }
20
21 /// The destination address for this payment channel. While the payment channel is open, this
22 /// address is the only one that can receive XRP from the channel. This comes from the
23 /// `Destination` field of the transaction that created the channel.
24 fn destination(&self) -> Result<AccountID> {
25 ledger_object::get_field(self.get_slot_num(), sfield::Destination)
26 }
27
28 /// The Sequence field (Optional).
29 fn sequence(&self) -> Result<Option<u32>> {
30 ledger_object::get_field_optional(self.get_slot_num(), sfield::Sequence)
31 }
32
33 /// Total XRP, in drops, that have been allocated to this channel. This includes amounts that
34 /// have been paid to the destination address. This is initially set by the transaction that
35 /// created the channel and can be increased if the source address sends a `PaymentChannelFund`
36 /// transaction.
37 fn amount(&self) -> Result<Amount> {
38 ledger_object::get_field(self.get_slot_num(), sfield::Amount)
39 }
40
41 /// Total XRP, in drops already paid out by the channel. The difference between this value and
42 /// the `Amount` field is how much can still be paid to the destination address with
43 /// `PaymentChannelClaim` transactions. If the channel closes, the remaining difference is
44 /// returned to the source address.
45 fn balance(&self) -> Result<Amount> {
46 ledger_object::get_field(self.get_slot_num(), sfield::Balance)
47 }
48
49 /// Public key, in hexadecimal, of the key pair that can be used to sign claims against this
50 /// channel. This can be any valid secp256k1 or Ed25519 public key. This is set by the
51 /// transaction that created the channel and must match the public key used in claims against
52 /// the channel. The channel source address can also send XRP from this channel to the
53 /// destination without signed claims.
54 fn public_key(&self) -> Result<PublicKeyBlob> {
55 ledger_object::get_field(self.get_slot_num(), sfield::PublicKey)
56 }
57
58 /// Number of seconds the source address must wait to close the channel if it still has any XRP
59 /// in it. Smaller values mean that the destination address has less time to redeem any
60 /// outstanding claims after the source address requests to close the channel. Can be any value
61 /// that fits in a 32-bit unsigned integer (0 to 2^32-1). This is set by the transaction that
62 /// creates the channel.
63 fn settle_delay(&self) -> Result<u32> {
64 ledger_object::get_field(self.get_slot_num(), sfield::SettleDelay)
65 }
66
67 /// The mutable expiration time for this payment channel, in seconds since the Ripple Epoch. The
68 /// channel is expired if this value is present and smaller than the previous ledger's
69 /// `close_time` field. See Channel Expiration for more details.
70 fn expiration(&self) -> Result<Option<u32>> {
71 ledger_object::get_field_optional(self.get_slot_num(), sfield::Expiration)
72 }
73
74 /// The immutable expiration time for this payment channel, in seconds since the Ripple Epoch.
75 /// This channel is expired if this value is present and smaller than the previous ledger's
76 /// `close_time` field. This is optionally set by the transaction that created the channel, and
77 /// cannot be changed.
78 fn cancel_after(&self) -> Result<Option<u32>> {
79 ledger_object::get_field_optional(self.get_slot_num(), sfield::CancelAfter)
80 }
81
82 /// An arbitrary tag to further specify the source for this payment channel, such as a hosted
83 /// recipient at the owner's address.
84 fn source_tag(&self) -> Result<Option<u32>> {
85 ledger_object::get_field_optional(self.get_slot_num(), sfield::SourceTag)
86 }
87
88 /// An arbitrary tag to further specify the destination for this payment channel, such as a
89 /// hosted recipient at the destination address.
90 fn destination_tag(&self) -> Result<Option<u32>> {
91 ledger_object::get_field_optional(self.get_slot_num(), sfield::DestinationTag)
92 }
93
94 /// A hint indicating which page of the source address's owner directory links to this entry, in
95 /// case the directory consists of multiple pages.
96 fn owner_node(&self) -> Result<u64> {
97 ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
98 }
99
100 /// The identifying hash of the transaction that most recently modified this entry.
101 fn previous_txn_id(&self) -> Result<Hash256> {
102 ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
103 }
104
105 /// The index of the ledger that contains the transaction that most recently modified this
106 /// entry.
107 fn previous_txn_lgr_seq(&self) -> Result<u32> {
108 ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
109 }
110
111 /// A hint indicating which page of the destination's owner directory links to this entry, in
112 /// case the directory consists of multiple pages. Omitted on payment channels created before
113 /// enabling the fixPayChanRecipientOwnerDir amendment.
114 fn destination_node(&self) -> Result<Option<u64>> {
115 ledger_object::get_field_optional(self.get_slot_num(), sfield::DestinationNode)
116 }
117}
118
119/// Trait providing access to fields specific to the current PayChannel object.
120pub trait CurrentPayChannelFields: CurrentLedgerObjectCommonFields {
121 /// The source address that owns this payment channel. This comes from the sending address of
122 /// the transaction that created the channel.
123 fn account(&self) -> Result<AccountID> {
124 current_ledger_object::get_field(sfield::Account)
125 }
126
127 /// The destination address for this payment channel. While the payment channel is open, this
128 /// address is the only one that can receive XRP from the channel. This comes from the
129 /// `Destination` field of the transaction that created the channel.
130 fn destination(&self) -> Result<AccountID> {
131 current_ledger_object::get_field(sfield::Destination)
132 }
133
134 /// The Sequence field (Optional).
135 fn sequence(&self) -> Result<Option<u32>> {
136 current_ledger_object::get_field_optional(sfield::Sequence)
137 }
138
139 /// Total XRP, in drops, that have been allocated to this channel. This includes amounts that
140 /// have been paid to the destination address. This is initially set by the transaction that
141 /// created the channel and can be increased if the source address sends a `PaymentChannelFund`
142 /// transaction.
143 fn amount(&self) -> Result<Amount> {
144 current_ledger_object::get_field(sfield::Amount)
145 }
146
147 /// Total XRP, in drops already paid out by the channel. The difference between this value and
148 /// the `Amount` field is how much can still be paid to the destination address with
149 /// `PaymentChannelClaim` transactions. If the channel closes, the remaining difference is
150 /// returned to the source address.
151 fn balance(&self) -> Result<Amount> {
152 current_ledger_object::get_field(sfield::Balance)
153 }
154
155 /// Public key, in hexadecimal, of the key pair that can be used to sign claims against this
156 /// channel. This can be any valid secp256k1 or Ed25519 public key. This is set by the
157 /// transaction that created the channel and must match the public key used in claims against
158 /// the channel. The channel source address can also send XRP from this channel to the
159 /// destination without signed claims.
160 fn public_key(&self) -> Result<PublicKeyBlob> {
161 current_ledger_object::get_field(sfield::PublicKey)
162 }
163
164 /// Number of seconds the source address must wait to close the channel if it still has any XRP
165 /// in it. Smaller values mean that the destination address has less time to redeem any
166 /// outstanding claims after the source address requests to close the channel. Can be any value
167 /// that fits in a 32-bit unsigned integer (0 to 2^32-1). This is set by the transaction that
168 /// creates the channel.
169 fn settle_delay(&self) -> Result<u32> {
170 current_ledger_object::get_field(sfield::SettleDelay)
171 }
172
173 /// The mutable expiration time for this payment channel, in seconds since the Ripple Epoch. The
174 /// channel is expired if this value is present and smaller than the previous ledger's
175 /// `close_time` field. See Channel Expiration for more details.
176 fn expiration(&self) -> Result<Option<u32>> {
177 current_ledger_object::get_field_optional(sfield::Expiration)
178 }
179
180 /// The immutable expiration time for this payment channel, in seconds since the Ripple Epoch.
181 /// This channel is expired if this value is present and smaller than the previous ledger's
182 /// `close_time` field. This is optionally set by the transaction that created the channel, and
183 /// cannot be changed.
184 fn cancel_after(&self) -> Result<Option<u32>> {
185 current_ledger_object::get_field_optional(sfield::CancelAfter)
186 }
187
188 /// An arbitrary tag to further specify the source for this payment channel, such as a hosted
189 /// recipient at the owner's address.
190 fn source_tag(&self) -> Result<Option<u32>> {
191 current_ledger_object::get_field_optional(sfield::SourceTag)
192 }
193
194 /// An arbitrary tag to further specify the destination for this payment channel, such as a
195 /// hosted recipient at the destination address.
196 fn destination_tag(&self) -> Result<Option<u32>> {
197 current_ledger_object::get_field_optional(sfield::DestinationTag)
198 }
199
200 /// A hint indicating which page of the source address's owner directory links to this entry, in
201 /// case the directory consists of multiple pages.
202 fn owner_node(&self) -> Result<u64> {
203 current_ledger_object::get_field(sfield::OwnerNode)
204 }
205
206 /// The identifying hash of the transaction that most recently modified this entry.
207 fn previous_txn_id(&self) -> Result<Hash256> {
208 current_ledger_object::get_field(sfield::PreviousTxnID)
209 }
210
211 /// The index of the ledger that contains the transaction that most recently modified this
212 /// entry.
213 fn previous_txn_lgr_seq(&self) -> Result<u32> {
214 current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
215 }
216
217 /// A hint indicating which page of the destination's owner directory links to this entry, in
218 /// case the directory consists of multiple pages. Omitted on payment channels created before
219 /// enabling the fixPayChanRecipientOwnerDir amendment.
220 fn destination_node(&self) -> Result<Option<u64>> {
221 current_ledger_object::get_field_optional(sfield::DestinationNode)
222 }
223}
224
225#[derive(Debug, Clone, Copy, Eq, PartialEq)]
226pub struct PayChannel {
227 pub(crate) slot_num: i32,
228}
229
230impl PayChannel {
231 /// Binds this handle to a host-managed slot holding a PayChannel ledger object.
232 pub fn new(slot_num: i32) -> Self {
233 Self { slot_num }
234 }
235}
236
237impl LedgerObjectCommonFields for PayChannel {
238 fn get_slot_num(&self) -> i32 {
239 self.slot_num
240 }
241}
242
243impl PayChannelFields for PayChannel {}
244
245#[cfg(test)]
246mod tests {
247 use super::*;
248 use crate::host::host_bindings_trait::MockHostBindings;
249 use crate::host::setup_mock;
250 use crate::objects::test_utils::*;
251
252 #[test]
253 fn read_all_fields() {
254 let mut mock = MockHostBindings::new();
255 mock_all_fields_present(&mut mock);
256 let _guard = setup_mock(mock);
257
258 let obj = PayChannel::new(0);
259
260 assert!(obj.account().is_ok());
261 assert!(obj.destination().is_ok());
262 assert!(obj.amount().is_ok());
263 assert!(obj.balance().is_ok());
264 assert!(obj.public_key().is_ok());
265 assert!(obj.settle_delay().is_ok());
266 assert!(obj.owner_node().is_ok());
267 assert!(obj.previous_txn_id().is_ok());
268 assert!(obj.previous_txn_lgr_seq().is_ok());
269 assert!(obj.sequence().is_ok());
270 assert!(obj.expiration().is_ok());
271 assert!(obj.cancel_after().is_ok());
272 assert!(obj.source_tag().is_ok());
273 assert!(obj.destination_tag().is_ok());
274 assert!(obj.destination_node().is_ok());
275 }
276
277 #[test]
278 fn optional_fields_none() {
279 let mut mock = MockHostBindings::new();
280 mock_all_fields_not_found(&mut mock);
281 let _guard = setup_mock(mock);
282
283 let obj = PayChannel::new(0);
284
285 assert!(obj.sequence().unwrap().is_none());
286 assert!(obj.expiration().unwrap().is_none());
287 assert!(obj.cancel_after().unwrap().is_none());
288 assert!(obj.source_tag().unwrap().is_none());
289 assert!(obj.destination_tag().unwrap().is_none());
290 assert!(obj.destination_node().unwrap().is_none());
291 }
292}