Class: Cryptos::Connectors::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptos/connectors/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program: 'bitcoin-cli', network: 'regtest', verbose: false) ⇒ Cli

Returns a new instance of Cli.



6
7
8
9
10
# File 'lib/cryptos/connectors/cli.rb', line 6

def initialize(program: 'bitcoin-cli', network: 'regtest', verbose: false)
  @program = program
  @network = network
  @verbose = verbose
end

Instance Attribute Details

#networkObject (readonly)

Returns the value of attribute network.



4
5
6
# File 'lib/cryptos/connectors/cli.rb', line 4

def network
  @network
end

#programObject (readonly)

Returns the value of attribute program.



4
5
6
# File 'lib/cryptos/connectors/cli.rb', line 4

def program
  @program
end

#verboseObject (readonly)

Returns the value of attribute verbose.



4
5
6
# File 'lib/cryptos/connectors/cli.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#generate(blocks: 1, run_mode: :system) ⇒ Object



20
21
22
# File 'lib/cryptos/connectors/cli.rb', line 20

def generate(blocks: 1, run_mode: :system)
 run "generate #{blocks}"
end

#generate_to_address(address, blocks: 101, run_mode: :system, verbose: false) ⇒ Object



16
17
18
# File 'lib/cryptos/connectors/cli.rb', line 16

def generate_to_address(address, blocks: 101, run_mode: :system, verbose: false)
  run "generatetoaddress #{blocks} #{address}"
end

#get_block_countObject



36
37
38
# File 'lib/cryptos/connectors/cli.rb', line 36

def get_block_count
  run("getblockcount", run_mode: :inline).to_i
end

#get_received_by_address(address) ⇒ Object



40
41
42
43
# File 'lib/cryptos/connectors/cli.rb', line 40

def get_received_by_address(address)
  result = run "getreceivedbyaddress #{address}", run_mode: :inline
  result.strip
end

#import_address(address, run_mode: :system) ⇒ Object



12
13
14
# File 'lib/cryptos/connectors/cli.rb', line 12

def import_address(address, run_mode: :system)
  run "importaddress #{address} '' false", run_mode: run_mode
end

#list_unspent(address, run_mode: :inline) ⇒ Object



24
25
26
# File 'lib/cryptos/connectors/cli.rb', line 24

def list_unspent(address, run_mode: :inline)
  run "listunspent 1 9999 \"[\\\"#{address}\\\"]\"", run_mode: run_mode
end

#run(args, run_mode: :system, debug: false) ⇒ Object

private



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cryptos/connectors/cli.rb', line 47

def run(args, run_mode: :system, debug: false)
  cmd = "#{program} -#{network} #{args}"
  puts "==> #{cmd} run_mode=#{run_mode} verbose=#{debug}" if debug || verbose
  case run_mode
  when :inline
    output = `#{cmd}`
    puts output if debug || verbose
    output
  when :system
    success = system cmd, out: '/dev/null'
    fail "failed command: #{args}" unless success
    success
  when :daemon
    pid = spawn cmd
    sleep (ENV['BOOTSTRAP'] || 10).to_i
    pid
  else
    raise "dont know how to run #{run_mode}"
  end
end

#send_raw_transaction(rawtx, run_mode: :system) ⇒ Object



28
29
30
# File 'lib/cryptos/connectors/cli.rb', line 28

def send_raw_transaction(rawtx, run_mode: :system)
  run "sendrawtransaction #{rawtx}", run_mode: run_mode
end

#test_raw_transaction(rawtx, run_mode: :inline) ⇒ Object



32
33
34
# File 'lib/cryptos/connectors/cli.rb', line 32

def test_raw_transaction(rawtx, run_mode: :inline)
  run "testmempoolaccept '[\"#{rawtx}\"]'", run_mode: run_mode
end