Class: Sibit::Bin

Inherits:
Thor
  • Object
show all
Defined in:
lib/sibit/bin.rb

Overview

Command-line interface for Sibit.

Provides commands to interact with the Bitcoin network.

Example:

Sibit::Bin.start(['price'])
Sibit::Bin.start(['balance', '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'])

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sibit/bin.rb', line 37

def self.exit_on_failure?
  true
end

.handle_argument_error(command, error, args, _arity) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/sibit/bin.rb', line 41

def self.handle_argument_error(command, error, args, _arity)
  return new.help(command.name) if args.include?('--help') || args.include?('-h')
  unknown = args.find { |a| a.start_with?('-') }
  if unknown
    warn("Unknown option: #{unknown}")
    exit(1)
  end
  raise(error)
end

Instance Method Details

#balance(address) ⇒ Object



88
89
90
# File 'lib/sibit/bin.rb', line 88

def balance(address)
  log.info(client.balance(address, trust: options[:trust]))
end

#create(key) ⇒ Object



79
80
81
82
83
# File 'lib/sibit/bin.rb', line 79

def create(key)
  log.debug("Private key provided: #{key.ellipsized(8).inspect}")
  k = Sibit::Key.new(key)
  log.info(options[:base58] ? k.base58 : k.bech32)
end

#feesObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/sibit/bin.rb', line 57

def fees
  sibit = client
  fees = sibit.fees
  log.info(
    %i[S M L XL].map do |m|
      sat = fees[m] * 250
      "#{m}: #{sat}sat / $#{format('%<usd>.02f', usd: (sat * sibit.price / 100_000_000))}"
    end.join("\n")
  )
end

#generateObject



74
75
76
# File 'lib/sibit/bin.rb', line 74

def generate
  log.info(client.generate)
end

#latestObject



69
70
71
# File 'lib/sibit/bin.rb', line 69

def latest
  log.info(client.latest)
end

#pay(amount, fee, sources, target, change) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sibit/bin.rb', line 102

def pay(amount, fee, sources, target, change)
  keys = sources.split(',')
  sweep = amount.upcase == 'MAX'
  if sweep
    addrs =
      keys.map do |k|
        kk = Sibit::Key.new(k)
        options[:base58] ? kk.base58 : kk.bech32
      end
    amount = addrs.sum { |a| client.balance(a, trust: options[:trust]) }
  end
  amount = Integer(amount, 10) if amount.is_a?(String) && /^[0-9]+$/.match?(amount)
  fee = Integer(fee, 10) if /^[0-9]+$/.match?(fee)
  amount -= fee if sweep && fee.is_a?(Integer)
  args = [amount, fee, keys, target, change]
  kwargs = {
    skip_utxo: options[:skip_utxo], base58: options[:base58],
    price: options[:price], trust: options[:trust]
  }
  unless options[:yes] || options[:dry]
    client(dry: true).pay(*args, **kwargs)
    print('Do you confirm this payment? (yes/no): ')
    unless $stdin.gets&.strip&.downcase == 'yes'
      raise(Sibit::Error, 'Payment cancelled by user')
    end
  end
  log.info(client.pay(*args, **kwargs))
end

#priceObject



52
53
54
# File 'lib/sibit/bin.rb', line 52

def price
  log.info(client.price)
end

#versionObject



132
133
134
# File 'lib/sibit/bin.rb', line 132

def version
  log.info(Sibit::VERSION)
end