From 69252493f75a5e704dd0336c5a1ed5a0c18ccbec Mon Sep 17 00:00:00 2001 From: Pan Xiong Date: Sun, 22 Oct 2023 21:20:10 +1100 Subject: [PATCH] update funding fee job --- app/jobs/get_funding_fee_histories_job.rb | 16 +++++++++++----- app/services/okx_futures_service.rb | 7 ++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/jobs/get_funding_fee_histories_job.rb b/app/jobs/get_funding_fee_histories_job.rb index 243ce53..5f4f8ec 100644 --- a/app/jobs/get_funding_fee_histories_job.rb +++ b/app/jobs/get_funding_fee_histories_job.rb @@ -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 @@ -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 diff --git a/app/services/okx_futures_service.rb b/app/services/okx_futures_service.rb index 4e06826..51039c8 100644 --- a/app/services/okx_futures_service.rb +++ b/app/services/okx_futures_service.rb @@ -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