infura_ruby 1.1.0 → 1.2.1

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: bec78f6153262da586b44f024f9247c7508af7c7
4
- data.tar.gz: cb2f8572bec68b2a76817af04e4645fd2e823cb3
3
+ metadata.gz: 2cab21eb980c99eb83827f434ed6f742e858e37a
4
+ data.tar.gz: 2b5fe568f354d5009e3fafe26cf75d812575902e
5
5
  SHA512:
6
- metadata.gz: a3f091c2737ec85074f1b0659661ad27d35e0b0ba1208066f9f21be7224bc52f885507b0f6f9a2116e7c0b44f6c53ee599ce4161b30213ef63240628cee98397
7
- data.tar.gz: 82edfdc537ea144ecff266f6d1273e74d31e493b41fbf14bfc8780aa1548a4381a1abc4ed5554cf77ad8724fdf9361ceb54adc6eb8ae6f07410061074d16acb5
6
+ metadata.gz: 553ba0800d07eb6e324de103c5ed8ab72104eea4f3c190486b4f9e49c88eb2101ba02e79fc0a6ae8781d5abb53cf35b6f0fda0d56f807240f6fcd968764634e1
7
+ data.tar.gz: 8b00a4adc2230efe369783de5077f4f031649620d997de79322e11f5b21f792fc28d9cd6ebd0207f9eb6537d62f50674af1a1568541a2c52803b561b81d318c6
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # InfuraRuby
2
2
 
3
- Ruby gem to wrap the [INFURA](https://github.com/ethereum/wiki/wiki/JSON-RPC() API which gives HTTP API access to ethereum and IPFS nodes. The API uses the same format as the [JSON RPC spec](https://github.com/ethereum/wiki/wiki/JSON-RPCi) for normal ethereum nodes.
3
+ Ruby gem to wrap the [INFURA](https://github.com/ethereum/wiki/wiki/JSON-RPC) API which gives HTTP API access to ethereum and IPFS nodes. The API uses the same format as the [JSON RPC spec](https://github.com/ethereum/wiki/wiki/JSON-RPC) for normal ethereum nodes.
4
4
 
5
- For now, I only need the `getBalance` call an so that is all I will build. Feel free to add the rest of the functionality or I may get to it over time...
5
+ For now, I only need the `getBalance` call and so that is all I have built. Feel free to add the rest of the functionality or I may get to it over time...
6
6
 
7
7
  ## Usage
8
8
 
@@ -19,4 +19,13 @@ infura = InfuraRuby.client(api_key: key)
19
19
 
20
20
  # get the balance (in wei) of an address
21
21
  infura.get_balance('0x81F631b8615EaB75d38DaC4d4bce4A5b63e10310') #=> 591686024850016
22
+
23
+ # This can be qualified with 4 different tags to get the balance at the tag's time.
24
+ # 'latest' - latest balance (default) with at least 1 confirmation
25
+ # 'pending' - balance including pending transactions
26
+ # 'earliest' - balance at the time of the genesis block or earliest known block
27
+ # '0x123' - balance at the time of chain height `0x123` (hex string)
28
+
29
+ # balance including unconfirmed transactions
30
+ infura.get_balance('0x81F631b8615EaB75d38DaC4d4bce4A5b63e10310', tag: 'pending')
22
31
  ```
@@ -1,5 +1,5 @@
1
- require 'infura_ruby/version'
2
- require 'infura_ruby/client'
1
+ require_relative 'infura_ruby/version'
2
+ require_relative 'infura_ruby/client'
3
3
 
4
4
  module InfuraRuby
5
5
  class InvalidApiKeyError < StandardError; end
@@ -3,6 +3,11 @@ require 'json'
3
3
 
4
4
  module InfuraRuby
5
5
  class Client
6
+ class InfuraCallError < StandardError; end
7
+ class InvalidEthereumAddressError < StandardError; end
8
+
9
+ ETHEREUM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/
10
+
6
11
  # Infura URLs for each network.
7
12
  NETWORK_URLS = {
8
13
  main: 'https://mainnet.infura.io',
@@ -14,6 +19,13 @@ module InfuraRuby
14
19
  'eth_getBalance'
15
20
  ].freeze
16
21
 
22
+ BLOCK_PARAMETERS = [
23
+ /^0x[0-9a-fA-F]{1,}$/, # an integer block number (hex string)
24
+ /^earliest$/, # for the earliest/genesis block
25
+ /^latest$/, # for the latest mined block
26
+ /^pending$/ # for the pending state/transactions
27
+ ].freeze
28
+
17
29
  def initialize(api_key:, network: :main)
18
30
  validate_api_key(api_key)
19
31
  validate_network(network)
@@ -22,16 +34,27 @@ module InfuraRuby
22
34
  @network = network
23
35
  end
24
36
 
37
+ # TODO: move calls out of client - worth doing when we have > 1.
25
38
  # Returns balance of address in wei as integer.
26
- def get_balance(address)
39
+ def get_balance(address, tag: 'latest')
40
+ validate_address(address)
41
+ validate_block_tag(tag)
42
+
27
43
  resp = conn.post do |req|
28
44
  req.headers['Content-Type'] = 'application/json'
29
- req.body = json_rpc(method: 'eth_getBalance', params: [address, 'latest']).to_json
45
+ req.body = json_rpc(method: 'eth_getBalance', params: [address, tag]).to_json
30
46
  end
31
-
32
47
  resp_body = JSON.parse(resp.body)
33
- wei_amount_hex_string = resp_body['result']
34
- wei_amount_hex_string.to_i(16)
48
+
49
+ if resp_body['error']
50
+ raise InfuraCallError.new(
51
+ "Error (#{resp_body['error']['code']}): Infura API call "\
52
+ "eth_getBalance gave message: '#{resp_body['error']['message']}'"
53
+ )
54
+ else
55
+ wei_amount_hex_string = resp_body['result']
56
+ wei_amount_hex_string.to_i(16)
57
+ end
35
58
  end
36
59
 
37
60
  private
@@ -39,7 +62,7 @@ module InfuraRuby
39
62
  # TODO: this JSON RPC object should be a whole object / gem.
40
63
  def json_rpc(method:, params:)
41
64
  validate_json_rpc_method(method)
42
-
65
+
43
66
  {
44
67
  "jsonrpc" => "2.0",
45
68
  "method" => method,
@@ -48,8 +71,22 @@ module InfuraRuby
48
71
  }
49
72
  end
50
73
 
74
+ def validate_block_tag(tag)
75
+ if BLOCK_PARAMETERS.none? { |regex| regex =~ tag.to_s }
76
+ raise NotImplementedError.new("Block parameter tag '#{tag}' does not exist.")
77
+ end
78
+ end
79
+
80
+ def validate_address(address)
81
+ if ETHEREUM_ADDRESS_REGEX !~ address
82
+ raise InvalidEthereumAddressError.new("'#{address}' is not a valid ethereum address.")
83
+ end
84
+ end
85
+
51
86
  def validate_json_rpc_method(method)
52
- raise NotImplementedError unless JSON_RPC_METHODS.include?(method)
87
+ if !JSON_RPC_METHODS.include?(method)
88
+ raise NotImplementedError.new("JSON RPC method '#{method}' does not exist.")
89
+ end
53
90
  end
54
91
 
55
92
  def conn
@@ -1,3 +1,3 @@
1
1
  module InfuraRuby
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infura_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Borrey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-25 00:00:00.000000000 Z
11
+ date: 2017-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  requirements: []
102
102
  rubyforge_project:
103
- rubygems_version: 2.6.9
103
+ rubygems_version: 2.5.1
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Library for the INFURA API.