bitsy-bitcoin 0.3.2 → 0.5.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: b5339258fc12295fa939531c099a525fe941877e
4
- data.tar.gz: 0acd7760d503fec1511c5c1d71a34e183d4bdcfe
3
+ metadata.gz: 26107994e7453d8a676d90db622bcf7e86122d0a
4
+ data.tar.gz: 09e2b2d6883d04cb6781d6bc142064935a6f80a2
5
5
  SHA512:
6
- metadata.gz: 08083f3b6b50e6545234c5ce0461f1cddbc9c86cbb7d9ca6bf38853d0f80276792a9a24084f0fb32110f1bd1ef807a7710ace7cf60b97d68f88d2b431ebe258e
7
- data.tar.gz: 86b17e3f52e2e40ad5265916cb03215a0ad793b2a76e02919484ac5042b8c777d961247424bd69735019be2f8d1d5b969ce3c28e66aabca2f4931fd831bad594
6
+ metadata.gz: 0d35ac415e27a8bb4b943ed19dc2950293e9955a6b8732115b636a7bc802700bcd9379e42c50437a6ecd2b40558513e8de8fb8c689a900b43397079af4f9313f
7
+ data.tar.gz: 37aa5bf710b689f31aa22ab39f07e52eaf817633a80ec5c21223d9e4eb44ec38177071d21a0e299386600daf4001f463c7e73bab34295905b5f55b10f7e90a24
@@ -33,7 +33,7 @@ module Bitsy
33
33
  end
34
34
 
35
35
  def total_received_amount
36
- self.transactions.credits.sum(:amount)
36
+ read_attribute :total_received_amount_cache
37
37
  end
38
38
 
39
39
  def total_tax_sent
@@ -27,6 +27,8 @@ module Bitsy
27
27
  delegate :added_tax_rate, to: :payment_depot, prefix: true
28
28
  delegate :total_received_amount, to: :payment_depot, prefix: true
29
29
 
30
+ after_create :update_payment_depot_cache
31
+
30
32
  def forward_tax_fee
31
33
  ForwardTaxCalculator.calculate(self.amount,
32
34
  self.payment_depot_min_payment,
@@ -38,5 +40,13 @@ module Bitsy
38
40
  def owner_fee
39
41
  self.amount - self.forward_tax_fee
40
42
  end
43
+
44
+ def update_payment_depot_cache
45
+ if payment_type == "receive"
46
+ payment_depot.total_received_amount_cache += amount
47
+ payment_depot.save
48
+ end
49
+ end
50
+
41
51
  end
42
52
  end
@@ -2,33 +2,45 @@ module Bitsy
2
2
  class BuildSendManyHash
3
3
 
4
4
  include LightService::Action
5
+ expects :payment_transactions, :amount_for_splitting, :total_amount
6
+ promises :send_many_hash, :computed_transaction_fee
5
7
 
6
8
  executed do |ctx|
7
- payment_txs = ctx.fetch(:payment_transactions)
9
+ ctx.send_many_hash = send_many_hash_from(ctx)
10
+ total_payments = total_payments_from(ctx.send_many_hash)
11
+ ctx.computed_transaction_fee =
12
+ (ctx.total_amount * 100_000_000.0 - total_payments).round
13
+ end
14
+
15
+ private
16
+
17
+ def self.send_many_hash_from(ctx)
8
18
  send_many_hash = {}
9
19
 
10
- payment_txs.each do |payment_tx|
11
- tx_hash = for_transaction(payment_tx)
12
- tx_hash.each do |address, value|
20
+ ctx.payment_transactions.each do |tx|
21
+ tx_hash = BuildSendManyHashForTransaction.execute(
22
+ payment_transaction: tx,
23
+ amount_for_splitting: ctx.amount_for_splitting,
24
+ total_amount: ctx.total_amount,
25
+ ).transaction_send_many_hash
26
+
27
+ tx_hash.each do |address, amount|
13
28
  if send_many_hash.has_key?(address)
14
- send_many_hash[address] = send_many_hash[address] + value
29
+ send_many_hash[address] += amount
15
30
  else
16
- send_many_hash[address] = value
31
+ send_many_hash[address] = amount
17
32
  end
18
33
  end
19
34
  end
20
35
 
21
- ctx[:send_many_hash] = send_many_hash
36
+ send_many_hash
22
37
  end
23
38
 
24
- def self.for_transaction(payment_transaction)
25
- payment_depot = payment_transaction.payment_depot
26
- owner_address = payment_depot.owner_address
27
- tax_address = payment_depot.tax_address
28
- {
29
- tax_address => payment_transaction.forward_tax_fee.to_f * 100_000_000,
30
- owner_address => payment_transaction.owner_fee.to_f * 100_000_000,
31
- }
39
+ def self.total_payments_from(send_many_hash)
40
+ send_many_hash.inject(0) do |sum, hash|
41
+ sum += hash[1]
42
+ sum
43
+ end
32
44
  end
33
45
 
34
46
  end
@@ -0,0 +1,44 @@
1
+ module Bitsy
2
+ class BuildSendManyHashForTransaction
3
+
4
+ include LightService::Action
5
+ expects :payment_transaction, :amount_for_splitting, :total_amount
6
+ promises :transaction_send_many_hash
7
+
8
+ executed do |ctx|
9
+ tax_amount = tax_amount_from(ctx)
10
+ owner_amount = owner_amount_from(ctx)
11
+
12
+ payment_depot = ctx.payment_transaction.payment_depot
13
+
14
+ ctx.transaction_send_many_hash = {
15
+ payment_depot.tax_address => tax_amount,
16
+ payment_depot.owner_address => owner_amount,
17
+ }
18
+ end
19
+
20
+ private
21
+
22
+ def self.tax_amount_from(ctx)
23
+ fee_less_share_for_transaction_fee(
24
+ ctx.payment_transaction.forward_tax_fee,
25
+ ctx.amount_for_splitting,
26
+ ctx.total_amount,
27
+ )
28
+ end
29
+
30
+ def self.owner_amount_from(ctx)
31
+ fee_less_share_for_transaction_fee(
32
+ ctx.payment_transaction.owner_fee,
33
+ ctx.amount_for_splitting,
34
+ ctx.total_amount,
35
+ )
36
+ end
37
+
38
+ def self.fee_less_share_for_transaction_fee(fee, amount_for_splitting, total_amount)
39
+ percent_of_total = fee / total_amount
40
+ (percent_of_total * 100_000_000 * amount_for_splitting).to_i
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ module Bitsy
2
+ class BuildSendManyHashWithTransactionFee
3
+
4
+ include LightService::Organizer
5
+ include LightService::Action
6
+ expects :payment_transactions, :transaction_fee
7
+
8
+ executed do |ctx|
9
+ with(ctx).reduce(ComputeAmountForSplitting, BuildSendManyHash)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Bitsy
2
+ class ComputeAmountForSplitting
3
+
4
+ include LightService::Action
5
+ expects :payment_transactions, :transaction_fee
6
+ promises :amount_for_splitting, :total_amount
7
+
8
+ executed do |ctx|
9
+ ctx.total_amount = ctx.payment_transactions.sum(:amount)
10
+ ctx.amount_for_splitting = ctx.total_amount - ctx.transaction_fee
11
+ end
12
+
13
+ end
14
+ end
@@ -9,10 +9,12 @@ module Bitsy
9
9
 
10
10
  return unless past_threshold?(payment_transactions)
11
11
  with(
12
- payment_transactions: payment_transactions
12
+ payment_transactions: payment_transactions,
13
+ transaction_fee: Bitsy.config.transaction_fee,
13
14
  ).reduce([
14
15
  InstantiateBlockchainWallet,
15
- BuildSendManyHash,
16
+ BuildSendManyHashWithTransactionFee,
17
+ LogSendMany,
16
18
  SendPayments,
17
19
  AssociatesTransactions
18
20
  ])
@@ -0,0 +1,18 @@
1
+ module Bitsy
2
+ class LogSendMany
3
+
4
+ include LightService::Action
5
+ expects :payment_transactions, :computed_transaction_fee, :send_many_hash
6
+
7
+ executed do |ctx|
8
+ message = ["Will forward transactions for payment transactions:"]
9
+ message << ctx.payment_transactions.map(&:id).join(", ")
10
+ message << "hash: #{ctx.send_many_hash.inspect}"
11
+ message = message.join("\n")
12
+
13
+ logger = Logger.new(Bitsy.config.send_many_log_path)
14
+ logger.info(message)
15
+ end
16
+
17
+ end
18
+ end
@@ -2,11 +2,20 @@ module Bitsy
2
2
  class SendPayments
3
3
 
4
4
  include LightService::Action
5
- expects :send_many_hash, :wallet
5
+ expects :send_many_hash, :wallet, :computed_transaction_fee
6
6
  promises :forwarding_transaction_id
7
7
 
8
8
  executed do |ctx|
9
- payment_response = ctx.wallet.send_many(ctx.send_many_hash)
9
+ if Bitsy.config.debug
10
+ ctx.forwarding_transaction_id = nil
11
+ next ctx
12
+ end
13
+
14
+ payment_response = ctx.wallet.send_many(
15
+ ctx.send_many_hash,
16
+ nil,
17
+ ctx.computed_transaction_fee,
18
+ )
10
19
  ctx.forwarding_transaction_id = payment_response.tx_hash
11
20
  end
12
21
 
@@ -13,6 +13,14 @@ Bitsy.configure do |c|
13
13
  # List of allowed secrets from Blockchain.info. The callback URL should look
14
14
  # like this http://app.com/bitsy/v1/blockchain_notifications?secret=mysecret
15
15
  c.blockchain_secrets = %w(secret)
16
+
17
+ # Write out the payment transactions, addresses, and other details. These
18
+ # transactions are the ones being forwarded.
19
+ c.send_many_log_path = Rails.root.join("log", "send_many.log")
20
+
21
+ # WHen debug is true, no money will be forwarded. This is for testing
22
+ # purposes.
23
+ c.debug = false
16
24
  end
17
25
 
18
26
  Sidekiq.configure_server do |config|
@@ -0,0 +1,5 @@
1
+ class AddTotalReceivedAmountCacheToPaymentDepots < ActiveRecord::Migration
2
+ def change
3
+ add_column :bitsy_payment_depots, :total_received_amount_cache, :float, null: false, default: 0
4
+ end
5
+ end
data/lib/bitsy/config.rb CHANGED
@@ -7,6 +7,8 @@ module Bitsy
7
7
  :safe_confirmation_threshold,
8
8
  :blockchain_secrets,
9
9
  :blockchain,
10
+ :debug,
11
+ :send_many_log_path
10
12
  )
11
13
 
12
14
  def initialize(file_path)
data/lib/bitsy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bitsy
2
- VERSION = "0.3.2"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -14,6 +14,14 @@ Bitsy.configure do |c|
14
14
  # List of allowed secrets from Blockchain.info. The callback URL should look
15
15
  # like this http://app.com/bitsy/v1/blockchain_notifications?secret=mysecret
16
16
  c.blockchain_secrets = %w(secret)
17
+
18
+ # Write out the payment transactions, addresses, and other details. These
19
+ # transactions are the ones being forwarded.
20
+ c.send_many_log_path = Rails.root.join("log", "send_many.log")
21
+
22
+ # WHen debug is true, no money will be forwarded. This is for testing
23
+ # purposes.
24
+ c.debug = false
17
25
  end
18
26
 
19
27
  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.3.2
4
+ version: 0.5.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-11-13 00:00:00.000000000 Z
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -310,10 +310,14 @@ files:
310
310
  - app/services/bitsy/amount_for_initial_tax_calculator.rb
311
311
  - app/services/bitsy/associates_transactions.rb
312
312
  - app/services/bitsy/build_send_many_hash.rb
313
+ - app/services/bitsy/build_send_many_hash_for_transaction.rb
314
+ - app/services/bitsy/build_send_many_hash_with_transaction_fee.rb
315
+ - app/services/bitsy/compute_amount_for_splitting.rb
313
316
  - app/services/bitsy/create_payment_depot.rb
314
317
  - app/services/bitsy/forward_payments.rb
315
318
  - app/services/bitsy/forward_tax_calculator.rb
316
319
  - app/services/bitsy/instantiate_blockchain_wallet.rb
320
+ - app/services/bitsy/log_send_many.rb
317
321
  - app/services/bitsy/process_blockchain_notification.rb
318
322
  - app/services/bitsy/send_payments.rb
319
323
  - app/services/bitsy/updating/get_latest_block.rb
@@ -332,6 +336,7 @@ files:
332
336
  - db/migrate/20141106123922_create_bitsy_blockchain_notifications.rb
333
337
  - db/migrate/20141108002656_drop_occurred_at_and_received_at_from_payment_transactions.rb
334
338
  - db/migrate/20141109072242_change_payment_depot_balance_cache_default.rb
339
+ - db/migrate/20141115001846_add_total_received_amount_cache_to_payment_depots.rb
335
340
  - lib/bitsy-bitcoin.rb
336
341
  - lib/bitsy.rb
337
342
  - lib/bitsy/config.rb