Class: CoinSync::Importers::BittrexCSV

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

Defined Under Namespace

Classes: HistoryEntry

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/coinsync/importers/bittrex_csv.rb', line 33

def read_transaction_list(source)
  contents = source.read.gsub("\u0000", '').gsub("\r", '')
  entries = []
  transactions = []

  CSV.parse(contents, col_sep: ',') do |line|
    next if line[0] == 'OrderUuid'

    entries << HistoryEntry.new(line)
  end

  entries.sort_by! { |e| [e.time_closed, e.uuid] }

  entries.each do |entry|
    case entry.type
    when 'LIMIT_BUY', 'MARKET_BUY' then
      transactions << Transaction.new(
        exchange: 'Bittrex',
        time: entry.time_closed,
        bought_amount: entry.quantity,
        bought_currency: entry.asset,
        sold_amount: entry.price + entry.commission,
        sold_currency: entry.currency
      )
    when 'LIMIT_SELL', 'MARKET_SELL' then
      transactions << Transaction.new(
        exchange: 'Bittrex',
        time: entry.time_closed,
        bought_amount: entry.price - entry.commission,
        bought_currency: entry.currency,
        sold_amount: entry.quantity,
        sold_currency: entry.asset
      )
    else
      raise "Bittrex importer error: unexpected entry type '#{entry.type}'"
    end
  end

  transactions
end