Skip to content

Commit

Permalink
get current price from coin elite
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Nov 20, 2023
1 parent 7990d1b commit e660e1a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 13 additions & 4 deletions app/jobs/get_okx_spot_trades_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def perform(user_id: nil)
from_symbol = original_symbol.split("-#{FEE_SYMBOL}")[0]
event_time = Time.at(d['cTime'].to_i / 1000)
cost = get_spot_cost(user_id, original_symbol, event_time.to_date) || price
current_price = get_current_price(original_symbol, user_id)
current_price = get_current_price(original_symbol, user_id, from_symbol)
revenue = get_revenue(trade_type, amount, cost, qty, current_price) if revenue.to_f == 0
roi = revenue / amount
tx = OriginTransaction.where(order_id: d['ordId'], user_id: user_id, source: SOURCE).first_or_initialize
Expand Down Expand Up @@ -55,18 +55,27 @@ def perform(user_id: nil)
end
end

def get_current_price(symbol, user_id)
def get_current_price(symbol, user_id, from_symbol)
price = $redis.get("okx_spot_price_#{symbol}").to_f
if price == 0
price = OkxSpotsService.new(user_id: user_id).get_price(symbol)["data"].first["last"] rescue 0
price = OkxSpotsService.new(user_id: user_id).get_price(symbol)["data"].first["last"].to_f rescue 0
price = get_coin_price(from_symbol) if price.zero?
$redis.set("okx_spot_price_#{symbol}", price, ex: 2.hours)
end

price.to_f
end

def get_coin_price(symbol)
date = Date.yesterday
url = ENV['COIN_ELITE_URL'] + "/api/coins/history_price?symbol=#{symbol}&from_date=#{date}&to_date=#{date}"
response = RestClient.get(url)
data = JSON.parse(response.body)
data['result'].values[0].to_f rescue nil
end

def update_tx(tx)
tx.current_price = get_current_price(tx.original_symbol, tx.user_id)
tx.current_price = get_current_price(tx.original_symbol, tx.user_id, tx.from_symbol)
if tx.cost.to_f.zero?
tx.cost = get_spot_cost(tx.user_id, tx.original_symbol, tx.event_time.to_date) || tx.price
end
Expand Down
15 changes: 12 additions & 3 deletions app/jobs/get_spot_trades_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def perform(symbol, user_id: nil)
origin_symbol = "#{symbol}#{TRADE_SYMBOL}"
txs = OriginTransaction.where(source: SOURCE, original_symbol: origin_symbol, user_id: user_id)
trades = BinanceSpotsService.new(user_id: user_id).get_my_trades(origin_symbol)
current_price = get_current_price(origin_symbol, user_id)
current_price = get_current_price(origin_symbol, user_id, symbol)
if trades.is_a?(String) || trades.blank?
Rails.logger.debug "获取不到#{origin_symbol}的交易记录"
txs.each{|tx| update_tx(tx, current_price)} if txs.any?
Expand Down Expand Up @@ -38,16 +38,25 @@ def perform(symbol, user_id: nil)
$redis.del("origin_transactions_total_summary_#{user_id}")
end

def get_current_price(symbol, user_id)
def get_current_price(symbol, user_id, from_symbol)
price = $redis.get("binance_spot_price_#{symbol}").to_f
if price == 0
price = BinanceSpotsService.new(user_id: user_id).get_price(symbol)[:price] rescue 0
price = BinanceSpotsService.new(user_id: user_id).get_price(symbol)[:price].to_f rescue 0
price = get_coin_price(from_symbol) if price.zero?
$redis.set("binance_spot_price_#{symbol}", price, ex: 2.hours)
end

price.to_f
end

def get_coin_price(symbol)
date = Date.yesterday
url = ENV['COIN_ELITE_URL'] + "/api/coins/history_price?symbol=#{symbol}&from_date=#{date}&to_date=#{date}"
response = RestClient.get(url)
data = JSON.parse(response.body)
data['result'].values[0].to_f rescue nil
end

def update_tx(tx, current_price)
tx.current_price = current_price
tx.cost = get_spot_cost(tx.user_id, tx.original_symbol, tx.event_time.to_date) || tx.price
Expand Down

0 comments on commit e660e1a

Please sign in to comment.