dpp/asset_lock/reduced_asset_lock_value/
mod.rs

1use crate::asset_lock::reduced_asset_lock_value::v0::AssetLockValueV0;
2use crate::fee::Credits;
3use crate::ProtocolError;
4use bincode::{Decode, Encode};
5use derive_more::From;
6use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
7use platform_value::Bytes32;
8use platform_version::version::PlatformVersion;
9
10mod v0;
11
12pub use v0::{AssetLockValueGettersV0, AssetLockValueSettersV0};
13
14#[derive(
15    Debug,
16    Clone,
17    Encode,
18    Decode,
19    PlatformSerialize,
20    PlatformDeserialize,
21    From,
22    PartialEq,
23    serde::Serialize,
24    serde::Deserialize,
25)]
26#[platform_serialize(unversioned)]
27pub enum AssetLockValue {
28    V0(AssetLockValueV0),
29}
30
31impl AssetLockValue {
32    pub fn new(
33        initial_credit_value: Credits,
34        tx_out_script: Vec<u8>,
35        remaining_credit_value: Credits,
36        used_tags: Vec<Bytes32>,
37        platform_version: &PlatformVersion,
38    ) -> Result<Self, ProtocolError> {
39        match platform_version
40            .dpp
41            .asset_lock_versions
42            .reduced_asset_lock_value
43            .default_current_version
44        {
45            0 => Ok(AssetLockValue::V0(AssetLockValueV0 {
46                initial_credit_value,
47                tx_out_script,
48                remaining_credit_value,
49                used_tags,
50            })),
51            version => Err(ProtocolError::UnknownVersionMismatch {
52                method: "ReducedAssetLockValue::new".to_string(),
53                known_versions: vec![0],
54                received: version,
55            }),
56        }
57    }
58}
59
60impl AssetLockValueGettersV0 for AssetLockValue {
61    fn initial_credit_value(&self) -> Credits {
62        match self {
63            AssetLockValue::V0(v0) => v0.initial_credit_value,
64        }
65    }
66
67    fn tx_out_script(&self) -> &Vec<u8> {
68        match self {
69            AssetLockValue::V0(v0) => &v0.tx_out_script,
70        }
71    }
72
73    fn tx_out_script_owned(self) -> Vec<u8> {
74        match self {
75            AssetLockValue::V0(v0) => v0.tx_out_script,
76        }
77    }
78
79    fn remaining_credit_value(&self) -> Credits {
80        match self {
81            AssetLockValue::V0(v0) => v0.remaining_credit_value,
82        }
83    }
84
85    fn used_tags_ref(&self) -> &Vec<Bytes32> {
86        match self {
87            AssetLockValue::V0(v0) => &v0.used_tags,
88        }
89    }
90}
91
92impl AssetLockValueSettersV0 for AssetLockValue {
93    fn set_initial_credit_value(&mut self, value: Credits) {
94        match self {
95            AssetLockValue::V0(v0) => v0.initial_credit_value = value,
96        }
97    }
98
99    fn set_tx_out_script(&mut self, value: Vec<u8>) {
100        match self {
101            AssetLockValue::V0(v0) => v0.tx_out_script = value,
102        }
103    }
104
105    fn set_remaining_credit_value(&mut self, value: Credits) {
106        match self {
107            AssetLockValue::V0(v0) => v0.remaining_credit_value = value,
108        }
109    }
110
111    fn set_used_tags(&mut self, tags: Vec<Bytes32>) {
112        match self {
113            AssetLockValue::V0(v0) => v0.used_tags = tags,
114        }
115    }
116
117    fn add_used_tag(&mut self, tag: Bytes32) {
118        match self {
119            AssetLockValue::V0(v0) => v0.used_tags.push(tag),
120        }
121    }
122}