use std::collections::HashMap;
use serde_json::{json, Map, Value};
use super::check::{digest, Error};
use super::names::*;
#[derive(Clone, Debug)]
pub struct HistoryRecord {
pub payload: Vec<u8>,
pub committed_at: String,
}
#[derive(Clone, Debug)]
pub struct HistoryResult {
pub result: Vec<u8>,
pub verified: bool,
}
#[derive(Clone, Debug, Default)]
pub struct HistoryInput {
pub records: Vec<HistoryRecord>,
pub results: Vec<HistoryResult>,
pub counterparty_half_verified: bool,
}
const UNPRICED: &str = "dealprofile: a deal has no recorded amount";
struct Rec {
committed_at: String,
kind: String,
deal_id: String,
refs: HashMap<String, Vec<String>>,
body: Map<String, Value>,
has_class: bool,
class: String,
}
impl Rec {
fn str(&self, member: &str) -> &str {
self.body.get(member).and_then(Value::as_str).unwrap_or("")
}
fn reference(&self, rel: &str) -> &str {
match self.refs.get(rel).map(Vec::as_slice) {
Some([d]) => d,
_ => "",
}
}
}
fn not_json(why: &str) -> Error {
Error::Invalid(format!("dealprofile: a deal record is not JSON: {why}"))
}
fn string_member(obj: &Map<String, Value>, name: &str) -> Result<String, Error> {
match obj.get(name) {
None | Some(Value::Null) => Ok(String::new()),
Some(Value::String(s)) => Ok(s.clone()),
Some(_) => Err(not_json(&format!("member {name} is not a string"))),
}
}
fn refs_of(obj: &Map<String, Value>) -> Result<Vec<(String, String)>, Error> {
let items = match obj.get("refs") {
None | Some(Value::Null) => return Ok(Vec::new()),
Some(Value::Array(a)) => a,
Some(_) => return Err(not_json("refs is not an array")),
};
let mut out = Vec::new();
for r in items {
match r {
Value::Null => out.push((String::new(), String::new())),
Value::Object(o) => {
string_member(o, "type")?;
string_member(o, "digest_alg")?;
out.push((string_member(o, "rel")?, string_member(o, "digest")?));
}
_ => return Err(not_json("a ref is not an object")),
}
}
Ok(out)
}
fn read_record(r: &HistoryRecord) -> Result<Option<(String, Rec)>, Error> {
if r.payload.starts_with(&[0xEF, 0xBB, 0xBF]) {
return Err(not_json("a byte order mark"));
}
let text = String::from_utf8_lossy(&r.payload);
let top: Value = serde_json::from_str(&text).map_err(|e| not_json(&e.to_string()))?;
if top.is_null() {
return Ok(None);
}
let Value::Object(top) = top else {
return Err(not_json("not an object"));
};
let block = match top.get("x-deal-v0") {
None | Some(Value::Null) => None,
Some(Value::Object(b)) => Some(b),
Some(_) => return Err(not_json("the x-deal-v0 block is not an object")),
};
let typ = string_member(&top, "type")?;
let chain_id = string_member(&top, "chain_id")?;
let body = match top.get("body") {
None | Some(Value::Null) => Map::new(),
Some(Value::Object(b)) => b.clone(),
Some(_) => return Err(not_json("body is not an object")),
};
let top_refs = refs_of(&top)?;
let (kind, deal_id, refs) = if let Some(b) = block {
(
string_member(b, "record_type")?,
string_member(b, "deal_id")?,
refs_of(b)?,
)
} else if typ.ends_with("/v0") {
(typ, chain_id, top_refs)
} else {
return Ok(None);
};
let mut by_rel: HashMap<String, Vec<String>> = HashMap::new();
for (rel, d) in refs {
by_rel.entry(rel).or_default().push(d);
}
let class = top.get("counterparty_class");
let d = digest(&r.payload)?;
Ok(Some((
d,
Rec {
committed_at: r.committed_at.clone(),
kind,
deal_id,
refs: by_rel,
body,
has_class: class.is_some(),
class: class.and_then(Value::as_str).unwrap_or("").to_string(),
},
)))
}
const CLASSES: [&str; 3] = ["person_agent", "business_agent", "shop_preauth"];
fn counterparty_class(records: &[Rec]) -> Option<&str> {
let mut carriers = records.iter().filter(|r| r.has_class);
let (c, None) = (carriers.next()?, carriers.next()) else {
return None;
};
if c.kind != TYPE_CHECK || c.str("action") != "pay" || !CLASSES.contains(&c.class.as_str()) {
return None;
}
Some(&c.class)
}
pub fn standard_history_deal(input: &HistoryInput) -> Result<Option<Value>, Error> {
let mut records: Vec<Rec> = Vec::new();
let mut by_digest: HashMap<String, usize> = HashMap::new();
for r in &input.records {
if let Some((d, rec)) = read_record(r)? {
by_digest.insert(d, records.len());
records.push(rec);
}
}
let Some(first) = records.first() else {
return Ok(None);
};
if first.deal_id.starts_with("sale-") || first.kind == TYPE_SALE {
return Ok(None);
}
let Some(agreed) = records.iter().find(|r| {
(r.kind == TYPE_ACTION || r.kind == TYPE_ACTION_RECORD)
&& matches!(r.str("action"), "commit" | "pay")
}) else {
return Ok(None);
};
let deal_id = first.deal_id.clone();
let amount = agreed_amount(agreed, &records, &by_digest).map_err(|e| match e {
Error::Unpriced(m) => Error::Unpriced(format!("deal {deal_id}: {m}")),
e => e,
})?;
let mut entry = Map::new();
entry.insert("deal".into(), deal_id.into());
entry.insert("agreed_at".into(), minute(&agreed.committed_at)?.into());
if let Some(class) = counterparty_class(&records) {
entry.insert("counterparty_class".into(), class.into());
}
entry.insert("role".into(), deal_role(&records).into());
entry.insert("amount".into(), amount);
entry.insert(
"outcome".into(),
deal_outcome(&records, &by_digest, &input.results).into(),
);
entry.insert(
"happened".into(),
happened(&records, &by_digest, input.counterparty_half_verified).into(),
);
entry.insert("disputes".into(), json!([]));
Ok(Some(Value::Object(entry)))
}
fn minute(at: &str) -> Result<String, Error> {
let bad = || Error::Invalid(format!("dealprofile: committed_at {at:?} is not RFC 3339"));
let b = at.as_bytes();
let num = |from: usize, to: usize| -> Result<i64, Error> {
at.get(from..to)
.filter(|s| s.bytes().all(|c| c.is_ascii_digit()))
.and_then(|s| s.parse().ok())
.ok_or_else(bad)
};
if b.len() < 20
|| b[4] != b'-'
|| b[7] != b'-'
|| !matches!(b[10], b'T' | b't')
|| b[13] != b':'
|| b[16] != b':'
{
return Err(bad());
}
let (y, mo, d, h, mi, sec) = (
num(0, 4)?,
num(5, 7)?,
num(8, 10)?,
num(11, 13)?,
num(14, 16)?,
num(17, 19)?,
);
let leap = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
let month_days = [
31,
if leap { 29 } else { 28 },
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
];
if !(1..=12).contains(&mo)
|| d < 1
|| d > month_days[mo as usize - 1]
|| h > 23
|| mi > 59
|| sec > 59
{
return Err(bad());
}
let mut i = 19;
if b[i] == b'.' {
let start = i + 1;
i = start;
while i < b.len() && b[i].is_ascii_digit() {
i += 1;
}
if i == start {
return Err(bad());
}
}
let offset = match b.get(i) {
Some(b'Z' | b'z') if i + 1 == b.len() => 0,
Some(sign @ (b'+' | b'-')) if i + 6 == b.len() && b[i + 3] == b':' => {
let (oh, om) = (num(i + 1, i + 3)?, num(i + 4, i + 6)?);
if oh > 23 || om > 59 {
return Err(bad());
}
let m = oh * 60 + om;
if *sign == b'+' {
m
} else {
-m
}
}
_ => return Err(bad()),
};
let (yy, mm) = if mo <= 2 {
(y - 1, mo + 9)
} else {
(y, mo - 3)
};
let era = yy.div_euclid(400);
let yoe = yy - era * 400;
let doy = (153 * mm + 2) / 5 + d - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
let days = era * 146097 + doe - 719468;
let minutes = days * 1440 + h * 60 + mi - offset;
let (days, rem) = (minutes.div_euclid(1440), minutes.rem_euclid(1440));
let z = days + 719468;
let era = z.div_euclid(146097);
let doe = z - era * 146097;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + i64::from(month <= 2);
Ok(format!(
"{year:04}-{month:02}-{day:02}T{:02}:{:02}:00Z",
rem / 60,
rem % 60
))
}
fn agreed_amount(
step: &Rec,
records: &[Rec],
by_digest: &HashMap<String, usize>,
) -> Result<Value, Error> {
let get = |d: &str| by_digest.get(d).map(|&i| &records[i]);
let unpriced = |why: String| Error::Unpriced(format!("{UNPRICED}: {why}"));
let mut check = None;
if step.kind == TYPE_ACTION_RECORD {
let d = step
.body
.get("evaluation_ref")
.and_then(|r| r.get("digest"))
.and_then(Value::as_str)
.unwrap_or("");
if let Some(ev) = get(d).filter(|e| e.kind == TYPE_ACTION_EVALUATION) {
check =
get(ev.str("proposed_action_digest")).filter(|c| c.kind == TYPE_PROPOSED_ACTION);
}
} else if let Some(ap) =
get(step.reference("authorized_by")).filter(|a| a.kind == TYPE_APPROVAL)
{
if let Some(v) = get(ap.reference("approves")).filter(|v| v.kind == TYPE_VERDICT) {
check = get(v.reference("checks")).filter(|c| c.kind == TYPE_CHECK);
}
}
let Some(check) = check else {
return Err(unpriced(format!(
"the {} step's check is not on its log",
step.str("action")
)));
};
let minor = check
.body
.get("amount_minor")
.and_then(Value::as_number)
.and_then(|n| n.as_i64())
.filter(|m| *m >= 0);
let currency = check.str("currency");
let Some(minor) = minor.filter(|_| !currency.is_empty()) else {
return Err(unpriced("its check states no amount and currency".into()));
};
let Some(scale) = history_asset_scale(currency) else {
return Err(unpriced(format!(
"currency {currency:?} has no ISO 4217 minor units"
)));
};
Ok(json!({"value": minor.to_string(), "assetCode": currency, "assetScale": scale}))
}
fn deal_role(records: &[Rec]) -> &'static str {
for r in records {
if r.kind == TYPE_BASELINE {
let seller = r
.body
.get("intent")
.and_then(|i| i.get("party_role"))
.and_then(Value::as_str)
== Some("seller");
return if seller { "provided" } else { "bought" };
}
}
"bought"
}
fn deal_outcome(
records: &[Rec],
by_digest: &HashMap<String, usize>,
results: &[HistoryResult],
) -> &'static str {
for r in records {
if r.kind == TYPE_OUTCOME_NOTE && r.str("outcome") == "did_not_hold" {
if let Some(&i) = by_digest.get(r.reference("confirms")) {
if records[i].kind == TYPE_CLOSE {
return "did_not_hold";
}
}
}
}
reconcile_outcome(results)
}
fn reconcile_outcome(results: &[HistoryResult]) -> &'static str {
let Some(res) = results.iter().rev().find(|r| r.verified) else {
return "undecided";
};
let Ok(Value::Object(doc)) =
serde_json::from_str::<Value>(&String::from_utf8_lossy(&res.result))
else {
return "undecided";
};
let Some(Value::Array(claims)) = doc.get("claims") else {
return "undecided";
};
let mut verdicts = Vec::new();
for c in claims {
match c {
Value::Null => verdicts.push(""),
Value::Object(o) => match o.get("verdict") {
None | Some(Value::Null) => verdicts.push(""),
Some(Value::String(s)) => verdicts.push(s.as_str()),
Some(_) => return "undecided",
},
_ => return "undecided",
}
}
if verdicts.is_empty() {
return "undecided";
}
if verdicts.iter().all(|v| *v == "met") {
"held"
} else {
"undecided"
}
}
fn happened(
records: &[Rec],
by_digest: &HashMap<String, usize>,
counterparty_half: bool,
) -> &'static str {
if counterparty_half {
return "stamped_by_both";
}
let last_close = records
.iter()
.rfind(|r| r.kind == TYPE_CLOSE && r.str("outcome") != "open");
if let Some(fin) = last_close {
if let Some(&i) = by_digest.get(fin.reference("outcome")) {
let o = &records[i];
if (o.kind == TYPE_OUTCOME || o.kind == TYPE_ACTION_OUTCOME)
&& o.str("status") == "received"
{
return "receipt_only";
}
}
}
"no_record"
}
const MINOR_UNIT_EXCEPTIONS: [(&str, u8); 26] = [
("BIF", 0),
("CLP", 0),
("DJF", 0),
("GNF", 0),
("ISK", 0),
("JPY", 0),
("KMF", 0),
("KRW", 0),
("PYG", 0),
("RWF", 0),
("UGX", 0),
("UYI", 0),
("VND", 0),
("VUV", 0),
("XAF", 0),
("XOF", 0),
("XPF", 0),
("BHD", 3),
("IQD", 3),
("JOD", 3),
("KWD", 3),
("LYD", 3),
("OMR", 3),
("TND", 3),
("CLF", 4),
("UYW", 4),
];
const TWO_DECIMAL_CURRENCIES: &str = " AED AFN ALL AMD ANG AOA ARS AUD AWG AZN BAM BBD BDT BGN BMD BND BOB BOV BRL BSD BTN BWP BYN BZD \
CAD CDF CHE CHF CHW CNY COP COU CRC CUP CVE CZK DKK DOP DZD EGP ERN ETB EUR FJD FKP GBP GEL GHS GIP GMD GTQ GYD \
HKD HNL HTG HUF IDR ILS INR IRR JMD KES KGS KHR KPW KYD KZT LAK LBP LKR LRD LSL MAD MDL MGA MKD MMK MNT MOP MRU \
MUR MVR MWK MXN MXV MYR MZN NAD NGN NIO NOK NPR NZD PAB PEN PGK PHP PKR PLN QAR RON RSD RUB SAR SBD SCR SDG SEK \
SGD SHP SLE SOS SRD SSP STN SVC SYP SZL THB TJS TMT TOP TRY TTD TWD TZS UAH USD USN UYU UZS VED VES WST XCD XCG \
YER ZAR ZMW ZWG ";
const HISTORY_ASSET_SCALES: &[(&str, u8)] = &[("BTC", 11)];
pub(crate) fn history_asset_scale(currency: &str) -> Option<u8> {
HISTORY_ASSET_SCALES
.iter()
.find(|(c, _)| *c == currency)
.map(|(_, s)| *s)
.or_else(|| minor_units(currency))
}
pub fn minor_units(currency: &str) -> Option<u8> {
if let Some((_, s)) = MINOR_UNIT_EXCEPTIONS.iter().find(|(c, _)| *c == currency) {
return Some(*s);
}
(currency.len() == 3 && TWO_DECIMAL_CURRENCIES.contains(&format!(" {currency} "))).then_some(2)
}
#[cfg(test)]
mod tests {
use super::minute;
#[test]
fn minute_floors_to_utc() {
for (at, want) in [
("2026-10-06T09:09:59Z", "2026-10-06T09:09:00Z"),
("2026-10-06T09:09:59.123456Z", "2026-10-06T09:09:00Z"),
("2026-10-06T01:30:00+05:30", "2026-10-05T20:00:00Z"),
("2026-10-06T23:30:00-02:00", "2026-10-07T01:30:00Z"),
("2024-02-29T00:00:00Z", "2024-02-29T00:00:00Z"),
("2026-01-01T00:00:00+01:00", "2025-12-31T23:00:00Z"),
] {
assert_eq!(minute(at).unwrap(), want, "{at}");
}
}
#[test]
fn minute_refuses_what_is_not_rfc_3339() {
for at in [
"",
"2026-10-06",
"2026-10-06T09:09Z",
"2026-10-06T09:09:xxZ",
"2026-13-01T00:00:00Z",
"2026-02-30T00:00:00Z",
"2025-02-29T00:00:00Z",
"2026-10-06T24:00:00Z",
"2026-10-06T09:60:00Z",
"2026-10-06T09:09:60Z",
"2026-10-06T09:09:00.Z",
"2026-10-06T09:09:00",
"2026-10-06T09:09:00+24:00",
"2026-10-06T09:09:00+01",
"2026-10-06T09:09:00Zjunk",
] {
assert!(minute(at).is_err(), "{at}");
}
}
}