Class: CoinSync::Importers::BitBay20

Inherits:
Base
  • Object
show all
Defined in:
lib/coinsync/importers/bitbay20.rb

Defined Under Namespace

Classes: HistoryEntry

Constant Summary collapse

OP_PAY_BUYING =
'Pay for buying currency'
OP_PAY_SELLING =
'Pay for selling currency'
OP_PURCHASE =
'Currency purchase'
OP_SALE =
'Currency sale'
OP_FEE =
'Transaction fee'
MAX_TIME_DIFFERENCE =
5.0
TRANSACTION_TYPES =
[OP_PAY_BUYING, OP_PAY_SELLING, OP_PURCHASE, OP_SALE, OP_FEE]

Instance Method Summary collapse

Methods inherited from Base

#can_build?, #initialize, register_commands, register_importer, registered_commands

Constructor Details

This class inherits a constructor from CoinSync::Importers::Base

Instance Method Details

#read_transaction_list(source) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/coinsync/importers/bitbay20.rb', line 61

def read_transaction_list(source)
  csv = CSV.new(source, col_sep: ';')

  matching = []
  transactions = []

  csv.each do |line|
    next if line.empty?
    next if line[0] !~ /^\d/

    entry = HistoryEntry.new(line)

    next unless TRANSACTION_TYPES.include?(entry.type)

    if !matching.empty? && matching.any? { |e| (e.date - entry.date).abs > MAX_TIME_DIFFERENCE }
      if matching.any? { |e| e.type != OP_FEE }
        raise "BitBay importer error: Couldn't match some history lines"
      else
        matching.clear
      end
    end

    matching << entry

    if matching.length == 3
      matching.sort_by!(&:type)
      types = matching.map(&:type)
      time = matching.map(&:date).sort.last

      if types == [OP_PURCHASE, OP_PAY_BUYING, OP_FEE] &&
        matching[0].crypto? && matching[0].amount > 0 &&
        matching[1].fiat? && matching[1].amount < 0 &&
        matching[2].crypto? && matching[2].amount <= 0 &&
        matching[0].currency == matching[2].currency
          transactions << Transaction.new(
            exchange: 'BitBay',
            bought_currency: matching[0].currency,
            sold_currency: matching[1].currency,
            time: time,
            bought_amount: matching[0].amount + matching[2].amount,
            sold_amount: -matching[1].amount
          )
      elsif types == [OP_SALE, OP_PAY_SELLING, OP_FEE] &&
        matching[0].crypto? && matching[0].amount < 0 &&
        matching[1].fiat? && matching[1].amount > 0 &&
        matching[2].fiat? && matching[2].amount <= 0 &&
        matching[1].currency == matching[2].currency
          transactions << Transaction.new(
            exchange: 'BitBay',
            bought_currency: matching[1].currency,
            sold_currency: matching[0].currency,
            time: time,
            bought_amount: matching[1].amount + matching[2].amount,
            sold_amount: -matching[0].amount
          )
      elsif types == [OP_PURCHASE, OP_SALE, OP_FEE] &&
        matching[0].fiat? && matching[0].amount > 0 &&
        matching[1].crypto? && matching[1].amount < 0 &&
        matching[2].fiat? && matching[2].amount <= 0 &&
        matching[0].currency == matching[2].currency
          transactions << Transaction.new(
            exchange: 'BitBay',
            bought_currency: matching[0].currency,
            sold_currency: matching[1].currency,
            time: time,
            bought_amount: matching[0].amount + matching[2].amount,
            sold_amount: -matching[1].amount
          )
      else
        raise "BitBay importer error: Couldn't match some history lines"
      end

      matching.clear
    end
  end

  if !matching.empty?
    if matching.any? { |l| l.type != OP_FEE }
      raise "BitBay importer error: Couldn't match some history lines"
    end
  end

  transactions.reverse
end