Advertisement
nuringa

Untitled

Feb 8th, 2021
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.21 KB | None | 0 0
  1. module Worker
  2.   class EthClientToHot
  3.     def initialize
  4.       @logger = Logger.new("#{Rails.root}/log/check_erc20_to_huobi.log")
  5.     end
  6.  
  7.     def process(payload, metadata, delivery_info)
  8.       payload.symbolize_keys!
  9.       @logger.info payload
  10.       rpc = Rpc.send(payload[:coin])
  11.       gas_price = rpc.get_gas_price
  12.       @logger.info gas_price
  13.       amount = payload[:amount].to_d
  14.       address = check_min_deposit(payload[:coin], rpc, amount)
  15.       @logger.info 'address'
  16.       @logger.info address
  17.       return nil unless address
  18.       params = rpc.params_for_transaction(
  19.           payload[:from],
  20.           address,
  21.           amount,
  22.           gas_price
  23.       )
  24.       gas_limit = rpc.get_gas_limit(params)
  25.       @logger.info gas_limit
  26.       params[:gas] = "0x#{gas_limit.to_s(16)}"
  27.       @logger.info params
  28.       fee = gas_limit * gas_price  / 1e+18
  29.  
  30.       if rpc_eth.get_balance(payload[:from]) < fee
  31.         @logger.info "balance < #{fee}"
  32.         from = get_from_address(fee)
  33.         @logger.info 'from address'
  34.         @logger.info from
  35.         if from
  36.           @logger.info "create transaction amount: #{fee}"
  37.           @logger.info rpc_eth.create_transaction(from: from,
  38.                                      to: payload[:from],
  39.                                      amount: fee)
  40.         else
  41.           SystemMailer.withdraw_insufficient_funds(
  42.               "Low balance to process Withdraw to huobi , amount < 0.006 ETH for erc20."
  43.           ).deliver
  44.           # SystemMailer.not_enough_balance("ETH fee").deliver
  45.         end
  46.  
  47.       end
  48.       transfer_to_huobi_wallet(rpc, payload, params, fee)
  49.     rescue => error
  50.       ExceptionNotifier.notify_exception(error, data: {payload: payload})
  51.       raise $!
  52.     end
  53.  
  54.     def get_from_address(fee)
  55.       if rpc_eth.get_balance(CONFIG_ETH['erc20_fee_wallet']) > fee * 3
  56.         CONFIG_ETH['erc20_fee_wallet']
  57.       elsif rpc_eth.get_balance(CONFIG_ETH['hot_wallet']) > fee * 3
  58.         SystemMailer.not_enough_balance("ETH fee").deliver
  59.         CONFIG_ETH['hot_wallet']
  60.       end
  61.     end
  62.  
  63.     def transfer_to_huobi_wallet(rpc, payload, params, fee, retry_count = 100)
  64.       @logger.info 'try transfer_to_huobi_wallet'
  65.       @logger.info retry_count
  66.       return if retry_count == 0
  67.       sleep 1.minute
  68.       if rpc_eth.get_balance(payload[:from]) < fee
  69.         @logger.info "balance < #{fee}"
  70.         transfer_to_huobi_wallet(rpc, payload, params, fee, retry_count - 1)
  71.       else
  72.         @logger.info 'start transaction'
  73.         @logger.info 'create transaction'
  74.         txid = rpc.send_transaction(params)
  75.         @logger.info txid
  76.         if txid
  77.           PSIM::Deposit.find(payload[:deposit_id]).update(huobi_txid: txid)
  78.           @logger.info txid
  79.         else
  80.           sleep 1.minute
  81.           transfer_to_huobi_wallet(rpc, payload, params, fee, retry_count - 1)
  82.         end
  83.       end
  84.     end
  85.  
  86.     def rpc_eth
  87.       @rpc_eth ||= Rpc.eth
  88.     end
  89.  
  90.     def check_min_deposit(coin, rpc, amount)
  91.       response = rpc.huobi_deposit_address
  92.       asset = Asset.find(coin)
  93.       return rpc.config['hot_wallet'] unless response
  94.       (asset.min_deposit > amount) ? rpc.config['hot_wallet'] : response['data']['address']
  95.     end
  96.   end
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement