class CoinSync::Importers::BittrexCSV

Public Instance Methods

read_transaction_list(source) click to toggle source
# 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