Skip to content

Commit

Permalink
update funding fee job
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Oct 25, 2023
1 parent dd11ddf commit 4f5842a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions app/jobs/get_funding_fee_histories_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ def perform(sync_ranking: false)
$redis.set('top_3_symbol_funding_rates', result.to_json)
else
date = Date.yesterday
BinanceFuturesService.new.get_funding_fee_histories(date.strftime('%Q'))
OkxFuturesService.new.get_funding_fee_histories

UserPosition.available.where(user_id: nil).each do |up|
generate_history(up, date)
end

UserSyncedPosition.available.pluck(:user_id).uniq.each do |user_id|
BinanceFuturesService.new(user_id: user_id).get_funding_fee_histories(date.strftime('%Q'))
OkxFuturesService.new(user_id: user_id).get_funding_fee_histories
end

UserSyncedPosition.available.each do |up|
generate_history(up, date)
sleep 5
end
end

Expand All @@ -46,8 +47,9 @@ def generate_history(up, date)

def get_fee(symbol, source, date, user_id)
if source == 'binance'
fee_list = BinanceFuturesService.new(user_id: user_id).get_funding_fee_histories(symbol, date.strftime('%Q'))
daily_fees = fee_list.select{|r| Time.at(r['time']/1000).to_date == date}
fee_list = JSON.parse($redis.get("binance_funding_fee_histories_user_#{user_id}")) rescue nil
return 0 if fee_list.nil?
daily_fees = fee_list.select{|r| Time.at(r['time']/1000).to_date == date && r['symbol'] == symbol}
daily_fees.sum{|f| f['income'].to_f}
elsif source == 'okx'
fee_list = JSON.parse($redis.get("okx_funding_fee_histories_user_#{user_id}")) rescue nil
Expand Down
8 changes: 5 additions & 3 deletions app/services/binance_futures_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ def get_taker_long_short_ratio(symbol)
JSON.parse(response).last
end

def get_funding_fee_histories(symbol, start_time)
def get_funding_fee_histories(start_time, symbol=nil)
url = BASE_URL + "/fapi/v1/income?"
payload = {symbol: symbol, incomeType: 'FUNDING_FEE', timestamp: get_timestamp, startTime: start_time, limit: 1000}
do_request("get", url, payload)
payload = {incomeType: 'FUNDING_FEE', timestamp: get_timestamp, startTime: start_time, limit: 1000}
payload.merge!({symbol: symbol}) if symbol
response = do_request("get", url, payload)
$redis.set("binance_funding_fee_histories_user_#{@user&.id}", response.to_json)
end

private
Expand Down

0 comments on commit 4f5842a

Please sign in to comment.