bitsy-bitcoin 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57d2955e23dabcf1cb91b4cd3ba839512852d795
4
- data.tar.gz: b3a10f6821852d2faae60f8f7afce54e051dbe97
3
+ metadata.gz: 6dc9926eed4195fdde26c052f88bd3847dd612c6
4
+ data.tar.gz: c2501edd14c431e09ccc0d052dd56bf0193ab283
5
5
  SHA512:
6
- metadata.gz: 876e9b21251ae829b51a0a40c873e3871a7541b593b6708b0a415d69c39fb79b10c3792efa200fd3d90c248a1aa05f57f1d19142f3909421293a2d6f48ca5007
7
- data.tar.gz: 8c87da8bc7857a4f31ee7258a4880a052d683ea9654ccc372699b54402dadd5e45dd59224885d8710b7d18739a779ebd16b296ed9eecbe97bb675cb1b93956f0
6
+ metadata.gz: 78e4d71e5000101667f18299f2fba3dc23e9cbc242b8cb4582a18fb32e65391fb81fc230266c9f3e65f74576f95e17872bf3c47b97bbed8e6710bcce7e53251e
7
+ data.tar.gz: 25b02015f4e2b296618d5e653f4015a19cbfa2c1c0964c65c6f3cadea15ae26b003ced797228f4adc77bd8ad78e46edb635f6aff76a8af328d7db160e93d8983
@@ -4,7 +4,9 @@ module Bitsy
4
4
 
5
5
  def index
6
6
  bn = BlockchainNotification.new(blockchain_notification_params)
7
- if bn.save
7
+ if bn.test
8
+ render text: "*ok*", status: 200
9
+ elsif bn.save
8
10
  BlockchainNotificationJob.perform_async(bn.id)
9
11
  render text: "*ok*", status: 200
10
12
  else
@@ -21,6 +23,7 @@ module Bitsy
21
23
  :input_address,
22
24
  :confirmations,
23
25
  :secret,
26
+ :test,
24
27
  )
25
28
  end
26
29
 
@@ -0,0 +1,12 @@
1
+ module Bitsy
2
+ class CheckPaymentsJob
3
+
4
+ include Sidekiq::Worker
5
+ sidekiq_options retry: false
6
+
7
+ def perform
8
+ CheckForPayments.execute
9
+ end
10
+
11
+ end
12
+ end
@@ -1,6 +1,8 @@
1
1
  module Bitsy
2
2
  class BlockchainNotification < ActiveRecord::Base
3
3
 
4
+ attr_reader :test
5
+
4
6
  validates(
5
7
  :value,
6
8
  :transaction_hash,
@@ -11,5 +13,9 @@ module Bitsy
11
13
 
12
14
  validates(:secret, inclusion: {in: Bitsy.config.blockchain_secrets})
13
15
 
16
+ def test=(b)
17
+ @test = ["true", true].include?(b)
18
+ end
19
+
14
20
  end
15
21
  end
@@ -2,11 +2,13 @@ require 'securerandom'
2
2
 
3
3
  module Bitsy
4
4
  class PaymentDepot < ActiveRecord::Base
5
+
5
6
  has_many :transactions, class_name: 'PaymentTransaction'
6
7
 
7
8
  alias_attribute :balance, :balance_cache
8
9
  after_initialize :set_uuid
9
10
  scope :with_balance, -> { where('balance_cache > 0.0') }
11
+ scope :checked_at_is_past, -> { where(arel_table[:checked_at].lt(Time.now)) }
10
12
  validates(
11
13
  :initial_tax_rate,
12
14
  inclusion: {
@@ -24,6 +26,13 @@ module Bitsy
24
26
  self.min_payment * (1 - self.initial_tax_rate)
25
27
  end
26
28
 
29
+ def reset_checked_at!
30
+ self.update_attributes(
31
+ check_count: self.check_count + 1,
32
+ checked_at: (self.check_count**2).seconds.from_now,
33
+ )
34
+ end
35
+
27
36
  def balance_tax_amount
28
37
  ForwardTaxCalculator.calculate(self.balance,
29
38
  self.min_payment,
@@ -0,0 +1,14 @@
1
+ module Bitsy
2
+ class CheckForPayments
3
+
4
+ include LightService::Organizer
5
+
6
+ def self.execute
7
+ reduce(
8
+ GetLatestBlock,
9
+ CheckPaymentDepotTransactions,
10
+ )
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module Bitsy
2
+ class CheckPaymentDepotTransaction
3
+
4
+ include LightService::Action
5
+ expects :latest_block, :payment_depot
6
+
7
+ executed do |ctx|
8
+ blockchain_address = Blockchain.get_address(ctx.payment_depot.address)
9
+ blockchain_address.txs.each do |tx|
10
+ ProcessBlockchainBlockexplorerTransaction.execute(
11
+ payment_depot: ctx.payment_depot,
12
+ latest_block: ctx.latest_block,
13
+ blockchain_transaction: tx,
14
+ )
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Bitsy
2
+ class CheckPaymentDepotTransactions
3
+
4
+ include LightService::Action
5
+ expects :latest_block, :payment_depots
6
+
7
+ executed do |ctx|
8
+ ctx.payment_depots.each do |payment_depot|
9
+ CheckPaymentDepotTransaction.execute(
10
+ payment_depot: payment_depot,
11
+ latest_block: ctx.latest_block,
12
+ )
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Bitsy
2
+ class GetLatestBlock
3
+
4
+ include LightService::Action
5
+ promises :latest_block
6
+
7
+ executed do |ctx|
8
+ ctx.latest_block = Blockchain.get_latest_block
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ module Bitsy
2
+ class ProcessBlockchainBlockexplorerTransaction
3
+
4
+ include LightService::Action
5
+ expects :payment_depot, :latest_block, :blockchain_transaction
6
+
7
+ executed do |ctx|
8
+ tx = ctx.blockchain_transaction
9
+ next ctx if PaymentTransaction.exists?(transaction_id: tx.hash)
10
+ payment_depot = ctx.payment_depot
11
+ output = tx.outputs.find { |o| o.address == payment_depot.address }
12
+ PaymentTransaction.create!(
13
+ payment_depot_id: payment_depot.id,
14
+ amount: output.value / 100_000_000.0,
15
+ receiving_address: output.address,
16
+ payment_type: "receive",
17
+ confirmations: confirmations_of(tx, ctx.latest_block.height),
18
+ transaction_id: tx.hash,
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ def self.confirmations_of(tx, block_height)
25
+ if tx.block_height
26
+ block_height + 1 - tx.block_height
27
+ else
28
+ 0
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ class AddCheckCountAndCheckedAtToPaymentDepot < ActiveRecord::Migration
2
+ def change
3
+ add_column :bitsy_payment_depots, :check_count, :integer, null: false, default: 0
4
+ add_column :bitsy_payment_depots, :checked_at, :datetime
5
+ end
6
+ end
data/lib/bitsy/config.rb CHANGED
@@ -8,7 +8,8 @@ module Bitsy
8
8
  :blockchain_secrets,
9
9
  :blockchain,
10
10
  :debug,
11
- :send_many_log_path
11
+ :send_many_log_path,
12
+ :check_limit,
12
13
  )
13
14
 
14
15
  def initialize(file_path)
data/lib/bitsy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bitsy
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -25,4 +25,5 @@ module Clockwork
25
25
 
26
26
  every(10.minutes, 'Bitsy::TransactionsSyncJob')
27
27
  every(15.minutes, 'Bitsy::ForwardJob')
28
+ every(5.seconds, 'Bitsy::CheckPaymentsJob')
28
29
  end
@@ -22,6 +22,11 @@ Bitsy.configure do |c|
22
22
  # WHen debug is true, no money will be forwarded. This is for testing
23
23
  # purposes.
24
24
  c.debug = false
25
+
26
+ # Bitsy will check the transactions of the payment depot manually if it hasn't
27
+ # been paid for yet. It will check 40 times, in approximately check count ** 2
28
+ # power intervals. Ex: check_count = 3, then it will check again in 9 seconds.
29
+ c.check_limit = 40
25
30
  end
26
31
 
27
32
  Sidekiq.configure_server do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitsy-bitcoin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramon Tayag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2014-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -282,6 +282,20 @@ dependencies:
282
282
  - - ">="
283
283
  - !ruby/object:Gem::Version
284
284
  version: '0'
285
+ - !ruby/object:Gem::Dependency
286
+ name: timecop
287
+ requirement: !ruby/object:Gem::Requirement
288
+ requirements:
289
+ - - ">="
290
+ - !ruby/object:Gem::Version
291
+ version: '0'
292
+ type: :development
293
+ prerelease: false
294
+ version_requirements: !ruby/object:Gem::Requirement
295
+ requirements:
296
+ - - ">="
297
+ - !ruby/object:Gem::Version
298
+ version: '0'
285
299
  description: A mountable Rails engine to create a payment server that can handle the
286
300
  money in your Bitcoin ecosystem
287
301
  email:
@@ -301,6 +315,7 @@ files:
301
315
  - app/controllers/bitsy/v1/truncations_controller.rb
302
316
  - app/helpers/bitsy/application_helper.rb
303
317
  - app/jobs/bitsy/blockchain_notification_job.rb
318
+ - app/jobs/bitsy/check_payments_job.rb
304
319
  - app/jobs/bitsy/forward_job.rb
305
320
  - app/jobs/bitsy/transactions_sync_job.rb
306
321
  - app/models/bitsy/blockchain_notification.rb
@@ -312,15 +327,19 @@ files:
312
327
  - app/services/bitsy/build_send_many_hash.rb
313
328
  - app/services/bitsy/build_send_many_hash_for_transaction.rb
314
329
  - app/services/bitsy/build_send_many_hash_with_transaction_fee.rb
330
+ - app/services/bitsy/check_for_payments.rb
331
+ - app/services/bitsy/check_payment_depot_transaction.rb
332
+ - app/services/bitsy/check_payment_depot_transactions.rb
315
333
  - app/services/bitsy/compute_amount_for_splitting.rb
316
334
  - app/services/bitsy/create_payment_depot.rb
317
335
  - app/services/bitsy/forward_payments.rb
318
336
  - app/services/bitsy/forward_tax_calculator.rb
337
+ - app/services/bitsy/get_latest_block.rb
319
338
  - app/services/bitsy/instantiate_blockchain_wallet.rb
320
339
  - app/services/bitsy/log_send_many.rb
340
+ - app/services/bitsy/process_blockchain_blockexplorer_transaction.rb
321
341
  - app/services/bitsy/process_blockchain_notification.rb
322
342
  - app/services/bitsy/send_payments.rb
323
- - app/services/bitsy/updating/get_latest_block.rb
324
343
  - app/services/bitsy/updating/select_transactions.rb
325
344
  - app/services/bitsy/updating/sync_transactions.rb
326
345
  - app/services/bitsy/updating/update_transaction.rb
@@ -338,6 +357,7 @@ files:
338
357
  - db/migrate/20141109072242_change_payment_depot_balance_cache_default.rb
339
358
  - db/migrate/20141115001846_add_total_received_amount_cache_to_payment_depots.rb
340
359
  - db/migrate/20141129064958_change_total_received_amount_cache_to_decimal.rb
360
+ - db/migrate/20141209131150_add_check_count_and_checked_at_to_payment_depot.rb
341
361
  - lib/bitsy-bitcoin.rb
342
362
  - lib/bitsy.rb
343
363
  - lib/bitsy/config.rb
@@ -1,14 +0,0 @@
1
- module Bitsy
2
- module Updating
3
- class GetLatestBlock
4
-
5
- include LightService::Action
6
- promises :latest_block
7
-
8
- executed do |ctx|
9
- ctx.latest_block = Blockchain.get_latest_block
10
- end
11
-
12
- end
13
- end
14
- end