Module: Etheruby::ContractBase

Defined in:
lib/etheruby/contract_base.rb

Defined Under Namespace

Modules: ClassMethods Classes: NoContractMethodError, NoPasswordForAddress

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/etheruby/contract_base.rb', line 44

def method_missing(sym, *args)
  unless self.class.contract_methods.include? sym
    sym = self.to_camel_case(sym)
  end
  raise NoContractMethodError.new (
                                    "The method #{sym} does not exist in the #{self.class.to_s} contract."
                                  ) unless self.class.contract_methods.include? sym
  execute_contract_method(self.class.contract_methods[sym], args)
end

Class Method Details

.included(base) ⇒ Object



119
120
121
# File 'lib/etheruby/contract_base.rb', line 119

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#addressObject



107
108
109
# File 'lib/etheruby/contract_base.rb', line 107

def address
  address_fix(@address)
end

#address_fix(addr) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/etheruby/contract_base.rb', line 111

def address_fix(addr)
  if addr.is_a? ::String
    addr
  else
    "0x#{addr.to_s(16).rjust(40, "0")}"
  end
end

#at_address(address) ⇒ Object



26
27
28
29
# File 'lib/etheruby/contract_base.rb', line 26

def at_address(address)
  address = address[2..64].to_i(16) if address.is_a? ::String
  @address = address
end

#call_api(composed_body) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/etheruby/contract_base.rb', line 85

def call_api(composed_body)
  if composed_body.has_key? :gas or composed_body.has_key? :value
    return do_eth_transaction(composed_body)
  else
    return do_eth_call(composed_body)
  end
end

#contract_method(name, &blk) ⇒ Object



31
32
33
34
35
36
# File 'lib/etheruby/contract_base.rb', line 31

def contract_method(name, &blk)
  cmd = Etheruby::ContractMethodDSL.new(name)
  cmd.instance_exec &blk if blk
  @c_methods[name] = cmd.validate!
  @logger.debug("Registred method #{name}")
end

#do_eth_call(composed_body) ⇒ Object



93
94
95
96
97
98
# File 'lib/etheruby/contract_base.rb', line 93

def do_eth_call(composed_body)
  Etheruby::Client.eth.call(
    composed_body,
    Etheruby::Configuration.default_height || "latest"
  )
end

#do_eth_transaction(composed_body) ⇒ Object



100
101
102
103
104
105
# File 'lib/etheruby/contract_base.rb', line 100

def do_eth_transaction(composed_body)
  from_address = composed_body[:from] || Etheruby::Configuration.default_from
  pwd = Etheruby::Configuration.addresses[from_address]
  composed_body[:from] = address_fix(from_address)
  Etheruby::Client.personal.sendTransaction(composed_body, pwd)
end

#execute_contract_method(method_info, args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/etheruby/contract_base.rb', line 54

def execute_contract_method(method_info, args)
  arguments = Etheruby::ArgumentsGenerator.new(method_info[:params], args).to_s
  data = "#{method_info[:signature]}#{arguments}"
  composed_body = { to: self.address, data: data }

  [ :gas, :gasPrice, :value, :from ].each { |kw|
    composed_body[kw] = method_info[kw] if method_info.has_key? kw
  }

  @logger.debug("Calling #{method_info[:name]} with parameters #{composed_body.inspect}")

  response = if method_info.has_key?(:force_transac) and method_info[:force_transac]
    do_eth_transaction composed_body
  else
    call_api composed_body
  end

  if response.is_a? Hash and response.has_key? 'error'
    @logger.error("Failed contract execution #{response['error']['message']}")
  elsif response.is_a? Hash and response.has_key? 'result'
    @logger.debug("Response from API for #{method_info[:name]} : #{response.inspect}")
    if method_info.has_key? :returns
      Etheruby::ResponseParser.new(method_info[:returns], response['result']).parse
    else
      response['result']
    end
  else
    @logger.error("Failed contract execution response : #{response.inspect}")
  end
end

#to_camel_case(sym) ⇒ Object



38
39
40
41
42
# File 'lib/etheruby/contract_base.rb', line 38

def to_camel_case(sym)
  ar = sym.to_s.split("_").map{|i| i.capitalize}
  ar[0] = ar.first.downcase
  ar.join.to_sym
end