1+ //! Token payment metadata and helpers.
2+ //!
3+ //! This module defines the versioned `TokenPaymentInfo` wrapper used to describe how a
4+ //! client intends to pay with tokens for an operation (for example, creating,
5+ //! transferring, purchasing, or updating the price of a document/NFT).
6+ //! It captures which token to use, optional price bounds, and who covers gas fees.
7+ //!
8+ //! The enum is versioned to allow future evolution without breaking callers. The
9+ //! current implementation is [`v0::TokenPaymentInfoV0`]. Accessors are provided via
10+ //! [`v0::v0_accessors::TokenPaymentInfoAccessorsV0`], and convenience methods (such
11+ //! as `token_id()` and `is_valid_for_required_cost()`) are available through
12+ //! [`methods::v0::TokenPaymentInfoMethodsV0`].
13+ //!
14+ //! Typical usage:
15+ //!
16+ //! ```ignore
17+ //! use dpp::tokens::gas_fees_paid_by::GasFeesPaidBy;
18+ //! use dpp::data_contract::TokenContractPosition;
19+ //! use dpp::tokens::token_payment_info::{TokenPaymentInfo, v0::TokenPaymentInfoV0};
20+ //!
21+ //! // Client indicates payment preferences for a transition
22+ //! let info: TokenPaymentInfo = TokenPaymentInfoV0 {
23+ //! // `None` => use a token defined on the current contract
24+ //! payment_token_contract_id: None,
25+ //! // Which token (by position/index) on the contract to use
26+ //! token_contract_position: 0u16,
27+ //! // Optional bounds to guard against unexpected price changes
28+ //! minimum_token_cost: None,
29+ //! maximum_token_cost: Some(1_000u64.into()),
30+ //! // Who pays gas: user, contract owner, or prefer contract owner
31+ //! gas_fees_paid_by: GasFeesPaidBy::DocumentOwner,
32+ //! }.into();
33+ //! ```
34+ //!
35+ //! Deserialization from a platform `BTreeMap<String, Value>` requires a
36+ //! `$format_version` key. For V0 the map may contain:
37+ //! - `paymentTokenContractId` (`Identifier` as bytes)
38+ //! - `tokenContractPosition` (`u16`)
39+ //! - `minimumTokenCost` (`u64`)
40+ //! - `maximumTokenCost` (`u64`)
41+ //! - `gasFeesPaidBy` (one of: `"DocumentOwner"`, `"ContractOwner"`, `"PreferContractOwner"`)
42+ //!
43+ //! Unknown `$format_version` values yield an `UnknownVersionMismatch` error.
44+ //!
145use crate :: balances:: credits:: TokenAmount ;
246use crate :: data_contract:: TokenContractPosition ;
347use crate :: tokens:: gas_fees_paid_by:: GasFeesPaidBy ;
@@ -47,6 +91,15 @@ pub mod v0;
4791 ) ,
4892 derive( Serialize , Deserialize )
4993) ]
94+ /// Versioned container describing how a client intends to pay with tokens.
95+ ///
96+ /// The `TokenPaymentInfo` enum allows the protocol to evolve the underlying structure
97+ /// across versions while keeping a stable API for callers. Use the accessor trait
98+ /// [`v0::v0_accessors::TokenPaymentInfoAccessorsV0`] to read or update fields, and
99+ /// [`methods::v0::TokenPaymentInfoMethodsV0`] for helpers like `token_id()` and
100+ /// `is_valid_for_required_cost()`.
101+ ///
102+ /// See [`v0::TokenPaymentInfoV0`] for the current set of fields and semantics.
50103pub enum TokenPaymentInfo {
51104 #[ display( "V0({})" , "_0" ) ]
52105 V0 ( TokenPaymentInfoV0 ) ,
@@ -128,6 +181,9 @@ impl TryFrom<BTreeMap<String, Value>> for TokenPaymentInfo {
128181 type Error = ProtocolError ;
129182
130183 fn try_from ( map : BTreeMap < String , Value > ) -> Result < Self , Self :: Error > {
184+ // Expect a `$format_version` discriminator and dispatch to the
185+ // corresponding versioned structure. This allows backward-compatible
186+ // support for older serialized payloads.
131187 let format_version = map. get_str ( "$format_version" ) ?;
132188 match format_version {
133189 "0" => {
@@ -149,6 +205,10 @@ impl TryFrom<BTreeMap<String, Value>> for TokenPaymentInfo {
149205#[ cfg( feature = "state-transition-value-conversion" ) ]
150206impl TryFrom < TokenPaymentInfo > for Value {
151207 type Error = Error ;
208+ /// Serialize the versioned token payment info into a platform `Value`.
209+ ///
210+ /// This mirrors the map format accepted by `TryFrom<BTreeMap<String, Value>>`,
211+ /// including the `$format_version` discriminator.
152212 fn try_from ( value : TokenPaymentInfo ) -> Result < Self , Self :: Error > {
153213 platform_value:: to_value ( value)
154214 }
0 commit comments