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
use crate::{
crypto::hash::Hash,
interfaces::{Address, Value},
};
use chain_impl_mockchain::utxo::Entry;
use serde::{Deserialize, Serialize};
/// the Unspent Transaction Output information.
///
/// This object contains all the information we know about a [UTxO].
/// This data is different from the [`AccountState`] which represents
/// the state of an account in the ledger.
///
/// [UTxO]: https://en.wikipedia.org/wiki/Unspent_transaction_output
/// [`AccountState`]: ./struct.AccountState.html
///
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct UTxOInfo {
transaction_id: Hash,
index_in_transaction: u8,
address: Address,
associated_fund: Value,
}
impl UTxOInfo {
pub fn new(
transaction_id: Hash,
index_in_transaction: u8,
address: Address,
associated_fund: Value,
) -> Self {
UTxOInfo {
transaction_id,
index_in_transaction,
address,
associated_fund,
}
}
/// the Transaction identifier (its hash) that will be used to reference
/// to this UTxO as an input in a new transaction.
///
/// Along with the `index_in_transaction` this uniquely identifies an UTxO
#[inline]
pub fn transaction_id(&self) -> &Hash {
&self.transaction_id
}
/// the output index, will be needed as an input in a new transaction.
///
/// Along with the `transaction_id` this uniquely identifies an UTxO
#[inline]
pub fn index_in_transaction(&self) -> u8 {
self.index_in_transaction
}
/// the address to identify who can spend the UTxO. This is part of the
/// data actually present as output of the source transaction.
#[inline]
pub fn address(&self) -> &Address {
&self.address
}
/// the associated funds in this UTxO. This will be used in a new transaction
/// input to guarantee self validation of the transaction's balance.
#[inline]
pub fn associated_fund(&self) -> &Value {
&self.associated_fund
}
}
/* ---------------- Conversion --------------------------------------------- */
impl<'a> From<Entry<'a, chain_addr::Address>> for UTxOInfo {
fn from(utxo_entry: Entry<'a, chain_addr::Address>) -> Self {
Self {
transaction_id: utxo_entry.fragment_id.into(),
index_in_transaction: utxo_entry.output_index,
address: utxo_entry.output.address.clone().into(),
associated_fund: utxo_entry.output.value.into(),
}
}
}
/// The UTxO data about output without its location in blockchain
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct UTxOOutputInfo {
address: Address,
value: Value,
}
/* ---------------- Conversion --------------------------------------------- */
impl UTxOOutputInfo {
pub fn into_utxo_info(self, fragment_id: Hash, index_in_transaction: u8) -> UTxOInfo {
UTxOInfo {
transaction_id: fragment_id,
index_in_transaction,
address: self.address,
associated_fund: self.value,
}
}
}