# 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_id,coinbase_id,type,status_,amount_amount,amount_currency,native_amount_amount,native_amount_currency,created_at,resource,resource_path,description,network_status,network_network_name,network_hash,network_transaction_fee_amount,network_transaction_fee_currency,to_resource,to_address,to_email,from_resource,from_resource_path,from_id,from_name,cancelable,idem,buy_total_amount,buy_total_currency,buy_subtotal_amount,buy_subtotal_currency,buy_fee_amount,buy_fee_currency,buy_id,buy_payment_method_name,sell_total_amount,sell_total_currency,sell_subtotal_amount,sell_subtotal_currency,sell_fee_amount,sell_fee_currency,sell_id,sell_payment_method_name,trade_fee_amount,trade_fee_currency,trade_id,trade_payment_method_name,advanced_trade_fill_fill_price,advanced_trade_fill_product_id,advanced_trade_fill_order_id,advanced_trade_fill_commission,advanced_trade_fill_order_side,native_amount_price,native_network_transaction_fee_amount,network_transaction_amount_amount,advanced_trade_fill_real_value_amount,advanced_trade_fill_pair_lhs,advanced_trade_fill_pair_rhs,advanced_trade_fill_cost_basis_amount,direction,subaccount

# The above fields; in order and in human-readable form:
#
# account_id
# coinbase_id
# type
# status_
# amount_amount
# amount_currency
# native_amount_amount
# native_amount_currency
# created_at
# resource
# resource_path
# description
# network_status
# network_network_name
# network_hash
# network_transaction_fee_amount
# network_transaction_fee_currency
# to_resource
# to_address
# to_email
# from_resource
# from_resource_path
# from_id
# from_name
# cancelable
# idem
# buy_total_amount
# buy_total_currency
# buy_subtotal_amount
# buy_subtotal_currency
# buy_fee_amount
# buy_fee_currency
# buy_id
# buy_payment_method_name
# sell_total_amount
# sell_total_currency
# sell_subtotal_amount
# sell_subtotal_currency
# sell_fee_amount
# sell_fee_currency
# sell_id
# sell_payment_method_name
# trade_fee_amount
# trade_fee_currency
# trade_id
# trade_payment_method_name
# advanced_trade_fill_fill_price
# advanced_trade_fill_product_id
# advanced_trade_fill_order_id
# advanced_trade_fill_commission
# advanced_trade_fill_order_side
# native_amount_price
# native_network_transaction_fee_amount
# network_transaction_amount_amount
# advanced_trade_fill_real_value_amount
# advanced_trade_fill_pair_lhs
# advanced_trade_fill_pair_rhs
# advanced_trade_fill_cost_basis_amount
# direction
# subaccount

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

account1 assets:coinbase:%subaccount:%amount_currency
amount %amount_amount "%amount_currency"

# NOTE: server time is UTC, as are the timestamps.
description %created_at +0000

comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id

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

# `delayed_canceled` should not alter the journal
if %type ^delayed_canceled$
  skip

# ---------------------------------------------------------------------------- #
#                                   Banking                                    #
# ---------------------------------------------------------------------------- #

if %type ^fiat_deposit$
  account2 equity:coinbase:%subaccount:withdrawal:%amount_currency

if %type ^fiat_withdrawal$
  amount -%amount_amount "%amount_currency"
  account2 equity:coinbase:%subaccount:deposit:%amount_currency

# ---------------------------------------------------------------------------- #
#                                   Staking                                    #
# ---------------------------------------------------------------------------- #

# NOTE: see the Vault TODO regarding why a staking subaccount isn't used
if %type (^staking_transfer$|^unstaking_transfer$)
  account2 assets:coinbase:%subaccount:%amount_currency

# ---------------------------------------------------------------------------- #
#                                   Network                                    #
# ---------------------------------------------------------------------------- #

# NOTE: use custom rules if income
if %type ^send$
& %direction ^IN$
  account2 equity:coinbase:%subaccount:withdrawal:%amount_currency

# NOTE: use custom rules if expense or gift
if %type ^send$
& %direction ^OUT$
  amount -%amount_amount "%amount_currency"
  account2 equity:coinbase:%subaccount:deposit:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, direction:%direction

if %type ^send$
& %direction (^IN$|^OUT$)
& %network_network_name [a-z]
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, network_name:%network_network_name, direction:%direction

#
# On-chain
#

if %type ^send$
& %direction (^IN$|^OUT$)
& %network_hash ([a-z]|[0-9])
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, network_status:%network_status, network_name:%network_network_name, txid:%network_hash, direction:%direction

if %type ^send$
& %direction (^IN$|^OUT$)
& %to_resource ^address$
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, network_name:%network_network_name, to_address:%to_address, txid:%network_hash, direction:%direction

if %type ^send$
& %direction ^OUT$
& %native_network_transaction_fee_amount [1-9]
  amount -%network_transaction_amount_amount "%amount_currency"
  account3 assets:coinbase:%subaccount:%network_transaction_fee_currency
  amount3 -%network_transaction_fee_amount "%network_transaction_fee_currency" @@ %native_network_transaction_fee_amount %native_amount_currency
  account4 expenses:coinbase:%subaccount:fees:send:%native_amount_currency
  amount4 %native_network_transaction_fee_amount %native_amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, network_name:%network_network_name, to_address:%to_address, txid:%network_hash, direction:%direction, taxed_as:SPEND
  comment3 %created_at +0000,SPEND,coinbase:%subaccount,%network_transaction_fee_currency,%network_transaction_fee_amount,%native_amount_currency,%native_network_transaction_fee_amount,FEE
# Marked as comment3 in case user-defined rules use comment2 to SPEND/GIFT the non-fee amount

#
# Off-chain
#

if %type ^send$
& %direction (^IN$|^OUT$)
& %to_resource ^address$
& %network_status ^off_blockchain$
  account2 expenses:coinbase:%subaccount:send:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, network_name:%network_network_name, to_address:%to_address, direction:%direction

if %type ^send$
& %direction ^OUT$
& %to_resource ^email$
  account2 expenses:coinbase:%subaccount:send_email:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, to_resource:%to_resource, direction:%direction, taxed_as:SPEND
  comment2 %created_at +0000,SPEND,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,Send via email

if %type ^send$
& %direction ^OUT$
& %to_resource ^email$
& %to_email [a-z]
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, idem:%idem, network_status:%network_status, to_resource:%to_resource, to_email:%to_email, direction:%direction, taxed_as:SPEND
  comment2 %created_at +0000,SPEND,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,Send to %to_email

# TODO: phone number support

# ---------------------------------------------------------------------------- #
#                               Coinbase Vault                                 #
# ---------------------------------------------------------------------------- #

#  - Treatment of subaccounts are from the perspective of the wallet.
#
#  - If you have staking income that was deposited into a vault subaccount,
#    after you make a full withdrawal, a negative balance will show - but will
#    be balanced by a positive amount in the wallet subaccount.
#
#    This is less than ideal but, since the only available datastore is a
#    linear CSV, it will be unknown where the staking income was deposited to.
#
#    See the `preprocess` TODO regarding vault.

if %type (^vault_withdrawal$|^transfer$)
  account2 assets:coinbase:%subaccount:vault:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, direction:%direction

#
# From wallet -> To vault
#

# NOTE: apparently there's no such thing as vault_deposit ...
if %type ^transfer$
& %direction ^OUT$
  amount -%amount_amount "%amount_currency"

# Remove duplicate vault tx (use wallet tx instead)
if %type ^transfer$
& %direction ^IN$
  skip

#
# To wallet <- From vault
#

# Remove duplicate vault tx (use wallet tx instead)
if %type ^vault_withdrawal$
& %direction ^OUT$
  skip

# ---------------------------------------------------------------------------- #
#                               Coinbase Borrow                                #
# ---------------------------------------------------------------------------- #

# WARNING:
#
#   The Spring 2024 SIWC V2 update has completely removed the ability to know
#   which tx is a Coinbase Borrow tx... All that remains is `%type ^tx$` and
#   nothing more (other than the schema's "Required" metadata).
#
#   As a result, to accurately account for borrow/loan payment, %coinbase_id
#   must be manually entered in custom rule on a per borrow/loan transaction.
#
#   NOTE: I imagine this is because Coinbase Borrow no longer offers loans.

# ---------------------------------------------------------------------------- #
#                                 Coinbase Pro                                 #
# ---------------------------------------------------------------------------- #

# From wallet -> To Pro
if %type (^pro_deposit$|^exchange_deposit$)
  amount -%amount_amount "%amount_currency"
  account2 equity:coinbase-pro:deposit:%amount_currency

# To wallet <- From Pro
if %type ^pro_withdrawal$
  account2 equity:coinbase-pro:withdrawal:%amount_currency

# ---------------------------------------------------------------------------- #
#                                Coinbase Card                                 #
# ---------------------------------------------------------------------------- #

# Rebates ("rewards")

if %type ^send$
& %description reward
& %from_name (^Coinbase Card$|^Card Rewards$)
  amount %amount_amount "%amount_currency" @@ %native_amount_amount %native_amount_currency
  account2 income:coinbase:%subaccount:card:%native_amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:REBATE
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,,

# Crypto Spend

if %type ^cardspend$
  amount -%amount_amount "%amount_currency" @@ %native_amount_amount %native_amount_currency
  account2 expenses:coinbase:%subaccount:card:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:SPEND
  comment2 %created_at +0000,SPEND,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,Coinbase Card

# Cash Spend (Native Fiat Wallet)
# TODO: any reference to USD should be a backreferenced %native_amount_currency
if %type ^cardspend$
& %amount_currency ^USD$
  amount -%amount_amount "%amount_currency"
  account2 expenses:coinbase:%subaccount:card:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id
  comment2

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

# Quiz rewards

if %type ^send$
& %from_name ^Coinbase Earn$
  account2 income:coinbase:%subaccount:earn:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:INCOME
  comment2 %created_at +0000,INCOME,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,REWARD

if %type ^earn_payout$
  account2 income:coinbase:%subaccount:earn:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:INCOME
  comment2 %created_at +0000,INCOME,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,REWARD

if %type ^tx$
& %description ^Earn Task$
  account2 income:coinbase:%subaccount:earn:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:INCOME
  comment2 %created_at +0000,INCOME,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,REWARD

# Interest

if %type ^interest$
  account2 income:coinbase:%subaccount:interest:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:INCOME
  comment2 %created_at +0000,INCOME,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,INTEREST

# Staking

if %type (^inflation_reward$|^staking_reward$)
  account2 income:coinbase:%subaccount:staking:%amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:INCOME
  comment2 %created_at +0000,INCOME,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,STAKING

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

# Remove effective duplicates (erroneous fiat double-txs) e.g., trading USD-USD
# TODO: any reference to USD should be a backreferenced %native_amount_currency
if %type (^buy$|^sell$)
& %amount_currency ^USD$
  skip

# ---------------------------------------------------------------------------- #
#                                Coinbase Trade                                #
# ---------------------------------------------------------------------------- #

#
# BUY
#

if %type ^buy$
  amount %amount_amount "%amount_currency" @@ %buy_total_amount "%buy_total_currency"
  account2 assets:coinbase:%subaccount:%buy_total_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, buy_id:%buy_id, taxed_as:BUY
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%amount_currency,%amount_amount,%buy_total_currency,%buy_total_amount,,

if %type ^buy$
& %buy_fee_amount [1-9]
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%amount_currency,%amount_amount,%buy_total_currency,%buy_total_amount,%buy_fee_currency,%buy_fee_amount
# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes)

# TODO: HACK: see #51 and respective lib_taxes work-around
if %type ^buy$
& %buy_fee_amount [1-9]
& %buy_fee_currency ^USD$
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%amount_currency,%amount_amount,%buy_total_currency,%buy_total_amount,,

if %type ^buy$
& %buy_total_currency ^USDC$
  amount %amount_amount "%amount_currency" @@ %native_amount_amount %native_amount_currency
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,,

if %type ^buy$
& %buy_fee_amount [1-9]
  amount %amount_amount "%amount_currency" @@ %buy_subtotal_amount "%buy_subtotal_currency"
  account2 assets:coinbase:%subaccount:%buy_subtotal_currency
  amount3 -%buy_fee_amount "%buy_fee_currency"
  account3 assets:coinbase:%subaccount:%buy_fee_currency
  amount4 %buy_fee_amount "%buy_fee_currency"
  account4 expenses:coinbase:%subaccount:fees:trading:%buy_fee_currency

if %type ^buy$
& %buy_payment_method_name [a-z]
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, buy_id:%buy_id, buy_payment_method_name:%buy_payment_method_name, taxed_as:BUY

if %type ^buy$
& %buy_fee_amount [1-9]
& %buy_payment_method_name (bank|checking|credit|debit|saving)
  amount5 %buy_total_amount "%buy_total_currency"
  account5 assets:coinbase:%subaccount:%buy_total_currency
  amount6 -%buy_total_amount "%buy_total_currency"
  account6 equity:coinbase:%subaccount:withdrawal:%buy_total_currency

if %type ^buy$
& %buy_fee_amount ^[^1-9]*$
& %buy_payment_method_name (bank|checking|credit|debit|saving)
  amount3 %buy_total_amount "%buy_total_currency"
  account3 assets:coinbase:%subaccount:%buy_total_currency
  amount4 -%buy_total_amount "%buy_total_currency"
  account4 equity:coinbase:%subaccount:withdrawal:%buy_total_currency

#
# SELL
#

if %type ^sell$
  amount -%amount_amount "%amount_currency" @@ %sell_total_amount "%sell_total_currency"
  account2 assets:coinbase:%subaccount:%sell_total_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, sell_id:%sell_id, taxed_as:SELL
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%amount_currency,%amount_amount,%sell_total_currency,%sell_total_amount,,

if %type ^sell$
& %sell_fee_amount [1-9]
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%amount_currency,%amount_amount,%sell_total_currency,%sell_total_amount,%sell_fee_currency,%sell_fee_amount
# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes)

# TODO: HACK: see #51 and respective lib_taxes work-around
if %type ^sell$
& %sell_fee_amount [1-9]
& %sell_fee_currency ^USD$
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%amount_currency,%amount_amount,%sell_total_currency,%sell_total_amount,,

if %type ^sell$
& %sell_total_currency ^USDC$
  amount -%amount_amount %amount_currency @@ %native_amount_amount %native_amount_currency
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,,

if %type ^sell$
& %sell_fee_amount [1-9]
  amount -%amount_amount "%amount_currency" @@ %sell_subtotal_amount "%sell_subtotal_currency"
  account2 assets:coinbase:%subaccount:%sell_subtotal_currency
  amount3 -%sell_fee_amount "%sell_fee_currency"
  account3 assets:coinbase:%subaccount:%sell_fee_currency
  amount4 %sell_fee_amount "%sell_fee_currency"
  account4 expenses:coinbase:%subaccount:fees:trading:%sell_fee_currency

if %type ^sell$
& %sell_payment_method_name [a-z]
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, sell_id:%sell_id, sell_payment_method_name:%sell_payment_method_name, taxed_as:SELL

if %type ^sell$
& %sell_fee_amount [1-9]
& %sell_payment_method_name (bank|checking|credit|debit|saving)
  amount5 -%sell_total_amount "%sell_total_currency"
  account5 assets:coinbase:%subaccount:%sell_total_currency
  amount6 %sell_total_amount "%sell_total_currency"
  account6 equity:coinbase:%subaccount:deposit:%sell_total_currency

if %type ^sell$
& %sell_fee_amount ^[^1-9]*$
& %sell_payment_method_name (bank|checking|credit|debit|saving)
  amount3 -%sell_total_amount "%sell_total_currency"
  account3 assets:coinbase:%subaccount:%sell_total_currency
  amount4 %sell_total_amount "%sell_total_currency"
  account4 equity:coinbase:%subaccount:deposit:%sell_total_currency

#
# Convert/Conversion
#

# The conversion is treated as:
#
#     token1->fiat->token2 i.e., sell->fiat->buy
#
# but the cost-basis should be the same as a straight conversion.
#
#
# Here is a mockup example of an API-provided conversion of USDC->ETH:
#
# -----------------
# On the SELL side:
# -----------------
#
#               (
#                    [id] => XXXXXXXX-XXXX-XXXX-XXXX-COINBASE_ID1
#                    [type] => trade
#                    [status] => completed
#                    [amount] => Array
#                        (
#                            [amount] => -100.000000
#                            [currency] => USDC
#                        )
#
#                    [native_amount] => Array
#                        (
#                            [amount] => -100.00
#                            [currency] => USD
#                        )
#
#                    [created_at] => 2020-02-02T02:02:02Z
#                    [resource] => transaction
#                    [resource_path] => /v2/accounts/XXXXXXXX-XXXX-XXXX-XXXX-XXACCOUNTID1/transactions/XXXXXXXX-XXXX-XXXX-XXXX-XCOINBASED1
#                    [trade] => Array
#                        (
#                            [fee] => Array
#                                (
#                                    [amount] => 0.00549387
#                                    [currency] => ETH
#                                )
#
#                            [id] => XXXXXXXX-XXXX-XXXX-SAME-TRADEID00001
#                            [payment_method_name] => USDC Wallet
#                        )
#
#                )
#
# --------------------
# And on the BUY side:
# --------------------
#
#                (
#                    [id] => XXXXXXXX-XXXX-XXXX-XXXX-XCOINBASEID2
#                    [type] => trade
#                    [status] => completed
#                    [amount] => Array
#                        (
#                            [amount] => 0.12345678
#                            [currency] => ETH
#                        )
#
#                    [native_amount] => Array
#                        (
#                            [amount] => 95.55
#                            [currency] => USD
#                        )
#
#                    [created_at] => 2020-02-02T02:02:04Z
#                    [resource] => transaction
#                    [resource_path] => /v2/accounts/XXXXXXXX-XXXX-XXXX-XXXX-XXACCOUNTID2/transactions/XXXXXXXX-XXXX-XXXX-XXXX-XCOINBASEID2
#                    [trade] => Array
#                        (
#                            [fee] => Array
#                                (
#                                    [amount] => 0.00549387
#                                    [currency] => ETH
#                                )
#
#                            [id] => XXXXXXXX-XXXX-XXXX-SAME-TRADEID00001
#                            [payment_method_name] => USDC Wallet
#                        )
#
#                )
#
# --------------------
# Will be produced as:
# --------------------
#
# 2020-02-02 2020-02-02 02:02:02 +0000  ; type:trade, status:completed, account_id:XXXXXXXX-XXXX-XXXX-XXXX-XXACCOUNTID1, coinbase_id:XXXXXXXX-XXXX-XXXX-XXXX-XCOINBASEID1, trade_id:XXXXXXXX-XXXX-XXXX-SAME-TRADEID00001, taxed_as:SELL
#     assets:coinbase:platform:USDC            -100.000000 USDC @@ 100.00 USD
#     equity:coinbase:platform:conversion:USD                      100.00 USD  ; 2020-02-02 02:02:02 +0000,SELL,coinbase:%subaccount,USDC,100.000000,USD,100.00,,
#
# 2020-02-02 2020-02-02 02:02:04 +0000  ; type:trade, status:completed, account_id:XXXXXXXX-XXXX-XXXX-XXXX-XXACCOUNTID2, coinbase_id:XXXXXXXX-XXXX-XXXX-XXXX-XCOINBASEID2, trade_id:XXXXXXXX-XXXX-XXXX-SAME-TRADEID00001, taxed_as:BUY
#     assets:coinbase:platform:ETH               0.12345678 ETH @@ 95.55 USD
#     equity:coinbase:platform:conversion:USD                     -95.55 USD  ; 2020-02-02 02:02:04 +0000,BUY,coinbase:%subaccount,ETH,0.12345678,USD,95.55,,
#
# NOTE: regarding cost-basis, there appears to be no `cost/proceeds +/- fee` but rather cost/proceeds at a wider spread.
#
# --------------------
# The rationale being:
# --------------------
#
#  - This API call doesn't provide a "conversion from/to" or equivalent nor
#    does it provide any associated market price or fee percentage.
#
#    However, total market price *could* be inferred via the non-stable crypto:
#
#         %native_amount_amount / (%amount_amount + %trade_fee_amount)
#
#    (assuming that %trade_fee_currency is identical to %amount_currency)
#
#    but this would need to be supported by a `preprocess` test of:
#
#         %payment_method_name
#
#    to create new column with token string:
#
#         "USDC" for "USDC Wallet"
#
#    as provided by the above BUY-side example because %payment_method_name is
#    the only available evidence of a "conversion from".
#
#    This would create a single tx; thus dropping the other side of trade.
#
#    However, this is avoided because:
#
#      * Not all trades have fees.
#      * Dropping a non-duplicate tx is confusing and appears not to be needed.
#
#  - An interested observation: you'll notice that the conversion timestamps
#    aren't identical when they *should* be (or, they could be, server-side,
#    retroactively).
#
#    This difference could be a result of any number of factors including
#    backend delay when updating accounts. Another factor could imply that the
#    trade being done behind the scenes is actually two executions of higher
#    liquidity pairings: token1->fiat / fiat->token2 and the "conversion" is
#    simply an external simplification of what is an internal set of trades.
#
#    In other words, the trade may not actually be a token1->token2 conversion
#    but rather a sell->fiat->buy transaction. In terms of accounting, this is
#    not only more accurate but I belive to also be more appropriate.
#
#    NOTE: crypto-crypto pairings in Advanced Trade have identical timestamps.
#
#  - Because the given %native_amount_amount includes the fee in the cost-basis,
#    and there is no total on the buy side (only subtotal), %trade_fee_amount
#    should not be added as it A) it's not needed B) there's no market price
#    given from this Coinbase API call to deteremine how they define the total
#    (see notes above).
#
#  - The drawback to using an equity conversion account in this instance is,
#    because of the lack of information provided by the API, the account will
#    almost certainly not be 0 after all conversions are completed.
#
#    This shouldn't be an issue though because A) the conversion account is not
#    meant to be treated as a real equity account and B) %trade_fee_id can be
#    used as a correlating tool to track the conversion.
#
#  Alternate solution: don't use Convert; use Buy/Sell or Advanced Trade.

#
# SELL
#

if %type ^trade$
& %direction ^OUT$
  amount -%amount_amount "%amount_currency" @@ %native_amount_amount %native_amount_currency
  account2 equity:coinbase:%subaccount:conversion:%native_amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:SELL
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,,

if %type ^trade$
& %direction ^OUT$
& %trade_id ([a-z]|[0-9])
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, trade_id:%trade_id, taxed_as:SELL

#
# BUY
#

if %type ^trade$
& %direction ^IN$
  amount %amount_amount "%amount_currency" @@ %native_amount_amount %native_amount_currency
  account2 equity:coinbase:%subaccount:conversion:%native_amount_currency
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, taxed_as:BUY
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%amount_currency,%amount_amount,%native_amount_currency,%native_amount_amount,,

if %type ^trade$
& %direction ^IN$
& %trade_id ([a-z]|[0-9])
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, trade_id:%trade_id, taxed_as:BUY

# ---------------------------------------------------------------------------- #
#                            Coinbase Advanced Trade                           #
# ---------------------------------------------------------------------------- #

# WARNING: their native amount is *not* the native amount of the fill price.

#
# BUY / SELL
#

# Sometimes they provide 0 %amount_amount and 0 %native_amount_amount but will
# also provide a commission in the same tx... Fortunately, since the fees are
# already caught in the below sections, these 0 value records can be skipped.
if %type ^advanced_trade_fill$
& %amount_amount ^[^1-9]*$
& %native_amount_amount ^[^1-9]*$
  skip

if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side (^buy$|^sell$)
  account1 assets:coinbase:%subaccount:%advanced_trade_fill_pair_lhs
  account2 assets:coinbase:%subaccount:%advanced_trade_fill_pair_rhs
  amount3 -%advanced_trade_fill_commission %advanced_trade_fill_pair_rhs
  account3 assets:coinbase:%subaccount:%advanced_trade_fill_pair_rhs
  amount4 %advanced_trade_fill_commission %advanced_trade_fill_pair_rhs
  account4 expenses:coinbase:%subaccount:fees:trading:%advanced_trade_fill_pair_rhs

# NOTE: see WARNING in preprocess about $0.00 Advanced Trade txs
if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side (^buy$|^sell$)
& %advanced_trade_fill_real_value_amount ^[^1-9]*$
  account1
  account2

#
# BUY
#

# Remove what is effectively a duplicate
if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^buy$
& %advanced_trade_fill_real_value_amount [1-9]
& %direction ^OUT$
  skip

if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^buy$
& %advanced_trade_fill_real_value_amount [1-9]
& %direction ^IN$
  amount %amount_amount "%advanced_trade_fill_pair_lhs" @@ %advanced_trade_fill_real_value_amount "%advanced_trade_fill_pair_rhs"
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, advanced_trade_fill_fill_price:%advanced_trade_fill_fill_price, advanced_trade_fill_product_id:%advanced_trade_fill_product_id, advanced_trade_fill_order_id:%advanced_trade_fill_order_id, advanced_trade_fill_order_side:%advanced_trade_fill_order_side, advanced_trade_fill_pair_lhs:%advanced_trade_fill_pair_lhs, advanced_trade_fill_pair_rhs:%advanced_trade_fill_pair_rhs, direction:%direction, taxed_as:BUY
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,,

if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^buy$
& %advanced_trade_fill_real_value_amount [1-9]
& %advanced_trade_fill_commission [1-9]
& %direction ^IN$
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_commission
# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes)

# TODO: HACK: see #51 and respective lib_taxes work-around
if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^buy$
& %advanced_trade_fill_real_value_amount [1-9]
& %advanced_trade_fill_commission [1-9]
& %advanced_trade_fill_pair_rhs ^USD$
& %direction ^IN$
  comment2 %created_at +0000,BUY,coinbase:%subaccount,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,,

#
# SELL
#

# Remove what is effectively a duplicate
if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^sell$
& %advanced_trade_fill_real_value_amount [1-9]
& %direction ^IN$
  skip

if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^sell$
& %advanced_trade_fill_real_value_amount [1-9]
& %direction ^OUT$
  amount -%amount_amount "%advanced_trade_fill_pair_lhs" @@ %advanced_trade_fill_real_value_amount "%advanced_trade_fill_pair_rhs"
  comment type:%type, status:%status_, account_id:%account_id, coinbase_id:%coinbase_id, advanced_trade_fill_fill_price:%advanced_trade_fill_fill_price, advanced_trade_fill_product_id:%advanced_trade_fill_product_id, advanced_trade_fill_order_id:%advanced_trade_fill_order_id, advanced_trade_fill_order_side:%advanced_trade_fill_order_side, advanced_trade_fill_pair_lhs:%advanced_trade_fill_pair_lhs, advanced_trade_fill_pair_rhs:%advanced_trade_fill_pair_rhs, direction:%direction, taxed_as:SELL
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,,

if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^sell$
& %advanced_trade_fill_real_value_amount [1-9]
& %advanced_trade_fill_commission [1-9]
& %direction ^OUT$
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_commission
# TODO: HACK: re: #51, FeeCurrency and Fee are added even though cost-basis is calculated (for non-fiat fee disposal, see lib_taxes)

# TODO: HACK: see #51 and respective lib_taxes work-around
if %type ^advanced_trade_fill$
& %advanced_trade_fill_order_side ^sell$
& %advanced_trade_fill_real_value_amount [1-9]
& %advanced_trade_fill_commission [1-9]
& %advanced_trade_fill_pair_rhs ^USD$
& %direction ^OUT$
  comment2 %created_at +0000,SELL,coinbase:%subaccount,%advanced_trade_fill_pair_lhs,%amount_amount,%advanced_trade_fill_pair_rhs,%advanced_trade_fill_cost_basis_amount,,

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