-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstructs.rs
151 lines (128 loc) · 3.41 KB
/
structs.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
use crate::constants::NAVAX_TO_AVAX_RATIO;
use crate::errors::{AvaxError, Result};
use crate::get_address;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use bytes::{Buf, Bytes};
use core::convert::TryFrom;
#[derive(Debug, Clone)]
pub struct LengthPrefixedVec<T: ParsedSizeAble> {
len: usize,
items: Vec<T>,
}
pub trait ParsedSizeAble {
fn parsed_size(&self) -> usize;
}
impl<T: ParsedSizeAble> LengthPrefixedVec<T> {
pub fn get_len(&self) -> usize {
self.len
}
pub fn get(&self, index: usize) -> Option<&T> {
self.items.get(index)
}
pub fn parsed_size(&self) -> usize {
4 + self
.items
.iter()
.fold(0, |acc, item| acc + item.parsed_size())
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.items.iter()
}
}
impl<T> TryFrom<Bytes> for LengthPrefixedVec<T>
where
T: TryFrom<Bytes, Error = AvaxError> + ParsedSizeAble,
{
type Error = AvaxError;
fn try_from(mut bytes: Bytes) -> Result<Self> {
if bytes.len() < 4 {
return Err(AvaxError::InvalidHex(
"Insufficient data for LengthPrefixedVec".to_string(),
));
}
let len = bytes.get_u32() as usize;
let mut items = Vec::with_capacity(len);
for _ in 0..len {
let item = T::try_from(bytes.clone())?;
bytes.advance(item.parsed_size());
items.push(item);
}
Ok(LengthPrefixedVec { len, items })
}
}
pub trait AvaxTxInfo {
fn get_total_input_amount(&self) -> u64;
fn get_total_output_amount(&self) -> u64;
fn get_output_amount(&self, address: String) -> u64 {
self.get_outputs_addresses()
.iter()
.find(|info| info.address[0] == address)
.map(|info| info.amount)
.unwrap_or(0)
}
fn get_fee_amount(&self) -> u64 {
self.get_total_input_amount() - self.get_total_output_amount()
}
fn get_outputs_addresses(&self) -> Vec<AvaxFromToInfo>;
fn get_network(&self) -> Option<String> {
None
}
fn get_network_key(&self) -> String {
"Network".to_string()
}
fn get_subnet_id(&self) -> Option<String> {
None
}
fn get_method_info(&self) -> Option<AvaxMethodInfo> {
None
}
fn get_reward_address(&self) -> Option<String> {
None
}
}
pub struct AvaxMethodInfo {
pub method_key: String,
pub method: String,
pub start_time: i64,
pub end_time: i64,
}
impl AvaxMethodInfo {
pub fn from_string(method: String) -> Self {
AvaxMethodInfo {
method_key: "Method".to_string(),
method,
start_time: 0,
end_time: 0,
}
}
pub fn with_method_key(mut self, method_key: String) -> Self {
self.method_key = method_key;
self
}
pub fn from(method: String, start_time: i64, end_time: i64) -> Self {
AvaxMethodInfo {
method_key: "Method".to_string(),
method,
start_time,
end_time,
}
}
}
#[derive(Debug, Clone)]
pub struct AvaxFromToInfo {
pub amount: u64,
pub address: Vec<String>,
pub path_prefix: String,
}
impl AvaxFromToInfo {
pub fn from(amount: u64, address: Vec<String>, path_prefix: String) -> Self {
AvaxFromToInfo {
amount,
address,
path_prefix,
}
}
}