Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Worker
- class EthClientToHot
- def initialize
- @logger = Logger.new("#{Rails.root}/log/check_erc20_to_huobi.log")
- end
- def process(payload, metadata, delivery_info)
- payload.symbolize_keys!
- @logger.info payload
- rpc = Rpc.send(payload[:coin])
- gas_price = rpc.get_gas_price
- @logger.info gas_price
- amount = payload[:amount].to_d
- address = check_min_deposit(payload[:coin], rpc, amount)
- @logger.info 'address'
- @logger.info address
- return nil unless address
- params = rpc.params_for_transaction(
- payload[:from],
- address,
- amount,
- gas_price
- )
- gas_limit = rpc.get_gas_limit(params)
- @logger.info gas_limit
- params[:gas] = "0x#{gas_limit.to_s(16)}"
- @logger.info params
- fee = gas_limit * gas_price / 1e+18
- if rpc_eth.get_balance(payload[:from]) < fee
- @logger.info "balance < #{fee}"
- from = get_from_address(fee)
- @logger.info 'from address'
- @logger.info from
- if from
- @logger.info "create transaction amount: #{fee}"
- @logger.info rpc_eth.create_transaction(from: from,
- to: payload[:from],
- amount: fee)
- else
- SystemMailer.withdraw_insufficient_funds(
- "Low balance to process Withdraw to huobi , amount < 0.006 ETH for erc20."
- ).deliver
- # SystemMailer.not_enough_balance("ETH fee").deliver
- end
- end
- transfer_to_huobi_wallet(rpc, payload, params, fee)
- rescue => error
- ExceptionNotifier.notify_exception(error, data: {payload: payload})
- raise $!
- end
- def get_from_address(fee)
- if rpc_eth.get_balance(CONFIG_ETH['erc20_fee_wallet']) > fee * 3
- CONFIG_ETH['erc20_fee_wallet']
- elsif rpc_eth.get_balance(CONFIG_ETH['hot_wallet']) > fee * 3
- SystemMailer.not_enough_balance("ETH fee").deliver
- CONFIG_ETH['hot_wallet']
- end
- end
- def transfer_to_huobi_wallet(rpc, payload, params, fee, retry_count = 100)
- @logger.info 'try transfer_to_huobi_wallet'
- @logger.info retry_count
- return if retry_count == 0
- sleep 1.minute
- if rpc_eth.get_balance(payload[:from]) < fee
- @logger.info "balance < #{fee}"
- transfer_to_huobi_wallet(rpc, payload, params, fee, retry_count - 1)
- else
- @logger.info 'start transaction'
- @logger.info 'create transaction'
- txid = rpc.send_transaction(params)
- @logger.info txid
- if txid
- PSIM::Deposit.find(payload[:deposit_id]).update(huobi_txid: txid)
- @logger.info txid
- else
- sleep 1.minute
- transfer_to_huobi_wallet(rpc, payload, params, fee, retry_count - 1)
- end
- end
- end
- def rpc_eth
- @rpc_eth ||= Rpc.eth
- end
- def check_min_deposit(coin, rpc, amount)
- response = rpc.huobi_deposit_address
- asset = Asset.find(coin)
- return rpc.config['hot_wallet'] unless response
- (asset.min_deposit > amount) ? rpc.config['hot_wallet'] : response['data']['address']
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement