ethlite 0.2.5 → 0.3.1
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/Manifest.txt +2 -2
- data/README.md +11 -10
- data/lib/ethlite/abi/{abi_coder.rb → codec.rb} +20 -10
- data/lib/ethlite/abi/type.rb +2 -2
- data/lib/ethlite/constant.rb +0 -3
- data/lib/ethlite/contract.rb +10 -13
- data/lib/ethlite/{abi/utils.rb → utils.rb} +10 -13
- data/lib/ethlite/version.rb +2 -2
- data/lib/ethlite.rb +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a91770dc04d591bf88353b4783080812f1659a7f679f09636b656a1a8d9ac7b7
|
4
|
+
data.tar.gz: 626dda3168008abf7743526be783722292af40633c4acbe1bea4ea649f77a602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31b7dce9e0b71c4ea56697272d731541ecd58fdbfac6d5a3ddc949f7d47263340fda578d37403f788ecc02b7b08152d75107fd856f4af54b256c1cbb52b55ab2
|
7
|
+
data.tar.gz: 852410528fc380e40cff8fd4eafd49a2697947d2b2a4ab73b6210c88c87c45ecfdd2c44c296dd473cb7a5e5c7d175625d774e3fb6f88a45da7f3356af2b21ea6
|
data/Manifest.txt
CHANGED
@@ -5,10 +5,10 @@ Rakefile
|
|
5
5
|
lib/digest/keccak.rb
|
6
6
|
lib/digest/sha3.rb
|
7
7
|
lib/ethlite.rb
|
8
|
-
lib/ethlite/abi/
|
8
|
+
lib/ethlite/abi/codec.rb
|
9
9
|
lib/ethlite/abi/type.rb
|
10
|
-
lib/ethlite/abi/utils.rb
|
11
10
|
lib/ethlite/constant.rb
|
12
11
|
lib/ethlite/contract.rb
|
12
|
+
lib/ethlite/utils.rb
|
13
13
|
lib/ethlite/version.rb
|
14
14
|
lib/jsonrpc/jsonrpc.rb
|
data/README.md
CHANGED
@@ -44,7 +44,7 @@ token_ids = (0..9)
|
|
44
44
|
token_ids.each do |token_id|
|
45
45
|
|
46
46
|
puts "==> calling tokenURI(#{token_id})..."
|
47
|
-
tokenURI = eth_call( contract_address,
|
47
|
+
tokenURI = eth_call( ETH_NODE, contract_address,
|
48
48
|
'tokenURI', ['uint256'], ['string'],
|
49
49
|
[token_id] )
|
50
50
|
puts " ...returns: #{tokenURI}"
|
@@ -56,14 +56,15 @@ And the "magic" hand-coded to-the-metal `eth_call` machinery:
|
|
56
56
|
|
57
57
|
```ruby
|
58
58
|
## construct a json-rpc call from scratch
|
59
|
-
def eth_call(
|
59
|
+
def eth_call( rpc,
|
60
|
+
contract_address,
|
60
61
|
name, inputs, outputs,
|
61
62
|
args )
|
62
63
|
|
63
64
|
## binary encode method sig(nature)
|
64
65
|
signature = "#{name}(#{inputs.join(',')})"
|
65
|
-
signature_hash = Ethlite::
|
66
|
-
Ethlite::
|
66
|
+
signature_hash = Ethlite::Utils.encode_hex(
|
67
|
+
Ethlite::Utils.keccak256(signature))[0..7]
|
67
68
|
|
68
69
|
pp signature
|
69
70
|
# => "tokenURI(uint256)"
|
@@ -71,8 +72,8 @@ def eth_call( contract_address,
|
|
71
72
|
# => "c87b56dd"
|
72
73
|
|
73
74
|
## binary encode method arg(ument)s
|
74
|
-
args_encoded = Ethlite::
|
75
|
-
Ethlite::Abi
|
75
|
+
args_encoded = Ethlite::Utils.encode_hex(
|
76
|
+
Ethlite::Abi.encode_abi( inputs, args) )
|
76
77
|
|
77
78
|
data = '0x' + signature_hash + args_encoded
|
78
79
|
|
@@ -84,15 +85,15 @@ def eth_call( contract_address,
|
|
84
85
|
]
|
85
86
|
|
86
87
|
## do the json-rpc request
|
87
|
-
response =
|
88
|
+
response = rpc.request( method, params )
|
88
89
|
|
89
90
|
puts "response:"
|
90
91
|
pp response
|
91
92
|
|
92
93
|
## decode binary result
|
93
|
-
string_data = Ethlite::
|
94
|
-
Ethlite::
|
95
|
-
result = Ethlite::Abi
|
94
|
+
string_data = Ethlite::Utils.decode_hex(
|
95
|
+
Ethlite::Utils.remove_0x_head(response))
|
96
|
+
result = Ethlite::Abi.decode_abi( outputs, string_data )
|
96
97
|
result.length == 1 ? result[0] : result
|
97
98
|
end
|
98
99
|
```
|
@@ -8,13 +8,7 @@ module Ethlite
|
|
8
8
|
#
|
9
9
|
# @see https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
|
10
10
|
#
|
11
|
-
|
12
|
-
|
13
|
-
extend self
|
14
|
-
|
15
|
-
include Constant
|
16
|
-
|
17
|
-
|
11
|
+
class Codec
|
18
12
|
class EncodingError < StandardError; end
|
19
13
|
class DecodingError < StandardError; end
|
20
14
|
class ValueError < StandardError; end
|
@@ -43,7 +37,6 @@ module Ethlite
|
|
43
37
|
|
44
38
|
"#{head}#{tail}"
|
45
39
|
end
|
46
|
-
alias :encode :encode_abi
|
47
40
|
|
48
41
|
##
|
49
42
|
# Encodes a single value (static or dynamic).
|
@@ -263,7 +256,7 @@ module Ethlite
|
|
263
256
|
|
264
257
|
parsed_types.zip(outputs).map {|(type, out)| decode_type(type, out) }
|
265
258
|
end
|
266
|
-
|
259
|
+
|
267
260
|
|
268
261
|
def zero_padding data, pos, count, start_positions
|
269
262
|
if pos >= data.size
|
@@ -416,8 +409,25 @@ module Ethlite
|
|
416
409
|
raise EncodingError, "Cannot decode int: #{n}"
|
417
410
|
end
|
418
411
|
end
|
412
|
+
end # class Codec
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
def self.codec
|
420
|
+
@codec ||= Codec.new
|
421
|
+
end
|
422
|
+
|
423
|
+
def self.encode_abi(types, args)
|
424
|
+
codec.encode_abi( types, args )
|
425
|
+
end
|
426
|
+
|
427
|
+
def self.decode_abi(types, data, raise_errors = false)
|
428
|
+
codec.decode_abi( types, data, raise_errors )
|
429
|
+
end
|
419
430
|
|
420
|
-
end
|
421
431
|
|
422
432
|
end # module Abi
|
423
433
|
end # module Ethlite
|
data/lib/ethlite/abi/type.rb
CHANGED
@@ -117,8 +117,8 @@ module Ethlite
|
|
117
117
|
def subtype
|
118
118
|
@subtype ||= self.class.new(base, sub, dims[0...-1])
|
119
119
|
end
|
120
|
+
end # class Type
|
120
121
|
|
121
|
-
end
|
122
122
|
|
123
123
|
class Tuple < Type
|
124
124
|
|
@@ -193,7 +193,7 @@ module Ethlite
|
|
193
193
|
def subtype
|
194
194
|
@subtype ||= Tuple.new(types, dims[0...-1])
|
195
195
|
end
|
196
|
-
end
|
196
|
+
end # class Tuple
|
197
197
|
|
198
198
|
|
199
199
|
end # module Abi
|
data/lib/ethlite/constant.rb
CHANGED
data/lib/ethlite/contract.rb
CHANGED
@@ -9,12 +9,10 @@
|
|
9
9
|
constant = !!abi['constant'] || abi['stateMutability']=='view'
|
10
10
|
input_types = abi['inputs'] ? abi['inputs'].map{|a| _parse_component_type( a ) } : []
|
11
11
|
output_types = abi['outputs'] ? abi['outputs'].map{|a| _parse_component_type( a ) } : []
|
12
|
-
type = abi['type'] || 'function'
|
13
12
|
|
14
13
|
new( name, inputs: input_types,
|
15
14
|
outputs: output_types,
|
16
|
-
constant: constant
|
17
|
-
type: type )
|
15
|
+
constant: constant )
|
18
16
|
end
|
19
17
|
|
20
18
|
def self._parse_component_type( argument )
|
@@ -38,20 +36,19 @@
|
|
38
36
|
|
39
37
|
def initialize( name, inputs:,
|
40
38
|
outputs: [],
|
41
|
-
constant: true
|
42
|
-
type: 'function' )
|
39
|
+
constant: true )
|
43
40
|
@name = name
|
44
41
|
@constant = constant
|
45
42
|
@input_types = inputs
|
46
43
|
@output_types = outputs
|
47
|
-
|
48
|
-
@
|
49
|
-
@signature_hash = Abi::Utils.signature_hash( @signature, type=='event' ? 64 : 8)
|
44
|
+
@signature = "#{@name}(#{@input_types.join(',')})"
|
45
|
+
@signature_hash = Utils.signature_hash( @signature, 8 )
|
50
46
|
end
|
51
47
|
|
48
|
+
|
52
49
|
def do_call( rpc, contract_address, args )
|
53
|
-
data = '0x' + @signature_hash +
|
54
|
-
Abi
|
50
|
+
data = '0x' + @signature_hash + Utils.encode_hex(
|
51
|
+
Abi.encode_abi(@input_types, args) )
|
55
52
|
|
56
53
|
method = 'eth_call'
|
57
54
|
params = [{ to: contract_address,
|
@@ -63,11 +60,11 @@
|
|
63
60
|
puts "response:"
|
64
61
|
pp response
|
65
62
|
|
66
|
-
string_data =
|
67
|
-
|
63
|
+
string_data = Utils.decode_hex(
|
64
|
+
Utils.remove_0x_head(response))
|
68
65
|
return nil if string_data.empty?
|
69
66
|
|
70
|
-
result = Abi
|
67
|
+
result = Abi.decode_abi( @output_types, string_data )
|
71
68
|
puts
|
72
69
|
puts "result decode_abi:"
|
73
70
|
pp result
|
@@ -1,11 +1,7 @@
|
|
1
1
|
module Ethlite
|
2
|
-
module Abi
|
3
|
-
module Utils
|
4
2
|
|
5
|
-
extend self
|
6
|
-
|
7
|
-
include Constant
|
8
3
|
|
4
|
+
module UtilHelper
|
9
5
|
##
|
10
6
|
# Not the keccak in sha3, although it's underlying lib named SHA3
|
11
7
|
#
|
@@ -214,16 +210,17 @@ module Abi
|
|
214
210
|
end
|
215
211
|
end
|
216
212
|
|
217
|
-
|
218
|
-
|
219
|
-
"#{method_name}(#{arg_types.join(',')})"
|
213
|
+
def signature_hash( signature, length=8 )
|
214
|
+
encode_hex( keccak256(signature) )[0...length]
|
220
215
|
end
|
221
216
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
217
|
+
end # module UtilHelper
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
module Utils
|
222
|
+
extend UtilHelper
|
223
|
+
end
|
226
224
|
|
227
|
-
end # module Abi
|
228
225
|
end # module Ethlite
|
229
226
|
|
data/lib/ethlite/version.rb
CHANGED
data/lib/ethlite.rb
CHANGED
@@ -24,11 +24,11 @@ require_relative 'jsonrpc/jsonrpc'
|
|
24
24
|
require_relative 'ethlite/version' # note: let version always go first
|
25
25
|
|
26
26
|
require_relative 'ethlite/constant'
|
27
|
+
require_relative 'ethlite/utils'
|
27
28
|
|
28
29
|
|
29
30
|
require_relative 'ethlite/abi/type'
|
30
|
-
require_relative 'ethlite/abi/
|
31
|
-
require_relative 'ethlite/abi/abi_coder'
|
31
|
+
require_relative 'ethlite/abi/codec'
|
32
32
|
|
33
33
|
|
34
34
|
require_relative 'ethlite/contract'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ethlite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cocos
|
@@ -89,11 +89,11 @@ files:
|
|
89
89
|
- lib/digest/keccak.rb
|
90
90
|
- lib/digest/sha3.rb
|
91
91
|
- lib/ethlite.rb
|
92
|
-
- lib/ethlite/abi/
|
92
|
+
- lib/ethlite/abi/codec.rb
|
93
93
|
- lib/ethlite/abi/type.rb
|
94
|
-
- lib/ethlite/abi/utils.rb
|
95
94
|
- lib/ethlite/constant.rb
|
96
95
|
- lib/ethlite/contract.rb
|
96
|
+
- lib/ethlite/utils.rb
|
97
97
|
- lib/ethlite/version.rb
|
98
98
|
- lib/jsonrpc/jsonrpc.rb
|
99
99
|
homepage: https://github.com/pixelartexchange/artbase
|