class VChainClient::BlockcypherBlockchainAdapter

Public Class Methods

new(api_token, adapter_config, app_config) click to toggle source
# 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

Public Instance Methods

getName() click to toggle source
# File lib/vchain_client/blockcypher_blockchain_adapter.rb, line 19
def getName()
  return "BlockcypherBlockchainAdapter"
end
getOpReturn(raw_tx) click to toggle source
# 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) click to toggle source
# 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