Skip to content

Commit

Permalink
add new platforms page
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Jan 6, 2024
1 parent 3f1e676 commit 9dc07f3
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 4 deletions.
12 changes: 12 additions & 0 deletions app/controllers/origin_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def users
@total_summary = txs.total_summary(current_user.id)
end

def new_platforms
@page_index = 38
sort = params[:sort].presence || "event_time"
sort_type = params[:sort_type].presence || "desc"
txs = OriginTransaction.where(source: ['gate', 'bitget']).order("#{sort} #{sort_type}")
@total_txs = txs
txs = filter_txs(txs)
@event_date = Date.parse(params[:event_date]) rescue nil
txs = txs.where(event_time: @event_date.all_day) if @event_date.present?
@txs = txs.page(params[:page]).per(20)
end

def edit
@tx = OriginTransaction.find params[:id]
end
Expand Down
98 changes: 98 additions & 0 deletions app/jobs/get_gate_spot_trades_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
class GetGateSpotTradesJob < ApplicationJob
queue_as :daily_job
SOURCE = 'gate'.freeze
FEE_SYMBOL = 'USDT'.freeze

def perform(user_id: nil)
result = GateSpotsService.new.get_orders rescue nil
return if result.blank?

txs = OriginTransaction.where(source: SOURCE, user_id: user_id)
ids = []

OriginTransaction.transaction do
result.each do |d|
order_id = d['id']
next if OriginTransaction.exists?(order_id: order_id, user_id: user_id, source: SOURCE)
original_symbol = d['currency_pair']
qty = d['amount'].to_f
price = d['price'].to_f
amount = qty * price
trade_type = d['side'].downcase
from_symbol = original_symbol.split("_#{FEE_SYMBOL}")[0]
event_time = Time.at(d['create_time'])
cost = get_spot_cost(user_id, original_symbol, event_time.to_date) || price
current_price = get_current_price(original_symbol, user_id, from_symbol)
revenue = get_revenue(trade_type, amount, cost, qty, current_price)
roi = revenue / amount
tx = OriginTransaction.where(order_id: order_id, user_id: user_id, source: SOURCE).first_or_initialize
tx.update(
original_symbol: original_symbol,
from_symbol: from_symbol,
to_symbol: FEE_SYMBOL,
fee_symbol: d['fee_currency'],
trade_type: trade_type,
price: price,
qty: qty,
amount: amount,
fee: d['fee'].to_f.abs,
cost: cost,
revenue: revenue,
roi: roi,
current_price: current_price,
event_time: event_time
)
update_tx(tx)
ids.push(tx.id)
end

txs.where.not(id: ids).each do |tx|
update_tx(tx)
end
end
end

def get_current_price(symbol, user_id, from_symbol)
price = $redis.get("gate_spot_price_#{symbol}").to_f
if price == 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("gate_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.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
tx.revenue = get_revenue(tx.trade_type, tx.amount, tx.cost, tx.qty, tx.current_price)
tx.roi = tx.revenue / tx.amount
tx.save
end

def get_revenue(trade_type, amount, cost, qty, current_price)
if trade_type == 'sell'
amount - cost * qty
else
current_price * qty - amount
end
end

def get_spot_cost(user_id, origin_symbol, date)
cost = SpotBalanceSnapshotRecord.joins(:spot_balance_snapshot_info)
.find_by(spot_balance_snapshot_info: {user_id: user_id, event_date: date}, origin_symbol: origin_symbol, source: SOURCE)&.price.to_f
cost = UserSpotBalance.find_by(user_id: user_id, origin_symbol: origin_symbol, source: SOURCE)&.price.to_f if cost.zero? && date == Date.today
cost
end
end
2 changes: 2 additions & 0 deletions app/jobs/get_spot_transactions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def perform

GetOkxSpotTradesJob.perform_later

GetGateSpotTradesJob.perform_later

ForceGcJob.perform_later
$redis.set("get_spot_transactions_refresh_time", Time.now)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/origin_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class OriginTransaction < ApplicationRecord
FILTER_SYMBOL = %w(USDC).freeze

def self.available
OriginTransaction.where('from_symbol NOT IN (?) and ((from_symbol IN (?) AND amount >= 50) OR from_symbol NOT IN (?))', FILTER_SYMBOL, SKIP_SYMBOLS, SKIP_SYMBOLS)
OriginTransaction.where.not(source: ['gate', 'bitget']).where('from_symbol NOT IN (?) and ((from_symbol IN (?) AND amount >= 50) OR from_symbol NOT IN (?))', FILTER_SYMBOL, SKIP_SYMBOLS, SKIP_SYMBOLS)
end

def self.year_to_date
Expand Down
6 changes: 3 additions & 3 deletions app/services/gate_spots_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def initialize(user_id: nil)
end
end

def get_orders
def get_trades(options='')
begin
request_path = '/api/v4/spot/orders'
query_string = 'status=finished'
request_path = '/api/v4/spot/my_trades'
query_string = options
do_request("get", request_path, query_string)
rescue => e
format_error_msg(e)
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
</li> -->
</ul>
</li>
<li class="nav-item">
<%= link_to "新平台现货交易记录列表", new_platforms_origin_transactions_path, class: "nav-link #{@page_index == 38 ? 'active' : ''}" %>
</li>
<% if current_user %>
<li class="nav-item dropdown">
<button class="nav-link btn dropdown-toggle <%= 'active' if @page_index.in?([3, 4, 7, 13, 18, 19, 20, 21]) %>" type="button" id="dropdownMenuTradingHistory" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Expand Down
27 changes: 27 additions & 0 deletions app/views/origin_transactions/new_platforms.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class="m-3">
<h3>现货交易记录列表</h3>
<div class="mb-3 mt-3">
<%= form_tag new_platforms_origin_transactions_path, id: "search_targets", class: "position-relative", method: "GET" do %>
<div class="input-group mb-3 position-filter">
<%= select_tag(:search, options_for_select(@total_txs.pluck(:original_symbol).uniq, @symbol), { prompt: '请选择币种...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:campaign, options_for_select(@total_txs.pluck(:campaign).compact.uniq, @campaign),{ prompt: '请选择campaign...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:trade_type, options_for_select([['买入', 'buy'], ['卖出', 'sell']], @trade_type),{ prompt: '请选择交易类别...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= select_tag(:source, options_for_select(@total_txs.pluck(:source).compact.uniq, @source),{ prompt: '请选择来源...', class: 'select2-dropdown form-control' }) %>
<span class='ms-3'></span>
<%= text_field_tag :event_date, @event_date, class: "form-control datepicker", type: "text", placeholder: "请选择交易日期", autocomplete: "off" %>
<span class='ms-3'></span>
<button type="submit" class="btn btn-primary mx-3">确定</button>
<%= link_to "Reset", new_platforms_origin_transactions_path, class: 'btn btn-warning me-3' %>
</div>
<% end %>
</div>
<div id="trading-histories-container">
<%= render partial: "list", locals: { source_type: :public } %>
</div>
</div>

<div id="transactions-campaign-modal">
</div>
2 changes: 2 additions & 0 deletions app/views/origin_transactions/new_platforms.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#trading-histories-container").html("<%= j render partial: "list", locals: { source_type: :public } %>");
$('[data-bs-toggle="tooltip"]').tooltip();
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
get :users
post :add_key
get :revenue_chart
get :new_platforms
end
end

Expand Down

0 comments on commit 9dc07f3

Please sign in to comment.