1
+ //! helpers and primitive types for querying Cardano [Db-Sync] data using [sqlx]
2
+ //!
3
+ //! # About
4
+ //!
5
+ //! This crate is meant to provide help when writing queries for data produced by [Db-Sync],
6
+ //! a Cardano blockchain indexer. Db-Sync keeps the indexed data in a Postgre database that
7
+ //! can be queried using SQL. This crate defines primitive types that correspond to column
8
+ //! types used in the [Db-Sync schema], along with some other helpers and casts to types
9
+ //! defined in [sidechain_domain].
10
+ //!
11
+ //! [sqlx]: https://github.com/launchbadge/sqlx
12
+ //! [Db-Sync]: https://github.com/IntersectMBO/cardano-db-sync
13
+ //! [Db-Sync schema]: https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/schema.md
1
14
use num_traits:: ToPrimitive ;
2
15
use sidechain_domain:: * ;
3
16
use sqlx:: database:: Database ;
@@ -6,11 +19,17 @@ use sqlx::error::BoxDynError;
6
19
use sqlx:: postgres:: PgTypeInfo ;
7
20
use sqlx:: * ;
8
21
9
- /// Generates sqlx implementations for an unsigned wrapper of types that are signed.
10
- /// We expect that values will have always 0 as the most significant bit.
11
- /// For example TxIndex is in range of [0, 2^15-1], it will be u16 in domain,
12
- /// but it requires encoding and decoding like i16.
13
- /// See txindex, word31 and word63 types in db-sync schema definition.
22
+ /// Macro to handle numeric types that are non-negative but are stored by Db-Sync using
23
+ /// signed SQL types.
24
+ ///
25
+ /// This macro generates an unsigned numeric domain type and sqlx trait implementation for
26
+ /// decoding it from signed data coming from Db-Sync database. It expects that values will
27
+ /// always have 0 as the most significant bit.
28
+ ///
29
+ /// For example because [TxIndex] is in range of \[0, 2^15-1\], it will be [u16] in domain,
30
+ /// but it requires encoding and decoding as i16.
31
+ ///
32
+ /// See `txindex`, `word31` `and` word63 types in Db-Sync schema definition.
14
33
#[ macro_export]
15
34
macro_rules! sqlx_implementations_for_wrapper {
16
35
( $WRAPPED: ty, $DBTYPE: expr, $NAME: ty, $DOMAIN: ty) => {
@@ -62,28 +81,42 @@ macro_rules! sqlx_implementations_for_wrapper {
62
81
} ;
63
82
}
64
83
84
+ /// Cardano block number
65
85
#[ derive( Debug , Copy , Ord , PartialOrd , Clone , PartialEq , Eq ) ]
66
86
pub struct BlockNumber ( pub u32 ) ;
67
87
sqlx_implementations_for_wrapper ! ( i32 , "INT4" , BlockNumber , McBlockNumber ) ;
68
88
89
+ /// Cardano epoch number
69
90
#[ derive( Debug , Copy , Clone , PartialEq ) ]
70
91
pub struct EpochNumber ( pub u32 ) ;
71
92
sqlx_implementations_for_wrapper ! ( i32 , "INT4" , EpochNumber , McEpochNumber ) ;
72
93
94
+ /// Cardano slot number
73
95
#[ derive( Debug , Copy , Clone , PartialEq ) ]
74
96
pub struct SlotNumber ( pub u64 ) ;
75
97
sqlx_implementations_for_wrapper ! ( i64 , "INT8" , SlotNumber , McSlotNumber ) ;
76
98
99
+ /// Index of a Cardano transaction output
100
+ ///
101
+ /// This type corresponds to the following SQL type defined by Db-Sync.
102
+ /// ```sql
77
103
/// CREATE DOMAIN txindex AS smallint CONSTRAINT txindex_check CHECK ((VALUE >= 0));
104
+ /// ```
78
105
#[ derive( Debug , Clone , PartialEq ) ]
79
106
pub struct TxIndex ( pub u16 ) ;
80
107
sqlx_implementations_for_wrapper ! ( i16 , "INT2" , TxIndex , UtxoIndex ) ;
81
108
109
+ /// Index of a Cardano transaction with its block
82
110
#[ derive( Debug , Clone , Copy , PartialOrd , Ord , PartialEq , Eq ) ]
83
111
pub struct TxIndexInBlock ( pub u32 ) ;
84
112
sqlx_implementations_for_wrapper ! ( i32 , "INT4" , TxIndexInBlock , McTxIndexInBlock ) ;
85
113
114
+ /// Number of ADA expressed in Lovelace (1 million-th of ADA)
115
+ ///
116
+ /// This type corresponds to the following SQL type defined by Db-Sync.
117
+ /// ```sql
86
118
/// CREATE DOMAIN int65type AS numeric (20, 0) CHECK (VALUE >= -18446744073709551615 AND VALUE <= 18446744073709551615);
119
+ /// ```
87
120
#[ derive( Debug , Clone , PartialEq ) ]
88
121
pub struct TxValue ( pub i128 ) ;
89
122
@@ -101,7 +134,12 @@ impl<'r> Decode<'r, Postgres> for TxValue {
101
134
}
102
135
}
103
136
137
+ /// Number of ADA delegated by a Cardano delegator to a single SPO, expressed in Lovelace (1 million-th of ADA)
138
+ ///
139
+ /// This type corresponds to the following SQL type defined by Db-Sync.
140
+ /// ```sql
104
141
/// CREATE DOMAIN "lovelace" AS numeric(20,0) CONSTRAINT flyway_needs_this CHECK (VALUE >= 0::numeric AND VALUE <= '18446744073709551615'::numeric);
142
+ /// ```
105
143
#[ derive( Debug , Clone , PartialEq ) ]
106
144
pub struct StakeDelegation ( pub u64 ) ;
107
145
@@ -119,6 +157,7 @@ impl<'r> Decode<'r, Postgres> for StakeDelegation {
119
157
}
120
158
}
121
159
160
+ /// Cardano native asset name, typically UTF-8 encoding of a human-readable name
122
161
#[ derive( Debug , Clone , sqlx:: FromRow , PartialEq ) ]
123
162
pub struct AssetName ( pub Vec < u8 > ) ;
124
163
@@ -128,6 +167,7 @@ impl From<sidechain_domain::AssetName> for AssetName {
128
167
}
129
168
}
130
169
170
+ /// Cardano minting policy ID. This value is obtained by hashing the Plutus script of the policy.
131
171
#[ derive( Debug , Clone , sqlx:: FromRow , PartialEq ) ]
132
172
pub struct PolicyId ( pub Vec < u8 > ) ;
133
173
@@ -137,9 +177,12 @@ impl From<sidechain_domain::PolicyId> for PolicyId {
137
177
}
138
178
}
139
179
180
+ /// Full identifier of a Cardano native asset
140
181
#[ derive( Debug , Clone , sqlx:: FromRow , PartialEq ) ]
141
182
pub struct Asset {
183
+ /// Minting policy ID of the asset
142
184
pub policy_id : PolicyId ,
185
+ /// Asset name
143
186
pub asset_name : AssetName ,
144
187
}
145
188
@@ -156,15 +199,27 @@ impl From<sidechain_domain::AssetId> for Asset {
156
199
}
157
200
}
158
201
159
- #[ derive( Debug , Copy , Clone , sqlx:: FromRow , PartialEq ) ]
160
- pub struct EpochNumberRow ( pub EpochNumber ) ;
161
-
162
- impl From < EpochNumberRow > for EpochNumber {
163
- fn from ( r : EpochNumberRow ) -> Self {
164
- r. 0
202
+ /// Helper row type for querying just epoch number
203
+ #[ allow( deprecated) ]
204
+ mod epoch_number_row {
205
+ use super :: * ;
206
+ #[ deprecated(
207
+ since = "1.7.0" ,
208
+ note = "Deprecated due to not being either a primitive type or a complete Db-Sync table row."
209
+ ) ]
210
+ #[ derive( Debug , Copy , Clone , sqlx:: FromRow , PartialEq ) ]
211
+ pub struct EpochNumberRow ( pub EpochNumber ) ;
212
+
213
+ #[ allow( deprecated) ]
214
+ impl From < EpochNumberRow > for EpochNumber {
215
+ fn from ( r : EpochNumberRow ) -> Self {
216
+ r. 0
217
+ }
165
218
}
166
219
}
220
+ pub use epoch_number_row:: * ;
167
221
222
+ /// Cardano address in human-readable form. Either Base58 for Byron addresses and Bech32 for Shelley.
168
223
#[ derive( Debug , Clone , sqlx:: FromRow , PartialEq ) ]
169
224
pub struct Address ( pub String ) ;
170
225
@@ -174,5 +229,8 @@ impl From<MainchainAddress> for Address {
174
229
}
175
230
}
176
231
232
+ /// Cardano epoch nonce, ie. random 32 bytes generated by Cardano every epoch.
233
+ ///
234
+ /// This value can be used as a tamper-proof randomness seed.
177
235
#[ derive( Debug , Clone , sqlx:: FromRow , PartialEq ) ]
178
236
pub struct MainchainEpochNonce ( pub Vec < u8 > ) ;
0 commit comments