This repository was archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlib.rs
More file actions
275 lines (232 loc) · 6.51 KB
/
lib.rs
File metadata and controls
275 lines (232 loc) · 6.51 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
pub mod address;
pub mod hash;
pub mod lazy;
pub mod utils;
pub use address::{Address, AddressPayload, AddressType, CodeHashIndex};
pub use {anyhow, anyhow::Result, async_trait::async_trait, derive_more, minstant};
use ckb_types::{bytes::Bytes, core::BlockNumber, h256, packed, H256};
use derive_more::Display;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display};
pub const MULTISIG_TYPE_HASH: H256 =
h256!("0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8");
pub const PREFIX_MAINNET: &str = "ckb";
pub const PREFIX_TESTNET: &str = "ckt";
pub const NETWORK_MAINNET: &str = "ckb";
pub const NETWORK_TESTNET: &str = "ckb_testnet";
pub const NETWORK_STAGING: &str = "ckb_staging";
pub const NETWORK_DEV: &str = "ckb_dev";
pub const SECP256K1: &str = "secp256k1_blake160";
pub const SUDT: &str = "sudt";
pub const ACP: &str = "anyone_can_pay";
pub const CHEQUE: &str = "cheque";
pub const DAO: &str = "dao";
pub const PW_LOCK: &str = "pw_lock";
#[derive(Clone, Debug, PartialEq, Eq)]
enum ErrorKind {
Cli,
DB,
Extension,
Rpc,
Service,
Storage,
Utils,
}
#[derive(Clone, Debug, Display, PartialEq, Eq)]
#[display(fmt = "Mercury {:?} Error {:?}", kind, error)]
pub struct MercuryError<T> {
kind: ErrorKind,
error: T,
}
impl<T: Debug + Display> std::error::Error for MercuryError<T> {}
impl<T: Debug + Display> MercuryError<T> {
pub fn cli(error: T) -> Self {
Self::new(ErrorKind::Cli, error)
}
pub fn db(error: T) -> Self {
Self::new(ErrorKind::DB, error)
}
pub fn extension(error: T) -> Self {
Self::new(ErrorKind::Extension, error)
}
pub fn rpc(error: T) -> Self {
Self::new(ErrorKind::Rpc, error)
}
pub fn service(error: T) -> Self {
Self::new(ErrorKind::Service, error)
}
pub fn storage(error: T) -> Self {
Self::new(ErrorKind::Storage, error)
}
pub fn utils(error: T) -> Self {
Self::new(ErrorKind::Utils, error)
}
fn new(kind: ErrorKind, error: T) -> Self {
MercuryError { kind, error }
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Order {
#[serde(alias = "asc")]
Asc,
#[serde(alias = "desc")]
Desc,
}
impl Default for Order {
fn default() -> Self {
Order::Asc
}
}
impl Order {
pub fn is_asc(&self) -> bool {
*self == Order::Asc
}
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum NetworkType {
Mainnet,
Testnet,
Staging,
Dev,
}
impl NetworkType {
pub fn from_prefix(value: &str) -> Option<NetworkType> {
match value {
PREFIX_MAINNET => Some(NetworkType::Mainnet),
PREFIX_TESTNET => Some(NetworkType::Testnet),
_ => None,
}
}
pub fn to_prefix(self) -> &'static str {
match self {
NetworkType::Mainnet => PREFIX_MAINNET,
NetworkType::Testnet => PREFIX_TESTNET,
NetworkType::Staging => PREFIX_TESTNET,
NetworkType::Dev => PREFIX_TESTNET,
}
}
pub fn from_raw_str(value: &str) -> Option<NetworkType> {
match value {
NETWORK_MAINNET => Some(NetworkType::Mainnet),
NETWORK_TESTNET => Some(NetworkType::Testnet),
NETWORK_STAGING => Some(NetworkType::Staging),
NETWORK_DEV => Some(NetworkType::Dev),
_ => None,
}
}
pub fn to_str(self) -> &'static str {
match self {
NetworkType::Mainnet => NETWORK_MAINNET,
NetworkType::Testnet => NETWORK_TESTNET,
NetworkType::Staging => NETWORK_STAGING,
NetworkType::Dev => NETWORK_DEV,
}
}
}
impl fmt::Display for NetworkType {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.to_str())
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Display, Hash, PartialEq, Eq)]
#[display(fmt = "range from {} to {}", from, to)]
pub struct Range {
pub from: u64,
pub to: u64,
}
impl Range {
pub fn new(from: u64, to: u64) -> Self {
Range { from, to }
}
pub fn is_in(&self, num: u64) -> bool {
num >= self.from && num <= self.to
}
pub fn min(&self) -> u64 {
self.from
}
pub fn max(&self) -> u64 {
self.to
}
}
#[derive(Serialize, Deserialize, Default, Clone, Debug, Hash, PartialEq, Eq)]
pub struct PaginationRequest {
pub cursor: Option<u64>,
pub order: Order,
pub limit: Option<u16>,
pub skip: Option<u64>,
pub return_count: bool,
}
impl PaginationRequest {
pub fn new(
cursor: Option<u64>,
order: Order,
limit: Option<u16>,
skip: Option<u64>,
return_count: bool,
) -> PaginationRequest {
PaginationRequest {
cursor,
order,
limit,
skip,
return_count,
}
}
pub fn order(mut self, order: Order) -> Self {
self.set_order(order);
self
}
pub fn set_order(&mut self, order: Order) {
self.order = order;
}
pub fn limit(mut self, limit: Option<u16>) -> Self {
self.set_limit(limit);
self
}
pub fn set_limit(&mut self, limit: Option<u16>) {
self.limit = limit;
}
pub fn update_by_response(&mut self, next_cursor: Option<u64>) {
self.cursor = next_cursor;
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq)]
pub struct PaginationResponse<T> {
pub response: Vec<T>,
pub next_cursor: Option<u64>,
pub count: Option<u64>,
}
impl<T> PaginationResponse<T> {
pub fn new(response: Vec<T>) -> Self {
Self {
response,
next_cursor: None,
count: None,
}
}
}
impl<T> Default for PaginationResponse<T> {
fn default() -> Self {
Self::new(vec![])
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct DetailedCell {
pub epoch_number: u64,
pub block_number: BlockNumber,
pub block_hash: H256,
pub tx_index: u32,
pub out_point: packed::OutPoint,
pub cell_output: packed::CellOutput,
pub cell_data: Bytes,
pub consumed_block_number: Option<u64>,
pub consumed_block_hash: Option<H256>,
pub consumed_tx_hash: Option<H256>,
pub consumed_tx_index: Option<u32>,
pub consumed_input_index: Option<u32>,
pub since: Option<u64>,
}
pub fn display_list_as_hex<T: AsRef<[u8]>>(list: &[T]) {
list.iter()
.for_each(|i| println!("{:?}", hex::encode(i.as_ref())))
}