onchain 2.15 → 2.16
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/lib/onchain/providers/insight_api.rb +126 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b79c9529ca07f30c5431215ef6228a144de406c
|
4
|
+
data.tar.gz: 06d5126dd81d2e85af5ad616d2eb37f1287bf972
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c8c38eb6c3c853f9ebe46c2ad4ff4fc8742089cd40c60db4bf1f71c3d9e0a8bdf34fcd5bd2aa1727f255aadb7fc4abe69cb70d2179cb23647830fccdfcc0195
|
7
|
+
data.tar.gz: c5d43867556cb8f630bff338d1333be0dcddb1fcb6199cd79e7d5f454d7115b8a5fc5aafb6e4eb4747c3d84de655c7bd4e99c150cf756bd03c535a8c7db3f062
|
@@ -0,0 +1,126 @@
|
|
1
|
+
class OnChain::BlockChain
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def get_insight_url(network)
|
5
|
+
if network == :bitcoin
|
6
|
+
return "https://insight.bitpay.com/api/"
|
7
|
+
elsif network == :zcash_testnet
|
8
|
+
return "https://explorer.testnet.z.cash/api/"
|
9
|
+
end
|
10
|
+
return "https://test-insight.bitpay.com/api/"
|
11
|
+
end
|
12
|
+
|
13
|
+
def insight_address_history(address, network = :bitcoin)
|
14
|
+
|
15
|
+
base_url = get_insight_url(network) + "addr/" + address
|
16
|
+
json = fetch_response(base_url, true)
|
17
|
+
|
18
|
+
return parse_insight_address_tx(address, json, network)
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_insight_address_tx(address, json, network)
|
23
|
+
|
24
|
+
hist = []
|
25
|
+
if json.key?('transactions')
|
26
|
+
txs = json['transactions']
|
27
|
+
txs.each do |tx|
|
28
|
+
row = {}
|
29
|
+
row[:hash] = tx[tx]
|
30
|
+
|
31
|
+
# OK, go and get the actual transaction
|
32
|
+
base_url = get_insight_url(network) + "tx/" + tx
|
33
|
+
tx_json = fetch_response(base_url, true)
|
34
|
+
|
35
|
+
row[:time] = tx_json["time"]
|
36
|
+
row[:addr] = {}
|
37
|
+
row[:outs] = {}
|
38
|
+
|
39
|
+
inputs = tx_json['vin']
|
40
|
+
val = 0
|
41
|
+
recv = "Y"
|
42
|
+
inputs.each do |input|
|
43
|
+
row[:addr][input["addr"]] = input["addr"]
|
44
|
+
if input["addr"] == address
|
45
|
+
recv = "N"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
tx_json["vout"].each do |out|
|
50
|
+
out_addr = out["scriptPubKey"]["addresses"][0]
|
51
|
+
row[:outs][out_addr] = out_addr
|
52
|
+
if recv == "Y" and out_addr == address
|
53
|
+
val = val + out["value"].to_f
|
54
|
+
elsif recv == "N" and out_addr != address
|
55
|
+
val = val + out["value"].to_f
|
56
|
+
end
|
57
|
+
end
|
58
|
+
row[:total] = val
|
59
|
+
row[:recv] = recv
|
60
|
+
hist << row
|
61
|
+
end
|
62
|
+
return hist
|
63
|
+
else
|
64
|
+
'Error'
|
65
|
+
end
|
66
|
+
return hist
|
67
|
+
end
|
68
|
+
|
69
|
+
def insight_send_tx(tx_hex, network = :bitcoin)
|
70
|
+
|
71
|
+
uri = URI.parse(get_url(network) + "tx/push")
|
72
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
73
|
+
|
74
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
75
|
+
request.body = '{"rawtx":"' + tx_hex + '"}'
|
76
|
+
response = http.request(request)
|
77
|
+
|
78
|
+
res = JSON.parse(response.body)
|
79
|
+
|
80
|
+
mess = 'Unknown'
|
81
|
+
stat = 'Unknown'
|
82
|
+
tx_hash = res["txid"]
|
83
|
+
|
84
|
+
ret = "{\"status\":\"#{stat}\",\"data\":\"#{tx_hash}\",\"code\":200,\"message\":\"#{mess}\"}"
|
85
|
+
return JSON.parse(ret)
|
86
|
+
end
|
87
|
+
|
88
|
+
def insight_get_balance(address, network = :bitcoin)
|
89
|
+
|
90
|
+
if cache_read(address + network.to_s) == nil
|
91
|
+
|
92
|
+
base_url = get_insight_url(network) + "addr/#{address}/balance"
|
93
|
+
bal_string = fetch_response(base_url, false)
|
94
|
+
bal = bal_string.to_i / 100000000.0
|
95
|
+
cache_write(address + network.to_s, bal, BALANCE_CACHE_FOR)
|
96
|
+
end
|
97
|
+
|
98
|
+
return cache_read(address + network.to_s)
|
99
|
+
end
|
100
|
+
|
101
|
+
def insight_get_unspent_outs(address, network = :bitcoin)
|
102
|
+
|
103
|
+
base_url = get_insight_url(network) + "addr/#{address}/utxo"
|
104
|
+
json = fetch_response(base_url, true)
|
105
|
+
|
106
|
+
unspent = []
|
107
|
+
|
108
|
+
json.each do |data|
|
109
|
+
line = []
|
110
|
+
line << data['txid']
|
111
|
+
line << data['vout']
|
112
|
+
line << data['scriptPubKey']
|
113
|
+
line << (data['amount'].to_f * 100000000).to_i
|
114
|
+
unspent << line
|
115
|
+
end
|
116
|
+
|
117
|
+
return unspent
|
118
|
+
end
|
119
|
+
|
120
|
+
def insight_get_transaction(txhash, network = :bitcoin)
|
121
|
+
base = get_insight_url(network) + "rawtx/" + txhash
|
122
|
+
return fetch_response(URI::encode(base))['rawtx']
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onchain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '2.
|
4
|
+
version: '2.16'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Number 6
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/onchain/payments.rb
|
93
93
|
- lib/onchain/providers/blockchaininfo_api.rb
|
94
94
|
- lib/onchain/providers/blockr_api.rb
|
95
|
+
- lib/onchain/providers/insight_api.rb
|
95
96
|
- lib/onchain/sweeper.rb
|
96
97
|
- lib/onchain/transaction.rb
|
97
98
|
homepage: https://github.com/onchain/onchain-gem
|