This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathschema.rs
202 lines (186 loc) · 7.31 KB
/
schema.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// RGB standard library
// Written in 2020 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
use core::ops::Neg;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::ToPrimitive;
use lnpbp::rgb::schema::{
script, AssignmentAction, Bits, DataFormat, DiscreteFiniteFieldFormat, GenesisSchema,
Occurences, Schema, StateFormat, StateSchema, TransitionSchema,
};
use crate::error::ServiceErrorDomain;
use crate::type_map;
#[derive(Debug, Display, Error, From)]
#[display_from(Debug)]
pub enum SchemaError {
#[derive_from(core::option::NoneError)]
NotAllFieldsPresent,
WrongSchemaId,
}
impl From<SchemaError> for ServiceErrorDomain {
fn from(err: SchemaError) -> Self {
ServiceErrorDomain::Schema(format!("{}", err))
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display, ToPrimitive, FromPrimitive)]
#[display_from(Debug)]
#[repr(u16)]
pub enum FieldType {
Ticker = 0,
Name = 1,
Description = 2,
TotalSupply = 3,
IssuedSupply = 4,
DustLimit = 5,
Precision = 6,
PruneProof = 7,
Timestamp = 8,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display, ToPrimitive, FromPrimitive)]
#[display_from(Debug)]
#[repr(u16)]
pub enum AssignmentsType {
Issue = 0,
Assets = 1,
Prune = 2,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display, ToPrimitive, FromPrimitive)]
#[display_from(Debug)]
#[repr(u16)]
pub enum TransitionType {
Issue = 0,
Transfer = 1,
Prune = 2,
}
pub fn schema() -> Schema {
Schema {
field_types: type_map! {
// Rational: if we will use just 26 letters of English alphabet (and
// we are not limited by them), we will have 26^8 possible tickers,
// i.e. > 208 trillions, which is sufficient amount
FieldType::Ticker => DataFormat::String(8),
FieldType::Name => DataFormat::String(256),
// Description may contain URL, text or text representation of
// Ricardian contract. We use all available size, in case the
// contract is long. If the contract still doesn't fit, a hash or
// URL should be used instead, pointing to the full contract text
FieldType::Description => DataFormat::String(core::u16::MAX),
FieldType::TotalSupply => DataFormat::Unsigned(Bits::Bit64, 0, core::u64::MAX as u128),
FieldType::Precision => DataFormat::Unsigned(Bits::Bit8, 0, 18u128),
FieldType::IssuedSupply => DataFormat::Unsigned(Bits::Bit64, 0, core::u64::MAX as u128),
FieldType::DustLimit => DataFormat::Unsigned(Bits::Bit64, 0, core::u64::MAX as u128),
FieldType::PruneProof => DataFormat::Bytes(core::u16::MAX),
// While UNIX timestamps allow negative numbers; in context of RGB Schema, assets
// can't be issued in the past before RGB or Bitcoin even existed; so we prohibit
// all the dates before RGB release
// TODO: Update lower limit with the first RGB release
// Current lower time limit is 07/04/2020 @ 1:54pm (UTC)
FieldType::Timestamp => DataFormat::Integer(Bits::Bit64, 1593870844, core::i64::MAX as i128)
},
assignment_types: type_map! {
AssignmentsType::Issue => StateSchema {
format: StateFormat::Declarative,
abi: bmap! {
AssignmentAction::Validate => script::Procedure::Standard(script::StandardProcedure::IssueControl)
}
},
AssignmentsType::Assets => StateSchema {
format: StateFormat::DiscreteFiniteField(DiscreteFiniteFieldFormat::Unsigned64bit),
abi: bmap! {
AssignmentAction::Validate => script::Procedure::Standard(script::StandardProcedure::ConfidentialAmount)
}
},
AssignmentsType::Prune => StateSchema {
format: StateFormat::Declarative,
abi: bmap! {
AssignmentAction::Validate => script::Procedure::Standard(script::StandardProcedure::Prunning)
}
}
},
genesis: GenesisSchema {
metadata: type_map! {
FieldType::Ticker => Occurences::Once,
FieldType::Name => Occurences::Once,
FieldType::Description => Occurences::NoneOrOnce,
FieldType::TotalSupply => Occurences::Once,
FieldType::IssuedSupply => Occurences::Once,
FieldType::DustLimit => Occurences::NoneOrOnce,
FieldType::Precision => Occurences::Once,
FieldType::Timestamp => Occurences::Once
},
defines: type_map! {
AssignmentsType::Issue => Occurences::NoneOrOnce,
AssignmentsType::Assets => Occurences::NoneOrUpTo(None),
AssignmentsType::Prune => Occurences::NoneOrUpTo(None)
},
abi: bmap! {},
},
transitions: type_map! {
TransitionType::Issue => TransitionSchema {
metadata: type_map! {
FieldType::IssuedSupply => Occurences::Once
},
closes: type_map! {
AssignmentsType::Issue => Occurences::Once
},
defines: type_map! {
AssignmentsType::Issue => Occurences::NoneOrOnce,
AssignmentsType::Prune => Occurences::NoneOrUpTo(None),
AssignmentsType::Assets => Occurences::NoneOrUpTo(None)
},
abi: bmap! {}
},
TransitionType::Transfer => TransitionSchema {
metadata: type_map! {},
closes: type_map! {
AssignmentsType::Assets => Occurences::OnceOrUpTo(None)
},
defines: type_map! {
AssignmentsType::Assets => Occurences::NoneOrUpTo(None)
},
abi: bmap! {}
},
TransitionType::Prune => TransitionSchema {
metadata: type_map! {
FieldType::PruneProof => Occurences::NoneOrUpTo(None)
},
closes: type_map! {
AssignmentsType::Prune => Occurences::OnceOrUpTo(None),
AssignmentsType::Assets => Occurences::OnceOrUpTo(None)
},
defines: type_map! {
AssignmentsType::Prune => Occurences::NoneOrUpTo(None),
AssignmentsType::Assets => Occurences::NoneOrUpTo(None)
},
abi: bmap! {}
}
},
}
}
impl Neg for FieldType {
type Output = usize;
fn neg(self) -> Self::Output {
self.to_usize().unwrap()
}
}
impl Neg for AssignmentsType {
type Output = usize;
fn neg(self) -> Self::Output {
self.to_usize().unwrap()
}
}
impl Neg for TransitionType {
type Output = usize;
fn neg(self) -> Self::Output {
self.to_usize().unwrap()
}
}