1

I am working on a ruby application but getting a strange error that I believe might be caused by activerecord/activemodel. The error is as follows:

Error on PaymentTransaction::Normal: undefined local variable or method `c' for #<Deposits::Mobitglobal:0x0055995b000b78>
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activemodel-4.0.12/lib/active_model/attribute_methods.rb:439:in `method_missing'
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.12/lib/active_record/attribute_methods.rb:168:in `method_missing'

I have tried using ruby-2.3.0 as a suggested solution thinking that activerecord/activemodel in that version handles it differently. But the result is the same.

The process starts from the file: coin_rpc.rb


require 'net/http'
require 'uri'
require 'json'

class CoinRPC

  class JSONRPCError < RuntimeError; end
  class ConnectionRefusedError < StandardError; end

  def initialize(uri)
    @uri = URI.parse(uri)
  end

  def self.[](currency)
    c = Currency.find_by_code(currency.to_s)
    if c && c.rpc
      name = c.family || 'BTC'
      "::CoinRPC::#{name}".constantize.new(c.rpc)
    end
  end

  def method_missing(name, *args)
    handle name, *args
  end

  def handle
    raise "Not implemented"
  end
  class BTC < self
    def handle(name, *args)
      post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
      resp = JSON.parse( http_post_request(post_body) )
      raise JSONRPCError, resp['error'] if resp['error']
      result = resp['result']
      result.symbolize_keys! if result.is_a? Hash
      result
    end
    def http_post_request(post_body)
      http    = Net::HTTP.new(@uri.host, @uri.port)
      request = Net::HTTP::Post.new(@uri.request_uri)
      request.basic_auth @uri.user, @uri.password
      request.content_type = 'application/json'
      request.body = post_body
      http.request(request).body
    rescue Errno::ECONNREFUSED => e
      raise ConnectionRefusedError
    end

    def safe_getbalance
      begin
        getbalance
      rescue
        'N/A'
      end
    end
  end

  class ETH < self
    def handle(name, *args)
      post_body = {"jsonrpc" => "2.0", 'method' => name, 'params' => args, 'id' => '1' }.to_json
      resp = JSON.parse( http_post_request(post_body) )
      raise JSONRPCError, resp['error'] if resp['error']
      result = resp['result']
      result.symbolize_keys! if result.is_a? Hash
      result
    end
    def http_post_request(post_body)
      http    = Net::HTTP.new(@uri.host, @uri.port)
      request = Net::HTTP::Post.new(@uri.request_uri)
      request.basic_auth @uri.user, @uri.password
      request.content_type = 'application/json'
      request.body = post_body
      http.request(request).body
    rescue Errno::ECONNREFUSED => e
      raise ConnectionRefusedError
    end

    def safe_getbalance
      begin
        (open(@uri.host + '/cgi-bin/total.cgi').read.rstrip.to_f)
      rescue
        'N/A'
      end
    end
  end
end

The error seems to come somewhere between the two definitions in the above file:

def self.[](currency)
    c = Currency.find_by_code(currency.to_s)
    if c && c.rpc
      name = c.family || 'BTC'
      "::CoinRPC::#{name}".constantize.new(c.rpc)
    end
  end

  def method_missing(name, *args)
    handle name, *args
  end

The file about calls the "family" name, in this case "BTC" in the following file: currencies.yml

- id: 9
  key: mobitglobal
  code: mbg
  symbol: "฿"
  coin: true
  family: BTC
  quick_withdraw_max: 1000
  rpc: http://username:password@127.0.0.1:11111
  blockchain: http://cryptoid.net/explorer/MBGL?txid=#{txid}
  address_url: http://cryptoid.net/explorer/MBGL?txid=#{address}

At that point the call should be looking for "family" and then if set to BTC then continue the operation in coin_rpc.rb:

class BTC < self
    def handle(name, *args)
      post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
      resp = JSON.parse( http_post_request(post_body) )
      raise JSONRPCError, resp['error'] if resp['error']
      result = resp['result']
      result.symbolize_keys! if result.is_a? Hash
      result
    end
    def http_post_request(post_body)
      http    = Net::HTTP.new(@uri.host, @uri.port)
      request = Net::HTTP::Post.new(@uri.request_uri)
      request.basic_auth @uri.user, @uri.password
      request.content_type = 'application/json'
      request.body = post_body
      http.request(request).body
    rescue Errno::ECONNREFUSED => e
      raise ConnectionRefusedError
    end

    def safe_getbalance
      begin
        getbalance
      rescue
        'N/A'
      end
    end

some info was obfuscated for security reasons. (username/password)

The expected result is null, indicating no error. But instead I get the error:

Error on PaymentTransaction::Normal: undefined local variable or method `c' for #<Deposits::Mobitglobal:0x0055995b000b78>
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activemodel-4.0.12/lib/active_model/attribute_methods.rb:439:in `method_missing'
/home/deploy/peatio/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.12/lib/active_record/attribute_methods.rb:168:in `method_missing'

Any help or insight into where I could be going wrong would be greatly appreciated

dagnyc
  • 29
  • 4

0 Answers0