etheruby 1.0.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/etheruby/contract.rb +108 -94
- data/lib/etheruby/ether_multipliable.rb +1 -5
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63c64dface6667998a6f2cf763ddddb5d7237937
|
4
|
+
data.tar.gz: b8af106669bdea61b1ab7db1a1e074554fa2dd0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09d980f7363f40097b0768bd7242b70fa4266d3952fc1735a8ff0381da7c410da75d5f665a9a1ff909eb9f90a304a4ede7358dc113d33559d31edbbbe301117f
|
7
|
+
data.tar.gz: 58bc4a1edff18866bec0c81895d23f164dc6b7ca9dcf88ae7f45a7bb3ad86d9f973db5ad6795d6c28b9afe19411d3565537f6df750c7b2aaa5c774a57c0b056b
|
data/lib/etheruby/contract.rb
CHANGED
@@ -1,118 +1,132 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
1
3
|
require_relative 'client'
|
2
4
|
require_relative 'contract_method_dsl'
|
3
5
|
require_relative 'arguments_generator'
|
4
6
|
require_relative 'response_parser'
|
5
7
|
|
6
|
-
module Etheruby
|
8
|
+
module Etheruby::Contract
|
9
|
+
|
7
10
|
class NoContractMethodError < StandardError; end
|
8
11
|
class NoPasswordForAddress < StandardError; end
|
9
12
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
@@c_methods = {}
|
14
|
-
@@logger = ::Logger.new(STDOUT)
|
15
|
-
@@logger.level = if ENV.has_key? 'ETHERUBY_DEBUG'
|
16
|
-
::Logger::DEBUG
|
17
|
-
else
|
18
|
-
::Logger::WARN
|
19
|
-
end
|
20
|
-
@@logger.progname = "Etheruby Contract '#{subclass.name}'"
|
21
|
-
end
|
13
|
+
def contract_methods
|
14
|
+
@c_methods
|
15
|
+
end
|
22
16
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
def initialize
|
18
|
+
@c_methods = {}
|
19
|
+
@logger = ::Logger.new(STDOUT)
|
20
|
+
@logger.level = if ENV.has_key? 'ETHERUBY_DEBUG'
|
21
|
+
::Logger::DEBUG
|
22
|
+
else
|
23
|
+
::Logger::WARN
|
24
|
+
end
|
25
|
+
@logger.progname = "Etheruby Contract '#{self.class.name}'"
|
26
|
+
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
@@logger.debug("Registred method #{name}")
|
33
|
-
end
|
28
|
+
def at_address(address)
|
29
|
+
address = address[2..64].to_i(16) if address.is_a? ::String
|
30
|
+
@address = address
|
31
|
+
end
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
"The method #{sym} does not exist in the #{self.class.to_s} contract."
|
47
|
-
) unless @@c_methods.include? sym
|
48
|
-
execute_contract_method(@@c_methods[sym], args)
|
49
|
-
end
|
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
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
@@logger.debug("Calling #{method_info[:name]} with parameters #{composed_body.inspect}")
|
61
|
-
|
62
|
-
response = if method_info.has_key?(:force_transac) and method_info[:force_transac]
|
63
|
-
do_eth_transaction composed_body
|
64
|
-
else
|
65
|
-
call_api composed_body
|
66
|
-
end
|
67
|
-
|
68
|
-
if response.is_a? Hash and response.has_key? 'error'
|
69
|
-
@@logger.error("Failed contract execution #{response['error']['message']}")
|
70
|
-
elsif response.is_a? Hash and response.has_key? 'result'
|
71
|
-
@@logger.debug("Response from API for #{method_info[:name]} : #{response.inspect}")
|
72
|
-
if method_info.has_key? :returns
|
73
|
-
ResponseParser.new(method_info[:returns], response['result']).parse
|
74
|
-
else
|
75
|
-
response['result']
|
76
|
-
end
|
77
|
-
else
|
78
|
-
@@logger.error("Failed contract execution response : #{response.inspect}")
|
79
|
-
end
|
80
|
-
end
|
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
|
81
55
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
return do_eth_call(composed_body)
|
87
|
-
end
|
88
|
-
end
|
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 }
|
89
60
|
|
90
|
-
|
91
|
-
|
92
|
-
|
61
|
+
[ :gas, :gasPrice, :value, :from ].each { |kw|
|
62
|
+
composed_body[kw] = method_info[kw] if method_info.has_key? kw
|
63
|
+
}
|
93
64
|
|
94
|
-
|
95
|
-
from_address = composed_body[:from] || Configuration.default_from
|
96
|
-
pwd = Configuration.addresses[from_address]
|
97
|
-
composed_body[:from] = address_fix(from_address)
|
98
|
-
Client.personal.sendTransaction(composed_body, pwd)
|
99
|
-
end
|
65
|
+
@logger.debug("Calling #{method_info[:name]} with parameters #{composed_body.inspect}")
|
100
66
|
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
104
72
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
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']
|
111
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")}"
|
112
118
|
end
|
113
119
|
end
|
114
120
|
|
115
|
-
|
121
|
+
module ClassMethods
|
122
|
+
def method_missing(sym, *args, &blk)
|
123
|
+
self.instance.send(sym, *args, &blk)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.included(base)
|
128
|
+
base.include(::Singleton)
|
129
|
+
base.extend(ClassMethods)
|
130
|
+
end
|
116
131
|
|
117
|
-
Contract = Etheruby::contract
|
118
132
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: etheruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jérémy SEBAN
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -109,7 +109,7 @@ files:
|
|
109
109
|
- lib/etheruby/type_matchers.rb
|
110
110
|
- tasks/etheruby/etheruby.yml
|
111
111
|
- tasks/etheruby/setup.task
|
112
|
-
homepage: https://github.com/
|
112
|
+
homepage: https://github.com/FranceChain-Solutions/etheruby
|
113
113
|
licenses:
|
114
114
|
- MIT
|
115
115
|
metadata: {}
|
@@ -121,7 +121,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
121
121
|
requirements:
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '2.
|
124
|
+
version: '2.4'
|
125
125
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
126
|
requirements:
|
127
127
|
- - ">="
|