Last active
February 26, 2018 11:37
-
-
Save veganstraightedge/d71bb38e7d8aa0bf3e81564fb6e6be10 to your computer and use it in GitHub Desktop.
The very start of what will likely become an Infura API wrapper gem. Lots TODO still.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Infura helper class | |
require "http" | |
require "json" | |
class Infura | |
NETWORK = :mainnet | |
API_BASE_URL = "https://api.infura.io/v1/jsonrpc/#{Infura::NETWORK}/" | |
class << self | |
def print_debug label, value | |
puts "#{label}:" | |
puts " #{value.inspect}" unless value.nil? | |
puts | |
end | |
def get(method, params: [], debug: false) | |
request_url = [Infura::API_BASE_URL, method.to_s].join | |
print_debug "Request URL", request_url if debug # DEBUGGING OUTPUT | |
headers = { | |
content_type: "application/json", | |
accept: "application/json", | |
token: Infura::API_TOKEN, | |
} | |
print_debug "Headers", headers if debug # DEBUGGING OUTPUT | |
data = { | |
jsonrpc: "2.0", | |
id: 1, | |
method: method, | |
params: params.to_json | |
} | |
print_debug "Data", data if debug # DEBUGGING OUTPUT | |
response = HTTP.headers(headers).get(request_url , params: data) | |
print_debug "Response", response if debug # DEBUGGING OUTPUT | |
result = JSON.parse(response.body)["result"] | |
print_debug "Result", result if debug # DEBUGGING OUTPUT | |
print_debug ":::END OF DEBUGGING::", nil if debug # DEBUGGING OUTPUT | |
result | |
end | |
end | |
end | |
### Usage | |
Infura::API_TOKEN = "TODO: REPLACE WITH YOUR API TOKEN FROM INFURA.IO" | |
response = Infura.get(:eth_getBalance, | |
params: [ | |
"0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7", | |
"latest" | |
]) | |
puts response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment