etheruby 1.0.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 384256862997b691646324f17aae5b7114b30919
4
- data.tar.gz: c5de6e19fff5ba0ec1564b3ae91e6e6d561a9b4e
3
+ metadata.gz: 63c64dface6667998a6f2cf763ddddb5d7237937
4
+ data.tar.gz: b8af106669bdea61b1ab7db1a1e074554fa2dd0c
5
5
  SHA512:
6
- metadata.gz: 073e28a69ee77267c32ee7db2c5edc44d87ed460913a09d3e47550a06ccd83197815328d722b0fb085dcc0c0bf70f299120182946c9145677e38c9b838d5e6c5
7
- data.tar.gz: 81605c560b0bc722580fac91aece06c72cbcab6726b82dcb783a9a2866803b53081fe78146976a768c733689fe7e056fe74954a570ebce9cdb38070867b2719a
6
+ metadata.gz: 09d980f7363f40097b0768bd7242b70fa4266d3952fc1735a8ff0381da7c410da75d5f665a9a1ff909eb9f90a304a4ede7358dc113d33559d31edbbbe301117f
7
+ data.tar.gz: 58bc4a1edff18866bec0c81895d23f164dc6b7ca9dcf88ae7f45a7bb3ad86d9f973db5ad6795d6c28b9afe19411d3565537f6df750c7b2aaa5c774a57c0b056b
@@ -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 contract
11
- Class.new do
12
- def self.inherited(subclass)
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
- def self.at_address(address)
24
- address = address[2..64].to_i(16) if address.is_a? ::String
25
- @@address = address
26
- end
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
- def self.method(name, &blk)
29
- cmd = ContractMethodDSL.new(name)
30
- cmd.instance_exec &blk if blk
31
- @@c_methods[name] = cmd.validate!
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
- def self.to_camel_case(sym)
36
- ar = sym.to_s.split("_").map{|i| i.capitalize}
37
- ar[0] = ar.first.downcase
38
- ar.join.to_sym
39
- end
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
- def self.method_missing(sym, *args)
42
- unless @@c_methods.include? sym
43
- sym = self.to_camel_case(sym)
44
- end
45
- raise NoContractMethodError.new (
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
- def self.execute_contract_method(method_info, args)
52
- arguments = ArgumentsGenerator.new(method_info[:params], args).to_s
53
- data = "#{method_info[:signature]}#{arguments}"
54
- composed_body = { to: self.address, data: data }
55
-
56
- [ :gas, :gasPrice, :value, :from ].each { |kw|
57
- composed_body[kw] = method_info[kw] if method_info.has_key? kw
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
- def self.call_api(composed_body)
83
- if composed_body.has_key? :gas or composed_body.has_key? :value
84
- return do_eth_transaction(composed_body)
85
- else
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
- def self.do_eth_call(composed_body)
91
- Client.eth.call(composed_body, Configuration.default_height || "latest")
92
- end
61
+ [ :gas, :gasPrice, :value, :from ].each { |kw|
62
+ composed_body[kw] = method_info[kw] if method_info.has_key? kw
63
+ }
93
64
 
94
- def self.do_eth_transaction(composed_body)
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
- def self.address
102
- address_fix(@@address)
103
- end
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
- def self.address_fix(addr)
106
- if addr.is_a? ::String
107
- addr
108
- else
109
- "0x#{addr.to_s(16).rjust(40, "0")}"
110
- end
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
- module_function :contract
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
@@ -25,11 +25,7 @@ module EtherMultipliable
25
25
  end
26
26
  end
27
27
 
28
- class Fixnum
29
- include EtherMultipliable
30
- end
31
-
32
- class Bignum
28
+ class Integer
33
29
  include EtherMultipliable
34
30
  end
35
31
 
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: 1.0.5
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-03-20 00:00:00.000000000 Z
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/MechanicalSloth/etheruby
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.3'
124
+ version: '2.4'
125
125
  required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  requirements:
127
127
  - - ">="