No commit activity in last 3 years
No release in over 3 years
bitcoin activerecord integration
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

bitcoin_active_record

Keep records of sent and received bitcoin transactions in your database using activerecord.

Quick start guide

  • Start bitcoin-qt or bitcoind with these options set in bitcoin.conf
server=1
rpcuser=change_this
rpcpassword=change_this
txindex=1 # needed for getting transaction sender keys
  • Add to gemfile, bundle install
  • Run generator
rails g bitcoin_active_record:install
  • Run db:migrate
  • Create a bitcoin client
client = BitcoinActiveRecord::Client.new(
  server: {
    url: 'http://127.0.0.1:8332',
    username: '',
    password: '',
  },
)
  • Run client.create_received_bitcoin_transactions to create records of received transactions
  • Run client.pay(public_key: public_key, amount: BigDecimal.new(1), comment: 'hello') to send a transaction and save a record of it in the database

Usage

This gem creates 4 models:

btc_address.rb

column_name type description
public_key string public key of a bitcoin address

bitcoin_transaction.rb

column_name type description
btc_address_id integer foreign key
amount decimal transaction amount
txid string bitcoin transaction id

received_bitcoin_transaction.rb

column_name type description
bitcoin_transaction_id integer The btc address associated with this bitcoin transaction id is the address of the person who sent the transaction
btc_address_id integer This is the address in your wallet that the transaction was sent to

sent_bitcoin_transaction.rb

column_name type description
bitcoin_transaction_id integer foreign key

Sent transactions are only recorded if you send them using the gem's api.

BitcoinActiveRecord::Client

Initialize options

client = BitcoinActiveRecord::Client.new(
  # required, bitcoind server credentials
  server: {
    url: 'http://127.0.0.1:8332',
    username: '',
    password: '',
  },
  # optional, amounts less than this will be ignored when running create_received_bitcoin_transactions
  minimum_amount: BigDecimal.new('0.001'),
  # optional, the wallet account you want to look for received transactions in
  account: :foo,
)
  • client.request(method, *args)

Send a request to the bitcoind server.

client.request(:getinfo)
client.request(:sendtoaddress, '1N2ZWQszjGDjaW5y3jAStuJQW23MbG1r4N', BigDecimal.new(1))
  • client.get_new_address

Get a new public key from the server

  • client.get_sender_address(txid)

Get the public key of the transaction sender for a transaction with id txid

  • client.pay

Pay someone and save a record of it as a SentBitcoinTransaction

client.pay(
  # required, public key you want to send BTC to
  public_key: '1N2ZWQszjGDjaW5y3jAStuJQW23MbG1r4N',
  # required, amount in BTC you want to send
  amount: BigDecimal.new('1.23'),
  # optional, transaction comment
  comment: 'foo',
) do |sent_bitcoin_transaction|
  # adding a block is optional
  # modify the sent_bitcoin_transaction record before it gets saved
  sent_bitcoin_transaction.custom_attribute = 'foo'
end
  • client.create_received_bitcoin_transactions

Create a ReceivedBitcoinTransaction model for every transaction from client.account that's >= client.minimum_amount. If the latest transaction is already in the database it will assume all earlier transactions have already been saved.