etheruby 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/etheruby.rb +1 -0
- data/lib/etheruby/contract.rb +2 -100
- data/lib/etheruby/contract_base.rb +124 -0
- data/lib/etheruby/contract_instance.rb +25 -0
- data/lib/etheruby/railtie.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03e991441d480f53c06da3f4ecec791df444fe14
|
4
|
+
data.tar.gz: 149da91a39786ce5c79e205dd835278810e57d8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8abbc10374fbc147f3ef1affc7b10bf5cb79c87cbb8ad47b3dd4b3918e00c6b2974ec21ada942c2bed82901276006e7b6a9668bafd114ffe9dc31cd502f013be
|
7
|
+
data.tar.gz: 7ccf898cbd557e98ca9b78df69b6e782f034f38b8548cb8e10b1bc05519b90652010f84f3740cab35edebfa9fe852bcb5a0885bd628b895c54168990072ea184
|
data/lib/etheruby.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'logger'
|
2
2
|
require_relative 'etheruby/client'
|
3
3
|
require_relative 'etheruby/contract'
|
4
|
+
require_relative 'etheruby/contract_instance'
|
4
5
|
require_relative 'etheruby/ether_multipliable'
|
5
6
|
require_relative 'etheruby/configuration'
|
6
7
|
require_relative 'etheruby/railtie' if defined?(Rails)
|
data/lib/etheruby/contract.rb
CHANGED
@@ -4,16 +4,10 @@ require_relative 'client'
|
|
4
4
|
require_relative 'contract_method_dsl'
|
5
5
|
require_relative 'arguments_generator'
|
6
6
|
require_relative 'response_parser'
|
7
|
+
require_relative 'contract_base'
|
7
8
|
|
8
9
|
module Etheruby::Contract
|
9
10
|
|
10
|
-
class NoContractMethodError < StandardError; end
|
11
|
-
class NoPasswordForAddress < StandardError; end
|
12
|
-
|
13
|
-
def contract_methods
|
14
|
-
@c_methods
|
15
|
-
end
|
16
|
-
|
17
11
|
def initialize
|
18
12
|
@c_methods = {}
|
19
13
|
@logger = ::Logger.new(STDOUT)
|
@@ -25,99 +19,6 @@ module Etheruby::Contract
|
|
25
19
|
@logger.progname = "Etheruby Contract '#{self.class.name}'"
|
26
20
|
end
|
27
21
|
|
28
|
-
def at_address(address)
|
29
|
-
address = address[2..64].to_i(16) if address.is_a? ::String
|
30
|
-
@address = address
|
31
|
-
end
|
32
|
-
|
33
|
-
def contract_method(name, &blk)
|
34
|
-
cmd = Etheruby::ContractMethodDSL.new(name)
|
35
|
-
cmd.instance_exec &blk if blk
|
36
|
-
@c_methods[name] = cmd.validate!
|
37
|
-
@logger.debug("Registred method #{name}")
|
38
|
-
end
|
39
|
-
|
40
|
-
def to_camel_case(sym)
|
41
|
-
ar = sym.to_s.split("_").map{|i| i.capitalize}
|
42
|
-
ar[0] = ar.first.downcase
|
43
|
-
ar.join.to_sym
|
44
|
-
end
|
45
|
-
|
46
|
-
def method_missing(sym, *args)
|
47
|
-
unless @c_methods.include? sym
|
48
|
-
sym = self.to_camel_case(sym)
|
49
|
-
end
|
50
|
-
raise NoContractMethodError.new (
|
51
|
-
"The method #{sym} does not exist in the #{self.class.to_s} contract."
|
52
|
-
) unless @c_methods.include? sym
|
53
|
-
execute_contract_method(@c_methods[sym], args)
|
54
|
-
end
|
55
|
-
|
56
|
-
def execute_contract_method(method_info, args)
|
57
|
-
arguments = Etheruby::ArgumentsGenerator.new(method_info[:params], args).to_s
|
58
|
-
data = "#{method_info[:signature]}#{arguments}"
|
59
|
-
composed_body = { to: self.address, data: data }
|
60
|
-
|
61
|
-
[ :gas, :gasPrice, :value, :from ].each { |kw|
|
62
|
-
composed_body[kw] = method_info[kw] if method_info.has_key? kw
|
63
|
-
}
|
64
|
-
|
65
|
-
@logger.debug("Calling #{method_info[:name]} with parameters #{composed_body.inspect}")
|
66
|
-
|
67
|
-
response = if method_info.has_key?(:force_transac) and method_info[:force_transac]
|
68
|
-
do_eth_transaction composed_body
|
69
|
-
else
|
70
|
-
call_api composed_body
|
71
|
-
end
|
72
|
-
|
73
|
-
if response.is_a? Hash and response.has_key? 'error'
|
74
|
-
@logger.error("Failed contract execution #{response['error']['message']}")
|
75
|
-
elsif response.is_a? Hash and response.has_key? 'result'
|
76
|
-
@logger.debug("Response from API for #{method_info[:name]} : #{response.inspect}")
|
77
|
-
if method_info.has_key? :returns
|
78
|
-
Etheruby::ResponseParser.new(method_info[:returns], response['result']).parse
|
79
|
-
else
|
80
|
-
response['result']
|
81
|
-
end
|
82
|
-
else
|
83
|
-
@logger.error("Failed contract execution response : #{response.inspect}")
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def call_api(composed_body)
|
88
|
-
if composed_body.has_key? :gas or composed_body.has_key? :value
|
89
|
-
return do_eth_transaction(composed_body)
|
90
|
-
else
|
91
|
-
return do_eth_call(composed_body)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def do_eth_call(composed_body)
|
96
|
-
Etheruby::Client.eth.call(
|
97
|
-
composed_body,
|
98
|
-
Etheruby::Configuration.default_height || "latest"
|
99
|
-
)
|
100
|
-
end
|
101
|
-
|
102
|
-
def do_eth_transaction(composed_body)
|
103
|
-
from_address = composed_body[:from] || Etheruby::Configuration.default_from
|
104
|
-
pwd = Etheruby::Configuration.addresses[from_address]
|
105
|
-
composed_body[:from] = address_fix(from_address)
|
106
|
-
Etheruby::Client.personal.sendTransaction(composed_body, pwd)
|
107
|
-
end
|
108
|
-
|
109
|
-
def address
|
110
|
-
address_fix(@address)
|
111
|
-
end
|
112
|
-
|
113
|
-
def address_fix(addr)
|
114
|
-
if addr.is_a? ::String
|
115
|
-
addr
|
116
|
-
else
|
117
|
-
"0x#{addr.to_s(16).rjust(40, "0")}"
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
22
|
module ClassMethods
|
122
23
|
def method_missing(sym, *args, &blk)
|
123
24
|
self.instance.send(sym, *args, &blk)
|
@@ -126,6 +27,7 @@ module Etheruby::Contract
|
|
126
27
|
|
127
28
|
def self.included(base)
|
128
29
|
base.include(::Singleton)
|
30
|
+
base.include(Etheruby::ContractBase)
|
129
31
|
base.extend(ClassMethods)
|
130
32
|
end
|
131
33
|
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
require_relative 'client'
|
4
|
+
require_relative 'contract_method_dsl'
|
5
|
+
require_relative 'arguments_generator'
|
6
|
+
require_relative 'response_parser'
|
7
|
+
|
8
|
+
module Etheruby::ContractBase
|
9
|
+
|
10
|
+
class NoContractMethodError < StandardError; end
|
11
|
+
class NoPasswordForAddress < StandardError; end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def contract_methods
|
15
|
+
@c_methods ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def contract_method(name, &blk)
|
19
|
+
cmd = Etheruby::ContractMethodDSL.new(name)
|
20
|
+
cmd.instance_exec &blk if blk
|
21
|
+
contract_methods[name] = cmd.validate!
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def at_address(address)
|
27
|
+
address = address[2..64].to_i(16) if address.is_a? ::String
|
28
|
+
@address = address
|
29
|
+
end
|
30
|
+
|
31
|
+
def contract_method(name, &blk)
|
32
|
+
cmd = Etheruby::ContractMethodDSL.new(name)
|
33
|
+
cmd.instance_exec &blk if blk
|
34
|
+
@c_methods[name] = cmd.validate!
|
35
|
+
@logger.debug("Registred method #{name}")
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_camel_case(sym)
|
39
|
+
ar = sym.to_s.split("_").map{|i| i.capitalize}
|
40
|
+
ar[0] = ar.first.downcase
|
41
|
+
ar.join.to_sym
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing(sym, *args)
|
45
|
+
unless self.class.contract_methods.include? sym
|
46
|
+
sym = self.to_camel_case(sym)
|
47
|
+
end
|
48
|
+
raise NoContractMethodError.new (
|
49
|
+
"The method #{sym} does not exist in the #{self.class.to_s} contract."
|
50
|
+
) unless self.class.contract_methods.include? sym
|
51
|
+
execute_contract_method(self.class.contract_methods[sym], args)
|
52
|
+
end
|
53
|
+
|
54
|
+
def execute_contract_method(method_info, args)
|
55
|
+
arguments = Etheruby::ArgumentsGenerator.new(method_info[:params], args).to_s
|
56
|
+
data = "#{method_info[:signature]}#{arguments}"
|
57
|
+
composed_body = { to: self.address, data: data }
|
58
|
+
|
59
|
+
[ :gas, :gasPrice, :value, :from ].each { |kw|
|
60
|
+
composed_body[kw] = method_info[kw] if method_info.has_key? kw
|
61
|
+
}
|
62
|
+
|
63
|
+
@logger.debug("Calling #{method_info[:name]} with parameters #{composed_body.inspect}")
|
64
|
+
|
65
|
+
response = if method_info.has_key?(:force_transac) and method_info[:force_transac]
|
66
|
+
do_eth_transaction composed_body
|
67
|
+
else
|
68
|
+
call_api composed_body
|
69
|
+
end
|
70
|
+
|
71
|
+
if response.is_a? Hash and response.has_key? 'error'
|
72
|
+
@logger.error("Failed contract execution #{response['error']['message']}")
|
73
|
+
elsif response.is_a? Hash and response.has_key? 'result'
|
74
|
+
@logger.debug("Response from API for #{method_info[:name]} : #{response.inspect}")
|
75
|
+
if method_info.has_key? :returns
|
76
|
+
Etheruby::ResponseParser.new(method_info[:returns], response['result']).parse
|
77
|
+
else
|
78
|
+
response['result']
|
79
|
+
end
|
80
|
+
else
|
81
|
+
@logger.error("Failed contract execution response : #{response.inspect}")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def call_api(composed_body)
|
86
|
+
if composed_body.has_key? :gas or composed_body.has_key? :value
|
87
|
+
return do_eth_transaction(composed_body)
|
88
|
+
else
|
89
|
+
return do_eth_call(composed_body)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def do_eth_call(composed_body)
|
94
|
+
Etheruby::Client.eth.call(
|
95
|
+
composed_body,
|
96
|
+
Etheruby::Configuration.default_height || "latest"
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
def do_eth_transaction(composed_body)
|
101
|
+
from_address = composed_body[:from] || Etheruby::Configuration.default_from
|
102
|
+
pwd = Etheruby::Configuration.addresses[from_address]
|
103
|
+
composed_body[:from] = address_fix(from_address)
|
104
|
+
Etheruby::Client.personal.sendTransaction(composed_body, pwd)
|
105
|
+
end
|
106
|
+
|
107
|
+
def address
|
108
|
+
address_fix(@address)
|
109
|
+
end
|
110
|
+
|
111
|
+
def address_fix(addr)
|
112
|
+
if addr.is_a? ::String
|
113
|
+
addr
|
114
|
+
else
|
115
|
+
"0x#{addr.to_s(16).rjust(40, "0")}"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.included(base)
|
120
|
+
base.extend(ClassMethods)
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'client'
|
2
|
+
require_relative 'contract_method_dsl'
|
3
|
+
require_relative 'arguments_generator'
|
4
|
+
require_relative 'response_parser'
|
5
|
+
require_relative 'contract_base'
|
6
|
+
|
7
|
+
module Etheruby::ContractInstance
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.include(Etheruby::ContractBase)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(address)
|
14
|
+
@address = at_address(address)
|
15
|
+
@logger = ::Logger.new(STDOUT)
|
16
|
+
@logger.level = if ENV.has_key? 'ETHERUBY_DEBUG'
|
17
|
+
::Logger::DEBUG
|
18
|
+
else
|
19
|
+
::Logger::WARN
|
20
|
+
end
|
21
|
+
@logger.progname = "Etheruby Contract '#{self.class.name}'"
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
end
|
data/lib/etheruby/railtie.rb
CHANGED
@@ -8,7 +8,7 @@ module Etheruby
|
|
8
8
|
# Loads configuration
|
9
9
|
initializer "etheruby.configure" do |app|
|
10
10
|
Etheruby::Configuration.conf = if File.exist? './config/etheruby.yml'
|
11
|
-
YAML.
|
11
|
+
YAML.load(ERB.new(File.read("./config/etheruby.yml")).result).with_indifferent_access
|
12
12
|
else
|
13
13
|
{}
|
14
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: etheruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jérémy SEBAN
|
@@ -92,6 +92,8 @@ files:
|
|
92
92
|
- lib/etheruby/client.rb
|
93
93
|
- lib/etheruby/configuration.rb
|
94
94
|
- lib/etheruby/contract.rb
|
95
|
+
- lib/etheruby/contract_base.rb
|
96
|
+
- lib/etheruby/contract_instance.rb
|
95
97
|
- lib/etheruby/contract_method_dsl.rb
|
96
98
|
- lib/etheruby/encoders/address.rb
|
97
99
|
- lib/etheruby/encoders/arrays.rb
|