Skip to content

Commit

Permalink
Fix basis calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed May 30, 2024
1 parent fdbfcf7 commit 8b55817
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. }
Expand Down
15 changes: 10 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ async fn process_exchange_sell(
None,
&mut 0.,
&mut 0.,
&mut 0.,
&mut false,
&mut 0.,
None,
Expand Down Expand Up @@ -1685,6 +1686,7 @@ async fn maybe_println_lot(
lot: &Lot,
current_price: Option<Decimal>,
liquidity_token_info: Option<&LiquidityTokenInfo>,
total_basis: &mut f64,
total_income: &mut f64,
total_cap_gain: &mut f64,
long_term_cap_gain: &mut bool,
Expand All @@ -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());

Expand All @@ -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();
Expand Down Expand Up @@ -1932,6 +1936,7 @@ async fn process_account_add(
None,
&mut 0.,
&mut 0.,
&mut 0.,
&mut false,
&mut 0.,
None,
Expand Down Expand Up @@ -2223,6 +2228,7 @@ async fn process_account_list(
let mut lots = account.lots.iter().collect::<Vec<_>>();
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.;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -3767,6 +3770,7 @@ async fn process_account_sync(
None,
&mut 0.,
&mut 0.,
&mut 0.,
&mut false,
&mut 0.,
Some(notifier),
Expand Down Expand Up @@ -3825,6 +3829,7 @@ async fn process_account_sync(
None,
&mut 0.,
&mut 0.,
&mut 0.,
&mut false,
&mut 0.,
Some(notifier),
Expand Down

0 comments on commit 8b55817

Please sign in to comment.