ethereum.rb 2.0.0 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4bfce795e5c0ba5c86e8731f2f7c04504b53000
4
- data.tar.gz: 52feb089966aabb77b7a1002d5a8aff8433049dd
3
+ metadata.gz: 118a182e1fe6df35ead4dd24f452ab8dbe163f1b
4
+ data.tar.gz: 95b4a183b908e5c835f95af4772069603c08fb38
5
5
  SHA512:
6
- metadata.gz: 40ff1fe3ac72b9f4676eb5350c1ada0e31ebcf9778eeab3b5d80036f5d4a8c0cc854d3988f4a11d044788ffadee6482ca5d0bf2e7c31e600521de045f9d93902
7
- data.tar.gz: 69eb7f4a2a31e04a883d0b266211fd13e411790afc9105e884494464e508db60fd4363e9f970aef9afb26b153501e1f35c89e4bf8dc456951c5f2da569d31b6b
6
+ metadata.gz: b6353836a9589c7517beacb7bb29c402c08cc942e774954653eaf4f69d4202002f4cd67edc8c458cd29a4479bf4528fcb38b326ce16bb9b3838bfeab739a616f
7
+ data.tar.gz: 1bcf61ed8af8111c10045e3a17e07011a8892f5db6d523d9a9863fc95078eee9856a398b82b3a6d1ec840d4ba22ea4af30a3f30ed4355e7ad75b0bfd30ca7d9a
data/lib/ethereum/abi.rb CHANGED
@@ -2,7 +2,12 @@ module Ethereum
2
2
  class Abi
3
3
 
4
4
  def self.parse_abi(abi)
5
- constructor_inputs = abi.detect {|x| x["type"] == "constructor"}["inputs"] rescue nil
5
+ constructor = abi.detect {|x| x["type"] == "constructor"}
6
+ if constructor.present?
7
+ constructor_inputs = constructor["inputs"].map { |input| Ethereum::FunctionInput.new(input) }
8
+ else
9
+ constructor_inputs = []
10
+ end
6
11
  functions = abi.select {|x| x["type"] == "function" }.map { |fun| Ethereum::Function.new(fun) }
7
12
  events = abi.select {|x| x["type"] == "event" }.map { |evt| Ethereum::ContractEvent.new(evt) }
8
13
  [constructor_inputs, functions, events]
@@ -48,7 +48,7 @@ module Ethereum
48
48
  if @constructor_inputs.present?
49
49
  raise "Missing constructor parameter" and return if params.length != @constructor_inputs.length
50
50
  end
51
- deploy_arguments = @formatter.construtor_params_to_payload(@constructor_inputs, params)
51
+ deploy_arguments = @encoder.encode_arguments(@constructor_inputs, params)
52
52
  payload = "0x" + @code + deploy_arguments
53
53
  tx = @client.eth_send_transaction({from: sender, data: payload})["result"]
54
54
  raise "Failed to deploy, did you unlock #{sender} account? Transaction hash: #{deploytx}" if tx.nil? || tx == "0x0000000000000000000000000000000000000000000000000000000000000000"
@@ -69,10 +69,7 @@ module Ethereum
69
69
  deploy_arguments = ""
70
70
  if @constructor_inputs.present?
71
71
  raise "Wrong number of arguments in a constructor" and return if params.length != @constructor_inputs.length
72
- @constructor_inputs.each_index do |i|
73
- args = [@constructor_inputs[i]["type"], params[i]]
74
- deploy_arguments << @formatter.to_payload(args)
75
- end
72
+ deploy_arguments = @encoder.encode_arguments(@constructor_inputs, params)
76
73
  end
77
74
  deploy_payload = @code + deploy_arguments
78
75
  result = @client.eth_estimate_gas({from: @sender, data: "0x" + deploy_payload})
@@ -38,17 +38,17 @@ module Ethereum
38
38
  end
39
39
 
40
40
  def encode_dynamic_bytes(value)
41
- location = encode_uint(32)
41
+ location = encode_uint(@inputs ? @inputs.size * 32 : 32)
42
42
  size = encode_uint(value.size)
43
43
  content = encode_bytes(value)
44
- location + size + content
44
+ [location, size + content]
45
45
  end
46
46
 
47
47
  def encode_string(value)
48
- location = encode_uint(32)
48
+ location = encode_uint(@inputs ? @inputs.size * 32 : 32)
49
49
  size = encode_uint(value.bytes.size)
50
50
  content = value.bytes.map {|x| x.to_s(16)}.join("").ljust(64, '0')
51
- location + size + content
51
+ [location, size + content]
52
52
  end
53
53
 
54
54
  def encode_address(value)
@@ -61,6 +61,22 @@ module Ethereum
61
61
  value.start_with?("0x") ? value : ("0x" + value)
62
62
  end
63
63
 
64
+ def encode_arguments(inputs, args)
65
+ @head = ""
66
+ @tail = ""
67
+ @inputs = inputs
68
+ inputs.each.with_index do |input, index|
69
+ encoded = encode(input.type, args[index])
70
+ if encoded.is_a? Array
71
+ @head << encoded[0]
72
+ @tail << encoded[1]
73
+ else
74
+ @head << encoded
75
+ end
76
+ end
77
+ @head + @tail
78
+ end
79
+
64
80
  private
65
81
  def to_twos_complement(number)
66
82
  (number & ((1 << 256) - 1))
@@ -6,7 +6,7 @@ module Ethereum
6
6
  def initialize(data)
7
7
  @name = data["name"]
8
8
  @constant = data["constant"]
9
- @inputs = data["inputs"].collect do |input|
9
+ @inputs = data["inputs"].map do |input|
10
10
  Ethereum::FunctionInput.new(input)
11
11
  end
12
12
  @outputs = data["outputs"].collect do |output|
@@ -1,3 +1,3 @@
1
1
  module Ethereum
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ethereum.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Kirejczyk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-28 00:00:00.000000000 Z
11
+ date: 2017-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler