stellar_base-rails 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41b8211660e2ba9849bf8f5a247fd388339864a234c564633b3d8c285060b3fb
4
- data.tar.gz: 9c7acced470d828762a303873c9d22e6b14161320ccf09b09da1b14a51faeebb
3
+ metadata.gz: 952ff7b6daad012f4455ec2fb98a767ad5db4736b8de3b09a145e33371f8125b
4
+ data.tar.gz: fa6d0be7aeb5bdfb813a966773d3770808de28284e4eefe32a4ec3bee5dfc37b
5
5
  SHA512:
6
- metadata.gz: 064d30d29693383dc5a63a70a64ee2218ca58ddfd6f1965c9f3564e5784edd880b7d60e841c5048193d51b0f55edcfff22c5e06ea847fdb318984ed3ef1f6772
7
- data.tar.gz: 76ab21c6ac5bc40dda0f2ded2a3e1cd5cf65398be344e41608df1856acea0f58cb575476e1dba5185b52f7ecae5b6063398925ad40430eb929691b93d325948f
6
+ metadata.gz: c30f41f12189c32702c59e33a0bb166ea185b657fc88c6302ff54d5fb6c7465f54da185351eaae567491151b1594e5a1b90769515b59b13882a37cf25be41223
7
+ data.tar.gz: 906829e824d97bab00df168602ec6614fd34da9aceea958121f0c227f56512dbfdd7e78435f107e2e0d453f3e88a0d1bc893ac77900b843e9757c563e48a23fb
@@ -1,4 +1,7 @@
1
1
  module StellarBase
2
- class ApplicationJob < ActiveJob::Base
2
+ class ApplicationJob
3
+
4
+ include Sidekiq::Worker
5
+
3
6
  end
4
7
  end
@@ -0,0 +1,17 @@
1
+ module StellarBase
2
+ class EnqueueSubscribeAccountsJob < ApplicationJob
3
+
4
+ extend LightService::Organizer
5
+
6
+ def perform
7
+ StellarBase.configuration.subscribe_to_accounts.each do |address|
8
+ AccountSubscription.where(address: address).first_or_create!
9
+ end
10
+
11
+ AccountSubscription.find_each do |account_subscription|
12
+ SubscribeAccountJob.perform_async(account_subscription.id)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module StellarBase
2
+ class SubscribeAccountJob < ApplicationJob
3
+
4
+ sidekiq_options(
5
+ retry: false,
6
+ lock: :until_executed,
7
+ unique_args: :unique_args,
8
+ on_conflict: :log,
9
+ )
10
+
11
+ def perform(account_subscription_id)
12
+ account_subscription = AccountSubscription.find(account_subscription_id)
13
+ SubscribeAccount.(account_subscription)
14
+ end
15
+
16
+ def self.unique_args(args)
17
+ args
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module StellarBase
2
+ class AccountSubscription < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ module StellarBase
2
+ module AccountSubscriptions
3
+ class ExecuteCallback
4
+
5
+ extend LightService::Action
6
+ expects :account_subscription, :tx, :operation
7
+
8
+ executed do |c|
9
+ StellarBase.configuration.on_account_event.(
10
+ c.account_subscription.address,
11
+ c.tx,
12
+ c.operation,
13
+ )
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module StellarBase
2
+ module AccountSubscriptions
3
+ class GetCursor
4
+
5
+ extend LightService::Action
6
+ expects :account_subscription, :stellar_sdk_client
7
+ promises :cursor
8
+
9
+ executed do |c|
10
+ c.cursor = c.account_subscription.cursor
11
+
12
+ next c if c.cursor.present?
13
+
14
+ c.cursor = c.stellar_sdk_client.horizon.
15
+ account(account_id: c.account_subscription.address).
16
+ operations(order: "desc", limit: 1).records.first.id
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module StellarBase
2
+ module AccountSubscriptions
3
+ class GetOperations
4
+
5
+ extend LightService::Action
6
+ expects(
7
+ :cursor,
8
+ :account_subscription,
9
+ :stellar_sdk_client,
10
+ :operation_limit,
11
+ )
12
+ promises :operations
13
+
14
+ executed do |c|
15
+ c.operations = c.stellar_sdk_client.horizon.
16
+ account(account_id: c.account_subscription.address).
17
+ operations(order: "asc", limit: c.operation_limit).records
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module StellarBase
2
+ module AccountSubscriptions
3
+ class GetTx
4
+
5
+ extend LightService::Action
6
+ expects :stellar_sdk_client, :operation
7
+ promises :tx
8
+
9
+ executed do |c|
10
+ hash = c.operation.transaction_hash
11
+ c.tx = c.stellar_sdk_client.horizon.transaction(hash: hash)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ module StellarBase
2
+ class SaveCursor
3
+
4
+ extend LightService::Action
5
+ expects :account_subscription, :operations
6
+
7
+ executed do |c|
8
+ c.account_subscription.update_attributes!(cursor: c.operations.last.id)
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ module StellarBase
2
+ class SubscribeAccount
3
+
4
+ extend LightService::Organizer
5
+ OPERATION_LIMIT = 200
6
+
7
+ def self.call(account_subscription, operation_limit: OPERATION_LIMIT)
8
+ with(
9
+ account_subscription: account_subscription,
10
+ operation_limit: operation_limit,
11
+ ).reduce(actions)
12
+ end
13
+
14
+ def self.actions
15
+ [
16
+ InitStellarClient,
17
+ AccountSubscriptions::GetCursor,
18
+ AccountSubscriptions::GetOperations,
19
+ iterate(:operations, [
20
+ AccountSubscriptions::GetTx,
21
+ AccountSubscriptions::ExecuteCallback,
22
+ ]),
23
+ AccountSubscriptions::SaveCursor,
24
+ ]
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ class CreateStellarBaseAccountSubscriptions < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :stellar_base_account_subscriptions do |t|
4
+ t.string :address, null: false
5
+ t.string :cursor
6
+ t.index :address, unique: true
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
data/lib/stellar_base.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "addressable"
1
2
  require "gem_config"
2
3
  require "stellar-base"
3
4
  require "light-service"
@@ -10,6 +11,9 @@ require "reform"
10
11
  require "reform/form/coercion"
11
12
  require "representable"
12
13
  require "toml-rb"
14
+ require "sidekiq"
15
+ require "sidekiq-cron"
16
+ require "sidekiq-unique-jobs"
13
17
 
14
18
  require "stellar_base/engine"
15
19
 
@@ -25,6 +29,8 @@ module StellarBase
25
29
  has :modules, default: [:bridge_callbacks]
26
30
  has :on_bridge_callback
27
31
  has :on_withdraw
32
+ has :on_account_event
33
+ has :subscribe_to_accounts, classes: [NilClass, Array]
28
34
  has :stellar_network, classes: String, default: "testnet"
29
35
  has :stellar_toml, classes: Hash, default: {}
30
36
  has :depositable_assets, classes: [NilClass, Array, String, Pathname]
@@ -35,6 +41,7 @@ module StellarBase
35
41
  convert_config_withdraw!
36
42
  convert_config_deposit!
37
43
  set_stellar_network!
44
+ configure_sidekiq_death_handler!
38
45
  end
39
46
 
40
47
  def self.on_deposit_trigger(network:, deposit_address:, tx_id:, amount:)
@@ -98,6 +105,15 @@ module StellarBase
98
105
  rescue Errno::ENOENT
99
106
  end
100
107
  private_class_method :try_from_yaml_file_path
108
+
109
+ def self.configure_sidekiq_death_handler!
110
+ Sidekiq.configure_server do |config|
111
+ config.death_handlers << ->(job, _ex) do
112
+ return unless job['unique_digest']
113
+ SidekiqUniqueJobs::Digests.del(digest: job['unique_digest'])
114
+ end
115
+ end
116
+ end
101
117
  end
102
118
 
103
119
  require "stellar_base/horizon_client"
@@ -0,0 +1,8 @@
1
+ FactoryBot.define do
2
+
3
+ factory :stellar_base_account_subscription, class: "StellarBase::AccountSubscription" do
4
+ address { Stellar::Account.random.address }
5
+ cursor { nil }
6
+ end
7
+
8
+ end
@@ -1,3 +1,3 @@
1
1
  module StellarBase
2
- VERSION = "0.7.1".freeze
2
+ VERSION = "0.8.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stellar_base-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ace Subido
@@ -94,6 +94,62 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: addressable
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sidekiq
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '5.1'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '5.1'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sidekiq-cron
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sidekiq-unique-jobs
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
97
153
  - !ruby/object:Gem::Dependency
98
154
  name: rails
99
155
  requirement: !ruby/object:Gem::Requirement
@@ -276,6 +332,20 @@ dependencies:
276
332
  - - ">="
277
333
  - !ruby/object:Gem::Version
278
334
  version: '0'
335
+ - !ruby/object:Gem::Dependency
336
+ name: rspec-sidekiq
337
+ requirement: !ruby/object:Gem::Requirement
338
+ requirements:
339
+ - - ">="
340
+ - !ruby/object:Gem::Version
341
+ version: '0'
342
+ type: :development
343
+ prerelease: false
344
+ version_requirements: !ruby/object:Gem::Requirement
345
+ requirements:
346
+ - - ">="
347
+ - !ruby/object:Gem::Version
348
+ version: '0'
279
349
  description: API Endpoints for the Stellar Protocol
280
350
  email:
281
351
  - ace.subido@gmail.com
@@ -307,7 +377,10 @@ files:
307
377
  - app/controllers/stellar_base/withdraw_controller.rb
308
378
  - app/helpers/stellar_base/application_helper.rb
309
379
  - app/jobs/stellar_base/application_job.rb
380
+ - app/jobs/stellar_base/enqueue_subscribe_accounts_job.rb
381
+ - app/jobs/stellar_base/subscribe_account_job.rb
310
382
  - app/mailers/stellar_base/application_mailer.rb
383
+ - app/models/stellar_base/account_subscription.rb
311
384
  - app/models/stellar_base/application_record.rb
312
385
  - app/models/stellar_base/bridge_callback.rb
313
386
  - app/models/stellar_base/deposit.rb
@@ -317,6 +390,11 @@ files:
317
390
  - app/representers/stellar_base/application_representer.rb
318
391
  - app/representers/stellar_base/deposit_request_representer.rb
319
392
  - app/representers/stellar_base/withdrawal_request_representer.rb
393
+ - app/services/stellar_base/account_subscriptions/execute_callback.rb
394
+ - app/services/stellar_base/account_subscriptions/get_cursor.rb
395
+ - app/services/stellar_base/account_subscriptions/get_operations.rb
396
+ - app/services/stellar_base/account_subscriptions/get_tx.rb
397
+ - app/services/stellar_base/account_subscriptions/save_cursor.rb
320
398
  - app/services/stellar_base/bridge_callbacks/check.rb
321
399
  - app/services/stellar_base/bridge_callbacks/compare.rb
322
400
  - app/services/stellar_base/bridge_callbacks/get_operation.rb
@@ -346,6 +424,7 @@ files:
346
424
  - app/services/stellar_base/gen_memo_for.rb
347
425
  - app/services/stellar_base/gen_random_string.rb
348
426
  - app/services/stellar_base/init_stellar_client.rb
427
+ - app/services/stellar_base/subscribe_account.rb
349
428
  - app/services/stellar_base/withdrawal_requests/call_on_withdraw.rb
350
429
  - app/services/stellar_base/withdrawal_requests/determine_fee.rb
351
430
  - app/services/stellar_base/withdrawal_requests/determine_max_amount.rb
@@ -363,10 +442,12 @@ files:
363
442
  - db/migrate/20180816135847_unique_stellar_base_bridge_callbacks_operation_id.rb
364
443
  - db/migrate/20180925045927_create_stellar_base_deposit_requests.rb
365
444
  - db/migrate/20181001070647_create_stellar_base_deposits.rb
445
+ - db/migrate/20181003072138_create_stellar_base_account_subscriptions.rb
366
446
  - lib/stellar_base-rails.rb
367
447
  - lib/stellar_base.rb
368
448
  - lib/stellar_base/engine.rb
369
449
  - lib/stellar_base/factories.rb
450
+ - lib/stellar_base/factories/account_subscription.rb
370
451
  - lib/stellar_base/factories/bridge_callbacks.rb
371
452
  - lib/stellar_base/factories/deposit_requests.rb
372
453
  - lib/stellar_base/factories/deposits.rb
@@ -395,7 +476,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
395
476
  version: '0'
396
477
  requirements: []
397
478
  rubyforge_project:
398
- rubygems_version: 2.7.6
479
+ rubygems_version: 2.7.7
399
480
  signing_key:
400
481
  specification_version: 4
401
482
  summary: Mountable Stellar API Endpoints for Rails