-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
get :users | ||
post :add_key | ||
get :revenue_chart | ||
get :new_platforms | ||
end | ||
end | ||
|
||
|