rgbstd/interface/
suppl.rs

1// RGB standard library for working with smart contracts on Bitcoin & Lightning
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2023 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22use amplify::confinement::{SmallBlob, TinyOrdMap, TinyString};
23use amplify::Bytes32;
24use rgb::{AssignmentType, ContractId, GlobalStateType};
25use strict_types::value;
26
27use crate::LIB_NAME_RGB_STD;
28
29/// Contract supplement identifier.
30///
31/// Contract supplement identifier commits to all of the supplement data.
32#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
33#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)]
34#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
35#[strict_type(lib = LIB_NAME_RGB_STD)]
36#[cfg_attr(
37    feature = "serde",
38    derive(Serialize, Deserialize),
39    serde(crate = "serde_crate", transparent)
40)]
41pub struct SupplId(
42    #[from]
43    #[from([u8; 32])]
44    Bytes32,
45);
46
47/// Contract supplement, providing non-consensus information about standard
48/// way of working with the contract data. Each contract can have only a single
49/// valid supplement; the supplement is attached to the contract via trusted
50/// provider signature (providers are ordered by the priority).
51#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
52#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
53#[strict_type(lib = LIB_NAME_RGB_STD)]
54#[cfg_attr(
55    feature = "serde",
56    derive(Serialize, Deserialize),
57    serde(crate = "serde_crate", rename_all = "camelCase")
58)]
59pub struct ContractSuppl {
60    pub contract_id: ContractId,
61    pub ticker: TickerSuppl,
62    /// Media kit is a URL string which provides JSON information on media files
63    /// and colors that should be used for UI,
64    pub media_kit: TinyString,
65    pub global_state: TinyOrdMap<AssignmentType, OwnedStateSuppl>,
66    pub owned_state: TinyOrdMap<AssignmentType, OwnedStateSuppl>,
67    /// TLV-encoded custom fields.
68    pub extensions: TinyOrdMap<u16, SmallBlob>,
69}
70
71#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
72#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
73#[strict_type(lib = LIB_NAME_RGB_STD, tags = custom)]
74#[cfg_attr(
75    feature = "serde",
76    derive(Serialize, Deserialize),
77    serde(crate = "serde_crate", rename_all = "camelCase")
78)]
79pub enum TickerSuppl {
80    #[strict_type(tag = 0, dumb)]
81    Absent,
82    #[strict_type(tag = 1)]
83    Global(GlobalStateType, value::Path),
84    #[strict_type(tag = 2)]
85    Owned(AssignmentType, value::Path),
86}
87
88#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
89#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
90#[strict_type(lib = LIB_NAME_RGB_STD)]
91#[cfg_attr(
92    feature = "serde",
93    derive(Serialize, Deserialize),
94    serde(crate = "serde_crate", rename_all = "camelCase")
95)]
96pub struct OwnedStateSuppl {
97    pub meaning: TinyString,
98    pub velocity: VelocityHint,
99}
100
101#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display, Default)]
102#[derive(StrictType, StrictEncode, StrictDecode)]
103#[strict_type(lib = LIB_NAME_RGB_STD, tags = repr, try_from_u8, into_u8)]
104#[cfg_attr(
105    feature = "serde",
106    derive(Serialize, Deserialize),
107    serde(crate = "serde_crate", rename_all = "camelCase")
108)]
109#[display(lowercase)]
110#[repr(u8)]
111pub enum VelocityHint {
112    #[default]
113    Unspecified = 0,
114    /// Should be used for thinks like secondary issuance for tokens which do
115    /// not inflate very often.
116    Seldom = 15,
117    /// Should be used for digital identity revocations.
118    Episodic = 31,
119    /// Should be used for digital art, shares, bonds etc.
120    Regular = 63,
121    /// Should be used for fungible tokens.
122    Frequent = 127,
123    /// Should be used for stablecoins and money.
124    HighFrequency = 255,
125}
126
127impl VelocityHint {
128    pub fn with_value(value: &u8) -> Self {
129        match *value {
130            0 => VelocityHint::Unspecified,
131            1..=15 => VelocityHint::Seldom,
132            16..=31 => VelocityHint::Episodic,
133            32..=63 => VelocityHint::Regular,
134            64..=127 => VelocityHint::Frequent,
135            128..=255 => VelocityHint::HighFrequency,
136        }
137    }
138}