Я получаю следующую ошибку при попытке вызова RPC из ruby. bitcoind
сервер работает. Я использую bitcoin-ruby
библиотеку.
Код Пробовал в irb для проверки:
require 'bitcoin'
=> true
irb(main):002:0> txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
=> "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
irb(main):003:0> rtx= bitcoinRPC('getrawtransaction',[txid])
Ошибка :
NoMethodError: undefined method `bitcoinRPC' for main:Object
from (irb):3
from /usr/bin/irb:11:in `<main>'
Любое предложение будет высоко оценено.
Заранее спасибо,
р
Используйте следующий метод для вызова BitcoinRPC с помощью Ruby:
Создайте рубиновый файл test.rb
как
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
ПРИМЕЧАНИЕ: username
и password
взяты из вашего bitcoin.conf
файла.
m2pathan