Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Worker
- class CheckWithdrawProcessing
- def start
- process_withdrawals
- update_huobi_txids
- end
- private
- def process_withdrawals
- balance = {}
- PSIM::Withdrawal.where(status: :processing).each do |w|
- next unless w.huobi_txid
- balance[w.asset_id] = rpc_balance(w.asset_id) unless balance[w.asset_id]
- unless balance[w.asset_id] < w.amount
- w.notify_psim
- balance[w.asset_id] = rpc_balance(w.asset_id) if w.status == 'done'
- end
- end
- end
- def rpc_balance(code)
- Rpc.send(code).balance
- end
- def update_huobi_txids
- huobi = HuobiConnector::RestApi.new
- withdrawals = find_withdrawals_for_update
- withdrawals.each do |withdrawal|
- response = huobi.withdraw(withdrawal['sum'], withdrawal['asset_id'])
- if response
- PSIM::Withdrawal.where(uuid: withdrawal['uuids'].split(","))
- .update_all(huobi_txid: response['data']['id'])
- end
- end
- end
- def find_withdrawals_for_update
- sql = <<-SQL
- SELECT asset_id, SUM(amount) ,STRING_AGG(uuid, ',' ORDER BY uuid) As uuids
- FROM transactions
- WHERE type = 'withdrawal' AND status = 'processing' AND huobi_txid IS NULL
- GROUP BY asset_id;
- SQL
- ActiveRecord::Base.connection.execute(sql)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement