class Blockchain::TransferService < ApplicationService

  attr_reader :params

  def initialize(params)
    @params  = params
  end

  def call
    ActiveRecord::Base.transaction do
      transfer_amount = params['transfer_amount']
      if (Float(transfer_amount) rescue false) == false or transfer_amount.to_i < 0 or Float(transfer_amount) != transfer_amount.to_i
        raise Error, "请输入正确的转账金额"
      end
      transfer_amount = params['transfer_amount'].to_i
      transfer_login = params['transfer_login']
      payer = params['payer_id'].to_s
      payee = User.find_by(login: transfer_login)
      if payee.nil?
        raise Error, "未找到接收转账的用户"
      else
        payee = payee.id.to_s
        token_name = params['project_id'].to_s
        # 调用token转移函数
        transfer_balance_on_blockchain(payer, payee, token_name, transfer_amount)
      end
    end
  end

  private

  def no_use
    puts "this function does not have any usage"
  end

end