Skip to content

Commit

Permalink
Handle two transactions with same timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Jun 28, 2017
1 parent 227ccfe commit fcf5557
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
51 changes: 44 additions & 7 deletions lib/rapflag/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,50 @@ def create_csv_file
'date_time',
] #< column header
) do |csv|
@history.each do | hist_item|
csv << [ hist_item['currency'],
hist_item['amount'],
hist_item['balance'],
hist_item['description'],
Time.at(hist_item['timestamp'].to_i).strftime(DATE_TIME_FORMAT),
]
skip_next = false
@history.each_with_index do | hist_item, index|
if skip_next
skip_next = false
next
end
timestamp = Time.at(hist_item['timestamp'].to_i).strftime(DATE_TIME_FORMAT)
next_timestamp = @history[index+1] && Time.at(@history[index+1]['timestamp'].to_i).strftime(DATE_TIME_FORMAT)
if next_timestamp.eql?(timestamp)
next_item = @history[index + 1]
this_day = Date.parse(timestamp).to_s
prev_day = (Date.parse(timestamp)-1).to_s
this_tx = history.find_all{|x| Time.at(x['timestamp'].to_i).to_date.to_s.eql?(this_day)}
prev_tx = history.find{|x| Time.at(x['timestamp'].to_i).to_date < Date.parse(timestamp)}
@total = prev_tx ? prev_tx['balance'].to_f : 0.0
this_tx.each{|x|to_add = x['amount'].to_f; @total = @total + to_add}
if @total == next_item['balance'].to_f
second = hist_item
first = next_item
else
first = hist_item
second = next_item
end
skip_next = true
csv << [ first['currency'],
first['amount'],
first['balance'],
first['description'],
Time.at(first['timestamp'].to_i).strftime(DATE_TIME_FORMAT),
]
csv << [ second['currency'],
second['amount'],
second['balance'],
second['description'],
Time.at(second['timestamp'].to_i).strftime(DATE_TIME_FORMAT),
]
else
csv << [ hist_item['currency'],
hist_item['amount'],
hist_item['balance'],
hist_item['description'],
timestamp,
]
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/bitfinex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def gen_trading
expect(lines[1].chomp).to eql(
'BTC,-0.00000005,0.0,Transfer of 0.0 BTC from wallet Exchange to Deposit on wallet Exchange,2016.12.03 21:20:47')
end
it 'should have some lines inverted if the timestamp match' do
expect(File.exist?(Bitfinex_CSV_Test_File)).to eql(true)
lines = IO.readlines(Bitfinex_CSV_Test_File)
first = lines.index("BTC,-0.0170128,11.0283072,Trading fees for 8.5064 BTC @ 442.06 on BFX (0.2%) on wallet Exchange,2016.02.21 23:48:48\n")
second = lines.index("BTC,-0.0004,11.04532,Trading fees for 0.2 BTC @ 442.1 on BFX (0.2%) on wallet Exchange,2016.02.21 23:48:48\n")
expect(second + 1).to eq first
end
end
end
context 'exchange option --clean' do
Expand Down

0 comments on commit fcf5557

Please sign in to comment.