Class: CoinSync::Outputs::SplitList

Inherits:
List
  • Object
show all
Defined in:
lib/coinsync/outputs/split_list.rb

Instance Method Summary collapse

Methods inherited from List

#headers, #process_transaction, #transaction_to_csv

Methods inherited from Base

register_output

Constructor Details

#initialize(config, target_file) ⇒ SplitList

Returns a new instance of SplitList.



17
18
19
20
21
22
23
# File 'lib/coinsync/outputs/split_list.rb', line 17

def initialize(config, target_file)
  super

  if @config.value_estimation
    @price_loader = @config.value_estimation.price_loader
  end
end

Instance Method Details

#fiat_transaction_to_csv(tx) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/coinsync/outputs/split_list.rb', line 82

def fiat_transaction_to_csv(tx)
  tx_type = @config.translate(tx.type.to_s.capitalize)

  is_split = tx.number.to_s.include?('.')
  is_incomplete = is_split && tx.bought_amount * tx.sold_amount == 0

  if is_split
    tx_type = @config.translate(Transaction::TYPE_SWAP.to_s.capitalize) + '/' + tx_type
  end

  csv = [
    tx.number || 0,
    tx.exchange,
    tx_type,
    @formatter.format_time(tx.time),
    @formatter.format_crypto(tx.crypto_amount),
    tx.crypto_currency.code
  ]

  if is_incomplete
    csv += [nil, nil, nil]
  else
    csv += [
      @formatter.format_fiat(tx.fiat_amount),
      @formatter.format_fiat_price(tx.price),
      tx.fiat_currency.code || ''
    ]
  end

  if @config.currency_conversion
    if is_incomplete
      csv += [nil, nil, nil]
    elsif tx.converted
      csv += [
        @formatter.format_fiat(tx.converted.fiat_amount),
        @formatter.format_fiat_price(tx.converted.price),
        tx.converted.exchange_rate && @formatter.format_float(tx.converted.exchange_rate, precision: 4)
      ]
    else
      csv += [
        @formatter.format_fiat(tx.fiat_amount),
        @formatter.format_fiat_price(tx.price),
        nil
      ]
    end
  end

  csv
end

#get_coin_price(coin, time) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/coinsync/outputs/split_list.rb', line 137

def get_coin_price(coin, time)
  if @price_loader
    print "$"

    begin
      @price_loader.get_price(coin, time)
    rescue Exception => e
      @price_loader.finalize
      raise
    end
  else
    [BigDecimal.new(0), FiatCurrency.new(nil)]
  end
end

#process_transactions(transactions, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coinsync/outputs/split_list.rb', line 25

def process_transactions(transactions, *args)
  split_list = []

  transactions.each do |tx|
    if tx.purchase? || tx.sale?
      split_list << tx
    else
      sale, purchase = split_transaction(tx)
      split_list << sale
      split_list << purchase
    end
  end

  @price_loader&.finalize

  if options = @config.currency_conversion
    converter = CurrencyConversionTask.new(options)
    converter.process_transactions(split_list)
  end

  super(split_list, *args)
end

#requires_currency_conversion?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/coinsync/outputs/split_list.rb', line 13

def requires_currency_conversion?
  false
end

#split_transaction(tx) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/coinsync/outputs/split_list.rb', line 48

def split_transaction(tx)
  if @classifier.is_purchase?(tx)
    base = tx.sold_currency
    base_price, fiat_currency = get_coin_price(base, tx.time)
    total_value = tx.sold_amount * base_price
  else
    base = tx.bought_currency
    base_price, fiat_currency = get_coin_price(base, tx.time)
    total_value = tx.bought_amount * base_price
  end

  sale = Transaction.new(
    number: "#{tx.number}.A",
    exchange: tx.exchange,
    time: tx.time,
    sold_currency: tx.sold_currency,
    sold_amount: tx.sold_amount,
    bought_currency: fiat_currency,
    bought_amount: total_value
  )

  purchase = Transaction.new(
    number: "#{tx.number}.B",
    exchange: tx.exchange,
    time: tx.time,
    bought_currency: tx.bought_currency,
    bought_amount: tx.bought_amount,
    sold_currency: fiat_currency,
    sold_amount: total_value
  )

  [sale, purchase]
end

#swap_transaction_to_csv(tx) ⇒ Object



132
133
134
135
# File 'lib/coinsync/outputs/split_list.rb', line 132

def swap_transaction_to_csv(tx)
  # sanity check - this should not happen
  raise "SplitList: unexpected unprocessed swap transaction"
end