Class: BitcoinActiveRecord::Client
- Inherits:
-
Object
- Object
- BitcoinActiveRecord::Client
- Defined in:
- lib/bitcoin_active_record/client.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#minimum_amount ⇒ Object
Returns the value of attribute minimum_amount.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #create_received_payments ⇒ Object
- #get_new_address ⇒ Object
- #get_sender_address(txid) ⇒ Object
-
#initialize(server: raise('server required'), minimum_amount: 0, account: '') ⇒ Client
constructor
A new instance of Client.
- #pay(public_key: raise('public key required'), amount: raise('amount required'), comment: '') {|sent_payment| ... } ⇒ Object
- #request(method, *args) ⇒ Object
Constructor Details
#initialize(server: raise('server required'), minimum_amount: 0, account: '') ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 |
# File 'lib/bitcoin_active_record/client.rb', line 9 def initialize(server: raise('server required'), minimum_amount: 0, account: '') @server = server @minimum_amount = minimum_amount @account = account @default_transaction_count = 25 end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
6 7 8 |
# File 'lib/bitcoin_active_record/client.rb', line 6 def account @account end |
#minimum_amount ⇒ Object
Returns the value of attribute minimum_amount.
5 6 7 |
# File 'lib/bitcoin_active_record/client.rb', line 5 def minimum_amount @minimum_amount end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
7 8 9 |
# File 'lib/bitcoin_active_record/client.rb', line 7 def server @server end |
Instance Method Details
#create_received_payments ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bitcoin_active_record/client.rb', line 74 def create_received_payments page = 0 while true transactions = get_received_transactions(page: page) return if transactions.size == 0 transactions.each do |transaction| txid = transaction['txid'] # already created payment for this transaction and all older ones return if Payment.where(txid: txid).count > 0 amount = transaction['amount'] from_key = get_sender_address(txid) received_payment = ReceivedPayment.create!( payment: Payment.new( # payment from this address btc_address: BtcAddress.find_or_initialize_by( public_key: from_key ), amount: amount, txid: txid, ), # sent to this address btc_address: BtcAddress.find_or_initialize_by(public_key: transaction['address']), ) puts("Received payment #{amount} BTC from #{from_key}: #{received_payment.inspect}") end page += 1 end end |
#get_new_address ⇒ Object
40 41 42 43 |
# File 'lib/bitcoin_active_record/client.rb', line 40 def get_new_address raise if Rails.env.test? request(:getnewaddress, @account) end |
#get_sender_address(txid) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bitcoin_active_record/client.rb', line 45 def get_sender_address(txid) addresses = [] raw_tx = request('decoderawtransaction', request('getrawtransaction', txid)) raw_tx['vin'].each do |input| input_raw_tx = request('decoderawtransaction', request('getrawtransaction', input['txid'])) addresses << input_raw_tx['vout'][input['vout']]['scriptPubKey']['addresses'][0] end addresses[0] end |
#pay(public_key: raise('public key required'), amount: raise('amount required'), comment: '') {|sent_payment| ... } ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bitcoin_active_record/client.rb', line 57 def pay(public_key: raise('public key required'), amount: raise('amount required'), comment: '') raise('amount cant be zero or negative') unless amount > 0 sent_payment = SentPayment.new( payment: Payment.new( btc_address: BtcAddress.find_or_initialize_by(public_key: public_key), amount: amount, txid: request(:sendtoaddress, public_key, amount, comment), ), ) yield(sent_payment) if block_given? sent_payment.save! sent_payment end |
#request(method, *args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bitcoin_active_record/client.rb', line 16 def request(method, *args) args.map! { |a| a.is_a?(BigDecimal) ? a.to_f : a } raise 'payments disabled in test/development' if method.to_sym == :sendtoaddress && (Rails.env.test? || Rails.env.development?) res = HTTParty.post( @server[:url], basic_auth: { username: @server[:username], password: @server[:password], }, headers: { 'Content-Type' => 'application/json', }, body: { method: method, params: args, id: 'jsonrpc', }.to_json, ) raise ApiError.new(res) unless res['error'].nil? res['result'] end |