Class: Sibit::Bin
- Inherits:
-
Thor
- Object
- Thor
- Sibit::Bin
- Defined in:
- lib/sibit/bin.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
- #balance(address) ⇒ Object
- #create(key) ⇒ Object
- #fees ⇒ Object
- #generate ⇒ Object
- #latest ⇒ Object
- #pay(amount, fee, sources, target, change) ⇒ Object
- #price ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ 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: [: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([:base58] ? k.base58 : k.bech32) end |
#fees ⇒ Object
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 |
#generate ⇒ Object
74 75 76 |
# File 'lib/sibit/bin.rb', line 74 def generate log.info(client.generate) end |
#latest ⇒ Object
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) [:base58] ? kk.base58 : kk.bech32 end amount = addrs.sum { |a| client.balance(a, trust: [: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: [:skip_utxo], base58: [:base58], price: [:price], trust: [:trust] } unless [:yes] || [: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 |
#price ⇒ Object
52 53 54 |
# File 'lib/sibit/bin.rb', line 52 def price log.info(client.price) end |