infura_ruby 1.1.0 → 1.2.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/README.md +11 -2
- data/lib/infura_ruby.rb +2 -2
- data/lib/infura_ruby/client.rb +44 -7
- data/lib/infura_ruby/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cab21eb980c99eb83827f434ed6f742e858e37a
|
4
|
+
data.tar.gz: 2b5fe568f354d5009e3fafe26cf75d812575902e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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
|
```
|
data/lib/infura_ruby.rb
CHANGED
data/lib/infura_ruby/client.rb
CHANGED
@@ -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,
|
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
|
-
|
34
|
-
|
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
|
-
|
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
|
data/lib/infura_ruby/version.rb
CHANGED
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
|
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
|
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.
|
103
|
+
rubygems_version: 2.5.1
|
104
104
|
signing_key:
|
105
105
|
specification_version: 4
|
106
106
|
summary: Library for the INFURA API.
|