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
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]
#![deny(rustdoc::broken_intra_doc_links)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(unstable_name_collisions)]
use std::{error, fmt};
#[doc(inline)]
pub use self::{
ad_slot::AdSlot,
ad_unit::AdUnit,
address::Address,
balances::Balances,
balances_map::{BalancesMap, UnifiedMap},
big_num::BigNum,
campaign::{Campaign, CampaignId},
chain::{Chain, ChainId, ChainOf},
channel::{Channel, ChannelId},
config::Config,
deposit::Deposit,
event_submission::EventSubmission,
ipfs::IPFS,
unified_num::UnifiedNum,
validator::{Validator, ValidatorDesc, ValidatorId},
};
mod ad_slot;
mod ad_unit;
pub mod address;
pub mod analytics;
pub mod balances;
pub mod balances_map;
pub mod big_num;
pub mod campaign;
pub mod campaign_validator;
mod chain;
pub mod channel;
pub mod config;
mod eth_checksum;
pub mod event_submission;
pub mod ipfs;
pub mod merkle_tree;
pub mod platform;
pub mod sentry;
pub mod spender;
pub mod targeting;
#[cfg(any(test, feature = "test-util"))]
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
pub mod test_util;
pub mod unified_num;
pub mod validator;
#[cfg(feature = "postgres")]
#[cfg_attr(docsrs, doc(cfg(feature = "postgres")))]
pub mod postgres {
use std::env::{self, VarError};
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
use once_cell::sync::Lazy;
use tokio_postgres::{Config, NoTls};
pub type DbPool = deadpool_postgres::Pool;
pub static POSTGRES_POOL: Lazy<Pool> = Lazy::new(|| {
let config = POSTGRES_CONFIG.clone();
let mgr_config = ManagerConfig {
recycling_method: RecyclingMethod::Verified,
};
let mgr = Manager::from_config(config, NoTls, mgr_config);
Pool::builder(mgr)
.max_size(42)
.build()
.expect("Should build test postgres pool")
});
pub static POSTGRES_USER: Lazy<String> =
Lazy::new(|| env::var("POSTGRES_USER").unwrap_or_else(|_| String::from("postgres")));
pub static POSTGRES_PASSWORD: Lazy<String> =
Lazy::new(|| env::var("POSTGRES_PASSWORD").unwrap_or_else(|_| String::from("postgres")));
pub static POSTGRES_HOST: Lazy<String> =
Lazy::new(|| env::var("POSTGRES_HOST").unwrap_or_else(|_| String::from("localhost")));
pub static POSTGRES_PORT: Lazy<u16> = Lazy::new(|| {
env::var("POSTGRES_PORT")
.unwrap_or_else(|_| String::from("5432"))
.parse()
.unwrap()
});
pub static POSTGRES_DB: Lazy<String> = Lazy::new(|| match env::var("POSTGRES_DB") {
Ok(database) => database,
Err(VarError::NotPresent) => POSTGRES_USER.clone(),
Err(err) => panic!("{}", err),
});
pub static POSTGRES_CONFIG: Lazy<Config> = Lazy::new(|| {
let mut config = Config::new();
config
.user(POSTGRES_USER.as_str())
.password(POSTGRES_PASSWORD.as_str())
.host(POSTGRES_HOST.as_str())
.port(*POSTGRES_PORT)
.dbname(POSTGRES_DB.as_ref());
config
});
}
mod deposit {
use crate::{BigNum, UnifiedNum};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Deposit<N> {
pub total: N,
}
impl Deposit<UnifiedNum> {
pub fn to_precision(&self, precision: u8) -> Deposit<BigNum> {
Deposit {
total: self.total.to_precision(precision),
}
}
pub fn from_precision(
deposit: Deposit<BigNum>,
precision: u8,
) -> Option<Deposit<UnifiedNum>> {
let total = UnifiedNum::from_precision(deposit.total, precision);
total.map(|total| Deposit { total })
}
}
impl<N: Default> Default for Deposit<N> {
fn default() -> Self {
Self {
total: Default::default(),
}
}
}
}
pub mod util {
#[doc(inline)]
pub use api::ApiUrl;
pub mod api;
pub mod logging;
}
#[derive(Debug, PartialEq, Eq)]
pub enum DomainError {
InvalidArgument(String),
RuleViolation(String),
}
impl fmt::Display for DomainError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DomainError::InvalidArgument(err) => write!(f, "{}", err),
DomainError::RuleViolation(err) => write!(f, "{}", err),
}
}
}
impl From<address::Error> for DomainError {
fn from(error: address::Error) -> Self {
Self::InvalidArgument(error.to_string())
}
}
impl error::Error for DomainError {
fn cause(&self) -> Option<&dyn error::Error> {
None
}
}
#[allow(clippy::upper_case_acronyms)]
pub trait ToETHChecksum: AsRef<[u8]> {
fn to_checksum(&self) -> String {
eth_checksum::checksum(&hex::encode(self.as_ref()))
}
}
impl ToETHChecksum for &[u8; 20] {}
pub trait ToHex {
fn to_hex(&self) -> String;
fn to_hex_prefixed(&self) -> String;
}
impl<T: AsRef<[u8]>> ToHex for T {
fn to_hex(&self) -> String {
hex::encode(self.as_ref())
}
fn to_hex_prefixed(&self) -> String {
format!("0x{}", self.as_ref().to_hex())
}
}