# 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/>.

# ------------------------------------------------------------------------- #
#                                 Compound                                  #
# ------------------------------------------------------------------------- #

# 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B - Compound: Comptroller (V2)

# 0x39aa39c021dfbae8fac545936693ac917d5e7563 - Compound: cUSDC
# 0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5 - Compound: cETH
# 0x5d3a536e4d6dbd6114cc1ead35777bab948e3643 - Compound: cDAI
# 0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9 - Compound: cUSDT

# 0xc00e94Cb662C3520282E6f5717214004A7f26888 - Compound: COMP Token Distribution

# TODO: expand as needed

#
# Compound: Lending/Collateral (Supply/Withdrawal)
#

# NOTE: Contract addresses are not added here because txs are sent/received
# directly to/from Compound contract address (so, this will remove redundency)
# TODO: this doesn't apply to older txs (see below in this file)

if %from_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type (^erc-20$|^normal$|^internal$)
& %amount_ [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:compound, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH
  comment2 %tx_hash,%date +0000,BUY,%account_name:%subaccount_name:%blockchain,%symbol,%amount_

if %from_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type ^internal$
& %amount_ [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, swapper:compound, subaccount_address:%subaccount_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH

if %to_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type (^erc-20$|^normal$|^internal$)
& %amount_ [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:compound, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH
  comment2 %tx_hash,%date +0000,SELL,%account_name:%subaccount_name:%blockchain,%symbol,%amount_

if %to_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type ^internal$
& %amount_ [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, swapper:compound, subaccount_address:%subaccount_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH

if %to_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type (^normal$)
& %symbol ^ETH$
& %fees [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:compound, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH
  comment3 %tx_hash,%date +0000,FEE,%account_name:%subaccount_name:%blockchain,%symbol,%fees

#
# Compound: Borrow/Repay
#

# WARNING: these rules only work with contemporary txs (single ERC-20 transfer)
# but not older txs (e.g., two ERC-20 transfers, circa 2021). MATCH txs that are
# clearly not BORROW/REPAY will be marked as such in these older txs.

# TODO: when did the spec change and how can this be resolved if both the
# contract address and from/to address are identical to older txs? Can the tx
# be tested by date?

# USDC

if %contract_address ^0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48$
& %from_address ^0x39aa39c021dfbae8fac545936693ac917d5e7563$
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  account2 liabilities:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:compound, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:BORROW
  comment2 %date +0000,BORROW,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,USD,,

if %contract_address ^0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48$
& %to_address ^0x39aa39c021dfbae8fac545936693ac917d5e7563$
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  account2 liabilities:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:compound, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:REPAY
  comment2 %date +0000,REPAY,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,USD,,

#
# Treat cTokens as equity (the "real" asset/liability is already accounted for)
#

if %from_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type (^erc-20$|^normal$)
& %amount_ [1-9]
& %symbol (^cETH$|^cUSDC$|^cDAI$|^cUSDT$)
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol

if %to_address (^0x39aa39c021dfbae8fac545936693ac917d5e7563$|^0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5$|^0x5d3a536e4d6dbd6114cc1ead35777bab948e3643$|^0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9$)
& %blockchain ^ethereum$
& %type (^erc-20$|^normal$)
& %amount_ [1-9]
& %symbol (^cETH$|^cUSDC$|^cDAI$|^cUSDT$)
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol

#
# Compound: Income (COMP rewards)
#

if %direction ^IN$
& %contract_address ^0xc00e94cb662c3520282e6f5717214004a7f26888$
& %from_address ^0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b$
  account2 income:%account_name:%subaccount_name:%blockchain:%symbol
  comment2 %date +0000,INCOME,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,USD,,COMP rewards

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