diff --git a/src/db.rs b/src/db.rs index 594fc80..08c8011 100644 --- a/src/db.rs +++ b/src/db.rs @@ -302,17 +302,20 @@ pub struct Lot { } impl Lot { + pub fn basis(&self, token: MaybeToken) -> f64 { + (self.acquisition.price() * Decimal::from_f64(token.ui_amount(self.amount)).unwrap()) + .try_into() + .unwrap() + } + // Figure the amount of income that the Lot incurred pub fn income(&self, token: MaybeToken) -> f64 { match self.acquisition.kind { // These lots were acquired pre-tax LotAcquistionKind::EpochReward { .. } | LotAcquistionKind::NotAvailable => { - (self.acquisition.price() - * Decimal::from_f64(token.ui_amount(self.amount)).unwrap()) - .try_into() - .unwrap() + self.basis(token) } - // Assume these kinds of lots are acquired with post-tax funds + // Assume these kinds of lots are acquired post-tax LotAcquistionKind::Exchange { .. } | LotAcquistionKind::Fiat | LotAcquistionKind::Swap { .. } diff --git a/src/main.rs b/src/main.rs index b27ada1..1d4affb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -941,6 +941,7 @@ async fn process_exchange_sell( None, &mut 0., &mut 0., + &mut 0., &mut false, &mut 0., None, @@ -1685,6 +1686,7 @@ async fn maybe_println_lot( lot: &Lot, current_price: Option, liquidity_token_info: Option<&LiquidityTokenInfo>, + total_basis: &mut f64, total_income: &mut f64, total_cap_gain: &mut f64, long_term_cap_gain: &mut bool, @@ -1697,6 +1699,7 @@ async fn maybe_println_lot( f64::try_from(Decimal::from_f64(token.ui_amount(lot.amount)).unwrap() * current_price) .unwrap() }); + let basis = lot.basis(token); let income = lot.income(token); let cap_gain = lot.cap_gain(token, current_price.unwrap_or_default()); @@ -1714,6 +1717,7 @@ async fn maybe_println_lot( } } + *total_basis += basis; *total_income += income; *total_cap_gain += cap_gain; *total_current_value += current_value.unwrap_or_default(); @@ -1932,6 +1936,7 @@ async fn process_account_add( None, &mut 0., &mut 0., + &mut 0., &mut false, &mut 0., None, @@ -2223,6 +2228,7 @@ async fn process_account_list( let mut lots = account.lots.iter().collect::>(); lots.sort_by_key(|lot| lot.acquisition.when); + let mut account_basis = 0.; let mut account_income = 0.; let mut account_current_value = 0.; let mut account_unrealized_short_term_gain = 0.; @@ -2241,6 +2247,7 @@ async fn process_account_list( lot, current_token_price, liquidity_token_info.as_ref(), + &mut account_basis, &mut account_income, &mut account_unrealized_gain, &mut long_term_cap_gain, @@ -2295,6 +2302,7 @@ async fn process_account_list( lot, current_token_price, liquidity_token_info.as_ref(), + &mut account_basis, &mut account_income, &mut account_unrealized_gain, &mut long_term_cap_gain, @@ -2321,11 +2329,6 @@ async fn process_account_list( } } - let account_basis = account_current_value - - account_income - - account_unrealized_short_term_gain - - account_unrealized_long_term_gain; - println!( " Value: ${}{}", account_current_value.separated_string_with_fixed_place(2), @@ -3767,6 +3770,7 @@ async fn process_account_sync( None, &mut 0., &mut 0., + &mut 0., &mut false, &mut 0., Some(notifier), @@ -3825,6 +3829,7 @@ async fn process_account_sync( None, &mut 0., &mut 0., + &mut 0., &mut false, &mut 0., Some(notifier),