dig_events_protocol/value.rs
1//! The payload newtypes carried inside events (SPEC §3).
2//!
3//! These are the leaf value types the engine and apps agree on: which wallet an event concerns
4//! ([`WalletId`]), how much value moved ([`Amount`], in the smallest on-chain unit), and which
5//! asset ([`AssetId`], the CAT tail hex; `None` on an event means native XCH). They are thin
6//! newtypes so the wire shape is a bare number/string, not a tagged object.
7
8use serde::{Deserialize, Serialize};
9
10/// The stable identifier of a wallet within an engine instance.
11///
12/// Serializes as a bare `u32` on the wire.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
14pub struct WalletId(pub u32);
15
16impl std::fmt::Display for WalletId {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 write!(f, "{}", self.0)
19 }
20}
21
22/// An on-chain value amount, in the smallest indivisible unit (mojos).
23///
24/// Serializes as a bare `u64` on the wire.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
26pub struct Amount(pub u64);
27
28impl Amount {
29 /// The raw value in the smallest unit (mojos).
30 pub fn mojos(self) -> u64 {
31 self.0
32 }
33}
34
35impl std::fmt::Display for Amount {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 write!(f, "{}", self.0)
38 }
39}
40
41/// A CAT asset identifier — the asset's TAIL hash, hex-encoded.
42///
43/// On an event's `asset` field, `Some(AssetId(..))` is a CAT and `None` is native XCH.
44/// Serializes as a bare string on the wire.
45#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
46pub struct AssetId(pub String);
47
48impl std::fmt::Display for AssetId {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "{}", self.0)
51 }
52}