stellar_base-rails 0.6.1 → 0.7.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: 9f634660d3aa8e4f2c16dbde0657f5b5be1d23f881911e863350a8800f1e6b5e
4
- data.tar.gz: e4d0e9d9ff41c4cdb82f195935b4423181ef3b3631f8238b96498839a1dc6f6c
3
+ metadata.gz: ecb3cdd428227714bb071c59f1ac3b40c85f131522f376363fc0722aa1272a71
4
+ data.tar.gz: 023c9c4dcf96b9a0d87a7cfb81d6059b987bc81bb5d4f3f2d82361db201db183
5
5
  SHA512:
6
- metadata.gz: 8104f50041913ea06f3ff64be43d1a0b428494077239899683c4a5f662641707c85e81099b3dc2e8f2354ab1cbe857ccb4a8664ab82a0706630fc4f6908a18bd
7
- data.tar.gz: 3277b1888a740c362b9789daa9c2adc3835841c360c5f2e4fb87eae8b1e8a329a8c51105f536e185b75fded1f8044559625a627201c4aeaf863c6d524400e7ac
6
+ metadata.gz: 9a205a0ec92f9c2b6de8bcddb9e02cff60122318039c537912844666ffdf4bd1cfe94f16343b63d2a45bf3215059d5af5daba40af2b2a72161c38e7099efe9d1
7
+ data.tar.gz: 4aa5d187e3f65f1a77b6a643aed6ca9b8ce76bbe04666342c2093f9fad2a7a67eebe657bfa4c28f3b5527fae5455af717e68c07565a481d2938d79fc3ec9f574
data/README.md CHANGED
@@ -54,6 +54,11 @@ This is the same distribution account that is setup in bridge. Currently, it is
54
54
  - Default: https://horizon.stellar.org
55
55
  - This is where the engine will check bridge callbacks if `c.check_bridge_callbacks_authenticity` is turned on
56
56
 
57
+ #### c.stellar_network
58
+ - Values(s): "public" or "testnet"
59
+ - Default: testnet
60
+ - This tells stellar_base-rails what network it will use to send assets/lumens with. Currently used by the deposit module. If someone deposits a real asset, stellar_base sends the corresponding token to the requester.
61
+
57
62
  #### c.stellar_toml
58
63
  - Value(s): Hash, follow Stellar's [documentation](https://www.stellar.org/developers/guides/concepts/stellar-toml.html) for `stellar.toml`
59
64
  - Example:
@@ -83,6 +88,16 @@ And then execute:
83
88
  $ bundle
84
89
  ```
85
90
 
91
+ If you're using `StellarBase.on_deposit_trigger` you'll need to install:
92
+
93
+ ```
94
+ # Gemfile
95
+ gem("stellar-sdk", {
96
+ github: "bloom-solutions/ruby-stellar-sdk",
97
+ branch: "payment-memo",
98
+ })
99
+ ```
100
+
86
101
  ## Development
87
102
 
88
103
  ```ruby
@@ -0,0 +1,7 @@
1
+ module StellarBase
2
+ class Deposit < ApplicationRecord
3
+
4
+ belongs_to :deposit_request, class_name: "StellarBase::DepositRequest"
5
+
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class CreateDeposit
4
+
5
+ extend LightService::Action
6
+ expects :tx_id, :amount, :deposit_request
7
+ promises :deposit
8
+
9
+ executed do |c|
10
+ c.deposit = Deposit.create!(
11
+ tx_id: c.tx_id,
12
+ amount: c.amount,
13
+ deposit_request_id: c.deposit_request.id,
14
+ )
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class FindConfig
4
+
5
+ extend LightService::Action
6
+
7
+ expects :network
8
+ promises :deposit_config
9
+
10
+ executed do |c|
11
+ depositable_assets = StellarBase.configuration.depositable_assets
12
+ c.deposit_config = depositable_assets.find do |asset|
13
+ asset[:network] == c.network
14
+ end
15
+
16
+ if c.deposit_config.blank?
17
+ c.fail_and_return! "No depositable_asset config for #{c.network}"
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class FindDeposit
4
+
5
+ extend LightService::Action
6
+
7
+ expects :deposit_request, :tx_id
8
+ promises :deposit
9
+
10
+ executed do |c|
11
+ c.deposit = Deposit.find_by(
12
+ deposit_request_id: c.deposit_request.id,
13
+ tx_id: c.tx_id,
14
+ )
15
+
16
+ c.skip_remaining! if c.deposit.present?
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class FindDepositRequest
4
+
5
+ extend LightService::Action
6
+
7
+ expects :deposit_config, :deposit_address
8
+ promises :deposit_request
9
+
10
+ executed do |c|
11
+ deposit_address = c.deposit_address
12
+ asset_code = c.deposit_config[:asset_code]
13
+
14
+ c.deposit_request = DepositRequest.find_by(
15
+ deposit_address: deposit_address,
16
+ asset_code: asset_code,
17
+ )
18
+
19
+ if c.deposit_request.blank?
20
+ c.fail_and_return! "No DepositRequest found for " +
21
+ [asset_code, deposit_address].join(":")
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class InitStellarAmount
4
+
5
+ extend LightService::Action
6
+ expects :stellar_asset, :amount
7
+ promises :stellar_amount
8
+
9
+ executed do |c|
10
+ c.stellar_amount = Stellar::Amount.new(c.amount, c.stellar_asset)
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class InitStellarAsset
4
+
5
+ extend LightService::Action
6
+ expects :deposit_request, :issuer_account
7
+ promises :stellar_asset
8
+
9
+ executed do |c|
10
+ c.stellar_asset = Stellar::Asset.alphanum4(
11
+ c.deposit_request.asset_code,
12
+ c.issuer_account.keypair,
13
+ )
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class InitStellarDistributionAccount
4
+
5
+ extend LightService::Action
6
+ expects :deposit_config
7
+ promises :distribution_account
8
+
9
+ executed do |c|
10
+ config = c.deposit_config
11
+ c.distribution_account = Stellar::Account
12
+ .from_seed(config[:distributor_seed])
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class InitStellarIssuerAccount
4
+
5
+ extend LightService::Action
6
+ expects :deposit_config
7
+ promises :issuer_account
8
+
9
+ executed do |c|
10
+ config = c.deposit_config
11
+ c.issuer_account = Stellar::Account.from_address(config[:issuer])
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class InitStellarRecipientAccount
4
+
5
+ extend LightService::Action
6
+ expects :deposit_request
7
+ promises :recipient_account
8
+
9
+ executed do |c|
10
+ c.recipient_account = Stellar::Account
11
+ .from_address(c.deposit_request.account_id)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class SendAsset
4
+
5
+ extend LightService::Action
6
+ expects(
7
+ :deposit_request,
8
+ :distribution_account,
9
+ :issuer_account,
10
+ :recipient_account,
11
+ :stellar_amount,
12
+ :stellar_sdk_client,
13
+ )
14
+ promises :stellar_tx_id
15
+
16
+ executed do |c|
17
+ msg = [
18
+ "issuing account: #{c.issuer_account.address}",
19
+ "recipient account: #{c.recipient_account.address}",
20
+ "stellar amount: #{c.stellar_amount.amount}#{c.stellar_amount.asset}",
21
+ "distribution account: #{c.distribution_account.address}",
22
+ "recipient memo: #{c.deposit_request.memo}",
23
+ ].join("; ")
24
+
25
+ Rails.logger.info(msg)
26
+
27
+ # TODO: handle when this fails
28
+ response = c.stellar_sdk_client.send_payment(
29
+ from: c.distribution_account,
30
+ to: c.recipient_account,
31
+ amount: c.stellar_amount,
32
+ memo: c.deposit_request.memo,
33
+ )
34
+
35
+ c.stellar_tx_id = response.to_hash["hash"]
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class Trigger
4
+
5
+ extend LightService::Organizer
6
+
7
+ def self.call(network, deposit_address, tx_id, amount)
8
+ with(
9
+ network: network,
10
+ deposit_address: deposit_address,
11
+ tx_id: tx_id,
12
+ amount: amount,
13
+ ).reduce(actions)
14
+ end
15
+
16
+ def self.actions
17
+ [
18
+ FindConfig,
19
+ FindDepositRequest,
20
+ FindDeposit,
21
+ CreateDeposit,
22
+ InitStellarClient,
23
+ InitStellarIssuerAccount,
24
+ InitStellarRecipientAccount,
25
+ InitStellarDistributionAccount,
26
+ InitStellarAsset,
27
+ InitStellarAmount,
28
+ SendAsset,
29
+ UpdateDeposit, # save the stellar operation id on it
30
+ ]
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ module StellarBase
2
+ module DepositRequests
3
+ class UpdateDeposit
4
+
5
+ extend LightService::Action
6
+ expects :stellar_tx_id, :deposit
7
+
8
+ executed do |c|
9
+ c.deposit.update!(stellar_tx_id: c.stellar_tx_id)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module StellarBase
2
+ class InitStellarClient
3
+
4
+ extend LightService::Action
5
+ promises :stellar_sdk_client
6
+
7
+ executed do |c|
8
+ c.stellar_sdk_client = Stellar::Client.new(
9
+ horizon: StellarBase.configuration.horizon_url,
10
+ )
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ class CreateStellarBaseDeposits < ActiveRecord::Migration[5.2]
2
+
3
+ def change
4
+ create_table :stellar_base_deposits do |t|
5
+ t.integer :deposit_request_id, index: true
6
+ t.string :tx_id, index: true
7
+ t.decimal :amount
8
+ t.string :stellar_tx_id
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_foreign_key(:stellar_base_deposits, :stellar_base_deposit_requests, {
14
+ column: :deposit_request_id,
15
+ })
16
+ end
17
+
18
+ end
data/lib/stellar_base.rb CHANGED
@@ -17,68 +17,87 @@ module StellarBase
17
17
  include GemConfig::Base
18
18
 
19
19
  with_configuration do
20
+ has :bridge_callbacks_mac_key, default: false
21
+ has :check_bridge_callbacks_authenticity, default: false
22
+ has :check_bridge_callbacks_mac_payload, default: false
23
+ has :distribution_account, classes: [NilClass, String]
20
24
  has :horizon_url, default: "https://horizon.stellar.org"
21
25
  has :modules, default: [:bridge_callbacks]
22
-
23
- has :distribution_account, classes: [NilClass, String]
24
-
25
26
  has :on_bridge_callback
26
- has :check_bridge_callbacks_authenticity, default: false
27
- has :check_bridge_callbacks_mac_payload, default: false
28
- has :bridge_callbacks_mac_key, default: false
29
-
30
- has :withdrawable_assets, classes: [NilClass, Array, String, Pathname]
31
27
  has :on_withdraw
32
-
33
- has :depositable_assets, classes: [NilClass, Array, String, Pathname]
34
-
28
+ has :stellar_network, classes: String, default: "testnet"
35
29
  has :stellar_toml, classes: Hash, default: {}
30
+ has :depositable_assets, classes: [NilClass, Array, String, Pathname]
31
+ has :withdrawable_assets, classes: [NilClass, Array, String, Pathname]
36
32
  end
37
33
 
38
34
  after_configuration_change do
39
- self.convert_config_withdraw!
40
- self.convert_config_deposit!
35
+ convert_config_withdraw!
36
+ convert_config_deposit!
37
+ set_stellar_network!
41
38
  end
42
39
 
40
+ def self.on_deposit_trigger(network:, deposit_address:, tx_id:, amount:)
41
+ result = DepositRequests::Trigger.(network, deposit_address, tx_id, amount)
42
+ raise StandardError, result.message if result.failure?
43
+ result
44
+ end
43
45
 
44
46
  def self.included_module?(module_name)
45
- self.configuration.modules&.include?(module_name)
47
+ configuration.modules&.include?(module_name)
48
+ end
49
+
50
+ def self.set_stellar_network!
51
+ stellar_network = configuration.stellar_network
52
+
53
+ case stellar_network
54
+ when "public"
55
+ Stellar.default_network = Stellar::Networks::PUBLIC
56
+ when "testnet"
57
+ Stellar.default_network = Stellar::Networks::TESTNET
58
+ else
59
+ raise(
60
+ ArgumentError,
61
+ "'#{stellar_network}' not a valid stellar_network config",
62
+ )
63
+ end
46
64
  end
47
65
 
48
66
  def self.convert_config_deposit!
49
- depositable_assets = self.configuration.depositable_assets
67
+ depositable_assets = configuration.depositable_assets
50
68
  return if depositable_assets.is_a?(Array) || depositable_assets.nil?
51
69
 
52
- self.configuration.depositable_assets =
70
+ configuration.depositable_assets =
53
71
  convert_config!(depositable_assets)
54
72
  end
55
73
 
56
74
  def self.convert_config_withdraw!
57
- withdrawable_assets = self.configuration.withdrawable_assets
75
+ withdrawable_assets = configuration.withdrawable_assets
58
76
  return if withdrawable_assets.is_a?(Array) || withdrawable_assets.nil?
59
77
 
60
- self.configuration.withdrawable_assets =
78
+ configuration.withdrawable_assets =
61
79
  convert_config!(withdrawable_assets)
62
80
  end
63
81
 
64
- private
65
-
66
82
  def self.convert_config!(asset_config)
67
83
  array_of_hashes = try_from_yaml_file_path(asset_config) ||
68
84
  try_from_json(asset_config)
69
85
 
70
86
  array_of_hashes.map(&:with_indifferent_access)
71
87
  end
88
+ private_class_method :convert_config!
72
89
 
73
90
  def self.try_from_json(str)
74
91
  JSON.parse(str)
75
92
  rescue JSON::ParserError
76
93
  end
94
+ private_class_method :try_from_json
77
95
 
78
96
  def self.try_from_yaml_file_path(str)
79
97
  YAML.load_file(str.to_s)
80
98
  rescue Errno::ENOENT
81
99
  end
100
+ private_class_method :try_from_yaml_file_path
82
101
  end
83
102
 
84
103
  require "stellar_base/horizon_client"
@@ -1,7 +1,8 @@
1
1
  FactoryBot.define do
2
-
3
2
  factory :stellar_base_deposit_request, class: "StellarBase::DepositRequest" do
4
3
  deposit_address { "dep-addr" }
4
+ asset_code { "BTCT" }
5
+ asset_type { "crypto" }
5
6
  issuer { "issuer-addr" }
6
7
  memo_type { "text" }
7
8
  memo { "ABAC" }
@@ -13,5 +14,4 @@ FactoryBot.define do
13
14
  fee_fixed { 0 }
14
15
  fee_percent { 0 }
15
16
  end
16
-
17
17
  end
@@ -0,0 +1,11 @@
1
+ FactoryBot.define do
2
+ factory :stellar_base_deposit, class: "StellarBase::Deposit" do
3
+ stellar_tx_id { "issuer-addr" }
4
+ deposit_request do
5
+ StellarBase::DepositRequest.first ||
6
+ association(:stellar_base_deposit_request)
7
+ end
8
+ tx_id { "dep-addr" }
9
+ amount { "crypto" }
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module StellarBase
2
- VERSION = "0.6.1".freeze
2
+ VERSION = "0.7.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stellar_base-rails
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
  - Ace Subido
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-28 00:00:00.000000000 Z
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: disposable
@@ -310,6 +310,7 @@ files:
310
310
  - app/mailers/stellar_base/application_mailer.rb
311
311
  - app/models/stellar_base/application_record.rb
312
312
  - app/models/stellar_base/bridge_callback.rb
313
+ - app/models/stellar_base/deposit.rb
313
314
  - app/models/stellar_base/deposit_request.rb
314
315
  - app/models/stellar_base/stellar_toml.rb
315
316
  - app/models/stellar_base/withdrawal_request.rb
@@ -329,9 +330,22 @@ files:
329
330
  - app/services/stellar_base/bridge_callbacks/process.rb
330
331
  - app/services/stellar_base/bridge_callbacks/verify_mac_payload.rb
331
332
  - app/services/stellar_base/configured_class_runner.rb
333
+ - app/services/stellar_base/deposit_requests/create_deposit.rb
334
+ - app/services/stellar_base/deposit_requests/find_config.rb
335
+ - app/services/stellar_base/deposit_requests/find_deposit.rb
336
+ - app/services/stellar_base/deposit_requests/find_deposit_request.rb
332
337
  - app/services/stellar_base/deposit_requests/find_depositable_asset.rb
338
+ - app/services/stellar_base/deposit_requests/init_stellar_amount.rb
339
+ - app/services/stellar_base/deposit_requests/init_stellar_asset.rb
340
+ - app/services/stellar_base/deposit_requests/init_stellar_distribution_account.rb
341
+ - app/services/stellar_base/deposit_requests/init_stellar_issuer_account.rb
342
+ - app/services/stellar_base/deposit_requests/init_stellar_recipient_account.rb
343
+ - app/services/stellar_base/deposit_requests/send_asset.rb
344
+ - app/services/stellar_base/deposit_requests/trigger.rb
345
+ - app/services/stellar_base/deposit_requests/update_deposit.rb
333
346
  - app/services/stellar_base/gen_memo_for.rb
334
347
  - app/services/stellar_base/gen_random_string.rb
348
+ - app/services/stellar_base/init_stellar_client.rb
335
349
  - app/services/stellar_base/withdrawal_requests/call_on_withdraw.rb
336
350
  - app/services/stellar_base/withdrawal_requests/determine_fee.rb
337
351
  - app/services/stellar_base/withdrawal_requests/determine_max_amount.rb
@@ -348,12 +362,14 @@ files:
348
362
  - db/migrate/20180816110314_create_stellar_base_withdrawal_requests.rb
349
363
  - db/migrate/20180816135847_unique_stellar_base_bridge_callbacks_operation_id.rb
350
364
  - db/migrate/20180925045927_create_stellar_base_deposit_requests.rb
365
+ - db/migrate/20181001070647_create_stellar_base_deposits.rb
351
366
  - lib/stellar_base-rails.rb
352
367
  - lib/stellar_base.rb
353
368
  - lib/stellar_base/engine.rb
354
369
  - lib/stellar_base/factories.rb
355
370
  - lib/stellar_base/factories/bridge_callbacks.rb
356
371
  - lib/stellar_base/factories/deposit_requests.rb
372
+ - lib/stellar_base/factories/deposits.rb
357
373
  - lib/stellar_base/factories/withdrawal_requests.rb
358
374
  - lib/stellar_base/horizon_client.rb
359
375
  - lib/stellar_base/rails/routes.rb