Skip to content

Commit c8746ed

Browse files
committed
Print current holding in summary mode
1 parent f798ac2 commit c8746ed

File tree

1 file changed

+119
-83
lines changed

1 file changed

+119
-83
lines changed

src/main.rs

+119-83
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,95 @@ async fn process_account_cost_basis(
19281928
Ok(())
19291929
}
19301930

1931+
fn print_current_holdings(
1932+
held_tokens: &BTreeMap::<MaybeToken, (/*price*/ Option<Decimal>, /*amount*/ u64, RealizedGain)>,
1933+
tax_rate: Option<&TaxRate>,
1934+
) {
1935+
println!("Current Holdings");
1936+
let mut held_tokens = held_tokens
1937+
.into_iter()
1938+
.map(
1939+
|(held_token, (current_token_price, total_held_amount, unrealized_gain))| {
1940+
let total_value = current_token_price.map(|current_token_price| {
1941+
f64::try_from(
1942+
Decimal::from_f64(held_token.ui_amount(*total_held_amount)).unwrap()
1943+
* current_token_price,
1944+
)
1945+
.unwrap()
1946+
});
1947+
1948+
(
1949+
held_token,
1950+
total_value,
1951+
current_token_price,
1952+
total_held_amount,
1953+
unrealized_gain,
1954+
)
1955+
},
1956+
)
1957+
.collect::<Vec<_>>();
1958+
1959+
// Order current holdings by `total_value`
1960+
held_tokens
1961+
.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
1962+
1963+
for (held_token, total_value, current_token_price, total_held_amount, unrealized_gain) in
1964+
held_tokens
1965+
{
1966+
if *total_held_amount == 0 {
1967+
continue;
1968+
}
1969+
1970+
let estimated_tax = tax_rate
1971+
.and_then(|tax_rate| {
1972+
let tax = unrealized_gain.short_term_cap_gain * tax_rate.short_term_gain
1973+
+ unrealized_gain.long_term_cap_gain * tax_rate.long_term_gain;
1974+
1975+
if tax > 0. {
1976+
Some(format!(
1977+
"; ${} estimated tax",
1978+
tax.separated_string_with_fixed_place(2)
1979+
))
1980+
} else {
1981+
None
1982+
}
1983+
})
1984+
.unwrap_or_default();
1985+
1986+
if held_token.fiat_fungible() {
1987+
println!(
1988+
" {:<7} {:<22}",
1989+
held_token.to_string(),
1990+
held_token.format_amount(*total_held_amount)
1991+
);
1992+
} else {
1993+
println!(
1994+
" {:<7} {:<22} [{}; ${:>4} per {:>4}{}]",
1995+
held_token.to_string(),
1996+
held_token.format_amount(*total_held_amount),
1997+
total_value
1998+
.map(|tv| {
1999+
format!(
2000+
"${:14} ({:>8}%)",
2001+
tv.separated_string_with_fixed_place(2),
2002+
((tv - unrealized_gain.basis) / unrealized_gain.basis * 100.)
2003+
.separated_string_with_fixed_place(2)
2004+
)
2005+
})
2006+
.unwrap_or_else(|| "?".into()),
2007+
current_token_price
2008+
.map(|current_token_price| f64::try_from(current_token_price)
2009+
.unwrap()
2010+
.separated_string_with_fixed_place(3))
2011+
.unwrap_or_else(|| "?".into()),
2012+
held_token,
2013+
estimated_tax,
2014+
);
2015+
}
2016+
}
2017+
println!();
2018+
}
2019+
19312020
async fn process_account_list(
19322021
db: &Db,
19332022
rpc_client: &RpcClient,
@@ -2035,6 +2124,24 @@ async fn process_account_list(
20352124
account.assert_lot_balance();
20362125

20372126
if summary_only {
2127+
if !account.lots.is_empty() {
2128+
let mut account_basis = 0.;
2129+
let mut account_value = 0.;
2130+
for lot in account.lots.iter() {
2131+
let value = current_token_price.map(|price| {
2132+
f64::try_from(
2133+
Decimal::from_f64(
2134+
account.token.ui_amount(lot.amount)
2135+
).unwrap() * price
2136+
).unwrap()
2137+
});
2138+
account_basis += lot.basis(account.token);
2139+
account_value += value.unwrap_or_default();
2140+
}
2141+
held_token.2.basis += account_basis;
2142+
total_current_basis += account_basis;
2143+
total_current_value += account_value;
2144+
}
20382145
continue;
20392146
}
20402147

@@ -2202,6 +2309,17 @@ async fn process_account_list(
22022309
println!();
22032310
}
22042311

2312+
if summary_only {
2313+
print_current_holdings(&held_tokens, db.get_tax_rate());
2314+
println!(
2315+
"Current Value: ${} ({}%)",
2316+
total_current_value.separated_string_with_fixed_place(2),
2317+
(((total_current_value - total_current_fiat_value) - total_current_basis)
2318+
/ total_current_basis
2319+
* 100.)
2320+
.separated_string_with_fixed_place(2),
2321+
);
2322+
}
22052323
if account_filter.is_some() || summary_only {
22062324
return Ok(());
22072325
}
@@ -2369,89 +2487,7 @@ async fn process_account_list(
23692487
}
23702488
println!();
23712489

2372-
println!("Current Holdings");
2373-
let mut held_tokens = held_tokens
2374-
.into_iter()
2375-
.map(
2376-
|(held_token, (current_token_price, total_held_amount, unrealized_gain))| {
2377-
let total_value = current_token_price.map(|current_token_price| {
2378-
f64::try_from(
2379-
Decimal::from_f64(held_token.ui_amount(total_held_amount)).unwrap()
2380-
* current_token_price,
2381-
)
2382-
.unwrap()
2383-
});
2384-
2385-
(
2386-
held_token,
2387-
total_value,
2388-
current_token_price,
2389-
total_held_amount,
2390-
unrealized_gain,
2391-
)
2392-
},
2393-
)
2394-
.collect::<Vec<_>>();
2395-
2396-
// Order current holdings by `total_value`
2397-
held_tokens
2398-
.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
2399-
2400-
for (held_token, total_value, current_token_price, total_held_amount, unrealized_gain) in
2401-
held_tokens
2402-
{
2403-
if total_held_amount == 0 {
2404-
continue;
2405-
}
2406-
2407-
let estimated_tax = tax_rate
2408-
.and_then(|tax_rate| {
2409-
let tax = unrealized_gain.short_term_cap_gain * tax_rate.short_term_gain
2410-
+ unrealized_gain.long_term_cap_gain * tax_rate.long_term_gain;
2411-
2412-
if tax > 0. {
2413-
Some(format!(
2414-
"; ${} estimated tax",
2415-
tax.separated_string_with_fixed_place(2)
2416-
))
2417-
} else {
2418-
None
2419-
}
2420-
})
2421-
.unwrap_or_default();
2422-
2423-
if held_token.fiat_fungible() {
2424-
println!(
2425-
" {:<7} {:<22}",
2426-
held_token.to_string(),
2427-
held_token.format_amount(total_held_amount)
2428-
);
2429-
} else {
2430-
println!(
2431-
" {:<7} {:<22} [{}; ${:>4} per {:>4}{}]",
2432-
held_token.to_string(),
2433-
held_token.format_amount(total_held_amount),
2434-
total_value
2435-
.map(|tv| {
2436-
format!(
2437-
"${:14} ({:>8}%)",
2438-
tv.separated_string_with_fixed_place(2),
2439-
((tv - unrealized_gain.basis) / unrealized_gain.basis * 100.)
2440-
.separated_string_with_fixed_place(2)
2441-
)
2442-
})
2443-
.unwrap_or_else(|| "?".into()),
2444-
current_token_price
2445-
.map(|current_token_price| f64::try_from(current_token_price)
2446-
.unwrap()
2447-
.separated_string_with_fixed_place(3))
2448-
.unwrap_or_else(|| "?".into()),
2449-
held_token,
2450-
estimated_tax,
2451-
);
2452-
}
2453-
}
2454-
println!();
2490+
print_current_holdings(&held_tokens, tax_rate);
24552491

24562492
println!("Summary");
24572493
println!(

0 commit comments

Comments
 (0)