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

# ---------------------------------------------------------------------------- #
#                                    Aave                                      #
# ---------------------------------------------------------------------------- #

# V2

# 0x028171bca77440897b824ca71d1c56cac55b68a3 - Aave: aDAI Token V2
# 0x030ba81f1c18d280636f32af80b9aad02cf0854e - Aave: aWETH Token V2
# 0x35f6b052c598d933d69a4eec4d04c73a191fe6c2 - Aave: aSNX Token V2
# 0x3ed3b47dd13ec9a98b44e6204a523e766b225811 - Aave: aUSDT Token V2
# 0x464c71f6c2f760dda6093dcb91c24c39e5d6e18c - Aave: Aave Collector V2
# 0x4da27a545c0c5b758a6ba100e3a049001de870f5 - Aave: Staked Aave
# 0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9 - Aave: Lending pool V2
# 0x9ff58f4ffb29fa2266ab25e75e2a8b3503311656 - Aave: aWBTC Token V2
# 0xbcca60bb61934080951369a648fb03df4f96263c - Aave: aUSDC Token V2
# 0xd37ee7e4f452c6638c96536e68090de8cbcdb583 - Aave: aGUSD Token V2
# 0x619beb58998eD2278e08620f97007e1116D5D25b - Aave: USDC Variable Debt V2
# TODO: expand as needed

# V3

# 0xb748952c7bc638f31775245964707bcc5ddfabfc - Aave: Migration Helper Mainnet v3
# 0x72e95b8931767c79ba4eee721354d6e99a61d004 - Aave: Ethereum Variable Debt USDC
# TODO: expand as needed

# TODO: ideally, all types are processed using 4byte method_id (deposit, swap, borrow, etc.)
# TODO: staked income must be calculated in custom rules file or _manual_ (integrate contract query?)

#
# Aave: Borrow/Repay
#

# Remove BORROW/REPAY aTokens from:
#  1. liabilities account
#     - they may not be assets or liabilities
#     - payables are in the actual asset borrowed
#  2. journal/taxes
#     - the actual asset is received in-tandem, so aTokens are treated as equity

# NOTE: This will look odd because it's techincally BORROW/REPAY of aTokens but,
#   since aTokens on older V2 Deposit/Withdrawal would be incorrectly marked as
#   BORROW/REPAY (because from_address/to_address is not contract_address), the
#   comment2 will be kept as BUY/SELL in the trades section of this file (but
#   it's not needed during tax generation outside of trades, so not a problem).

## BORROW

if %from_address (^0x028171bca77440897b824ca71d1c56cac55b68a3$|^0x3ed3b47dd13ec9a98b44e6204a523e766b225811$|^0x9ff58f4ffb29fa2266ab25e75e2a8b3503311656$|^0xbcca60bb61934080951369a648fb03df4f96263c$|^0xd37ee7e4f452c6638c96536e68090de8cbcdb583$)
& %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:aave, 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,BORROW,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,,,

if %from_address ^0x0000000000000000000000000000000000000000$
& %direction ^IN$
& %symbol (^aDAI$|^aGUSD$|^aSNX$|^aUSDC$|^aUSDT$|^aWBTC$|^aWETH$|^stkAAVE$)
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  comment2 %tx_hash,%date +0000,BORROW,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,,,

## REPAY

if %to_address (^0x028171bca77440897b824ca71d1c56cac55b68a3$|^0x3ed3b47dd13ec9a98b44e6204a523e766b225811$|^0x9ff58f4ffb29fa2266ab25e75e2a8b3503311656$|^0xbcca60bb61934080951369a648fb03df4f96263c$|^0xd37ee7e4f452c6638c96536e68090de8cbcdb583$)
& %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:aave, 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,REPAY,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,,,

if %to_address ^0x0000000000000000000000000000000000000000$
& %direction ^OUT$
& %symbol (^aDAI$|^aGUSD$|^aSNX$|^aUSDC$|^aUSDT$|^aWBTC$|^aWETH$|^stkAAVE$)
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  comment2 %tx_hash,%date +0000,REPAY,%account_name:%subaccount_name:%blockchain,%symbol,%amount_,,,

# Is actually a supply, not repay
if %to_address ^0xbcca60bb61934080951369a648fb03df4f96263c$
& %contract_address ^0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48$
& %amount_ [1-9]
  account2 assets:%account_name:%subaccount_name:%blockchain:payable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:aave, 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_

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

# NOTE: treated as a taxable event:
#   1. Take the whole line as a matched trade then
#   2. If the line is simply a supply/withdrawal then convert FEE to SPEND and
#      drop any other action. This will dump the aToken in MATCH and only apply
#      the borrow/repay to the real borrow/repay.

if %from_address (^0x0000000000000000000000000000000000000000$|^0x028171bca77440897b824ca71d1c56cac55b68a3$|^0x030ba81f1c18d280636f32af80b9aad02cf0854e$|^0x35f6b052c598d933d69a4eec4d04c73a191fe6c2$|^0x3ed3b47dd13ec9a98b44e6204a523e766b225811$|^0x464c71f6c2f760dda6093dcb91c24c39e5d6e18c$|^0x4da27a545c0c5b758a6ba100e3a049001de870f5$|^0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9$|^0x9ff58f4ffb29fa2266ab25e75e2a8b3503311656$)
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  account2 assets:%account_name:%subaccount_name:%blockchain:payable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:aave, 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 %to_address (^0x0000000000000000000000000000000000000000$|^0x028171bca77440897b824ca71d1c56cac55b68a3$|^0x030ba81f1c18d280636f32af80b9aad02cf0854e$|^0x35f6b052c598d933d69a4eec4d04c73a191fe6c2$|^0x3ed3b47dd13ec9a98b44e6204a523e766b225811$|^0x464c71f6c2f760dda6093dcb91c24c39e5d6e18c$|^0x4da27a545c0c5b758a6ba100e3a049001de870f5$|^0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9$|^0x9ff58f4fFB29fA2266Ab25e75e2A8b3503311656$)
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  account2 assets:%account_name:%subaccount_name:%blockchain:payable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:aave, 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 (^0x028171bca77440897b824ca71d1c56cac55b68a3$|^0x030ba81f1c18d280636f32af80b9aad02cf0854e$|^0x35f6b052c598d933d69a4eec4d04c73a191fe6c2$|^0x3ed3b47dd13ec9a98b44e6204a523e766b225811$|^0x464c71f6c2f760dda6093dcb91c24c39e5d6e18c$|^0x4da27a545c0c5b758a6ba100e3a049001de870f5$|^0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9$|^0x9ff58f4ffb29fa2266ab25e75e2a8b3503311656$)
& %blockchain ^ethereum$
& %type ^normal$
& %symbol ^ETH$
& %amount_ ^[^1-9]*$
& %fees [1-9]
  account1
  account2
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:aave, subaccount_address:%subaccount_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH
  comment2
  comment3 %tx_hash,%date +0000,FEE,%account_name:%subaccount_name:%blockchain,%symbol,%fees
# NOTE: must keep comment2 blank and comment3 filled

#
# Treat s/v/aTokens as placeholders (the "real" asset/liability is already accounted for)
#

# V2

if %from_address ^0x0000000000000000000000000000000000000000$
& %direction ^IN$
& %symbol (^aDAI$|^aGUSD$|^aSNX$|^aUSDC$|^aUSDT$|^aWBTC$|^aWETH$|^stkAAVE$)
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol

if %to_address ^0x0000000000000000000000000000000000000000$
& %direction ^OUT$
& %symbol (^aDAI$|^aGUSD$|^aSNX$|^aUSDC$|^aUSDT$|^aWBTC$|^aWETH$|^stkAAVE$)
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol

# V3

if %from_address ^0x0000000000000000000000000000000000000000$
& %direction ^IN$
& %symbol ^vEthUSDC$
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol

if %to_address ^0x0000000000000000000000000000000000000000$
& %direction ^OUT$
& %symbol ^vEthUSDC$
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol

#
# Migration: V2 -> V3
#

# aWETH -> aEthWETH
# NOTE: you'll need to `manual` the staked aWETH income and subsequent
#   difference to the accounts payable WETH subaccount in order to balance.

if %direction ^OUT$
& %to_address ^0xb748952c7bc638f31775245964707bcc5ddfabfc$
& %contract_address ^0x030ba81f1c18d280636f32af80b9aad02cf0854e$
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  amount -%amount_ %symbol
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, swapper:aave, 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 %direction ^IN$
& %from_address ^0x0000000000000000000000000000000000000000$
& %contract_address ^0x4d5f47fa6a74757f35c14fd3a6ef8e3c9bc514e8$
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  account1 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  account2 equity:%account_name:%subaccount_name:%blockchain:receivable:loans:%symbol
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, swapper:aave, 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 %direction ^OUT$
& %to_address ^0xb748952c7bc638f31775245964707bcc5ddfabfc$
& %blockchain ^ethereum$
& %type ^normal$
& %symbol ^ETH$
& %amount_ ^[^1-9]*$
& %fees [1-9]
  account1
  account2
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, method:%method_id, swapper:aave, subaccount_address:%subaccount_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction, taxed_as:MATCH
  comment2
  comment3 %tx_hash,%date +0000,FEE,%account_name:%subaccount_name:%blockchain,%symbol,%fees
# NOTE: must keep comment2 blank and comment3 filled

#
# Non-transferable placeholders are not trades
#

# aUSDC (variable debt) -> vEthUSDC

if %to_address ^0x0000000000000000000000000000000000000000$
& %contract_address ^0x619beb58998eD2278e08620f97007e1116D5D25b$
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, swapper:aave, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction
  comment2

if %from_address ^0x0000000000000000000000000000000000000000$
& %contract_address ^0x72e95b8931767c79ba4eee721354d6e99a61d004$
& %blockchain ^ethereum$
& %type ^erc-20$
& %amount_ [1-9]
  comment blockchain:%blockchain, type:%type, block:%block_number, blockid:%block_hash, index:%tx_index, swapper:aave, subaccount_address:%subaccount_address, contract_address:%contract_address, from_address:%from_address, to_address:%to_address, txid:%tx_hash, direction:%direction
  comment2

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