Fehler beim Zugriff auf den Bitcoin-Client von Ruby mit RPC

Ich erhalte die folgende Fehlermeldung, während ich den RPC-Aufruf von Ruby versuche. bitcoindServer läuft. Ich verwende bitcoin-rubydie Bibliothek.

Code versucht in irb zu testen:

require 'bitcoin'
=> true
irb(main):002:0> txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
=> "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
irb(main):003:0> rtx= bitcoinRPC('getrawtransaction',[txid])

Fehler :

NoMethodError: undefined method `bitcoinRPC' for main:Object
    from (irb):3
    from /usr/bin/irb:11:in `<main>'

Jeder Vorschlag wird sehr geschätzt.

Vielen Dank im Voraus,

R

Antworten (1)

Verwenden Sie die folgende Methode, um BitcoinRPC mit Ruby aufzurufen:

Ruby-Datei erstellen test.rbals

require 'net/http'
require 'uri'
require 'json'

class BitcoinRPC
  def initialize(service_url)
    @uri = URI.parse(service_url)
  end

  def method_missing(name, *args)
    post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
    resp = JSON.parse( http_post_request(post_body) )
    raise JSONRPCError, resp['error'] if resp['error']
    resp['result']
  end

  def http_post_request(post_body)
    http    = Net::HTTP.new(@uri.host, @uri.port)
    request = Net::HTTP::Post.new(@uri.request_uri)
    request.basic_auth @uri.user, @uri.password
    request.content_type = 'application/json'
    request.body = post_body
    http.request(request).body
  end

  class JSONRPCError < RuntimeError; end
end

if $0 == __FILE__
  h = BitcoinRPC.new('http://username:password@127.0.0.1:8332')
  txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
  p h.getrawtransaction(txid)
end

HINWEIS: usernameund passwordstammen aus Ihrer bitcoin.confDatei.

Markieren Sie dies als Antwort, wenn Sie erfolgreich sind.