# docker-finance | modern accounting for the power-user
#
# Copyright (C) 2021-2025 Aaron Fiore (Founder, Evergreen Crypto LLC)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

fields code_,date,type,sub_type,currency_one,currency_one_amount,currency_two,currency_two_amount,rate_currency,rate_amount,direction,subaccount

date-format %Y-%m-%d %H:%M:%S

description %date +0000

account1 assets:blockfi:%subaccount:%currency_one
amount %currency_one_amount %currency_one

comment type:%type, sub_type:%sub_type, direction:%direction

# NOTE: see preprocess
if %code_ SKIP
  skip

# TODO: add subaccounts

################################################################################
#                                                                              #
#                                 TRANSFER                                     #
#                                                                              #
################################################################################

# TODO: get time as only time and add as time:%time (not date, just time. hledger is not timestamp friendly)

# UPDATE: 2022-02-02: BlockFi now has a separation of concerns on their platform for crypto accounting
#
#   1. "Wallet account"
#   2. "BIA (blockfi interest account) account"
#
#   From the website:
#
#     "Note: With the launch of BlockFi Wallet, all trading activity,
#      credit card rewards accumulation and collateral transfers for
#      loan products are conducted within Wallet."
#
#   If docker-finance tries to create rules through hacks, the rules all work
#   except for the crucial `Withdrawal` (which is the same for either accounts),
#   thus ruining any chance to easily account. As a result, current treatment
#   is to treat the account as a single account by dropping the internal
#   transfers. Again, would love to know the difference between wallet
#   withdrawal and an older BIA-style withdrawal but they make no indication of
#   which-is-which via the given line.

if %sub_type ^BIA
  skip

if %direction ^OUT$
  amount -%currency_one_amount %currency_one

# Default transfers as equity accounts
if %direction ^IN$
  account2 equity:blockfi:%subaccount:withdrawal:%currency_one

if %direction ^OUT$
  account2 equity:blockfi:%subaccount:deposit:%currency_one

if %direction ^OUT$
& %sub_type ^Withdrawal Fee$
  account2 expenses:blockfi:%subaccount:fees:withdrawals:%currency_one
  comment type:%type, sub_type:%sub_type, direction:%direction, taxed_as:SPEND
  comment2 %date +0000,SPEND,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,,FEE

if %direction ^OUT$
& %sub_type ^Withdrawal Fee$
& %currency_one (^GUSD$|^PAX$|^USDC$)
  comment2 %date +0000,SPEND,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_one_amount,FEE

# ---------------------------------------------------------------------------- #
#                              Transfers (Trades)                              #
# ---------------------------------------------------------------------------- #

# Annoying: ACH Trades pulls fiat from blockfi account when it's actually from an external account
if %type ^ACH Trade$
  amount %currency_one_amount %currency_one @@ %currency_two_amount USD
  account2 equity:blockfi:%subaccount:USD
  comment type:%type, sub_type:%sub_type, code:%code_, taxed_as:BUY
  comment2 %date +0000,BUY,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_two_amount,,

# They don't document ACH Deposits, which are instant buys for stablecoins (currently only GUSD).
if %sub_type (^Ach Deposit$|^Wire Deposit$)
& %currency_one ^GUSD$
  amount %currency_one_amount %currency_one @@ %currency_one_amount USD
  account2 equity:blockfi:%subaccount:USD
  comment type:%type, sub_type:%sub_type, taxed_as:BUY
  comment2 %date +0000,BUY,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_one_amount,,

# They don't document ACH Withdrawals, which are instant sells for stablecoins.
if %sub_type ^Ach Withdrawal$
  amount -%currency_one_amount %currency_one @@ %currency_one_amount USD
  account2 equity:blockfi:%subaccount:USD
  comment type:%type, sub_type:%sub_type, taxed_as:SELL
  comment2 %date +0000,SELL,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_one_amount,,

################################################################################
#                                                                              #
#                                   TRADE                                      #
#                                                                              #
################################################################################

# Trades NOTE:
#  - Unless specified, all trades are treated as "sells"
#  - All trades are market orders (no limit option)
if %type ^TRADE$
  account1 assets:blockfi:%subaccount:%currency_two
  amount -%currency_two_amount %currency_two @@ %currency_one_amount %currency_one
  account2 assets:blockfi:%subaccount:%currency_one
  comment type:%type, sub_type:%sub_type, code:%code_, taxed_as:SELL
  comment2 %date +0000,SELL,blockfi:%subaccount,%currency_two,%currency_two_amount,%currency_one,%currency_one_amount,,

################################################################################
#                                                                              #
#                                   INCOME                                     #
#                                                                              #
################################################################################

if %direction ^IN$
& %sub_type (^Interest Payment$|^Bonus Payment$|^Cc Rewards Redemption$|^Cc Trading Rebate$)
  account2 income:blockfi:%subaccount:%currency_one
  comment type:%type, sub_type:%sub_type, direction:%direction, taxed_as:INCOME
  comment2 %date +0000,INCOME,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,,INTEREST

if %direction ^IN$
& %sub_type ^Interest Payment$
& %currency_one (^GUSD$|^PAX$|^USDC$)
  comment2 %date +0000,INCOME,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_one_amount,INTEREST

# ---------------------------------------------------------------------------- #
#                                   Bonuses                                    #
# ---------------------------------------------------------------------------- #

if %direction ^IN$
& %sub_type ^Bonus Payment$
  comment2 %date +0000,INCOME,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,,INCOME

if %direction ^IN$
& %sub_type ^Bonus Payment$
& %currency_one (^GUSD$|^PAX$|^USDC$)
  comment2 %date +0000,INCOME,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_one_amount,INCOME

# ---------------------------------------------------------------------------- #
#                                   Rebates                                    #
# ---------------------------------------------------------------------------- #

# Rebates are not considered taxable income. Still need open position.
if %direction ^IN$
& %sub_type (^Cc Rewards Redemption$|^Cc Trading Rebate$)
  comment type:%type, sub_type:%sub_type, direction:%direction, taxed_as:REBATE
  comment2 %date +0000,BUY,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,,,

if %direction ^IN$
& %sub_type (^Cc Rewards Redemption$|^Cc Trading Rebate$)
& %currency_one (^GUSD$|^PAX$|^USDC$)
  comment2 %date +0000,BUY,blockfi:%subaccount,%currency_one,%currency_one_amount,USD,%currency_one_amount,,

# vim: sw=2 sts=2 si ai et
