Skip to main content

dpp/tokens/token_payment_info/v0/
mod.rs

1pub mod v0_accessors;
2
3use crate::balances::credits::TokenAmount;
4use crate::data_contract::TokenContractPosition;
5use crate::tokens::gas_fees_paid_by::GasFeesPaidBy;
6use crate::tokens::token_payment_info::v0::v0_accessors::TokenPaymentInfoAccessorsV0;
7use crate::ProtocolError;
8use bincode::{Decode, Encode};
9use derive_more::Display;
10use platform_value::btreemap_extensions::BTreeValueRemoveFromMapHelper;
11use platform_value::{Identifier, Value};
12#[cfg(feature = "serde-conversion")]
13use serde::{Deserialize, Serialize};
14use std::collections::BTreeMap;
15
16#[derive(Debug, Clone, Copy, Encode, Decode, Default, PartialEq, Display)]
17// `json_safe_fields` auto-injects `json_safe_option_u64` on
18// `Option<TokenAmount>` (= `Option<u64>`) fields so JSON encodes large
19// values as strings — same convention as the rest of the wire shape.
20#[cfg_attr(feature = "json-conversion", crate::serialization::json_safe_fields)]
21#[cfg_attr(
22    feature = "serde-conversion",
23    derive(Serialize, Deserialize),
24    serde(rename_all = "camelCase")
25)]
26#[display(
27    "Contract ID: {:?}, Token Position: {:?}, Min Cost: {:?}, Max Cost: {:?}, Gas Fees Paid By: {}",
28    payment_token_contract_id,
29    token_contract_position,
30    minimum_token_cost,
31    maximum_token_cost,
32    gas_fees_paid_by
33)]
34pub struct TokenPaymentInfoV0 {
35    /// By default, we use a token in the same contract, this field must be set if the document
36    /// requires payment using another contracts token.
37    pub payment_token_contract_id: Option<Identifier>,
38    /// If we are expecting to pay with a token in a contract, which token are we expecting
39    /// to pay with?
40    /// We have this set so contract owners can't switch out to more valuable token.
41    /// For example if my Data contract
42    pub token_contract_position: TokenContractPosition,
43    /// Minimum token cost, this most often should not be set
44    pub minimum_token_cost: Option<TokenAmount>,
45    /// Maximum token cost, this most often should be set
46    /// If:
47    /// - a client does not have this set
48    /// - and the data contract allows the price of NFTs to be changed by the data contract's owner or allowed party.
49    ///   Then:
50    /// - The user could see the cost changed on them
51    pub maximum_token_cost: Option<TokenAmount>,
52    /// Who pays the gas fees, this needs to match what the contract allows
53    pub gas_fees_paid_by: GasFeesPaidBy,
54}
55
56impl TokenPaymentInfoAccessorsV0 for TokenPaymentInfoV0 {
57    // Getters
58    fn payment_token_contract_id(&self) -> Option<Identifier> {
59        self.payment_token_contract_id
60    }
61
62    fn payment_token_contract_id_ref(&self) -> &Option<Identifier> {
63        &self.payment_token_contract_id
64    }
65
66    fn token_contract_position(&self) -> TokenContractPosition {
67        self.token_contract_position
68    }
69
70    fn minimum_token_cost(&self) -> Option<TokenAmount> {
71        self.minimum_token_cost
72    }
73
74    fn maximum_token_cost(&self) -> Option<TokenAmount> {
75        self.maximum_token_cost
76    }
77
78    // Setters
79    fn set_payment_token_contract_id(&mut self, id: Option<Identifier>) {
80        self.payment_token_contract_id = id;
81    }
82
83    fn set_token_contract_position(&mut self, position: TokenContractPosition) {
84        self.token_contract_position = position;
85    }
86
87    fn set_minimum_token_cost(&mut self, cost: Option<TokenAmount>) {
88        self.minimum_token_cost = cost;
89    }
90
91    fn set_maximum_token_cost(&mut self, cost: Option<TokenAmount>) {
92        self.maximum_token_cost = cost;
93    }
94
95    fn gas_fees_paid_by(&self) -> GasFeesPaidBy {
96        self.gas_fees_paid_by
97    }
98
99    fn set_gas_fees_paid_by(&mut self, payer: GasFeesPaidBy) {
100        self.gas_fees_paid_by = payer;
101    }
102}
103
104impl TryFrom<BTreeMap<String, Value>> for TokenPaymentInfoV0 {
105    type Error = ProtocolError;
106
107    fn try_from(mut map: BTreeMap<String, Value>) -> Result<Self, Self::Error> {
108        Ok(TokenPaymentInfoV0 {
109            payment_token_contract_id: map.remove_optional_identifier("paymentTokenContractId")?,
110
111            token_contract_position: map
112                .remove_optional_integer("tokenContractPosition")?
113                .unwrap_or_default(),
114
115            minimum_token_cost: map.remove_optional_integer("minimumTokenCost")?,
116
117            maximum_token_cost: map.remove_optional_integer("maximumTokenCost")?,
118
119            gas_fees_paid_by: map
120                .remove_optional_string("gasFeesPaidBy")?
121                .map(|v| match v.as_str() {
122                    "DocumentOwner" => GasFeesPaidBy::DocumentOwner,
123                    "ContractOwner" => GasFeesPaidBy::ContractOwner,
124                    "PreferContractOwner" => GasFeesPaidBy::PreferContractOwner,
125                    _ => GasFeesPaidBy::default(),
126                })
127                .unwrap_or_default(),
128        })
129    }
130}