Class: VChainClient::BlockcypherBlockchainAdapter

Inherits:
BlockchainAdapter show all
Defined in:
lib/vchain_client/blockcypher_blockchain_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_token, adapter_config, app_config) ⇒ BlockcypherBlockchainAdapter

Returns a new instance of BlockcypherBlockchainAdapter.



11
12
13
14
15
16
17
# File 'lib/vchain_client/blockcypher_blockchain_adapter.rb', line 11

def initialize(api_token, adapter_config, app_config)
	@api_token = api_token

  @config = app_config

  @log = Log4r::Logger["vchain_client"]
end

Instance Method Details

#getNameObject



19
20
21
# File 'lib/vchain_client/blockcypher_blockchain_adapter.rb', line 19

def getName()
  return "BlockcypherBlockchainAdapter"
end

#getOpReturn(raw_tx) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/vchain_client/blockcypher_blockchain_adapter.rb', line 215

def getOpReturn(raw_tx)
	if raw_tx != nil
		if raw_tx.key?("outputs")
			if raw_tx["outputs"].length > 0
				raw_tx["outputs"].each { |vout|
					if vout.key?("script_type")
						if vout["script_type"] == "null-data"
							if vout.key?("data_string")
								return vout["data_string"]
							end
						end
					end
				}
			end
		end
	end

	return nil
end

#getTx(txid) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/vchain_client/blockcypher_blockchain_adapter.rb', line 23

def getTx(txid)
  url = "https://api.blockcypher.com/v1/btc/main/txs/"
url += txid
url += "?token="+ @api_token
url += "&rand="+ Random.rand(0...999999).to_s

  if @log.debug?
    @log.debug("[Blockcypher.getTx] input:")
    @log.debug("-> txid: #{txid}")

    @log.debug("[Blockcypher.getTx] will call '#{url}'")
  end

  req = nil

  begin
    
    req = RestClient.get(url)

  rescue => e
    if @log.error?
      @log.error("[Blockcypher.getTx] RestClient.get raised exception:")
      @log.error("#{e.class}, #{e.message}")
      @log.error("-> txid: #{txid}")
      @log.error("--> url: #{url}")
    end

    raise e
  end

  if req == nil
    if @log.error?
      @log.error("[Blockcypher.getTx] failed to call REST")
      @log.error("-> txid: #{txid}")
      @log.error("--> url: #{url}")
    end

    return nil
  end

  if req.code != 200
    if @log.error?
      @log.error("[Blockcypher.getTx] REST call returned "+ req.code.to_s)
      @log.error("-> txid: #{txid}")
      @log.error("--> url: #{url}")
    end

    return nil
  end

  if @log.debug?
    @log.debug("[Blockcypher.getTx] REST call response:")
    @log.debug(req.body)
  end

  res = JSON.parse req.body

  confirmations_threshold = 5
  if @config.key?("blockchain_confirmations")
    confirmations_threshold = @config["blockchain_confirmations"]
  end

  if res != nil

    if res.key?("confirmations")

    	if res["confirmations"] > confirmations_threshold

    		if res.key?("block_hash")

    			if res.key?("confirmed")

            block_timestamp = nil

            begin

    				  block_timestamp = DateTime.parse(res["confirmed"]).to_i

            rescue => e
              if @log.error?
                @log.error("[Blockcypher.getTx] DateTime.parse('confirmed').to_i raised exception:")
                @log.error("#{e.class}, #{e.message}")
                @log.error("-> txid: #{txid}")
                @log.error("--> url: #{url}")
              end

              raise e
            end

            if block_timestamp == nil
              if @log.error?
                @log.error("[Blockcypher.getTx] failed to convert 'confirmed' field to timestamp")
                @log.error("-> txid: #{txid}")
                @log.error("--> url: #{url}")
              end

              return nil
            end

            op_return = nil

            begin

    				  op_return = self.getOpReturn(res)

            rescue => e
              if @log.error?
                @log.error("[Blockcypher.getTx] getOpReturn raised exception:")
                @log.error("#{e.class}, #{e.message}")
                @log.error("-> txid: #{txid}")
                @log.error("--> res:")
                @log.error(res)
              end

              raise e
            end

    				if op_return != nil

    					prefix = op_return[0..2]

    					if prefix == "VCH"
    						op_return = op_return[5..op_return.length]

    						return {
								"size"            => res["size"],
								"block_hash"      => res["block_hash"],
								"block_timestamp" => block_timestamp,
								"op_return"       => op_return
    						}

              else
                if @log.error?
                  @log.error("[Blockcypher.getTx] wrong prefix '#{prefix}'")
                  @log.error("-> txid: #{txid}")
                  @log.error("--> res:")
                  @log.error(res)
                end
    					end

            else
              if @log.error?
                @log.error("[Blockcypher.getTx] failed to get OP_RETURN")
                @log.error("-> txid: #{txid}")
                @log.error("--> res:")
                @log.error(res)
              end
    				end

          else
            if @log.error?
              @log.error("[Blockcypher.getTx] no 'confirmed' field in response")
              @log.error("-> txid: #{txid}")
              @log.error("--> url: #{url}")
            end
    			end

        else
          if @log.error?
            @log.error("[Blockcypher.getTx] no 'block_hash' field in response")
            @log.error("-> txid: #{txid}")
            @log.error("--> url: #{url}")
          end
    		end

      else
        if @log.error?
          @log.error("[Blockcypher.getTx] less than 6 confirmations")
          @log.error("-> txid: #{txid}")
          @log.error("--> url: #{url}")
        end
    	end

    else
      if @log.error?
        @log.error("[Blockcypher.getTx] no 'confirmations' field")
        @log.error("-> txid: #{txid}")
        @log.error("--> url: #{url}")
      end
    end

  else
    if @log.error?
      @log.error("[Blockcypher.getTx] REST call - empty response")
      @log.error("-> txid: #{txid}")
      @log.error("--> url: #{url}")
    end
  end

  return nil
end