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 22, 2023
1 parent 79fc915 commit 6925249
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
16 changes: 11 additions & 5 deletions app/jobs/get_funding_fee_histories_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ def perform(sync_ranking: false)
$redis.set('top_3_symbol_funding_rates', result.to_json)
else
date = Date.yesterday
@okx_fee_list = OkxFuturesService.new.get_funding_fee_histories['data']
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|
OkxFuturesService.new(user_id: user_id).get_funding_fee_histories
end

UserSyncedPosition.available.each do |up|
generate_history(up, date)
end
Expand All @@ -32,20 +36,22 @@ def perform(sync_ranking: false)
end

def generate_history(up, date)
funding_fee = get_fee(up.origin_symbol, up.source, date)
funding_fee = get_fee(up.origin_symbol, up.source, date, up.user_id)
SnapshotPosition.joins(:snapshot_info).where(snapshot_info: { user_id: up.user_id }, origin_symbol: up.origin_symbol, event_date: date, source: up.source).each do |snapshot|
ffh = FundingFeeHistory.where(origin_symbol: snapshot.origin_symbol, event_date: date, source: snapshot.source, user_id: up.user_id, trade_type: up.trade_type).first_or_initialize
ffh.update(amount: funding_fee, snapshot_position_id: snapshot&.id)
end
end

def get_fee(symbol, source, date)
def get_fee(symbol, source, date, user_id)
if source == 'binance'
fee_list = BinanceFuturesService.new.get_funding_fee_histories(symbol, date.strftime('%Q'))
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}
daily_fees.sum{|f| f['income'].to_f}
elsif source == 'okx'
daily_fees = @okx_fee_list.select{|r| Time.at(r['ts'].to_i/1000).to_date == date && r['instId'] == symbol}
fee_list = JSON.parse($redis.get("okx_funding_fee_histories_user_#{user_id}")) rescue nil
return 0 if fee_list.nil?
daily_fees = fee_list['data'].select{|r| Time.at(r['ts'].to_i/1000).to_date == date && r['instId'] == symbol}
daily_fees.sum{|f| f['pnl'].to_f}
else
0
Expand Down
7 changes: 4 additions & 3 deletions app/services/okx_futures_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ def get_funding_rate(symbol, before=nil)
end
end

def get_funding_fee_histories(end_at=nil)
def get_funding_fee_histories(start_at=nil)
begin
request_path = "/api/v5/account/bills-archive?instType=SWAP&type=8"
request_path += "&end=#{end_at}" if end_at
do_request("get", request_path)
request_path += "&begin=#{start_at}" if start_at
response = do_request("get", request_path)
$redis.set("okx_funding_fee_histories_user_#{@user&.id}", response.to_json)
rescue => e
format_error_msg(e)
end
Expand Down

0 comments on commit 6925249

Please sign in to comment.