global: one big snapshot

This commit is contained in:
nym21
2025-08-02 16:59:22 +02:00
parent aa8b47a3dd
commit f7aa9424db
252 changed files with 6283 additions and 5264 deletions

View File

@@ -0,0 +1,448 @@
use std::fmt;
use bitcoin::{
Address, Network, ScriptBuf,
hex::{Case, DisplayHex},
opcodes,
script::Builder,
};
use brk_error::Error;
use derive_deref::{Deref, DerefMut};
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::OutputType;
#[derive(Debug, PartialEq, Eq)]
pub enum AddressBytes {
P2PK65(P2PK65Bytes),
P2PK33(P2PK33Bytes),
P2PKH(P2PKHBytes),
P2SH(P2SHBytes),
P2WPKH(P2WPKHBytes),
P2WSH(P2WSHBytes),
P2TR(P2TRBytes),
P2A(P2ABytes),
}
impl AddressBytes {
pub fn as_slice(&self) -> &[u8] {
match self {
AddressBytes::P2PK65(bytes) => &bytes[..],
AddressBytes::P2PK33(bytes) => &bytes[..],
AddressBytes::P2PKH(bytes) => &bytes[..],
AddressBytes::P2SH(bytes) => &bytes[..],
AddressBytes::P2WPKH(bytes) => &bytes[..],
AddressBytes::P2WSH(bytes) => &bytes[..],
AddressBytes::P2TR(bytes) => &bytes[..],
AddressBytes::P2A(bytes) => &bytes[..],
}
}
}
impl TryFrom<(&ScriptBuf, OutputType)> for AddressBytes {
type Error = Error;
fn try_from(tuple: (&ScriptBuf, OutputType)) -> Result<Self, Self::Error> {
let (script, outputtype) = tuple;
match outputtype {
OutputType::P2PK65 => {
let bytes = script.as_bytes();
let bytes = match bytes.len() {
67 => &bytes[1..66],
_ => {
dbg!(bytes);
return Err(Error::WrongLength);
}
};
Ok(Self::P2PK65(P2PK65Bytes(U8x65::from(bytes))))
}
OutputType::P2PK33 => {
let bytes = script.as_bytes();
let bytes = match bytes.len() {
35 => &bytes[1..34],
_ => {
dbg!(bytes);
return Err(Error::WrongLength);
}
};
Ok(Self::P2PK33(P2PK33Bytes(U8x33::from(bytes))))
}
OutputType::P2PKH => {
let bytes = &script.as_bytes()[3..23];
Ok(Self::P2PKH(P2PKHBytes(U8x20::from(bytes))))
}
OutputType::P2SH => {
let bytes = &script.as_bytes()[2..22];
Ok(Self::P2SH(P2SHBytes(U8x20::from(bytes))))
}
OutputType::P2WPKH => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2WPKH(P2WPKHBytes(U8x20::from(bytes))))
}
OutputType::P2WSH => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2WSH(P2WSHBytes(U8x32::from(bytes))))
}
OutputType::P2TR => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2TR(P2TRBytes(U8x32::from(bytes))))
}
OutputType::P2A => {
let bytes = &script.as_bytes()[2..];
Ok(Self::P2A(P2ABytes(U8x2::from(bytes))))
}
OutputType::P2MS => Err(Error::WrongAddressType),
OutputType::Unknown => Err(Error::WrongAddressType),
OutputType::Empty => Err(Error::WrongAddressType),
OutputType::OpReturn => Err(Error::WrongAddressType),
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2PK65Bytes(U8x65);
impl fmt::Display for P2PK65Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex_string(Case::Lower))
}
}
impl Serialize for P2PK65Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2PK65Bytes> for AddressBytes {
fn from(value: P2PK65Bytes) -> Self {
Self::P2PK65(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2PK33Bytes(U8x33);
impl fmt::Display for P2PK33Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex_string(Case::Lower))
}
}
impl Serialize for P2PK33Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2PK33Bytes> for AddressBytes {
fn from(value: P2PK33Bytes) -> Self {
Self::P2PK33(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2PKHBytes(U8x20);
impl fmt::Display for P2PKHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new()
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(*self.0)
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2PKHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2PKHBytes> for AddressBytes {
fn from(value: P2PKHBytes) -> Self {
Self::P2PKH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2SHBytes(U8x20);
impl fmt::Display for P2SHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new()
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(*self.0)
.push_opcode(opcodes::all::OP_EQUAL)
.into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2SHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2SHBytes> for AddressBytes {
fn from(value: P2SHBytes) -> Self {
Self::P2SH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2WPKHBytes(U8x20);
impl fmt::Display for P2WPKHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(0).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2WPKHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2WPKHBytes> for AddressBytes {
fn from(value: P2WPKHBytes) -> Self {
Self::P2WPKH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2WSHBytes(U8x32);
impl fmt::Display for P2WSHBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(0).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2WSHBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2WSHBytes> for AddressBytes {
fn from(value: P2WSHBytes) -> Self {
Self::P2WSH(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2TRBytes(U8x32);
impl fmt::Display for P2TRBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(1).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2TRBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2TRBytes> for AddressBytes {
fn from(value: P2TRBytes) -> Self {
Self::P2TR(value)
}
}
#[derive(Debug, Clone, Deref, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct P2ABytes(U8x2);
impl fmt::Display for P2ABytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let script = Builder::new().push_int(1).push_slice(*self.0).into_script();
let address = Address::from_script(&script, Network::Bitcoin).unwrap();
write!(f, "{address}")
}
}
impl Serialize for P2ABytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.to_string())
}
}
impl From<P2ABytes> for AddressBytes {
fn from(value: P2ABytes) -> Self {
Self::P2A(value)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x2([u8; 2]);
impl From<&[u8]> for U8x2 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 2];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x20([u8; 20]);
impl From<&[u8]> for U8x20 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 20];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x32([u8; 32]);
impl From<&[u8]> for U8x32 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 32];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x33(#[serde(with = "serde_bytes")] [u8; 33]);
impl From<&[u8]> for U8x33 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 33];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x64(#[serde(with = "serde_bytes")] [u8; 64]);
impl From<&[u8]> for U8x64 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 64];
arr.copy_from_slice(slice);
Self(arr)
}
}
#[derive(
Debug,
Clone,
Deref,
DerefMut,
PartialEq,
Eq,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct U8x65(#[serde(with = "serde_bytes")] [u8; 65]);
impl From<&[u8]> for U8x65 {
fn from(slice: &[u8]) -> Self {
let mut arr = [0; 65];
arr.copy_from_slice(slice);
Self(arr)
}
}

View File

@@ -0,0 +1,54 @@
use byteview::ByteView;
use derive_deref::Deref;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{AddressBytes, OutputType};
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
)]
pub struct AddressBytesHash([u8; 8]);
impl From<(&AddressBytes, OutputType)> for AddressBytesHash {
fn from((address_bytes, outputtype): (&AddressBytes, OutputType)) -> Self {
let mut slice = rapidhash::v3::rapidhash_v3(address_bytes.as_slice()).to_le_bytes();
slice[0] = slice[0].wrapping_add(outputtype as u8);
Self(slice)
}
}
impl From<[u8; 8]> for AddressBytesHash {
fn from(value: [u8; 8]) -> Self {
Self(value)
}
}
impl From<ByteView> for AddressBytesHash {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
}
}
impl From<&AddressBytesHash> for ByteView {
fn from(value: &AddressBytesHash) -> Self {
Self::new(value.as_bytes())
}
}
impl From<AddressBytesHash> for ByteView {
fn from(value: AddressBytesHash) -> Self {
Self::from(&value)
}
}

View File

@@ -0,0 +1,61 @@
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{
TypeIndex,
structs::{EmptyAddressIndex, LoadedAddressIndex},
};
const MIN_EMPTY_INDEX: u32 = u32::MAX - 4_000_000_000;
#[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout,
)]
pub struct AnyAddressIndex(TypeIndex);
impl AnyAddressIndex {
pub fn to_enum(&self) -> AnyAddressDataIndexEnum {
AnyAddressDataIndexEnum::from(*self)
}
}
impl From<LoadedAddressIndex> for AnyAddressIndex {
fn from(value: LoadedAddressIndex) -> Self {
if u32::from(value) >= MIN_EMPTY_INDEX {
panic!("")
}
Self(*value)
}
}
impl From<EmptyAddressIndex> for AnyAddressIndex {
fn from(value: EmptyAddressIndex) -> Self {
Self(*value + MIN_EMPTY_INDEX)
}
}
impl Serialize for AnyAddressIndex {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_enum().serialize(serializer)
}
}
#[derive(Debug, Serialize)]
pub enum AnyAddressDataIndexEnum {
Loaded(LoadedAddressIndex),
Empty(EmptyAddressIndex),
}
impl From<AnyAddressIndex> for AnyAddressDataIndexEnum {
fn from(value: AnyAddressIndex) -> Self {
let uvalue = u32::from(value.0);
if uvalue >= MIN_EMPTY_INDEX {
Self::Empty(EmptyAddressIndex::from(uvalue - MIN_EMPTY_INDEX))
} else {
Self::Loaded(LoadedAddressIndex::from(value.0))
}
}
}

View File

@@ -0,0 +1,134 @@
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div, Mul},
};
use brk_vecs::{CheckedSub, StoredCompressed};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Sats, StoredF64};
#[derive(
Debug,
Default,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct Bitcoin(f64);
impl Add for Bitcoin {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(Sats::from(self) + Sats::from(rhs))
}
}
impl AddAssign for Bitcoin {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Mul for Bitcoin {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::from(Sats::from(self) * Sats::from(rhs))
}
}
impl Mul<usize> for Bitcoin {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self::from(Sats::from(self) * rhs)
}
}
impl Div<Bitcoin> for Bitcoin {
type Output = StoredF64;
fn div(self, rhs: Bitcoin) -> Self::Output {
StoredF64::from(self.0 / rhs.0)
// Self::from(Sats::from(self) / Sats::from(rhs))
}
}
impl Div<usize> for Bitcoin {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(Sats::from(self) / rhs)
}
}
impl From<Sats> for Bitcoin {
fn from(value: Sats) -> Self {
Self(f64::from(value) / (f64::from(Sats::ONE_BTC)))
}
}
impl From<f64> for Bitcoin {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<StoredF64> for Bitcoin {
fn from(value: StoredF64) -> Self {
Self(*value)
}
}
impl From<Bitcoin> for f64 {
fn from(value: Bitcoin) -> Self {
value.0
}
}
impl From<usize> for Bitcoin {
fn from(value: usize) -> Self {
Self(value as f64)
}
}
impl PartialEq for Bitcoin {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for Bitcoin {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for Bitcoin {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Bitcoin {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl CheckedSub<usize> for Bitcoin {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Self(self.0 - rhs as f64))
}
}

View File

@@ -0,0 +1,52 @@
use std::{fmt, mem};
use bitcoin::hashes::Hash;
use bitcoincore_rpc::{Client, RpcApi};
use derive_deref::Deref;
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
#[derive(Debug, Deref, Clone, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct BlockHash([u8; 32]);
impl From<bitcoin::BlockHash> for BlockHash {
fn from(value: bitcoin::BlockHash) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<BlockHash> for bitcoin::BlockHash {
fn from(value: BlockHash) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<&BlockHash> for bitcoin::BlockHash {
fn from(value: &BlockHash) -> Self {
bitcoin::BlockHash::from_slice(&value.0).unwrap()
}
}
impl TryFrom<(&Client, Height)> for BlockHash {
type Error = bitcoincore_rpc::Error;
fn try_from((rpc, height): (&Client, Height)) -> Result<Self, Self::Error> {
Ok(Self::from(rpc.get_block_hash(u64::from(height))?))
}
}
impl fmt::Display for BlockHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", bitcoin::BlockHash::from(self))
}
}
impl Serialize for BlockHash {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

View File

@@ -0,0 +1,54 @@
use byteview::ByteView;
use derive_deref::Deref;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_8bytes;
use super::BlockHash;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
)]
pub struct BlockHashPrefix([u8; 8]);
impl From<BlockHash> for BlockHashPrefix {
fn from(value: BlockHash) -> Self {
Self::from(&value)
}
}
impl From<&BlockHash> for BlockHashPrefix {
fn from(value: &BlockHash) -> Self {
Self(copy_first_8bytes(&value[..]).unwrap())
}
}
impl From<ByteView> for BlockHashPrefix {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
}
}
impl From<&BlockHashPrefix> for ByteView {
fn from(value: &BlockHashPrefix) -> Self {
Self::new(value.as_bytes())
}
}
impl From<BlockHashPrefix> for ByteView {
fn from(value: BlockHashPrefix) -> Self {
Self::from(&value)
}
}

View File

@@ -0,0 +1,169 @@
use std::ops::{Add, Div, Mul};
use brk_vecs::{CheckedSub, StoredCompressed};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Dollars;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct Cents(i64);
const SIGNIFICANT_DIGITS: i32 = 4;
impl Cents {
pub const fn mint(value: i64) -> Self {
Self(value)
}
pub fn round_to_4_digits(self) -> Self {
let v = self.0;
let ilog10 = v.checked_ilog10().unwrap_or(0) as i32;
Self::from(if ilog10 >= SIGNIFICANT_DIGITS {
let log_diff = ilog10 - SIGNIFICANT_DIGITS + 1;
let pow = 10.0_f64.powi(log_diff);
((v as f64 / pow).round() * pow) as i64
} else {
v
})
}
}
impl From<Dollars> for Cents {
fn from(value: Dollars) -> Self {
Self((*value * 100.0).round() as i64)
}
}
impl From<Cents> for f64 {
fn from(value: Cents) -> Self {
value.0 as f64
}
}
impl From<i64> for Cents {
fn from(value: i64) -> Self {
Self(value)
}
}
impl From<u64> for Cents {
fn from(value: u64) -> Self {
Self(value as i64)
}
}
impl From<Cents> for usize {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!()
}
value.0 as usize
}
}
impl From<usize> for Cents {
fn from(value: usize) -> Self {
Self(value as i64)
}
}
impl From<Cents> for i64 {
fn from(value: Cents) -> Self {
value.0
}
}
impl From<Cents> for u64 {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!("Shouldn't convert neg cents to u64")
}
value.0 as u64
}
}
impl Add for Cents {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Div<Cents> for Cents {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl Div<usize> for Cents {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as i64)
}
}
impl From<u128> for Cents {
fn from(value: u128) -> Self {
if value > i64::MAX as u128 {
panic!("u128 bigger than i64")
}
Self(value as i64)
}
}
impl From<Cents> for u128 {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!("Shouldn't convert neg cents to u128")
}
value.0 as u128
}
}
impl Mul<Cents> for Cents {
type Output = Cents;
fn mul(self, rhs: Cents) -> Self::Output {
Self(self.0.checked_mul(rhs.0).unwrap())
}
}
impl Mul<i64> for Cents {
type Output = Cents;
fn mul(self, rhs: i64) -> Self::Output {
Self(self.0 * rhs)
}
}
impl Mul<usize> for Cents {
type Output = Cents;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as i64)
}
}
impl CheckedSub for Cents {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Cents::from)
}
}

View File

@@ -0,0 +1,110 @@
use brk_vecs::StoredCompressed;
use jiff::{Span, civil::Date as Date_, tz::TimeZone};
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{DateIndex, Timestamp};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct Date(u32);
impl Date {
pub const INDEX_ZERO: Self = Self(20090103);
pub const INDEX_ZERO_: Date_ = Date_::constant(2009, 1, 3);
pub const INDEX_ONE: Self = Self(20090109);
pub const INDEX_ONE_: Date_ = Date_::constant(2009, 1, 9);
pub const MIN_RATIO: Self = Self(20120101);
pub fn new(year: u16, month: u8, day: u8) -> Self {
Self(year as u32 * 1_00_00 + month as u32 * 1_00 + day as u32)
}
pub fn year(&self) -> u16 {
(self.0 / 1_00_00) as u16
}
pub fn month(&self) -> u8 {
((self.0 % 1_00_00) / 1_00) as u8
}
pub fn day(&self) -> u8 {
(self.0 % 1_00) as u8
}
pub fn into_jiff(self) -> Date_ {
self.into()
}
}
impl Default for Date {
fn default() -> Self {
Self::INDEX_ZERO
}
}
impl From<Date_> for Date {
fn from(value: Date_) -> Self {
Self::new(value.year() as u16, value.month() as u8, value.day() as u8)
}
}
impl From<Date> for Date_ {
fn from(value: Date) -> Self {
Self::new(value.year() as i16, value.month() as i8, value.day() as i8).unwrap()
}
}
impl From<Timestamp> for Date {
fn from(value: Timestamp) -> Self {
Self::from(Date_::from(
jiff::Timestamp::from(value).to_zoned(TimeZone::UTC),
))
}
}
impl From<DateIndex> for Date {
fn from(value: DateIndex) -> Self {
if value == DateIndex::default() {
Date::INDEX_ZERO
} else {
Self::from(
Self::INDEX_ONE_
.checked_add(Span::new().days(i64::from(value) - 1))
.unwrap(),
)
}
}
}
impl std::fmt::Display for Date {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
"{}-{:0>2}-{:0>2}",
self.year(),
self.month(),
self.day()
))
}
}
impl Serialize for Date {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

View File

@@ -0,0 +1,246 @@
use std::{
fmt,
ops::{Add, Rem},
};
use brk_error::Error;
use brk_vecs::{CheckedSub, FromCoarserIndex, Printable, StoredCompressed};
use jiff::Span;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{DecadeIndex, MonthIndex, QuarterIndex, SemesterIndex, WeekIndex, YearIndex};
use super::Date;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct DateIndex(u16);
impl DateIndex {
pub const BYTES: usize = size_of::<Self>();
}
impl From<DateIndex> for usize {
fn from(value: DateIndex) -> Self {
value.0 as usize
}
}
impl From<DateIndex> for u64 {
fn from(value: DateIndex) -> Self {
value.0 as u64
}
}
impl From<usize> for DateIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<DateIndex> for i64 {
fn from(value: DateIndex) -> Self {
value.0 as i64
}
}
impl Add<usize> for DateIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u16)
}
}
impl TryFrom<Date> for DateIndex {
type Error = Error;
fn try_from(value: Date) -> Result<Self, Self::Error> {
let value_ = jiff::civil::Date::from(value);
if value_ < Date::INDEX_ZERO_ {
Err(Error::UnindexableDate)
} else if value == Date::INDEX_ZERO {
Ok(Self(0))
} else if value_ < Date::INDEX_ONE_ {
Err(Error::UnindexableDate)
} else if value == Date::INDEX_ONE {
Ok(Self(1))
} else {
Ok(Self(Date::INDEX_ONE_.until(value_)?.get_days() as u16 + 1))
}
}
}
impl CheckedSub for DateIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Rem<usize> for DateIndex {
type Output = Self;
fn rem(self, rhs: usize) -> Self::Output {
Self(self.0 % rhs as u16)
}
}
impl fmt::Display for DateIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Printable for DateIndex {
fn to_string() -> &'static str {
"dateindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["d", "date", "dateindex"]
}
}
impl FromCoarserIndex<WeekIndex> for DateIndex {
fn min_from(coarser: WeekIndex) -> usize {
let coarser = usize::from(coarser);
if coarser == 0 {
0
} else if coarser == 1 {
1
} else {
4 + (coarser - 2) * 7
}
}
fn max_from_(coarser: WeekIndex) -> usize {
let coarser = usize::from(coarser);
if coarser == 0 {
0
} else if coarser == 1 {
3
} else {
3 + (coarser - 1) * 7
}
}
}
impl FromCoarserIndex<MonthIndex> for DateIndex {
fn min_from(coarser: MonthIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
let d = Date::new(2009, 1, 1)
.into_jiff()
.checked_add(Span::new().months(coarser))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
fn max_from_(coarser: MonthIndex) -> usize {
let d = Date::new(2009, 1, 31)
.into_jiff()
.checked_add(Span::new().months(u16::from(coarser)))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
impl FromCoarserIndex<QuarterIndex> for DateIndex {
fn min_from(coarser: QuarterIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
let d = Date::new(2009, 1, 1)
.into_jiff()
.checked_add(Span::new().months(3 * coarser))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
fn max_from_(coarser: QuarterIndex) -> usize {
let d = Date::new(2009, 3, 31)
.into_jiff()
.checked_add(Span::new().months(3 * u16::from(coarser)))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
impl FromCoarserIndex<SemesterIndex> for DateIndex {
fn min_from(coarser: SemesterIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
let d = Date::new(2009, 1, 1)
.into_jiff()
.checked_add(Span::new().months(6 * coarser))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
fn max_from_(coarser: SemesterIndex) -> usize {
let d = Date::new(2009, 5, 31)
.into_jiff()
.checked_add(Span::new().months(1 + 6 * u16::from(coarser)))
.unwrap();
DateIndex::try_from(Date::from(d)).unwrap().into()
}
}
impl FromCoarserIndex<YearIndex> for DateIndex {
fn min_from(coarser: YearIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
Self::try_from(Date::new(2009 + coarser, 1, 1))
.unwrap()
.into()
}
}
fn max_from_(coarser: YearIndex) -> usize {
Self::try_from(Date::new(2009 + u16::from(coarser), 12, 31))
.unwrap()
.into()
}
}
impl FromCoarserIndex<DecadeIndex> for DateIndex {
fn min_from(coarser: DecadeIndex) -> usize {
let coarser = u16::from(coarser);
if coarser == 0 {
0
} else {
Self::try_from(Date::new(2000 + 10 * coarser, 1, 1))
.unwrap()
.into()
}
}
fn max_from_(coarser: DecadeIndex) -> usize {
let coarser = u16::from(coarser);
Self::try_from(Date::new(2009 + (10 * coarser), 12, 31))
.unwrap()
.into()
}
}

View File

@@ -0,0 +1,125 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex, YearIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct DecadeIndex(u16);
impl From<u16> for DecadeIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<DecadeIndex> for u16 {
fn from(value: DecadeIndex) -> Self {
value.0
}
}
impl From<usize> for DecadeIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<DecadeIndex> for usize {
fn from(value: DecadeIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for DecadeIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<DecadeIndex> for DecadeIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for DecadeIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for DecadeIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<DateIndex> for DecadeIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for DecadeIndex {
fn from(value: Date) -> Self {
let year = value.year();
if year < 2000 {
panic!("unsupported")
}
Self((year - 2000) / 10)
}
}
impl CheckedSub for DecadeIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl From<YearIndex> for DecadeIndex {
fn from(value: YearIndex) -> Self {
let v = usize::from(value);
if v == 0 {
Self(0)
} else {
Self((((v - 1) / 10) + 1) as u16)
}
}
}
impl Printable for DecadeIndex {
fn to_string() -> &'static str {
"decadeindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["decade", "decadeindex"]
}
}

View File

@@ -0,0 +1,98 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct DifficultyEpoch(u16);
impl From<u16> for DifficultyEpoch {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for DifficultyEpoch {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<DifficultyEpoch> for usize {
fn from(value: DifficultyEpoch) -> Self {
value.0 as usize
}
}
impl Add for DifficultyEpoch {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for DifficultyEpoch {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Add<usize> for DifficultyEpoch {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Div<usize> for DifficultyEpoch {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}
impl From<Height> for DifficultyEpoch {
fn from(value: Height) -> Self {
Self((u32::from(value) / 2016) as u16)
}
}
impl CheckedSub for DifficultyEpoch {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for DifficultyEpoch {
fn to_string() -> &'static str {
"difficultyepoch"
}
fn to_possible_strings() -> &'static [&'static str] {
&["difficulty", "difficultyepoch"]
}
}

View File

@@ -0,0 +1,360 @@
use std::{
cmp::Ordering,
f64,
ops::{Add, AddAssign, Div, Mul},
};
use brk_vecs::{CheckedSub, StoredCompressed};
use derive_deref::Deref;
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Bitcoin, Cents, Close, High, Sats, StoredF32, StoredF64};
#[derive(
Debug,
Default,
Clone,
Copy,
Deref,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
Deserialize,
StoredCompressed,
)]
pub struct Dollars(f64);
impl Dollars {
pub const ZERO: Self = Self(0.0);
pub const NAN: Self = Self(f64::NAN);
pub const fn mint(dollars: f64) -> Self {
Self(dollars)
}
pub fn round_nearest_cent(self) -> Self {
Dollars((self.0 * 100.0).round() / 100.0)
}
pub fn round_to_4_digits(self) -> Self {
Self::from(Cents::from(self).round_to_4_digits())
}
}
impl From<f32> for Dollars {
fn from(value: f32) -> Self {
Self(value as f64)
}
}
impl From<f64> for Dollars {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<Cents> for Dollars {
fn from(value: Cents) -> Self {
Self(f64::from(value) / 100.0)
}
}
impl From<Dollars> for f32 {
fn from(value: Dollars) -> Self {
value.0 as f32
}
}
impl From<Dollars> for f64 {
fn from(value: Dollars) -> Self {
value.0
}
}
impl From<Close<Dollars>> for Dollars {
fn from(value: Close<Dollars>) -> Self {
Self(value.0)
}
}
impl From<High<Dollars>> for Dollars {
fn from(value: High<Dollars>) -> Self {
Self(value.0)
}
}
impl From<usize> for Dollars {
fn from(value: usize) -> Self {
Self(value as f64)
}
}
impl Add for Dollars {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(Cents::from(self) + Cents::from(rhs))
}
}
impl Div<Dollars> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
if self.is_nan() || rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(f64::from(self) / f64::from(rhs))
}
}
}
impl Div<Close<Dollars>> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Close<Dollars>) -> Self::Output {
if self.is_nan() || *rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(f64::from(self) / f64::from(*rhs))
}
}
}
impl Div<Dollars> for Close<Dollars> {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
if self.is_nan() || rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(f64::from(*self) / f64::from(rhs))
}
}
}
impl Div<usize> for Dollars {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
if self.is_nan() || rhs == 0 {
Dollars::NAN
} else {
Self::from(Cents::from(self) / rhs)
}
}
}
impl Div<StoredF64> for Dollars {
type Output = Self;
fn div(self, rhs: StoredF64) -> Self::Output {
self / f64::from(rhs)
}
}
impl Div<f64> for Dollars {
type Output = Self;
fn div(self, rhs: f64) -> Self::Output {
if self.is_nan() || rhs == 0.0 {
Dollars::NAN
} else {
Dollars::from(Cents::from(Self::from(self.0 / rhs)))
}
}
}
impl Div<Bitcoin> for Dollars {
type Output = Self;
fn div(self, rhs: Bitcoin) -> Self::Output {
if self.is_nan() {
self
} else {
Self(f64::from(self) / f64::from(rhs))
}
}
}
impl Mul<Dollars> for Dollars {
type Output = Self;
fn mul(self, rhs: Dollars) -> Self::Output {
Self::from(Cents::from(self) * Cents::from(rhs))
}
}
impl Mul<Close<Dollars>> for Dollars {
type Output = Self;
fn mul(self, rhs: Close<Dollars>) -> Self::Output {
Self::from(Cents::from(self) * Cents::from(*rhs))
}
}
impl Mul<Dollars> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: Dollars) -> Self::Output {
Dollars::from(Cents::from(*self) * Cents::from(rhs))
}
}
impl Mul<usize> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: usize) -> Self::Output {
Dollars::from(Cents::from(*self) * rhs)
}
}
impl Mul<StoredF64> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: StoredF64) -> Self::Output {
*self * rhs
}
}
impl Mul<f64> for Dollars {
type Output = Dollars;
fn mul(self, rhs: f64) -> Self::Output {
if rhs.fract() != 0.0 {
Self::from(self.0 * rhs)
} else {
self * rhs as i64
}
}
}
impl Mul<Bitcoin> for Dollars {
type Output = Self;
fn mul(self, rhs: Bitcoin) -> Self::Output {
self * Sats::from(rhs)
}
}
impl Mul<Bitcoin> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: Bitcoin) -> Self::Output {
*self * Sats::from(rhs)
}
}
impl Mul<Sats> for Dollars {
type Output = Self;
fn mul(self, rhs: Sats) -> Self::Output {
if self.is_nan() {
self
} else {
Self::from(Cents::from(
u128::from(rhs) * u128::from(Cents::from(self)) / Sats::ONE_BTC_U128,
))
}
}
}
impl Mul<StoredF32> for Dollars {
type Output = Self;
fn mul(self, rhs: StoredF32) -> Self::Output {
self * *rhs as f64
}
}
impl Mul<StoredF64> for Dollars {
type Output = Self;
fn mul(self, rhs: StoredF64) -> Self::Output {
self * *rhs
}
}
impl Mul<i64> for Dollars {
type Output = Self;
fn mul(self, rhs: i64) -> Self::Output {
Self::from(Cents::from(self) * rhs)
}
}
impl Mul<usize> for Dollars {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
if self.is_nan() {
self
} else {
Self::from(Cents::from(self) * rhs)
}
}
}
impl From<u128> for Dollars {
fn from(value: u128) -> Self {
Self::from(Cents::from(value))
}
}
impl From<StoredF64> for Dollars {
fn from(value: StoredF64) -> Self {
Self(*value)
}
}
impl From<Close<Dollars>> for u128 {
fn from(value: Close<Dollars>) -> Self {
u128::from(*value)
}
}
impl From<Dollars> for u128 {
fn from(value: Dollars) -> Self {
u128::from(Cents::from(value))
}
}
impl AddAssign for Dollars {
fn add_assign(&mut self, rhs: Self) {
*self = Dollars::from(Cents::from(*self) + Cents::from(rhs));
}
}
impl CheckedSub for Dollars {
fn checked_sub(self, rhs: Self) -> Option<Self> {
if self.is_nan() {
Some(self)
} else {
Cents::from(self)
.checked_sub(Cents::from(rhs))
.map(Dollars::from)
}
}
}
impl CheckedSub<usize> for Dollars {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Dollars::from(
Cents::from(self).checked_sub(Cents::from(rhs)).unwrap(),
))
}
}
impl PartialEq for Dollars {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for Dollars {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for Dollars {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Dollars {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}

View File

@@ -0,0 +1,27 @@
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{LoadedAddressData, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
pub struct EmptyAddressData {
pub transfered: Sats,
}
impl From<LoadedAddressData> for EmptyAddressData {
fn from(value: LoadedAddressData) -> Self {
Self::from(&value)
}
}
impl From<&LoadedAddressData> for EmptyAddressData {
fn from(value: &LoadedAddressData) -> Self {
if value.sent != value.received {
dbg!(&value);
panic!("Trying to convert not empty wallet to empty !");
}
Self {
transfered: value.sent,
}
}
}

View File

@@ -0,0 +1,70 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Deref,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct EmptyAddressIndex(TypeIndex);
impl From<TypeIndex> for EmptyAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<usize> for EmptyAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<u32> for EmptyAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<EmptyAddressIndex> for usize {
fn from(value: EmptyAddressIndex) -> Self {
usize::from(value.0)
}
}
impl Add<usize> for EmptyAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<EmptyAddressIndex> for EmptyAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for EmptyAddressIndex {
fn to_string() -> &'static str {
"emptyaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["emptyaddr", "emptyaddressindex"]
}
}

View File

@@ -0,0 +1,68 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct EmptyOutputIndex(TypeIndex);
impl From<TypeIndex> for EmptyOutputIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<EmptyOutputIndex> for u64 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(value.0)
}
}
impl From<EmptyOutputIndex> for usize {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(value.0)
}
}
impl From<usize> for EmptyOutputIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for EmptyOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<EmptyOutputIndex> for EmptyOutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for EmptyOutputIndex {
fn to_string() -> &'static str {
"emptyoutputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["emptyout", "emptyoutputindex"]
}
}

View File

@@ -0,0 +1,90 @@
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div},
};
use brk_vecs::StoredCompressed;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Sats, StoredU64};
#[derive(
Debug, Clone, Copy, Serialize, FromBytes, Immutable, IntoBytes, KnownLayout, StoredCompressed,
)]
pub struct Feerate(f32);
impl From<(Sats, StoredU64)> for Feerate {
fn from((sats, vsize): (Sats, StoredU64)) -> Self {
Self((f64::from(sats) / f64::from(vsize)) as f32)
}
}
impl From<f64> for Feerate {
fn from(value: f64) -> Self {
Self(value as f32)
}
}
impl From<Feerate> for f64 {
fn from(value: Feerate) -> Self {
value.0 as f64
}
}
impl Add for Feerate {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Feerate {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Div<usize> for Feerate {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self((self.0 as f64 / rhs as f64) as f32)
}
}
impl From<usize> for Feerate {
fn from(value: usize) -> Self {
Self(value as f32)
}
}
impl PartialEq for Feerate {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for Feerate {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for Feerate {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Feerate {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}

View File

@@ -0,0 +1,104 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct HalvingEpoch(u16);
impl HalvingEpoch {
pub fn new(value: u16) -> Self {
Self(value)
}
}
impl From<u16> for HalvingEpoch {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for HalvingEpoch {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<HalvingEpoch> for usize {
fn from(value: HalvingEpoch) -> Self {
value.0 as usize
}
}
impl Add for HalvingEpoch {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for HalvingEpoch {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Add<usize> for HalvingEpoch {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl From<Height> for HalvingEpoch {
fn from(value: Height) -> Self {
Self((u32::from(value) / 210_000) as u16)
}
}
impl CheckedSub for HalvingEpoch {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for HalvingEpoch {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}
impl Printable for HalvingEpoch {
fn to_string() -> &'static str {
"halvingepoch"
}
fn to_possible_strings() -> &'static [&'static str] {
&["halving", "halvingepoch"]
}
}

View File

@@ -0,0 +1,259 @@
use std::{
fmt::{self, Debug},
ops::{Add, AddAssign, Rem},
};
use bitcoincore_rpc::{Client, RpcApi};
use brk_vecs::{CheckedSub, Printable, Stamp, StoredCompressed};
use byteview::ByteView;
use derive_deref::Deref;
use serde::{Deserialize, Serialize};
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_4bytes;
use super::StoredU64;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Deref,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct Height(u32);
impl Height {
pub const ZERO: Self = Self(0);
pub const MAX: Self = Self(u32::MAX);
pub const fn new(height: u32) -> Self {
Self(height)
}
pub fn write(&self, path: &std::path::Path) -> Result<(), std::io::Error> {
std::fs::write(path, self.as_bytes())
}
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn incremented(self) -> Self {
Self(self.0 + 1)
}
pub fn decrement(&mut self) {
*self = self.decremented().unwrap();
}
pub fn decremented(self) -> Option<Self> {
self.checked_sub(1_u32)
}
pub fn is_zero(self) -> bool {
self == Self::ZERO
}
}
impl PartialEq<u64> for Height {
fn eq(&self, other: &u64) -> bool {
self.0 == *other as u32
}
}
impl Add<Height> for Height {
type Output = Self;
fn add(self, rhs: Height) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl Add<u32> for Height {
type Output = Self;
fn add(self, rhs: u32) -> Self::Output {
Self::from(self.0 + rhs)
}
}
impl Add<u64> for Height {
type Output = Self;
fn add(self, rhs: u64) -> Self::Output {
Self::from(self.0 + rhs as u32)
}
}
impl Add<usize> for Height {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u32)
}
}
impl CheckedSub<Height> for Height {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl CheckedSub<u32> for Height {
fn checked_sub(self, rhs: u32) -> Option<Self> {
self.0.checked_sub(rhs).map(Height::from)
}
}
impl CheckedSub<usize> for Height {
fn checked_sub(self, rhs: usize) -> Option<Self> {
self.0.checked_sub(rhs as u32).map(Height::from)
}
}
impl AddAssign<usize> for Height {
fn add_assign(&mut self, rhs: usize) {
*self = self.add(rhs);
}
}
impl CheckedSub<u64> for Height {
fn checked_sub(self, rhs: u64) -> Option<Self> {
self.0.checked_sub(rhs as u32).map(Height::from)
}
}
impl AddAssign<u64> for Height {
fn add_assign(&mut self, rhs: u64) {
*self = self.add(rhs);
}
}
impl Rem<Height> for Height {
type Output = Self;
fn rem(self, rhs: Height) -> Self::Output {
Self(self.0.rem(rhs.0))
}
}
impl Rem<usize> for Height {
type Output = Self;
fn rem(self, rhs: usize) -> Self::Output {
Self(self.0.rem(Height::from(rhs).0))
}
}
impl fmt::Display for Height {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u32> for Height {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<u64> for Height {
fn from(value: u64) -> Self {
Self(value as u32)
}
}
impl From<StoredU64> for Height {
fn from(value: StoredU64) -> Self {
Self(*value as u32)
}
}
impl From<usize> for Height {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Height> for usize {
fn from(value: Height) -> Self {
value.0 as usize
}
}
impl From<Height> for u32 {
fn from(value: Height) -> Self {
value.0
}
}
impl From<Height> for u64 {
fn from(value: Height) -> Self {
value.0 as u64
}
}
impl TryFrom<&Client> for Height {
type Error = bitcoincore_rpc::Error;
fn try_from(value: &Client) -> Result<Self, Self::Error> {
Ok((value.get_block_count()? as usize).into())
}
}
impl From<bitcoin::locktime::absolute::Height> for Height {
fn from(value: bitcoin::locktime::absolute::Height) -> Self {
Self(value.to_consensus_u32())
}
}
impl TryFrom<&std::path::Path> for Height {
type Error = brk_error::Error;
fn try_from(value: &std::path::Path) -> Result<Self, Self::Error> {
Ok(Self::read_from_bytes(std::fs::read(value)?.as_slice())?.to_owned())
}
}
impl From<ByteView> for Height {
fn from(value: byteview::ByteView) -> Self {
Self(u32::from_be_bytes(copy_first_4bytes(&value).unwrap()))
}
}
impl From<Height> for ByteView {
fn from(value: Height) -> Self {
Self::new(&value.0.to_be_bytes())
}
}
impl From<Stamp> for Height {
fn from(value: Stamp) -> Self {
let u = u64::from(value);
assert!(u <= u32::MAX as u64);
Self(u as u32)
}
}
impl From<Height> for Stamp {
fn from(value: Height) -> Self {
Self::from(value.0 as u64)
}
}
impl Printable for Height {
fn to_string() -> &'static str {
"height"
}
fn to_possible_strings() -> &'static [&'static str] {
&["h", "height"]
}
}

View File

@@ -0,0 +1,108 @@
use std::ops::{Add, AddAssign};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Vin;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct InputIndex(u64);
impl InputIndex {
pub fn incremented(self) -> Self {
Self(*self + 1)
}
}
impl Add<InputIndex> for InputIndex {
type Output = Self;
fn add(self, rhs: InputIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<Vin> for InputIndex {
type Output = Self;
fn add(self, rhs: Vin) -> Self::Output {
Self(self.0 + u64::from(rhs))
}
}
impl Add<usize> for InputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u64)
}
}
impl AddAssign<InputIndex> for InputIndex {
fn add_assign(&mut self, rhs: InputIndex) {
self.0 += rhs.0
}
}
impl CheckedSub<InputIndex> for InputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self::from)
}
}
impl From<InputIndex> for u32 {
fn from(value: InputIndex) -> Self {
if value.0 > u32::MAX as u64 {
panic!()
}
value.0 as u32
}
}
impl From<u64> for InputIndex {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<InputIndex> for u64 {
fn from(value: InputIndex) -> Self {
value.0
}
}
impl From<usize> for InputIndex {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<InputIndex> for usize {
fn from(value: InputIndex) -> Self {
value.0 as usize
}
}
impl Printable for InputIndex {
fn to_string() -> &'static str {
"inputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["in", "inputindex"]
}
}

View File

@@ -0,0 +1,76 @@
use brk_error::{Error, Result};
use brk_vecs::CheckedSub;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{Bitcoin, Dollars, EmptyAddressData, Sats};
#[derive(Debug, Default, Clone, Serialize, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct LoadedAddressData {
pub sent: Sats,
pub received: Sats,
pub realized_cap: Dollars,
pub outputs_len: u32,
padding: u32,
}
impl LoadedAddressData {
pub fn amount(&self) -> Sats {
(u64::from(self.received) - u64::from(self.sent)).into()
}
pub fn realized_price(&self) -> Dollars {
(self.realized_cap / Bitcoin::from(self.amount())).round_to_4_digits()
}
#[inline]
pub fn has_0_sats(&self) -> bool {
self.amount() == Sats::ZERO
}
#[inline]
pub fn has_0_utxos(&self) -> bool {
self.outputs_len == 0
}
pub fn receive(&mut self, amount: Sats, price: Option<Dollars>) {
self.received += amount;
self.outputs_len += 1;
if let Some(price) = price {
self.realized_cap += price * amount;
}
}
pub fn send(&mut self, amount: Sats, previous_price: Option<Dollars>) -> Result<()> {
if self.amount() < amount {
return Err(Error::Str("Previous_amount smaller than sent amount"));
}
self.sent += amount;
self.outputs_len -= 1;
if let Some(previous_price) = previous_price {
self.realized_cap = self
.realized_cap
.checked_sub(previous_price * amount)
.unwrap();
}
Ok(())
}
}
impl From<EmptyAddressData> for LoadedAddressData {
fn from(value: EmptyAddressData) -> Self {
Self::from(&value)
}
}
impl From<&EmptyAddressData> for LoadedAddressData {
fn from(value: &EmptyAddressData) -> Self {
Self {
sent: value.transfered,
received: value.transfered,
realized_cap: Dollars::ZERO,
outputs_len: 0,
padding: 0,
}
}
}

View File

@@ -0,0 +1,68 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct LoadedAddressIndex(TypeIndex);
impl From<TypeIndex> for LoadedAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<usize> for LoadedAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<LoadedAddressIndex> for usize {
fn from(value: LoadedAddressIndex) -> Self {
usize::from(value.0)
}
}
impl From<LoadedAddressIndex> for u32 {
fn from(value: LoadedAddressIndex) -> Self {
u32::from(value.0)
}
}
impl Add<usize> for LoadedAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl CheckedSub<LoadedAddressIndex> for LoadedAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for LoadedAddressIndex {
fn to_string() -> &'static str {
"loadedaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["loadedaddr", "loadedaddressindex"]
}
}

View File

@@ -0,0 +1,149 @@
use brk_error::{Error, Result};
mod addressbytes;
mod addressbyteshash;
mod anyaddressindex;
mod bitcoin;
mod blockhash;
mod blockhashprefix;
mod cents;
mod date;
mod dateindex;
mod decadeindex;
mod difficultyepoch;
mod dollars;
mod emptyaddressdata;
mod emptyaddressindex;
mod emptyoutputindex;
mod feerate;
mod halvingepoch;
mod height;
mod inputindex;
mod loadedaddressdata;
mod loadedaddressindex;
mod monthindex;
mod ohlc;
mod opreturnindex;
mod outputindex;
mod outputtype;
mod p2aaddressindex;
mod p2msoutputindex;
mod p2pk33addressindex;
mod p2pk65addressindex;
mod p2pkhaddressindex;
mod p2shaddressindex;
mod p2traddressindex;
mod p2wpkhaddressindex;
mod p2wshaddressindex;
mod quarterindex;
mod rawlocktime;
mod sats;
mod semesterindex;
mod stored_bool;
mod stored_f32;
mod stored_f64;
mod stored_u16;
mod stored_u32;
mod stored_u64;
mod stored_u8;
mod timestamp;
mod txid;
mod txidprefix;
mod txindex;
mod txversion;
mod typeindex;
mod typeindex_with_outputindex;
mod unit;
mod unknownoutputindex;
mod vin;
mod vout;
mod weekindex;
mod weight;
mod yearindex;
pub use addressbytes::*;
pub use addressbyteshash::*;
pub use anyaddressindex::*;
pub use bitcoin::*;
pub use blockhash::*;
pub use blockhashprefix::*;
pub use cents::*;
pub use date::*;
pub use dateindex::*;
pub use decadeindex::*;
pub use difficultyepoch::*;
pub use dollars::*;
pub use emptyaddressdata::*;
pub use emptyaddressindex::*;
pub use emptyoutputindex::*;
pub use feerate::*;
pub use halvingepoch::*;
pub use height::*;
pub use inputindex::*;
pub use loadedaddressdata::*;
pub use loadedaddressindex::*;
pub use monthindex::*;
pub use ohlc::*;
pub use opreturnindex::*;
pub use outputindex::*;
pub use outputtype::*;
pub use p2aaddressindex::*;
pub use p2msoutputindex::*;
pub use p2pk33addressindex::*;
pub use p2pk65addressindex::*;
pub use p2pkhaddressindex::*;
pub use p2shaddressindex::*;
pub use p2traddressindex::*;
pub use p2wpkhaddressindex::*;
pub use p2wshaddressindex::*;
pub use quarterindex::*;
pub use rawlocktime::*;
pub use sats::*;
pub use semesterindex::*;
pub use stored_bool::*;
pub use stored_f32::*;
pub use stored_f64::*;
pub use stored_u8::*;
pub use stored_u16::*;
pub use stored_u32::*;
pub use stored_u64::*;
pub use timestamp::*;
pub use txid::*;
pub use txidprefix::*;
pub use txindex::*;
pub use txversion::*;
pub use typeindex::*;
pub use typeindex_with_outputindex::*;
pub use unit::*;
pub use unknownoutputindex::*;
pub use vin::*;
pub use vout::*;
pub use weekindex::*;
pub use weight::*;
pub use yearindex::*;
#[allow(clippy::result_unit_err)]
pub fn copy_first_4bytes(slice: &[u8]) -> Result<[u8; 4]> {
let mut buf: [u8; 4] = [0; 4];
let buf_len = buf.len();
if slice.len() < buf_len {
return Err(Error::Str("Buffer is too small to convert to 8 bytes"));
}
slice.iter().take(buf_len).enumerate().for_each(|(i, r)| {
buf[i] = *r;
});
Ok(buf)
}
#[allow(clippy::result_unit_err)]
pub fn copy_first_8bytes(slice: &[u8]) -> Result<[u8; 8]> {
let mut buf: [u8; 8] = [0; 8];
let buf_len = buf.len();
if slice.len() < buf_len {
return Err(Error::Str("Buffer is too small to convert to 8 bytes"));
}
slice.iter().take(buf_len).enumerate().for_each(|(i, r)| {
buf[i] = *r;
});
Ok(buf)
}

View File

@@ -0,0 +1,116 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex, YearIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct MonthIndex(u16);
impl From<u16> for MonthIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<MonthIndex> for u16 {
fn from(value: MonthIndex) -> Self {
value.0
}
}
impl From<usize> for MonthIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<MonthIndex> for u64 {
fn from(value: MonthIndex) -> Self {
value.0 as u64
}
}
impl From<MonthIndex> for usize {
fn from(value: MonthIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for MonthIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<MonthIndex> for MonthIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for MonthIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for MonthIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<DateIndex> for MonthIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for MonthIndex {
fn from(value: Date) -> Self {
Self(u16::from(YearIndex::from(value)) * 12 + value.month() as u16 - 1)
}
}
impl CheckedSub for MonthIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for MonthIndex {
fn to_string() -> &'static str {
"monthindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["m", "month", "monthindex"]
}
}

View File

@@ -0,0 +1,577 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::StoredCompressed;
use derive_deref::{Deref, DerefMut};
use serde::{Serialize, Serializer, ser::SerializeTuple};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Cents, Dollars, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct OHLCCents {
pub open: Open<Cents>,
pub high: High<Cents>,
pub low: Low<Cents>,
pub close: Close<Cents>,
}
impl From<(Open<Cents>, High<Cents>, Low<Cents>, Close<Cents>)> for OHLCCents {
fn from(value: (Open<Cents>, High<Cents>, Low<Cents>, Close<Cents>)) -> Self {
Self {
open: value.0,
high: value.1,
low: value.2,
close: value.3,
}
}
}
impl From<Close<Cents>> for OHLCCents {
fn from(value: Close<Cents>) -> Self {
Self {
open: Open::from(value),
high: High::from(value),
low: Low::from(value),
close: value,
}
}
}
impl Serialize for OHLCCents {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&self.open)?;
tup.serialize_element(&self.high)?;
tup.serialize_element(&self.low)?;
tup.serialize_element(&self.close)?;
tup.end()
}
}
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct OHLCDollars {
pub open: Open<Dollars>,
pub high: High<Dollars>,
pub low: Low<Dollars>,
pub close: Close<Dollars>,
}
impl Serialize for OHLCDollars {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&self.open)?;
tup.serialize_element(&self.high)?;
tup.serialize_element(&self.low)?;
tup.serialize_element(&self.close)?;
tup.end()
}
}
impl From<(Open<Dollars>, High<Dollars>, Low<Dollars>, Close<Dollars>)> for OHLCDollars {
fn from(value: (Open<Dollars>, High<Dollars>, Low<Dollars>, Close<Dollars>)) -> Self {
Self {
open: value.0,
high: value.1,
low: value.2,
close: value.3,
}
}
}
impl From<Close<Dollars>> for OHLCDollars {
fn from(value: Close<Dollars>) -> Self {
Self {
open: Open::from(value),
high: High::from(value),
low: Low::from(value),
close: value,
}
}
}
impl From<OHLCCents> for OHLCDollars {
fn from(value: OHLCCents) -> Self {
Self::from(&value)
}
}
impl From<&OHLCCents> for OHLCDollars {
fn from(value: &OHLCCents) -> Self {
Self {
open: value.open.into(),
high: value.high.into(),
low: value.low.into(),
close: value.close.into(),
}
}
}
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)]
#[repr(C)]
pub struct OHLCSats {
pub open: Open<Sats>,
pub high: High<Sats>,
pub low: Low<Sats>,
pub close: Close<Sats>,
}
impl Serialize for OHLCSats {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(4)?;
tup.serialize_element(&self.open)?;
tup.serialize_element(&self.high)?;
tup.serialize_element(&self.low)?;
tup.serialize_element(&self.close)?;
tup.end()
}
}
impl From<(Open<Sats>, High<Sats>, Low<Sats>, Close<Sats>)> for OHLCSats {
fn from(value: (Open<Sats>, High<Sats>, Low<Sats>, Close<Sats>)) -> Self {
Self {
open: value.0,
high: value.1,
low: value.2,
close: value.3,
}
}
}
impl From<Close<Sats>> for OHLCSats {
fn from(value: Close<Sats>) -> Self {
Self {
open: Open::from(value),
high: High::from(value),
low: Low::from(value),
close: value,
}
}
}
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct Open<T>(T);
impl<T> Open<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for Open<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for Open<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<Open<T>> for f64
where
f64: From<T>,
{
fn from(value: Open<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for Open<T>
where
T: Copy,
{
fn from(value: Close<T>) -> Self {
Self(*value)
}
}
impl From<Open<Cents>> for Open<Dollars> {
fn from(value: Open<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for Open<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for Open<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for Open<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
#[derive(
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct High<T>(T);
impl<T> High<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for High<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for High<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<High<T>> for f64
where
f64: From<T>,
{
fn from(value: High<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for High<T>
where
T: Copy,
{
fn from(value: Close<T>) -> Self {
Self(*value)
}
}
impl From<High<Cents>> for High<Dollars> {
fn from(value: High<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for High<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for High<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for High<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct Low<T>(T);
impl<T> Low<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for Low<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for Low<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<Low<T>> for f64
where
f64: From<T>,
{
fn from(value: Low<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for Low<T>
where
T: Copy,
{
fn from(value: Close<T>) -> Self {
Self(*value)
}
}
impl From<Low<Cents>> for Low<Dollars> {
fn from(value: Low<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for Low<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for Low<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for Low<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Deref,
DerefMut,
Serialize,
StoredCompressed,
)]
#[repr(transparent)]
pub struct Close<T>(T);
impl<T> Close<T> {
pub fn new(value: T) -> Self {
Self(value)
}
}
impl<T> From<usize> for Close<T>
where
T: From<usize>,
{
fn from(value: usize) -> Self {
Self(T::from(value))
}
}
impl<T> From<f32> for Close<T>
where
T: From<f32>,
{
fn from(value: f32) -> Self {
Self(T::from(value))
}
}
impl<T> From<f64> for Close<T>
where
T: From<f64>,
{
fn from(value: f64) -> Self {
Self(T::from(value))
}
}
impl<T> From<Close<T>> for f32
where
f32: From<T>,
{
fn from(value: Close<T>) -> Self {
Self::from(value.0)
}
}
impl<T> From<Close<T>> for f64
where
f64: From<T>,
{
fn from(value: Close<T>) -> Self {
Self::from(value.0)
}
}
// impl<A, B> From<Close<A>> for Close<B>
// where
// B: From<A>,
// {
// fn from(value: Close<A>) -> Self {
// Self(B::from(*value))
impl From<Close<Cents>> for Close<Dollars> {
fn from(value: Close<Cents>) -> Self {
Self(Dollars::from(*value))
}
}
impl<T> Add for Close<T>
where
T: Add<Output = T>,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl<T> AddAssign for Close<T>
where
T: Add<Output = T> + Clone,
{
fn add_assign(&mut self, rhs: Self) {
**self = self.0.clone() + rhs.0
}
}
impl<T> Div<usize> for Close<T>
where
T: Div<usize, Output = T>,
{
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
// impl<T> Mul<usize> for Close<T>
// where
// T: Mul<usize, Output = T>,
// {
// type Output = Self;
// fn mul(self, rhs: usize) -> Self::Output {
// Self(self.0 * rhs)
// }
// }

View File

@@ -0,0 +1,70 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct OpReturnIndex(TypeIndex);
impl From<TypeIndex> for OpReturnIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<OpReturnIndex> for usize {
fn from(value: OpReturnIndex) -> Self {
Self::from(*value)
}
}
impl From<OpReturnIndex> for u64 {
fn from(value: OpReturnIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for OpReturnIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for OpReturnIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<OpReturnIndex> for OpReturnIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for OpReturnIndex {
fn to_string() -> &'static str {
"opreturnindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["op", "opreturn", "opreturnindex"]
}
}

View File

@@ -0,0 +1,124 @@
use std::ops::{Add, AddAssign};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_8bytes;
use super::Vout;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct OutputIndex(u64);
impl OutputIndex {
pub const ZERO: Self = Self(0);
pub const COINBASE: Self = Self(u64::MAX);
pub fn incremented(self) -> Self {
Self(*self + 1)
}
pub fn is_coinbase(self) -> bool {
self == Self::COINBASE
}
}
impl Add<OutputIndex> for OutputIndex {
type Output = Self;
fn add(self, rhs: OutputIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<Vout> for OutputIndex {
type Output = Self;
fn add(self, rhs: Vout) -> Self::Output {
Self(self.0 + u64::from(rhs))
}
}
impl Add<usize> for OutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u64)
}
}
impl AddAssign<OutputIndex> for OutputIndex {
fn add_assign(&mut self, rhs: OutputIndex) {
self.0 += rhs.0
}
}
impl CheckedSub<OutputIndex> for OutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self::from)
}
}
impl From<OutputIndex> for u32 {
fn from(value: OutputIndex) -> Self {
if value.0 > u32::MAX as u64 {
panic!()
}
value.0 as u32
}
}
impl From<u64> for OutputIndex {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<OutputIndex> for u64 {
fn from(value: OutputIndex) -> Self {
value.0
}
}
impl From<usize> for OutputIndex {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<OutputIndex> for usize {
fn from(value: OutputIndex) -> Self {
value.0 as usize
}
}
impl From<&[u8]> for OutputIndex {
fn from(value: &[u8]) -> Self {
Self(u64::from_be_bytes(copy_first_8bytes(value).unwrap()))
}
}
impl Printable for OutputIndex {
fn to_string() -> &'static str {
"outputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["out", "outputindex"]
}
}

View File

@@ -0,0 +1,381 @@
use bitcoin::{ScriptBuf, opcodes::all::OP_PUSHBYTES_2};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
#[repr(u8)]
pub enum OutputType {
P2PK65,
P2PK33,
P2PKH,
P2MS,
P2SH,
OpReturn,
P2WPKH,
P2WSH,
P2TR,
P2A,
Dummy10,
Dummy11,
Dummy12,
Dummy13,
Dummy14,
Dummy15,
Dummy16,
Dummy17,
Dummy18,
Dummy19,
Dummy20,
Dummy21,
Dummy22,
Dummy23,
Dummy24,
Dummy25,
Dummy26,
Dummy27,
Dummy28,
Dummy29,
Dummy30,
Dummy31,
Dummy32,
Dummy33,
Dummy34,
Dummy35,
Dummy36,
Dummy37,
Dummy38,
Dummy39,
Dummy40,
Dummy41,
Dummy42,
Dummy43,
Dummy44,
Dummy45,
Dummy46,
Dummy47,
Dummy48,
Dummy49,
Dummy50,
Dummy51,
Dummy52,
Dummy53,
Dummy54,
Dummy55,
Dummy56,
Dummy57,
Dummy58,
Dummy59,
Dummy60,
Dummy61,
Dummy62,
Dummy63,
Dummy64,
Dummy65,
Dummy66,
Dummy67,
Dummy68,
Dummy69,
Dummy70,
Dummy71,
Dummy72,
Dummy73,
Dummy74,
Dummy75,
Dummy76,
Dummy77,
Dummy78,
Dummy79,
Dummy80,
Dummy81,
Dummy82,
Dummy83,
Dummy84,
Dummy85,
Dummy86,
Dummy87,
Dummy88,
Dummy89,
Dummy90,
Dummy91,
Dummy92,
Dummy93,
Dummy94,
Dummy95,
Dummy96,
Dummy97,
Dummy98,
Dummy99,
Dummy100,
Dummy101,
Dummy102,
Dummy103,
Dummy104,
Dummy105,
Dummy106,
Dummy107,
Dummy108,
Dummy109,
Dummy110,
Dummy111,
Dummy112,
Dummy113,
Dummy114,
Dummy115,
Dummy116,
Dummy117,
Dummy118,
Dummy119,
Dummy120,
Dummy121,
Dummy122,
Dummy123,
Dummy124,
Dummy125,
Dummy126,
Dummy127,
Dummy128,
Dummy129,
Dummy130,
Dummy131,
Dummy132,
Dummy133,
Dummy134,
Dummy135,
Dummy136,
Dummy137,
Dummy138,
Dummy139,
Dummy140,
Dummy141,
Dummy142,
Dummy143,
Dummy144,
Dummy145,
Dummy146,
Dummy147,
Dummy148,
Dummy149,
Dummy150,
Dummy151,
Dummy152,
Dummy153,
Dummy154,
Dummy155,
Dummy156,
Dummy157,
Dummy158,
Dummy159,
Dummy160,
Dummy161,
Dummy162,
Dummy163,
Dummy164,
Dummy165,
Dummy166,
Dummy167,
Dummy168,
Dummy169,
Dummy170,
Dummy171,
Dummy172,
Dummy173,
Dummy174,
Dummy175,
Dummy176,
Dummy177,
Dummy178,
Dummy179,
Dummy180,
Dummy181,
Dummy182,
Dummy183,
Dummy184,
Dummy185,
Dummy186,
Dummy187,
Dummy188,
Dummy189,
Dummy190,
Dummy191,
Dummy192,
Dummy193,
Dummy194,
Dummy195,
Dummy196,
Dummy197,
Dummy198,
Dummy199,
Dummy200,
Dummy201,
Dummy202,
Dummy203,
Dummy204,
Dummy205,
Dummy206,
Dummy207,
Dummy208,
Dummy209,
Dummy210,
Dummy211,
Dummy212,
Dummy213,
Dummy214,
Dummy215,
Dummy216,
Dummy217,
Dummy218,
Dummy219,
Dummy220,
Dummy221,
Dummy222,
Dummy223,
Dummy224,
Dummy225,
Dummy226,
Dummy227,
Dummy228,
Dummy229,
Dummy230,
Dummy231,
Dummy232,
Dummy233,
Dummy234,
Dummy235,
Dummy236,
Dummy237,
Dummy238,
Dummy239,
Dummy240,
Dummy241,
Dummy242,
Dummy243,
Dummy244,
Dummy245,
Dummy246,
Dummy247,
Dummy248,
Dummy249,
Dummy250,
Dummy251,
Dummy252,
Dummy253,
Empty = 254,
Unknown = 255,
}
impl OutputType {
pub fn is_spendable(&self) -> bool {
match self {
Self::P2PK65 => true,
Self::P2PK33 => true,
Self::P2PKH => true,
Self::P2MS => true,
Self::P2SH => true,
Self::OpReturn => false,
Self::P2WPKH => true,
Self::P2WSH => true,
Self::P2TR => true,
Self::P2A => true,
Self::Empty => true,
Self::Unknown => true,
_ => unreachable!(),
}
}
pub fn is_address(&self) -> bool {
match self {
Self::P2PK65 => true,
Self::P2PK33 => true,
Self::P2PKH => true,
Self::P2MS => false,
Self::P2SH => true,
Self::OpReturn => false,
Self::P2WPKH => true,
Self::P2WSH => true,
Self::P2TR => true,
Self::P2A => true,
Self::Empty => false,
Self::Unknown => false,
_ => unreachable!(),
}
}
pub fn is_not_address(&self) -> bool {
!self.is_address()
}
pub fn is_unspendable(&self) -> bool {
!self.is_spendable()
}
pub fn as_vec() -> Vec<Self> {
vec![
Self::P2PK65,
Self::P2PK33,
Self::P2PKH,
Self::P2MS,
Self::P2SH,
Self::OpReturn,
Self::P2WPKH,
Self::P2WSH,
Self::P2TR,
Self::P2A,
Self::Empty,
Self::Unknown,
]
}
}
impl From<&ScriptBuf> for OutputType {
fn from(script: &ScriptBuf) -> Self {
if script.is_p2pk() {
let bytes = script.as_bytes();
match bytes.len() {
67 => Self::P2PK65,
35 => Self::P2PK33,
_ => {
dbg!(bytes);
unreachable!()
}
}
} else if script.is_p2pkh() {
Self::P2PKH
} else if script.is_multisig() {
Self::P2MS
} else if script.is_p2sh() {
Self::P2SH
} else if script.is_op_return() {
Self::OpReturn
} else if script.is_p2wpkh() {
Self::P2WPKH
} else if script.is_p2wsh() {
Self::P2WSH
} else if script.is_p2tr() {
Self::P2TR
} else if script.witness_version() == Some(bitcoin::WitnessVersion::V1)
&& script.len() == 4
&& script.as_bytes()[1] == OP_PUSHBYTES_2.to_u8()
&& script.as_bytes()[2..4] == [78, 115]
{
Self::P2A
} else if script.is_empty() {
Self::Empty
} else {
Self::Unknown
}
}
}

View File

@@ -0,0 +1,83 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2AAddressIndex(TypeIndex);
impl From<TypeIndex> for P2AAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2AAddressIndex> for TypeIndex {
fn from(value: P2AAddressIndex) -> Self {
value.0
}
}
impl From<P2AAddressIndex> for u32 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2AAddressIndex> for u64 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2AAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2AAddressIndex> for usize {
fn from(value: P2AAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2AAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2AAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2AAddressIndex> for P2AAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2AAddressIndex {
fn to_string() -> &'static str {
"p2aaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["aaddr", "p2aaddr", "p2aaddressindex"]
}
}

View File

@@ -0,0 +1,69 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2MSOutputIndex(TypeIndex);
impl From<TypeIndex> for P2MSOutputIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2MSOutputIndex> for usize {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<P2MSOutputIndex> for u64 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2MSOutputIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2MSOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2MSOutputIndex> for P2MSOutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2MSOutputIndex {
fn to_string() -> &'static str {
"p2msoutputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["msout", "p2msout", "p2msoutputindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2PK33AddressIndex(TypeIndex);
impl From<TypeIndex> for P2PK33AddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2PK33AddressIndex> for TypeIndex {
fn from(value: P2PK33AddressIndex) -> Self {
value.0
}
}
impl From<P2PK33AddressIndex> for u32 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PK33AddressIndex> for u64 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2PK33AddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2PK33AddressIndex> for usize {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2PK33AddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2PK33AddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2PK33AddressIndex> for P2PK33AddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2PK33AddressIndex {
fn to_string() -> &'static str {
"p2pk33addressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["pk33addr", "p2pk33addr", "p2pk33addressindex"]
}
}

View File

@@ -0,0 +1,83 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2PK65AddressIndex(TypeIndex);
impl From<TypeIndex> for P2PK65AddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2PK65AddressIndex> for TypeIndex {
fn from(value: P2PK65AddressIndex) -> Self {
value.0
}
}
impl From<P2PK65AddressIndex> for u32 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PK65AddressIndex> for u64 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PK65AddressIndex> for usize {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2PK65AddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<usize> for P2PK65AddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2PK65AddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2PK65AddressIndex> for P2PK65AddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2PK65AddressIndex {
fn to_string() -> &'static str {
"p2pk65addressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["pk65addr", "p2pk65addr", "p2pk65addressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2PKHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2PKHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2PKHAddressIndex> for TypeIndex {
fn from(value: P2PKHAddressIndex) -> Self {
value.0
}
}
impl From<P2PKHAddressIndex> for usize {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PKHAddressIndex> for u64 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2PKHAddressIndex> for u32 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2PKHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<usize> for P2PKHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2PKHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2PKHAddressIndex> for P2PKHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2PKHAddressIndex {
fn to_string() -> &'static str {
"p2pkhaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["pkhaddr", "p2pkhaddr", "p2pkhaddressindex"]
}
}

View File

@@ -0,0 +1,89 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2SHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2SHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2SHAddressIndex> for TypeIndex {
fn from(value: P2SHAddressIndex) -> Self {
value.0
}
}
impl From<P2SHAddressIndex> for u32 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2SHAddressIndex> for u64 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2SHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<u64> for P2SHAddressIndex {
fn from(value: u64) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2SHAddressIndex> for usize {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2SHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2SHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2SHAddressIndex> for P2SHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2SHAddressIndex {
fn to_string() -> &'static str {
"p2shaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["shaddr", "p2shaddr", "p2shaddressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2TRAddressIndex(TypeIndex);
impl From<TypeIndex> for P2TRAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2TRAddressIndex> for TypeIndex {
fn from(value: P2TRAddressIndex) -> Self {
value.0
}
}
impl From<P2TRAddressIndex> for u32 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2TRAddressIndex> for u64 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2TRAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2TRAddressIndex> for usize {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2TRAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2TRAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2TRAddressIndex> for P2TRAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2TRAddressIndex {
fn to_string() -> &'static str {
"p2traddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["traddr", "p2traddr", "p2traddressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2WPKHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2WPKHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2WPKHAddressIndex> for TypeIndex {
fn from(value: P2WPKHAddressIndex) -> Self {
value.0
}
}
impl From<P2WPKHAddressIndex> for u32 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2WPKHAddressIndex> for u64 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2WPKHAddressIndex> for usize {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2WPKHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<usize> for P2WPKHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2WPKHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2WPKHAddressIndex> for P2WPKHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2WPKHAddressIndex {
fn to_string() -> &'static str {
"p2wpkhaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["wpkhaddr", "p2wpkhaddr", "p2wpkhaddressindex"]
}
}

View File

@@ -0,0 +1,84 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct P2WSHAddressIndex(TypeIndex);
impl From<TypeIndex> for P2WSHAddressIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<P2WSHAddressIndex> for TypeIndex {
fn from(value: P2WSHAddressIndex) -> Self {
value.0
}
}
impl From<P2WSHAddressIndex> for u32 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<P2WSHAddressIndex> for u64 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<u32> for P2WSHAddressIndex {
fn from(value: u32) -> Self {
Self(TypeIndex::from(value))
}
}
impl From<P2WSHAddressIndex> for usize {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for P2WSHAddressIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for P2WSHAddressIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<P2WSHAddressIndex> for P2WSHAddressIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for P2WSHAddressIndex {
fn to_string() -> &'static str {
"p2wshaddressindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["wshaddr", "p2wshaddr", "p2wshaddressindex"]
}
}

View File

@@ -0,0 +1,104 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::MonthIndex;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct QuarterIndex(u16);
impl From<u16> for QuarterIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for QuarterIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<QuarterIndex> for u16 {
fn from(value: QuarterIndex) -> Self {
value.0
}
}
impl From<QuarterIndex> for usize {
fn from(value: QuarterIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for QuarterIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<QuarterIndex> for QuarterIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for QuarterIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for QuarterIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<MonthIndex> for QuarterIndex {
fn from(value: MonthIndex) -> Self {
Self((usize::from(value) / 3) as u16)
}
}
impl CheckedSub for QuarterIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for QuarterIndex {
fn to_string() -> &'static str {
"quarterindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["q", "quarter", "quarterindex"]
}
}

View File

@@ -0,0 +1,32 @@
use bitcoin::absolute::LockTime;
use brk_vecs::StoredCompressed;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug, Immutable, Clone, Copy, IntoBytes, KnownLayout, FromBytes, Serialize, StoredCompressed,
)]
pub struct RawLockTime(u32);
impl From<LockTime> for RawLockTime {
fn from(value: LockTime) -> Self {
Self(value.to_consensus_u32())
}
}
const CONSENSUS_DELIMITER: u32 = 500_000_000;
impl From<RawLockTime> for LockTime {
fn from(value: RawLockTime) -> Self {
let value = value.0;
if value >= CONSENSUS_DELIMITER {
bitcoin::locktime::absolute::Height::from_consensus(value)
.unwrap()
.into()
} else {
bitcoin::locktime::absolute::Time::from_consensus(value)
.unwrap()
.into()
}
}
}

View File

@@ -0,0 +1,251 @@
use std::{
iter::Sum,
ops::{Add, AddAssign, Div, Mul, SubAssign},
};
use bitcoin::Amount;
use brk_vecs::{CheckedSub, StoredCompressed};
use derive_deref::Deref;
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::StoredF64;
use super::{Bitcoin, Cents, Dollars, Height};
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
Deserialize,
StoredCompressed,
)]
pub struct Sats(u64);
#[allow(clippy::inconsistent_digit_grouping)]
impl Sats {
pub const ZERO: Self = Self(0);
pub const _1: Self = Self(1);
pub const _10: Self = Self(10);
pub const _100: Self = Self(100);
pub const _1K: Self = Self(1_000);
pub const _10K: Self = Self(10_000);
pub const _100K: Self = Self(100_000);
pub const _1M: Self = Self(1_000_000);
pub const _10M: Self = Self(10_000_000);
pub const _1BTC: Self = Self::ONE_BTC;
pub const _10BTC: Self = Self(10_00_000_000);
pub const _100BTC: Self = Self(100_00_000_000);
pub const _1K_BTC: Self = Self(1_000_00_000_000);
pub const _10K_BTC: Self = Self(10_000_00_000_000);
pub const _100K_BTC: Self = Self(100_000_00_000_000);
pub const ONE_BTC: Self = Self(1_00_000_000);
pub const MAX: Self = Self(u64::MAX);
pub const FIFTY_BTC: Self = Self(50_00_000_000);
pub const ONE_BTC_U128: u128 = 1_00_000_000;
pub fn new(sats: u64) -> Self {
Self(sats)
}
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}
pub fn is_not_zero(&self) -> bool {
*self != Self::ZERO
}
}
impl Add for Sats {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for Sats {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl CheckedSub<Sats> for Sats {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self::from)
}
}
impl CheckedSub<usize> for Sats {
fn checked_sub(self, rhs: usize) -> Option<Self> {
self.0.checked_sub(rhs as u64).map(Self::from)
}
}
impl SubAssign for Sats {
fn sub_assign(&mut self, rhs: Self) {
*self = self.checked_sub(rhs).unwrap();
// .unwrap_or_else(|| {
// dbg!((*self, rhs));
// unreachable!();
// });
}
}
impl Mul<Sats> for Sats {
type Output = Self;
fn mul(self, rhs: Sats) -> Self::Output {
Sats::from(self.0.checked_mul(rhs.0).unwrap())
}
}
impl Mul<usize> for Sats {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Sats::from(self.0.checked_mul(rhs as u64).unwrap())
}
}
impl Mul<u64> for Sats {
type Output = Self;
fn mul(self, rhs: u64) -> Self::Output {
Sats::from(self.0.checked_mul(rhs).unwrap())
}
}
impl Mul<Height> for Sats {
type Output = Self;
fn mul(self, rhs: Height) -> Self::Output {
Sats::from(self.0.checked_mul(u64::from(rhs)).unwrap())
}
}
impl Mul<StoredF64> for Sats {
type Output = Self;
fn mul(self, rhs: StoredF64) -> Self::Output {
Sats::from((self.0 as f64 * f64::from(rhs)) as u64)
}
}
impl Sum for Sats {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: u64 = iter.map(|sats| sats.0).sum();
Sats::from(sats)
}
}
impl Div<Dollars> for Sats {
type Output = Self;
fn div(self, rhs: Dollars) -> Self::Output {
let raw_cents = u64::from(Cents::from(rhs));
if raw_cents != 0 {
Self(self.0 * 100 / raw_cents)
} else {
Self::MAX
}
}
}
impl Div<Sats> for Sats {
type Output = Self;
fn div(self, rhs: Sats) -> Self::Output {
if rhs.0 == 0 {
Self(0)
} else {
Self(self.0 / rhs.0)
}
}
}
impl Div<usize> for Sats {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u64)
}
}
impl From<u64> for Sats {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<usize> for Sats {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<f64> for Sats {
fn from(value: f64) -> Self {
Self(value.round() as u64)
}
}
impl From<Sats> for f64 {
fn from(value: Sats) -> Self {
value.0 as f64
}
}
impl From<Sats> for usize {
fn from(value: Sats) -> Self {
value.0 as usize
}
}
impl From<Amount> for Sats {
fn from(value: Amount) -> Self {
Self(value.to_sat())
}
}
impl From<Sats> for Amount {
fn from(value: Sats) -> Self {
Self::from_sat(value.0)
}
}
impl From<Bitcoin> for Sats {
fn from(value: Bitcoin) -> Self {
Self((f64::from(value) * (Sats::ONE_BTC.0 as f64)).round() as u64)
}
}
impl From<Sats> for u64 {
fn from(value: Sats) -> Self {
value.0
}
}
impl From<u128> for Sats {
fn from(value: u128) -> Self {
if value > u64::MAX as u128 {
panic!("u128 bigger than u64")
}
Self(value as u64)
}
}
impl From<Sats> for u128 {
fn from(value: Sats) -> Self {
value.0 as u128
}
}
impl Mul<Sats> for usize {
type Output = Sats;
fn mul(self, rhs: Sats) -> Self::Output {
Self::Output::from(rhs.0 * self as u64)
}
}

View File

@@ -0,0 +1,104 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::MonthIndex;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct SemesterIndex(u16);
impl From<u16> for SemesterIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for SemesterIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<SemesterIndex> for u16 {
fn from(value: SemesterIndex) -> Self {
value.0
}
}
impl From<SemesterIndex> for usize {
fn from(value: SemesterIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for SemesterIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<SemesterIndex> for SemesterIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for SemesterIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for SemesterIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<MonthIndex> for SemesterIndex {
fn from(value: MonthIndex) -> Self {
Self((usize::from(value) / 6) as u16)
}
}
impl CheckedSub for SemesterIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for SemesterIndex {
fn to_string() -> &'static str {
"semesterindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["s", "semester", "semesterindex"]
}
}

View File

@@ -0,0 +1,50 @@
use brk_vecs::{Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug,
Deref,
Clone,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredBool(u16);
impl StoredBool {
pub const FALSE: Self = Self(0);
pub const TRUE: Self = Self(1);
}
impl From<bool> for StoredBool {
fn from(value: bool) -> Self {
if value { Self(1) } else { Self(0) }
}
}
impl From<StoredBool> for usize {
fn from(value: StoredBool) -> Self {
value.0 as usize
}
}
impl Printable for StoredBool {
fn to_string() -> &'static str {
"bool"
}
fn to_possible_strings() -> &'static [&'static str] {
&["bool"]
}
}

View File

@@ -0,0 +1,175 @@
use core::panic;
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div, Mul, Sub},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Dollars, StoredF64};
#[derive(
Debug,
Deref,
Default,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredF32(f32);
impl From<f32> for StoredF32 {
fn from(value: f32) -> Self {
Self(value)
}
}
impl From<f64> for StoredF32 {
fn from(value: f64) -> Self {
if value > f32::MAX as f64 {
panic!("f64 is too big")
}
Self(value as f32)
}
}
impl From<StoredF32> for f64 {
fn from(value: StoredF32) -> Self {
value.0 as f64
}
}
impl From<StoredF64> for StoredF32 {
fn from(value: StoredF64) -> Self {
Self(*value as f32)
}
}
impl From<usize> for StoredF32 {
fn from(value: usize) -> Self {
Self(value as f32)
}
}
impl CheckedSub<StoredF32> for StoredF32 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
Some(Self(self.0 - rhs.0))
}
}
impl Div<usize> for StoredF32 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as f32)
}
}
impl Add for StoredF32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredF32 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<StoredF32> for f32 {
fn from(value: StoredF32) -> Self {
value.0
}
}
impl From<Dollars> for StoredF32 {
fn from(value: Dollars) -> Self {
StoredF32::from(f64::from(value))
}
}
impl Div<Dollars> for StoredF32 {
type Output = Self;
fn div(self, rhs: Dollars) -> Self::Output {
Self::from(self.0 as f64 / *rhs)
}
}
impl Div<StoredF32> for StoredF32 {
type Output = Self;
fn div(self, rhs: StoredF32) -> Self::Output {
Self::from(self.0 / rhs.0)
}
}
impl Mul<usize> for StoredF32 {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as f32)
}
}
impl Mul<StoredF32> for usize {
type Output = StoredF32;
fn mul(self, rhs: StoredF32) -> Self::Output {
StoredF32(self as f32 * rhs.0)
}
}
impl Sub<StoredF32> for StoredF32 {
type Output = Self;
fn sub(self, rhs: StoredF32) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl PartialEq for StoredF32 {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for StoredF32 {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for StoredF32 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for StoredF32 {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl Printable for StoredF32 {
fn to_string() -> &'static str {
"f32"
}
fn to_possible_strings() -> &'static [&'static str] {
&["f32"]
}
}

View File

@@ -0,0 +1,149 @@
use std::{
cmp::Ordering,
f64,
ops::{Add, AddAssign, Div, Mul},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{Bitcoin, Dollars};
#[derive(
Debug,
Deref,
Default,
Clone,
Copy,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredF64(f64);
impl StoredF64 {
pub const NAN: Self = Self(f64::NAN);
}
impl From<f64> for StoredF64 {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<usize> for StoredF64 {
fn from(value: usize) -> Self {
Self(value as f64)
}
}
impl CheckedSub<StoredF64> for StoredF64 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
Some(Self(self.0 - rhs.0))
}
}
impl Mul<usize> for StoredF64 {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as f64)
}
}
impl Div<usize> for StoredF64 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as f64)
}
}
impl Div<StoredF64> for StoredF64 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl Add for StoredF64 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredF64 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<StoredF64> for f64 {
fn from(value: StoredF64) -> Self {
value.0
}
}
impl From<Dollars> for StoredF64 {
fn from(value: Dollars) -> Self {
Self(f64::from(value))
}
}
impl CheckedSub<usize> for StoredF64 {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Self(self.0 - rhs as f64))
}
}
impl PartialEq for StoredF64 {
fn eq(&self, other: &Self) -> bool {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => true,
(true, false) => false,
(false, true) => false,
(false, false) => self.0 == other.0,
}
}
}
impl Eq for StoredF64 {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl PartialOrd for StoredF64 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for StoredF64 {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl From<Bitcoin> for StoredF64 {
fn from(value: Bitcoin) -> Self {
Self(f64::from(value))
}
}
impl Printable for StoredF64 {
fn to_string() -> &'static str {
"f64"
}
fn to_possible_strings() -> &'static [&'static str] {
&["f64"]
}
}

View File

@@ -0,0 +1,183 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{
EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex,
P2PK65AddressIndex, P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex,
P2WSHAddressIndex, UnknownOutputIndex,
};
#[derive(
Debug,
Deref,
Clone,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredU16(u16);
impl StoredU16 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u16) -> Self {
Self(counter)
}
}
impl From<u16> for StoredU16 {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for StoredU16 {
fn from(value: usize) -> Self {
if value > u16::MAX as usize {
panic!("usize too big (value = {value})")
}
Self(value as u16)
}
}
impl CheckedSub<StoredU16> for StoredU16 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU16 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u16)
}
}
impl Add for StoredU16 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU16 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU16 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u16::MAX as f64 {
panic!()
}
Self(value as u16)
}
}
impl From<StoredU16> for f64 {
fn from(value: StoredU16) -> Self {
value.0 as f64
}
}
impl From<StoredU16> for usize {
fn from(value: StoredU16) -> Self {
value.0 as usize
}
}
impl From<P2PK65AddressIndex> for StoredU16 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PK33AddressIndex> for StoredU16 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PKHAddressIndex> for StoredU16 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<OpReturnIndex> for StoredU16 {
fn from(value: OpReturnIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2MSOutputIndex> for StoredU16 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2SHAddressIndex> for StoredU16 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WSHAddressIndex> for StoredU16 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WPKHAddressIndex> for StoredU16 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2TRAddressIndex> for StoredU16 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2AAddressIndex> for StoredU16 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<UnknownOutputIndex> for StoredU16 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<EmptyOutputIndex> for StoredU16 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl Printable for StoredU16 {
fn to_string() -> &'static str {
"u16"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u16"]
}
}

View File

@@ -0,0 +1,183 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{
EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex,
P2PK65AddressIndex, P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex,
P2WSHAddressIndex, UnknownOutputIndex,
};
#[derive(
Debug,
Deref,
Clone,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredU32(u32);
impl StoredU32 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u32) -> Self {
Self(counter)
}
}
impl From<u32> for StoredU32 {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for StoredU32 {
fn from(value: usize) -> Self {
if value > u32::MAX as usize {
panic!("usize too big (value = {value})")
}
Self(value as u32)
}
}
impl CheckedSub<StoredU32> for StoredU32 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU32 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u32)
}
}
impl Add for StoredU32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU32 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU32 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u32)
}
}
impl From<StoredU32> for f64 {
fn from(value: StoredU32) -> Self {
value.0 as f64
}
}
impl From<StoredU32> for usize {
fn from(value: StoredU32) -> Self {
value.0 as usize
}
}
impl From<P2PK65AddressIndex> for StoredU32 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PK33AddressIndex> for StoredU32 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2PKHAddressIndex> for StoredU32 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<OpReturnIndex> for StoredU32 {
fn from(value: OpReturnIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2MSOutputIndex> for StoredU32 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2SHAddressIndex> for StoredU32 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WSHAddressIndex> for StoredU32 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2WPKHAddressIndex> for StoredU32 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2TRAddressIndex> for StoredU32 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<P2AAddressIndex> for StoredU32 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<UnknownOutputIndex> for StoredU32 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl From<EmptyOutputIndex> for StoredU32 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(usize::from(value))
}
}
impl Printable for StoredU32 {
fn to_string() -> &'static str {
"u32"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u32"]
}
}

View File

@@ -0,0 +1,229 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{
DateIndex, EmptyOutputIndex, Height, InputIndex, MonthIndex, OpReturnIndex, OutputIndex,
P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex, P2PK65AddressIndex, P2PKHAddressIndex,
P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex, P2WSHAddressIndex, TxIndex,
UnknownOutputIndex, YearIndex,
};
#[derive(
Debug,
Default,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct StoredU64(u64);
impl StoredU64 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u64) -> Self {
Self(counter)
}
}
impl From<u64> for StoredU64 {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<StoredU64> for u64 {
fn from(value: StoredU64) -> Self {
value.0
}
}
impl From<StoredU64> for usize {
fn from(value: StoredU64) -> Self {
value.0 as usize
}
}
impl From<usize> for StoredU64 {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl CheckedSub<StoredU64> for StoredU64 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU64 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u64)
}
}
impl Add for StoredU64 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU64 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU64 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u64)
}
}
impl From<StoredU64> for f64 {
fn from(value: StoredU64) -> Self {
value.0 as f64
}
}
impl From<TxIndex> for StoredU64 {
fn from(value: TxIndex) -> Self {
Self(*value as u64)
}
}
impl From<InputIndex> for StoredU64 {
fn from(value: InputIndex) -> Self {
Self(*value)
}
}
impl From<Height> for StoredU64 {
fn from(value: Height) -> Self {
Self(*value as u64)
}
}
impl From<OutputIndex> for StoredU64 {
fn from(value: OutputIndex) -> Self {
Self(*value)
}
}
impl From<DateIndex> for StoredU64 {
fn from(value: DateIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<MonthIndex> for StoredU64 {
fn from(value: MonthIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<YearIndex> for StoredU64 {
fn from(value: YearIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2PK65AddressIndex> for StoredU64 {
fn from(value: P2PK65AddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2PK33AddressIndex> for StoredU64 {
fn from(value: P2PK33AddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2PKHAddressIndex> for StoredU64 {
fn from(value: P2PKHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<OpReturnIndex> for StoredU64 {
fn from(value: OpReturnIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2MSOutputIndex> for StoredU64 {
fn from(value: P2MSOutputIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2SHAddressIndex> for StoredU64 {
fn from(value: P2SHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2WSHAddressIndex> for StoredU64 {
fn from(value: P2WSHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2WPKHAddressIndex> for StoredU64 {
fn from(value: P2WPKHAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2TRAddressIndex> for StoredU64 {
fn from(value: P2TRAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<P2AAddressIndex> for StoredU64 {
fn from(value: P2AAddressIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<UnknownOutputIndex> for StoredU64 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(u64::from(value))
}
}
impl From<EmptyOutputIndex> for StoredU64 {
fn from(value: EmptyOutputIndex) -> Self {
Self::from(u64::from(value))
}
}
impl Printable for StoredU64 {
fn to_string() -> &'static str {
"u64"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u64"]
}
}

View File

@@ -0,0 +1,110 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, Printable};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
pub type StoredPhantom = StoredU8;
#[derive(
Default,
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct StoredU8(u8);
impl StoredU8 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u8) -> Self {
Self(counter)
}
}
impl From<u8> for StoredU8 {
fn from(value: u8) -> Self {
Self(value)
}
}
impl From<usize> for StoredU8 {
fn from(value: usize) -> Self {
Self(value as u8)
}
}
impl CheckedSub<StoredU8> for StoredU8 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredU8 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u8)
}
}
impl Add for StoredU8 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for StoredU8 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for StoredU8 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u8)
}
}
impl From<StoredU8> for f64 {
fn from(value: StoredU8) -> Self {
value.0 as f64
}
}
impl Add<usize> for StoredU8 {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0.checked_add(rhs as u8).unwrap())
}
}
impl From<StoredU8> for usize {
fn from(value: StoredU8) -> Self {
value.0 as usize
}
}
impl Printable for StoredU8 {
fn to_string() -> &'static str {
"u8"
}
fn to_possible_strings() -> &'static [&'static str] {
&["u8"]
}
}

View File

@@ -0,0 +1,154 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::{CheckedSub, StoredCompressed};
use derive_deref::Deref;
use jiff::{civil::date, tz::TimeZone};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Date;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct Timestamp(u32);
const ONE_HOUR_IN_SEC: u32 = 60 * 60;
const ONE_DAY_IN_SEC: u32 = 24 * 60 * 60;
const ONE_DAY_IN_SEC_F64: f64 = ONE_DAY_IN_SEC as f64;
impl Timestamp {
pub const ZERO: Self = Self(0);
pub fn new(timestamp: u32) -> Self {
Self(timestamp)
}
pub fn floor_seconds(self) -> Self {
let zoned = jiff::Timestamp::from(self).to_zoned(TimeZone::UTC);
let date_time = jiff::civil::DateTime::from(zoned);
let trunc_date_time = date(date_time.year(), date_time.month(), date_time.day()).at(
date_time.hour(),
date_time.minute(),
0,
0,
);
Self::from(trunc_date_time.to_zoned(TimeZone::UTC).unwrap().timestamp())
}
#[inline]
pub fn difference_in_days_between(&self, older: Self) -> usize {
// if self.0 < older.0 {
// unreachable!()
// }
((self.0 - older.0) / ONE_DAY_IN_SEC) as usize
}
#[inline]
pub fn difference_in_days_between_float(&self, older: Self) -> f64 {
// if self.0 < older.0 {
// unreachable!()
// }
(self.0 - older.0) as f64 / ONE_DAY_IN_SEC_F64
}
#[inline]
pub fn is_more_than_hour(&self) -> bool {
self.0 >= ONE_HOUR_IN_SEC
}
}
impl From<u32> for Timestamp {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<jiff::Timestamp> for Timestamp {
fn from(value: jiff::Timestamp) -> Self {
Self(value.as_second() as u32)
}
}
impl From<Timestamp> for jiff::Timestamp {
fn from(value: Timestamp) -> Self {
jiff::Timestamp::from_second(*value as i64).unwrap()
}
}
impl From<bitcoin::locktime::absolute::Time> for Timestamp {
fn from(value: bitcoin::locktime::absolute::Time) -> Self {
Self(value.to_consensus_u32())
}
}
impl From<usize> for Timestamp {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Date> for Timestamp {
fn from(value: Date) -> Self {
Self::from(
jiff::civil::Date::from(value)
.to_zoned(TimeZone::UTC)
.unwrap()
.timestamp(),
)
}
}
impl CheckedSub<Timestamp> for Timestamp {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for Timestamp {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u32)
}
}
impl Add for Timestamp {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Timestamp {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl From<f64> for Timestamp {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as u32)
}
}
impl From<Timestamp> for f64 {
fn from(value: Timestamp) -> Self {
value.0 as f64
}
}

View File

@@ -0,0 +1,42 @@
use std::{fmt, mem};
use bitcoin::hashes::Hash;
use derive_deref::Deref;
use serde::{Serialize, Serializer};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(Debug, Deref, Clone, PartialEq, Eq, Immutable, IntoBytes, KnownLayout, FromBytes)]
pub struct Txid([u8; 32]);
impl From<bitcoin::Txid> for Txid {
fn from(value: bitcoin::Txid) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<Txid> for bitcoin::Txid {
fn from(value: Txid) -> Self {
unsafe { mem::transmute(value) }
}
}
impl From<&Txid> for bitcoin::Txid {
fn from(value: &Txid) -> Self {
bitcoin::Txid::from_slice(&value.0).unwrap()
}
}
impl fmt::Display for Txid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", bitcoin::Txid::from(self))
}
}
impl Serialize for Txid {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

View File

@@ -0,0 +1,60 @@
use byteview::ByteView;
use derive_deref::Deref;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_8bytes;
use super::Txid;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
)]
pub struct TxidPrefix([u8; 8]);
impl From<Txid> for TxidPrefix {
fn from(value: Txid) -> Self {
Self::from(&value)
}
}
impl From<&Txid> for TxidPrefix {
fn from(value: &Txid) -> Self {
Self(copy_first_8bytes(&value[..]).unwrap())
}
}
impl From<ByteView> for TxidPrefix {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
}
}
impl From<&TxidPrefix> for ByteView {
fn from(value: &TxidPrefix) -> Self {
Self::new(value.as_bytes())
}
}
impl From<TxidPrefix> for ByteView {
fn from(value: TxidPrefix) -> Self {
Self::from(&value)
}
}
impl From<[u8; 8]> for TxidPrefix {
fn from(value: [u8; 8]) -> Self {
Self(value)
}
}

View File

@@ -0,0 +1,124 @@
use std::ops::{Add, AddAssign};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use byteview::ByteView;
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_4bytes;
use super::StoredU32;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct TxIndex(u32);
impl TxIndex {
pub const ZERO: Self = Self(0);
pub fn new(txindex: u32) -> Self {
Self(txindex)
}
pub fn incremented(self) -> Self {
Self(*self + 1)
}
}
impl Add<TxIndex> for TxIndex {
type Output = Self;
fn add(self, rhs: TxIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<usize> for TxIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u32)
}
}
impl AddAssign<TxIndex> for TxIndex {
fn add_assign(&mut self, rhs: TxIndex) {
self.0 += rhs.0
}
}
impl CheckedSub<TxIndex> for TxIndex {
fn checked_sub(self, rhs: TxIndex) -> Option<Self> {
self.0.checked_sub(rhs.0).map(TxIndex::from)
}
}
impl From<u32> for TxIndex {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<u64> for TxIndex {
fn from(value: u64) -> Self {
Self(value as u32)
}
}
impl From<TxIndex> for u64 {
fn from(value: TxIndex) -> Self {
value.0 as u64
}
}
impl From<usize> for TxIndex {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<TxIndex> for usize {
fn from(value: TxIndex) -> Self {
value.0 as usize
}
}
impl From<ByteView> for TxIndex {
fn from(value: ByteView) -> Self {
Self(u32::from_be_bytes(copy_first_4bytes(&value).unwrap()))
}
}
impl From<TxIndex> for ByteView {
fn from(value: TxIndex) -> Self {
Self::new(&value.to_be_bytes())
}
}
impl From<TxIndex> for StoredU32 {
fn from(value: TxIndex) -> Self {
Self::from(value.0)
}
}
impl Printable for TxIndex {
fn to_string() -> &'static str {
"txindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["tx", "txindex"]
}
}

View File

@@ -0,0 +1,54 @@
use brk_vecs::StoredCompressed;
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::StoredU16;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
StoredCompressed,
)]
pub struct TxVersion(u16);
impl TxVersion {
pub const ONE: Self = Self(1);
pub const TWO: Self = Self(2);
pub const THREE: Self = Self(3);
pub const NON_STANDARD: Self = Self(u16::MAX);
}
impl From<bitcoin::transaction::Version> for TxVersion {
fn from(value: bitcoin::transaction::Version) -> Self {
match value.0 {
1 => Self::ONE,
2 => Self::TWO,
3 => Self::THREE,
_ => Self::NON_STANDARD,
}
}
}
impl From<TxVersion> for bitcoin::transaction::Version {
fn from(value: TxVersion) -> Self {
Self(value.0 as i32)
}
}
impl From<TxVersion> for StoredU16 {
fn from(value: TxVersion) -> Self {
Self::from(value.0)
}
}

View File

@@ -0,0 +1,119 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, StoredCompressed};
use byteview::ByteView;
use serde::Serialize;
use zerocopy::IntoBytes;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::copy_first_4bytes;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct TypeIndex(u32);
impl TypeIndex {
pub fn increment(&mut self) {
self.0 += 1;
}
pub fn incremented(self) -> Self {
Self(self.0 + 1)
}
pub fn copy_then_increment(&mut self) -> Self {
let i = *self;
self.increment();
i
}
}
impl From<u32> for TypeIndex {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<TypeIndex> for u32 {
fn from(value: TypeIndex) -> Self {
value.0
}
}
impl From<u64> for TypeIndex {
fn from(value: u64) -> Self {
Self(value as u32)
}
}
impl From<TypeIndex> for u64 {
fn from(value: TypeIndex) -> Self {
value.0 as u64
}
}
impl From<usize> for TypeIndex {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<TypeIndex> for usize {
fn from(value: TypeIndex) -> Self {
value.0 as usize
}
}
impl Add<u32> for TypeIndex {
type Output = Self;
fn add(self, rhs: u32) -> Self::Output {
Self(self.0 + rhs)
}
}
impl Add<usize> for TypeIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs as u32)
}
}
impl Add<TypeIndex> for TypeIndex {
type Output = Self;
fn add(self, rhs: TypeIndex) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl From<&[u8]> for TypeIndex {
fn from(value: &[u8]) -> Self {
Self(u32::from_be_bytes(copy_first_4bytes(value).unwrap()))
}
}
impl From<ByteView> for TypeIndex {
fn from(value: ByteView) -> Self {
Self::from(value.as_bytes())
}
}
impl From<TypeIndex> for ByteView {
fn from(value: TypeIndex) -> Self {
Self::new(&value.0.to_be_bytes())
}
}
impl CheckedSub<TypeIndex> for TypeIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}

View File

@@ -0,0 +1,47 @@
use byteview::ByteView;
use serde::Serialize;
use super::{OutputIndex, TypeIndex};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)]
pub struct TypeIndexWithOutputindex {
typeindex: TypeIndex,
outputindex: OutputIndex,
}
impl From<(TypeIndex, OutputIndex)> for TypeIndexWithOutputindex {
fn from(value: (TypeIndex, OutputIndex)) -> Self {
Self {
typeindex: value.0,
outputindex: value.1,
}
}
}
impl From<ByteView> for TypeIndexWithOutputindex {
fn from(value: ByteView) -> Self {
let typeindex = TypeIndex::from(&value[0..4]);
let outputindex = OutputIndex::from(&value[4..12]);
Self {
typeindex,
outputindex,
}
}
}
impl From<TypeIndexWithOutputindex> for ByteView {
fn from(value: TypeIndexWithOutputindex) -> Self {
ByteView::from(&value)
}
}
impl From<&TypeIndexWithOutputindex> for ByteView {
fn from(value: &TypeIndexWithOutputindex) -> Self {
ByteView::from(
[
u32::from(value.typeindex).to_be_bytes().as_slice(),
u64::from(value.outputindex).to_be_bytes().as_slice(),
]
.concat(),
)
}
}

View File

@@ -0,0 +1,15 @@
use byteview::ByteView;
#[derive(Debug, Clone)]
pub struct Unit;
impl From<ByteView> for Unit {
fn from(_: ByteView) -> Self {
Self
}
}
impl From<Unit> for ByteView {
fn from(_: Unit) -> Self {
Self::new(&[])
}
}

View File

@@ -0,0 +1,70 @@
use std::ops::Add;
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::TypeIndex;
#[derive(
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Clone,
Copy,
Deref,
DerefMut,
Default,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
StoredCompressed,
)]
pub struct UnknownOutputIndex(TypeIndex);
impl From<TypeIndex> for UnknownOutputIndex {
fn from(value: TypeIndex) -> Self {
Self(value)
}
}
impl From<UnknownOutputIndex> for u64 {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<UnknownOutputIndex> for usize {
fn from(value: UnknownOutputIndex) -> Self {
Self::from(*value)
}
}
impl From<usize> for UnknownOutputIndex {
fn from(value: usize) -> Self {
Self(TypeIndex::from(value))
}
}
impl Add<usize> for UnknownOutputIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(*self + rhs)
}
}
impl CheckedSub<UnknownOutputIndex> for UnknownOutputIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for UnknownOutputIndex {
fn to_string() -> &'static str {
"unknownoutputindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["unknownout", "unknownoutputindex"]
}
}

View File

@@ -0,0 +1,31 @@
use derive_deref::Deref;
#[derive(Debug, Deref, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vin(u32);
impl Vin {
pub const ZERO: Self = Vin(0);
pub const ONE: Self = Vin(1);
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}
}
impl From<u32> for Vin {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for Vin {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Vin> for u64 {
fn from(value: Vin) -> Self {
value.0 as u64
}
}

View File

@@ -0,0 +1,30 @@
use derive_deref::Deref;
#[derive(Debug, Deref, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vout(u32);
impl Vout {
const ZERO: Self = Vout(0);
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
}
}
impl From<u32> for Vout {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for Vout {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<Vout> for u64 {
fn from(value: Vout) -> Self {
value.0 as u64
}
}

View File

@@ -0,0 +1,127 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct WeekIndex(u16);
impl From<u16> for WeekIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<WeekIndex> for u16 {
fn from(value: WeekIndex) -> Self {
value.0
}
}
impl From<usize> for WeekIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<WeekIndex> for usize {
fn from(value: WeekIndex) -> Self {
value.0 as usize
}
}
impl Add<WeekIndex> for WeekIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for WeekIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for WeekIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl Add<usize> for WeekIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl From<DateIndex> for WeekIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for WeekIndex {
fn from(value: Date) -> Self {
let date = jiff::civil::Date::from(value).iso_week_date();
let mut week: u16 = 0;
let mut year = 2009;
while date.year() > year {
let d = jiff::civil::Date::new(year, 6, 6).unwrap();
let i = d.iso_week_date();
let w = i.weeks_in_year();
week += w as u16;
year += 1;
}
week += date.week() as u16;
week -= 1;
Self(week)
}
}
impl CheckedSub for WeekIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Printable for WeekIndex {
fn to_string() -> &'static str {
"weekindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["w", "week", "weekindex"]
}
}

View File

@@ -0,0 +1,81 @@
use std::ops::{Add, AddAssign, Div};
use brk_vecs::StoredCompressed;
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
StoredCompressed,
)]
pub struct Weight(u64);
impl From<bitcoin::Weight> for Weight {
fn from(value: bitcoin::Weight) -> Self {
Self(value.to_wu())
}
}
impl From<Weight> for bitcoin::Weight {
fn from(value: Weight) -> Self {
Self::from_wu(value.0)
}
}
impl From<usize> for Weight {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<f64> for Weight {
fn from(value: f64) -> Self {
Self(value as u64)
}
}
impl From<Weight> for f64 {
fn from(value: Weight) -> Self {
value.0 as f64
}
}
impl Add for Weight {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Weight {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Div<usize> for Weight {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}
impl Div<Weight> for Weight {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}

View File

@@ -0,0 +1,122 @@
use std::{
fmt::Debug,
ops::{Add, AddAssign, Div},
};
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::{Date, DateIndex, MonthIndex};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
Serialize,
Deserialize,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
StoredCompressed,
)]
pub struct YearIndex(u16);
impl From<u16> for YearIndex {
fn from(value: u16) -> Self {
Self(value)
}
}
impl From<usize> for YearIndex {
fn from(value: usize) -> Self {
Self(value as u16)
}
}
impl From<YearIndex> for u64 {
fn from(value: YearIndex) -> Self {
value.0 as u64
}
}
impl From<YearIndex> for usize {
fn from(value: YearIndex) -> Self {
value.0 as usize
}
}
impl Add<usize> for YearIndex {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self::from(self.0 + rhs as u16)
}
}
impl Add<YearIndex> for YearIndex {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from(self.0 + rhs.0)
}
}
impl AddAssign for YearIndex {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0)
}
}
impl Div<usize> for YearIndex {
type Output = Self;
fn div(self, _: usize) -> Self::Output {
unreachable!()
}
}
impl From<DateIndex> for YearIndex {
fn from(value: DateIndex) -> Self {
Self::from(Date::from(value))
}
}
impl From<Date> for YearIndex {
fn from(value: Date) -> Self {
Self(value.year() - 2009)
}
}
impl From<YearIndex> for u16 {
fn from(value: YearIndex) -> Self {
value.0
}
}
impl CheckedSub for YearIndex {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl From<MonthIndex> for YearIndex {
fn from(value: MonthIndex) -> Self {
Self((usize::from(value) / 12) as u16)
}
}
impl Printable for YearIndex {
fn to_string() -> &'static str {
"yearindex"
}
fn to_possible_strings() -> &'static [&'static str] {
&["y", "year", "yearindex"]
}
}