/
output.rs
150 lines (133 loc) · 5.12 KB
/
output.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
use std::{borrow::Cow, ops::Deref};
use pallas_addresses::{Address, ByronAddress, Error as AddressError};
use pallas_codec::minicbor;
use pallas_primitives::{
alonzo,
babbage::{self, DatumOption, ScriptRef},
byron,
};
use crate::{Era, MultiEraOutput};
impl<'b> MultiEraOutput<'b> {
pub fn from_byron(output: &'b byron::TxOut) -> Self {
Self::Byron(Box::new(Cow::Borrowed(output)))
}
pub fn from_alonzo_compatible(output: &'b alonzo::TransactionOutput) -> Self {
Self::AlonzoCompatible(Box::new(Cow::Borrowed(output)))
}
pub fn from_babbage(output: &'b babbage::TransactionOutput) -> Self {
Self::Babbage(Box::new(Cow::Borrowed(output)))
}
pub fn datum(&self) -> Option<DatumOption> {
match self {
MultiEraOutput::AlonzoCompatible(x) => x.datum_hash.map(DatumOption::Hash),
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => x.datum_hash.map(DatumOption::Hash),
babbage::TransactionOutput::PostAlonzo(x) => x.datum_option.clone(),
},
_ => None,
}
}
pub fn script_ref(&self) -> Option<&ScriptRef> {
match &self {
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(_) => None,
babbage::TransactionOutput::PostAlonzo(x) => x.script_ref.as_ref(),
},
_ => None,
}
}
pub fn address(&self) -> Result<Address, AddressError> {
match self {
MultiEraOutput::AlonzoCompatible(x) => Address::from_bytes(&x.address),
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => Address::from_bytes(&x.address),
babbage::TransactionOutput::PostAlonzo(x) => Address::from_bytes(&x.address),
},
MultiEraOutput::Byron(x) => {
Ok(ByronAddress::new(&x.address.payload.0, x.address.crc).into())
}
}
}
pub fn ada_amount(&self) -> u64 {
match self {
MultiEraOutput::Byron(x) => x.amount,
MultiEraOutput::Babbage(x) => match x.deref().deref() {
babbage::TransactionOutput::Legacy(x) => match x.amount {
babbage::Value::Coin(c) => u64::from(c),
babbage::Value::Multiasset(c, _) => u64::from(c),
},
babbage::TransactionOutput::PostAlonzo(x) => match x.value {
babbage::Value::Coin(c) => u64::from(c),
babbage::Value::Multiasset(c, _) => u64::from(c),
},
},
MultiEraOutput::AlonzoCompatible(x) => match x.amount {
alonzo::Value::Coin(c) => u64::from(c),
alonzo::Value::Multiasset(c, _) => u64::from(c),
},
}
}
pub fn as_babbage(&self) -> Option<&babbage::TransactionOutput> {
match self {
MultiEraOutput::AlonzoCompatible(_) => None,
MultiEraOutput::Babbage(x) => Some(x),
MultiEraOutput::Byron(_) => None,
}
}
pub fn as_alonzo(&self) -> Option<&alonzo::TransactionOutput> {
match self {
MultiEraOutput::AlonzoCompatible(x) => Some(x),
MultiEraOutput::Babbage(_) => None,
MultiEraOutput::Byron(_) => None,
}
}
pub fn as_byron(&self) -> Option<&byron::TxOut> {
match self {
MultiEraOutput::AlonzoCompatible(_) => None,
MultiEraOutput::Babbage(_) => None,
MultiEraOutput::Byron(x) => Some(x),
}
}
pub fn encode(&self) -> Result<Vec<u8>, minicbor::encode::Error<std::io::Error>> {
match self {
Self::AlonzoCompatible(x) => minicbor::to_vec(x),
Self::Babbage(x) => minicbor::to_vec(x),
Self::Byron(x) => minicbor::to_vec(x),
}
}
pub fn decode(era: Era, cbor: &'b [u8]) -> Result<Self, minicbor::decode::Error> {
match era {
Era::Byron => {
let tx = minicbor::decode(cbor)?;
let tx = Box::new(Cow::Owned(tx));
Ok(Self::Byron(tx))
}
Era::Shelley | Era::Allegra | Era::Mary | Era::Alonzo => {
let tx = minicbor::decode(cbor)?;
let tx = Box::new(Cow::Owned(tx));
Ok(Self::AlonzoCompatible(tx))
}
Era::Babbage => {
let tx = minicbor::decode(cbor)?;
let tx = Box::new(Cow::Owned(tx));
Ok(Self::Babbage(tx))
}
}
}
}
#[cfg(test)]
mod tests {
use crate::MultiEraBlock;
#[test]
fn traverse_block_with_varied_outputs() {
let str = include_str!("../../test_data/alonzo24.block");
let bytes = hex::decode(str).unwrap();
let block = MultiEraBlock::decode(&bytes).unwrap();
for tx in block.txs() {
for output in tx.outputs() {
assert_ne!(output.ada_amount(), 0);
assert!(matches!(output.address(), Ok(_)));
}
}
}
}