etherlite 0.1.4 → 0.1.5

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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +92 -7
  3. data/etherlite.gemspec +2 -0
  4. data/lib/etherlite.rb +12 -7
  5. data/lib/etherlite/abi.rb +44 -4
  6. data/lib/etherlite/account/anonymous.rb +9 -0
  7. data/lib/etherlite/account/base.rb +56 -0
  8. data/lib/etherlite/account/local.rb +40 -0
  9. data/lib/etherlite/account/private_key.rb +51 -0
  10. data/lib/etherlite/api/node.rb +15 -12
  11. data/lib/etherlite/api/rpc.rb +55 -0
  12. data/lib/etherlite/commands/abi/load_contract.rb +24 -10
  13. data/lib/etherlite/commands/abi/load_function.rb +5 -5
  14. data/lib/etherlite/commands/abi/load_type.rb +1 -1
  15. data/lib/etherlite/commands/contract/event_base/decode_log_inputs.rb +30 -10
  16. data/lib/etherlite/configuration.rb +2 -1
  17. data/lib/etherlite/connection.rb +8 -4
  18. data/lib/etherlite/contract/base.rb +53 -1
  19. data/lib/etherlite/contract/function.rb +18 -14
  20. data/lib/etherlite/errors.rb +27 -0
  21. data/lib/etherlite/nonce_manager.rb +13 -14
  22. data/lib/etherlite/support/array.rb +43 -0
  23. data/lib/etherlite/transaction.rb +33 -1
  24. data/lib/etherlite/types/array_dynamic.rb +19 -5
  25. data/lib/etherlite/types/array_fixed.rb +19 -10
  26. data/lib/etherlite/types/{bool.rb → boolean.rb} +1 -1
  27. data/lib/etherlite/types/byte_string.rb +6 -0
  28. data/lib/etherlite/types/string.rb +6 -1
  29. data/lib/etherlite/utils.rb +4 -2
  30. data/lib/etherlite/version.rb +1 -1
  31. data/lib/generators/etherlite/templates/etherlite.yml +1 -0
  32. metadata +38 -8
  33. data/lib/etherlite/account.rb +0 -85
  34. data/lib/etherlite/commands/base.rb +0 -0
  35. data/lib/etherlite/commands/contract/function/encode_arguments.rb +0 -32
  36. data/lib/etherlite/pk_account.rb +0 -82
  37. data/lib/etherlite/types/array_base.rb +0 -36
@@ -1,82 +0,0 @@
1
- require 'eth'
2
-
3
- module Etherlite
4
- class PkAccount
5
- include Etherlite::Api::Address
6
-
7
- attr_reader :connection
8
-
9
- def initialize(_connection, _pk)
10
- @connection = _connection
11
- @key = Eth::Key.new priv: _pk
12
- end
13
-
14
- def normalized_address
15
- @normalized_address ||= Utils.normalize_address @key.address
16
- end
17
-
18
- def transfer_to(_target, _options = {})
19
- send_transaction(
20
- _options.fetch(:amount, 0), '', Utils.encode_address_param(_target), _options
21
- )
22
- end
23
-
24
- def call(_target, _function, *_params)
25
- _function = Utils.parse_function(_function) unless _function.is_a? Contract::Function
26
- options = _params.last.is_a?(Hash) ? _params.pop : {}
27
-
28
- if _function.constant?
29
- call_constant _target, _function, _params, options
30
- else
31
- value = options.fetch(:pay, 0)
32
- raise 'function is not payable' if value > 0 && !_function.payable?
33
-
34
- send_transaction(
35
- value, _function.encode(_params), Utils.encode_address_param(_target), options
36
- )
37
- end
38
- end
39
-
40
- private
41
-
42
- def call_constant(_target, _function, _params, _options)
43
- ipc_params = {
44
- from: json_encoded_address,
45
- to: Utils.encode_address_param(_target),
46
- data: _function.encode(_params)
47
- }
48
-
49
- block = Utils.encode_block_param _options.fetch(:block, :latest)
50
-
51
- _function.decode @connection, @connection.ipc_call(:eth_call, ipc_params, block)
52
- end
53
-
54
- def send_transaction(_value, _hex_data, _hex_address, _opt)
55
- tx_hash = nonce_manager.with_next_nonce_for(@key.address) do |nonce|
56
- tx = Eth::Tx.new(
57
- value: _value,
58
- data: _hex_data,
59
- gas_limit: _opt.fetch(:gas, 90_000),
60
- gas_price: _opt.fetch(:gas_price, gas_price),
61
- to: _hex_address,
62
- nonce: nonce
63
- )
64
-
65
- tx.sign @key
66
-
67
- @connection.ipc_call(:eth_sendRawTransaction, tx.hex)
68
- end
69
-
70
- Transaction.new @connection, tx_hash
71
- end
72
-
73
- def gas_price
74
- # TODO: improve on this
75
- @gas_price ||= Etherlite::Utils.hex_to_uint @connection.ipc_call(:eth_gasPrice)
76
- end
77
-
78
- def nonce_manager
79
- NonceManager.new @connection
80
- end
81
- end
82
- end
@@ -1,36 +0,0 @@
1
- module Etherlite::Types
2
- class ArrayBase < Base
3
- attr_reader :subtype
4
-
5
- def initialize(_subtype)
6
- raise ArgumentError, 'An array can not contain a dynamic type' if _subtype.dynamic?
7
- @subtype = _subtype
8
- end
9
-
10
- private
11
-
12
- def encode_values(_values)
13
- if @subtype.dynamic?
14
- encode_dynamic _values
15
- else
16
- _values.map { |i| @subtype.encode(i) }.join
17
- end
18
- end
19
-
20
- def encode_dynamic(_values)
21
- offset = 32 * @size # initial header offset
22
- header = []
23
- tail = []
24
-
25
- _values.each do |item|
26
- encoded_item = @subtype.encode(item)
27
-
28
- header << Etherlite::Utils.uint_to_hex(offset)
29
- tail << encoded_item
30
- offset += encoded_item.length / 2 # hex string, 2 characters per byte
31
- end
32
-
33
- header.join + tail.join
34
- end
35
- end
36
- end