Advertisement
nuringa

Untitled

Feb 2nd, 2021
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.40 KB | None | 0 0
  1. module Worker
  2.   class CheckWithdrawProcessing
  3.     def start
  4.       process_withdrawals
  5.  
  6.       update_huobi_txids
  7.     end
  8.  
  9.     private
  10.  
  11.     def process_withdrawals
  12.       balance = {}
  13.       PSIM::Withdrawal.where(status: :processing).each do |w|
  14.         next unless w.huobi_txid
  15.  
  16.         balance[w.asset_id] = rpc_balance(w.asset_id) unless balance[w.asset_id]
  17.  
  18.         unless balance[w.asset_id] < w.amount
  19.           w.notify_psim
  20.  
  21.           balance[w.asset_id] = rpc_balance(w.asset_id) if w.status == 'done'
  22.         end
  23.       end
  24.     end
  25.  
  26.     def rpc_balance(code)
  27.       Rpc.send(code).balance
  28.     end
  29.  
  30.     def update_huobi_txids
  31.       huobi = HuobiConnector::RestApi.new
  32.       withdrawals = find_withdrawals_for_update
  33.  
  34.       withdrawals.each do |withdrawal|
  35.         response = huobi.withdraw(withdrawal['sum'], withdrawal['asset_id'])
  36.  
  37.         if response
  38.           PSIM::Withdrawal.where(uuid: withdrawal['uuids'].split(","))
  39.                           .update_all(huobi_txid: response['data']['id'])
  40.         end
  41.       end
  42.     end
  43.  
  44.     def find_withdrawals_for_update
  45.       sql = <<-SQL
  46.       SELECT asset_id, SUM(amount) ,STRING_AGG(uuid, ',' ORDER BY uuid) As uuids
  47.       FROM transactions
  48.       WHERE type = 'withdrawal' AND status = 'processing' AND huobi_txid IS NULL
  49.       GROUP BY asset_id;
  50.       SQL
  51.  
  52.       ActiveRecord::Base.connection.execute(sql)
  53.     end
  54.   end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement