6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
73
74
75
|
# File 'lib/revolut/api/private/exchange.rb', line 6
def exchange(from:, to:, amount:, side: :sell)
status, success = nil, nil
transactions = []
error = {}
from = from.to_s.upcase
to = to.to_s.upcase
if amount.eql?(:all)
pocket = self.wallet.pocket(from)
if pocket
amount = pocket.balance
log "Will exchange total balance #{from} to #{to}. Balance: #{amount}."
end
end
if !from.to_s.empty? && !to.to_s.empty? && !amount.nil?
log "Will exchange #{amount} #{from} to #{to}"
payload = {
"fromCcy" => from,
"toCcy" => to,
"rateTimestamp" => Time.now.utc.to_i,
}
if side.eql?(:sell)
payload.merge!("fromAmount" => ::Revolut::Api::Utilities.convert_to_integer_amount(from, amount))
elsif side.eql?(:buy)
payload.merge!("toAmount" => ::Revolut::Api::Utilities.convert_to_integer_amount(to, amount))
end
data = post("exchange", data: payload.to_json)
if data && data.is_a?(Array) && data.any?
data.each do |response|
transactions << ::Revolut::Api::Response::Transaction.new(response)
end
complete_count = 0
pending_count = 0
transactions.each do |transaction|
complete_count += 1 if transaction.completed?
pending_count += 1 if transaction.pending?
end
success = (complete_count == data.count || pending_count == data.count)
status = :completed if complete_count == data.count
status = :pending if pending_count == data.count
log "Successfully exchanged #{amount} #{from} to #{to}!" if status.eql?(:completed)
log "Exchange from #{amount} #{from} to #{to} is currently pending" if status.eql?(:pending)
elsif data && data.is_a?(Hash)
error_message = data.fetch("message", nil)
error_code = data.fetch("code", nil)
status = :failed
success = false
error[:message] = error_message if !error_message.to_s.empty?
error[:code] = error_code if !error_code.to_s.empty?
log "Error occurred while trying to exchange #{amount} #{from} to #{to}. Error (#{error_code}): #{error_message}"
end
else
log "Missing some data required for the exchange. From: #{from}. To: #{to}. Amount: #{amount}."
end
return {status: status, success: success, transactions: transactions, error: error}
end
|