hledger-flow: coinbase: refactor for ccxt raw ledger

ccxt's raw ledger object resides in [info]
This commit is contained in:
2024-07-01 23:46:13 -07:00
parent 2d3ab7857c
commit 07023ea952

View File

@@ -88,67 +88,68 @@ function __parse()
# Allow numerical symbols # Allow numerical symbols
gsub(/1INCH/, "ONEINCH") gsub(/1INCH/, "ONEINCH")
# Print for rules consumption # Print [info] object for rules consumption
printf $1 OFS # account_id (prepended column) printf $1 OFS # account_id (prepended column)
printf $2 OFS # id (coinbase_id) printf $2 OFS # info_id (coinbase_id)
printf $3 OFS # type printf $3 OFS # info_type
printf $4 OFS # status printf $4 OFS # info_status
printf $5 OFS # amount_amount printf $5 OFS # info_amount_amount
printf $6 OFS # amount_currency printf $6 OFS # info_amount_currency
printf $7 OFS # native_amount_amount printf $7 OFS # info_native_amount_amount
printf $8 OFS # native_amount_currency printf $8 OFS # info_native_amount_currency
printf $9 OFS # created_at printf $9 OFS # info_created_at
printf $10 OFS # resource printf $10 OFS # info_resource
printf $11 OFS # resource_path printf $11 OFS # info_resource_path
printf $12 OFS # description printf $12 OFS # info_description
printf $13 OFS # network_status printf $13 OFS # info_network_status
printf $14 OFS # network_network_name printf $14 OFS # info_network_network_name
printf $15 OFS # network_hash (txid) printf $15 OFS # info_network_hash (txid)
printf("%.8f", $16); printf OFS # network_transaction_fee_amount printf("%.8f", $16); printf OFS # info_network_transaction_fee_amount
printf $17 OFS # network_transaction_fee_currency printf $17 OFS # info_network_transaction_fee_currency
printf $18 OFS # to_resource printf $18 OFS # info_to_resource
printf $19 OFS # to_address printf $19 OFS # info_to_address
printf $20 OFS # to_email printf $20 OFS # info_to_email
printf $21 OFS # from_resource printf $21 OFS # info_from_resource
printf $22 OFS # from_resource_path printf $22 OFS # info_from_resource_path
printf $23 OFS # from_id printf $23 OFS # info_from_id
printf $24 OFS # from_name printf $24 OFS # info_from_name
printf $25 OFS # cancelable printf $25 OFS # info_cancelable
printf $26 OFS # idem printf $26 OFS # info_idem
printf("%.8f", $27); printf OFS # buy_total_amount printf("%.8f", $27); printf OFS # info_buy_total_amount
printf $28 OFS # buy_total_currency printf $28 OFS # info_buy_total_currency
printf("%.8f", $29); printf OFS # buy_subtotal_amount printf("%.8f", $29); printf OFS # info_buy_subtotal_amount
printf $30 OFS # buy_subtotal_currency printf $30 OFS # info_buy_subtotal_currency
printf("%.8f", $31); printf OFS # buy_fee_amount printf("%.8f", $31); printf OFS # info_buy_fee_amount
printf $32 OFS # buy_fee_currency printf $32 OFS # info_buy_fee_currency
printf $33 OFS # buy_id printf $33 OFS # info_buy_id
printf $34 OFS # buy_payment_method_name printf $34 OFS # info_buy_payment_method_name
printf("%.8f", $35); printf OFS # sell_total_amount printf("%.8f", $35); printf OFS # info_sell_total_amount
printf $36 OFS # sell_total_currency printf $36 OFS # info_sell_total_currency
printf("%.8f", $37); printf OFS # sell_subtotal_amount printf("%.8f", $37); printf OFS # info_sell_subtotal_amount
printf $38 OFS # sell_subtotal_currency printf $38 OFS # info_sell_subtotal_currency
printf("%.8f", $39); printf OFS # sell_fee_amount printf("%.8f", $39); printf OFS # info_sell_fee_amount
printf $40 OFS # sell_fee_currency printf $40 OFS # info_sell_fee_currency
printf $41 OFS # sell_id printf $41 OFS # info_sell_id
printf $42 OFS # sell_payment_method_name printf $42 OFS # info_sell_payment_method_name
printf("%.8f", $43); printf OFS # trade_fee_amount printf("%.8f", $43); printf OFS # info_trade_fee_amount
printf $44 OFS # trade_fee_currency printf $44 OFS # info_trade_fee_currency
printf $45 OFS # trade_id printf $45 OFS # info_trade_id
printf $46 OFS # trade_payment_method_name printf $46 OFS # info_trade_payment_method_name
printf("%.8f", $47); printf OFS # advanced_trade_fill_fill_price printf("%.8f", $47); printf OFS # info_advanced_trade_fill_fill_price
printf $48 OFS # advanced_trade_fill_product_id printf $48 OFS # info_advanced_trade_fill_product_id
printf $49 OFS # advanced_trade_fill_order_id printf $49 OFS # info_advanced_trade_fill_order_id
printf("%.8f", $50); printf OFS # advanced_trade_fill_commission printf("%.8f", $50); printf OFS # info_advanced_trade_fill_commission
printf $51 OFS # advanced_trade_fill_order_side printf $51 OFS # info_advanced_trade_fill_order_side
# #
# Add new columns to calculate fees against native currency price # Add new columns to calculate fees against native currency price
# #
# WARNING: # WARNING:
# #
# - `amount_amount` and/or `native_amount_amount` may be given as 0 # - `info_amount_amount` and/or `info_native_amount_amount` may be
# because Coinbase does not display the full value of either column # given as 0 because Coinbase does not display the full value of
# for token values (satoshi, gwei, etc.) valued less than a $0.00. # either column for token values (satoshi, gwei, etc.) valued less
# than a $0.00.
# #
# Quite literally, the only value information that Coinbase will # Quite literally, the only value information that Coinbase will
# provide for these transactions is 0 or equivalent; so, an accurate # provide for these transactions is 0 or equivalent; so, an accurate
@@ -246,73 +247,73 @@ function parse()
local -r _account_id="${global_in_filename:0:-17}" local -r _account_id="${global_in_filename:0:-17}"
# #
# Reconstruct into "universal" header # Reconstruct [info] object (raw ledger) with "universal" header
# #
# Required schema, per documentation, that appears to exist amongst all entries # Required Coinbase schema, per documentation, that appears to exist amongst all entries
declare -g universal_header="id,type,status,amount_amount,amount_currency,native_amount_amount,native_amount_currency,created_at,resource,resource_path" declare -g universal_header="info_id,info_type,info_status,info_amount_amount,info_amount_currency,info_native_amount_amount,info_native_amount_currency,info_created_at,info_resource,info_resource_path"
# Selected header will always have minimum requirements # Selected header will always have minimum requirements
declare -g selected_header+="$universal_header" declare -g selected_header+="$universal_header"
# `description` # `description`
add_to_headers "description" add_to_headers "info_description"
# `network` # `network`
add_to_headers "network_status" add_to_headers "info_network_status"
add_to_headers "network_network_name" add_to_headers "info_network_network_name"
add_to_headers "network_hash" add_to_headers "info_network_hash"
add_to_headers "network_transaction_fee_amount" add_to_headers "info_network_transaction_fee_amount"
add_to_headers "network_transaction_fee_currency" add_to_headers "info_network_transaction_fee_currency"
# `to` # `to`
add_to_headers "to_resource" add_to_headers "info_to_resource"
add_to_headers "to_address" add_to_headers "info_to_address"
add_to_headers "to_email" add_to_headers "info_to_email"
# `from` # `from`
add_to_headers "from_resource" add_to_headers "info_from_resource"
add_to_headers "from_resource_path" add_to_headers "info_from_resource_path"
add_to_headers "from_id" add_to_headers "info_from_id"
add_to_headers "from_name" add_to_headers "info_from_name"
# Remaining SEND related # Remaining SEND related
add_to_headers "cancelable" add_to_headers "info_cancelable"
add_to_headers "idem" add_to_headers "info_idem"
# `buy` # `buy`
add_to_headers "buy_total_amount" add_to_headers "info_buy_total_amount"
add_to_headers "buy_total_currency" add_to_headers "info_buy_total_currency"
add_to_headers "buy_subtotal_amount" add_to_headers "info_buy_subtotal_amount"
add_to_headers "buy_subtotal_currency" add_to_headers "info_buy_subtotal_currency"
add_to_headers "buy_fee_amount" add_to_headers "info_buy_fee_amount"
add_to_headers "buy_fee_currency" add_to_headers "info_buy_fee_currency"
add_to_headers "buy_id" add_to_headers "info_buy_id"
add_to_headers "buy_payment_method_name" add_to_headers "info_buy_payment_method_name"
# `sell` # `sell`
add_to_headers "sell_total_amount" add_to_headers "info_sell_total_amount"
add_to_headers "sell_total_currency" add_to_headers "info_sell_total_currency"
add_to_headers "sell_subtotal_amount" add_to_headers "info_sell_subtotal_amount"
add_to_headers "sell_subtotal_currency" add_to_headers "info_sell_subtotal_currency"
add_to_headers "sell_fee_amount" add_to_headers "info_sell_fee_amount"
add_to_headers "sell_fee_currency" add_to_headers "info_sell_fee_currency"
add_to_headers "sell_id" add_to_headers "info_sell_id"
add_to_headers "sell_payment_method_name" add_to_headers "info_sell_payment_method_name"
# `trade` # `trade`
add_to_headers "trade_fee_amount" add_to_headers "info_trade_fee_amount"
add_to_headers "trade_fee_currency" add_to_headers "info_trade_fee_currency"
add_to_headers "trade_id" add_to_headers "info_trade_id"
add_to_headers "trade_payment_method_name" add_to_headers "info_trade_payment_method_name"
# `advanced_trade_fill` # `advanced_trade_fill`
# Note: Coinbase appears to always present this in an ordered set # Note: Coinbase appears to always present this in an ordered set
add_to_headers "advanced_trade_fill_fill_price" add_to_headers "info_advanced_trade_fill_fill_price"
add_to_headers "advanced_trade_fill_product_id" add_to_headers "info_advanced_trade_fill_product_id"
add_to_headers "advanced_trade_fill_order_id" add_to_headers "info_advanced_trade_fill_order_id"
add_to_headers "advanced_trade_fill_commission" add_to_headers "info_advanced_trade_fill_commission"
add_to_headers "advanced_trade_fill_order_side" add_to_headers "info_advanced_trade_fill_order_side"
# #
# Finalize the "universal" format and parse # Finalize the "universal" format and parse