# 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 account_name,subaccount_name,subaccount_address,id,fee,receiver_rewards,round_time,sender,sender_rewards,tx_type,amount_,receiver,direction,to_self

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

description %round_time +0000

comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, receiver:%receiver, txid:%id, to_self:%to_self, direction:%direction

# TODO: non-static symbol for asset support

account1 assets:%account_name:%subaccount_name:algorand:ALGO
amount %amount_ ALGO

# NOTE: use custom rules for default non-equity IN/OUT

# Default transfers as equity accounts
if %direction ^IN$
& %to_self ^false$
  account2 equity:%account_name:%subaccount_name:algorand:withdrawal:ALGO

if %direction ^OUT$
& %to_self ^false$
  account2 equity:%account_name:%subaccount_name:algorand:deposit:ALGO

# TODO: when depositing to AlgoFi and/or related defi, use accounts payable instead of equity

if %to_self ^true$
  account2 assets:%account_name:%subaccount_name:algorand:ALGO

if %direction ^OUT$
& %to_self ^false$
  amount -%amount_ ALGO

# ---------------------------------------------------------------------------- #
#                                      Fees                                    #
# ---------------------------------------------------------------------------- #

if %direction ^OUT$
& %fee [1-9]
  account3 assets:%account_name:%subaccount_name:algorand:ALGO
  amount3 -%fee ALGO
  account4 expenses:%account_name:%subaccount_name:algorand:fees:ALGO
  amount4 %fee ALGO
  comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, receiver:%receiver, txid:%id, to_self:%to_self, direction:%direction, taxed_as:SPEND
  comment3 %round_time +0000,SPEND,%account_name:%subaccount_name:algorand,ALGO,%fee,USD,,FEE

if %direction ^OUT$
& %fee [1-9]
& %tx_type ^appl$
  comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, txid:%id, to_self:%to_self, direction:%direction, taxed_as:SPEND

# Remove 0 value txs with no comment tags
if %direction ^OUT$
& %fee [1-9]
& %amount_ ^[^1-9]*$
  account1
  account2

# Skip empty txs w/ no fees or rewards
if %direction (^IN$|^OUT$)
& %to_self (^true$|^false$)
& %sender_rewards ^[^1-9]*$
& %receiver_rewards ^[^1-9]*$
& %fee ^[^1-9]*$
& %amount_ ^[^1-9]*$
  skip

################################################################################
#                                                                              #
#                              INCOME (staking)                                #
#                                                                              #
################################################################################

# ---------------------------------------------------------------------------- #
#                             Governance Rewards                               #
# ---------------------------------------------------------------------------- #

# TODO: all sender addresses
if %direction ^IN$
& %to_self ^false$
& %amount_ [1-9]
& %sender (^UD33QBPIM4ZO4B2WK5Y5DYT5J5LYY5FA3IF3G4AVYSCWLCSMS5NYDRW6GE$|^UAME4M7T2NWECVNCUDGQX6LJ7OVDLZP234GFQL3TH6YZUPRV3VF5NGRSRI$|^7K5TT4US7M3FM7L3XBJXSXLJGF2WCXPBV2YZJJO2FH46VCZOS3ICJ7E4QU$|^SAHBJDRHHRR72JHTWSXZR5VHQQUVC7S757TJZI656FWSDO3TZZWV3IGJV4$|^B6D7YFR7NUNZAA4E7OIHME4VUX4R554DMDIX3QM7JML33AMXXILRPJJM4M$|^75X4V7CEN6HW3EYSJEJLWDNVX3BOJPPEHU2S34FSEKIN5WEB2OZN2VL5T4$|^2K24MUDRJPOOZBUTE5WW44WCZZUPVWNYWVWG4Z2Z2ZZVCYJPVDWRVHVJEQ$)
  comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, receiver:%receiver, txid:%id, to_self:%to_self, direction:%direction, taxed_as:INCOME
  account2 income:%account_name:%subaccount_name:algorand:rewards:governance:ALGO
  comment2 %round_time +0000,INCOME,%account_name:%subaccount_name:algorand,ALGO,%amount_,USD,,STAKING

# ---------------------------------------------------------------------------- #
#                               Sender Rewards                                 #
# ---------------------------------------------------------------------------- #

if %direction ^OUT$
& %to_self ^false$
& %sender_rewards [1-9]
  account7 assets:%account_name:%subaccount_name:algorand:ALGO
  amount7 %sender_rewards ALGO
  account8 income:%account_name:%subaccount_name:algorand:rewards:ALGO
  amount8 -%sender_rewards ALGO
  comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, receiver:%receiver, txid:%id, to_self:%to_self, direction:%direction, taxed_as:INCOME
  comment7 %round_time +0000,INCOME,%account_name:%subaccount_name:algorand,ALGO,%sender_rewards,USD,,STAKING

if %direction ^OUT$
& %to_self ^false$
& %sender_rewards [1-9]
& %amount_ ^[^1-9]*$
  account1
  account2

# ---------------------------------------------------------------------------- #
#                               Receiver Rewards                               #
# ---------------------------------------------------------------------------- #

if %direction ^IN$
& %to_self ^false$
& %receiver_rewards [1-9]
  account5 assets:%account_name:%subaccount_name:algorand:ALGO
  amount5 %receiver_rewards ALGO
  account6 income:%account_name:%subaccount_name:algorand:rewards:ALGO
  amount6 -%receiver_rewards ALGO
  comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, receiver:%receiver, txid:%id, to_self:%to_self, direction:%direction, taxed_as:INCOME
  comment5 %round_time +0000,INCOME,%account_name:%subaccount_name:algorand,ALGO,%receiver_rewards,USD,,STAKING

if %direction ^IN$
& %to_self ^false$
& %receiver_rewards [1-9]
& %amount_ ^[^1-9]*$
  account1
  account2

# ---------------------------------------------------------------------------- #
#                   Sender/Receiver (to ourselves) Rewards                     #
# ---------------------------------------------------------------------------- #

if %to_self ^true$
& %sender_rewards [1-9]
  account3 assets:%account_name:%subaccount_name:algorand:ALGO
  amount3 %sender_rewards ALGO
  account4 income:%account_name:%subaccount_name:algorand:rewards:ALGO
  amount4 -%sender_rewards ALGO
  comment type:%tx_type, subaccount_address:%subaccount_address, sender:%sender, receiver:%receiver, txid:%id, to_self:%to_self, direction:%direction, taxed_as:INCOME
  comment3 %round_time +0000,INCOME,%account_name:%subaccount_name:algorand,ALGO,%sender_rewards,USD,,STAKING

if %to_self ^true$
& %sender_rewards [1-9]
& %amount_ ^[^1-9]*$
  account1
  account2

# ---------------------------------------------------------------------------- #
#                              Spam / Scamdrops                                #
# ---------------------------------------------------------------------------- #

include ./rules.d/spam.rules

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