Class: Peatio::Electrum::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/peatio/electrum/client.rb

Defined Under Namespace

Classes: ResponseError

Constant Summary collapse

Error =
Class.new(StandardError)
ConfigurationError =
Class.new(Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wallet_url) ⇒ Client

Returns a new instance of Client.



21
22
23
# File 'lib/peatio/electrum/client.rb', line 21

def initialize(wallet_url)
  @connection = Faraday.new(url: wallet_url)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/peatio/electrum/client.rb', line 5

def connection
  @connection
end

Instance Method Details

#broadcast(tx) ⇒ Object



101
102
103
# File 'lib/peatio/electrum/client.rb', line 101

def broadcast(tx)
  call('broadcast', [tx])
end

#call(method, params = []) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/peatio/electrum/client.rb', line 29

def call(method, params = [])
  body = {
    id: new_id,
    method: method,
    params: params
  }.to_json

  headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json'
  }

  response = JSON.parse(connection.post('/', body, headers).body)
  error = response['error']
  raise ResponseError.new(error['code'], error['message']) unless error.nil?

  response['result']
end

#create_addressObject



65
66
67
# File 'lib/peatio/electrum/client.rb', line 65

def create_address
  call('createnewaddress')
end

#get_address_balance(address) ⇒ Object



60
61
62
63
# File 'lib/peatio/electrum/client.rb', line 60

def get_address_balance(address)
  call('getaddressbalance', [address])
    .map { |k, v| [k, v.to_d] }.to_h
end

#get_balanceObject



56
57
58
# File 'lib/peatio/electrum/client.rb', line 56

def get_balance
  call('getbalance')['confirmed'].to_d
end

#get_local_heightObject



52
53
54
# File 'lib/peatio/electrum/client.rb', line 52

def get_local_height
  call('get_local_height', [])
end

#get_transaction(txid) ⇒ Object



77
78
79
# File 'lib/peatio/electrum/client.rb', line 77

def get_transaction(txid)
  call('gettransaction', [txid])
end

#get_tx_status(txid) ⇒ Object



73
74
75
# File 'lib/peatio/electrum/client.rb', line 73

def get_tx_status(txid)
  call('get_tx_status', [txid])
end

#history(year = nil, show_addresses = true, show_fiat = false, show_fees = true, from_height = nil, to_height = nil) ⇒ Object



81
82
83
# File 'lib/peatio/electrum/client.rb', line 81

def history(year = nil, show_addresses = true, show_fiat = false, show_fees = true, from_height = nil, to_height = nil)
  JSON.parse(call('history', [year, show_addresses, show_fiat, show_fees, from_height, to_height]))
end

#is_synchronizedObject



48
49
50
# File 'lib/peatio/electrum/client.rb', line 48

def is_synchronized
  call('is_synchronized', [])
end

#list_unspentObject



69
70
71
# File 'lib/peatio/electrum/client.rb', line 69

def list_unspent
  call('listunspent')
end

#new_idObject



25
26
27
# File 'lib/peatio/electrum/client.rb', line 25

def new_id
  (Time.now.to_f * 1000).to_i
end

#payto(destination, amount, opts = {}) ⇒ Object

Default options: fee: nil from_addr: nil change_addr: nil nocheck: false unsigned: false rbf: nil password: nil locktime: nil



94
95
96
97
98
99
# File 'lib/peatio/electrum/client.rb', line 94

def payto(destination, amount, opts = {})
  call('payto', {
    destination: destination,
    amount: amount
  }.merge(opts).compact)
end