stellar_base-rails 3.0.1 → 3.1.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
  SHA256:
3
- metadata.gz: dd04e0a80166c2240b0758ebaa6eae6cefab1304a57062ea349613e0698339f4
4
- data.tar.gz: 45b0f0c8323a6229b421b5baba5fab3d44cbf034396263f4ef56458ff7f9db6c
3
+ metadata.gz: 74b62f61385eba2ad71a788469192da7d9056875055582a17c38e982688f0bfc
4
+ data.tar.gz: 37012d044e08a99b22116cfc54b243a27440721ed4a26eee198c022d10d327c8
5
5
  SHA512:
6
- metadata.gz: 29006920d37951497ce155702bef8c6f0537a69ccf105a7a0ada4beafcc585fb090d06b5dbfbafd012c56e7c9ff32a350bf8667b9015b6fe94800ab882d457f1
7
- data.tar.gz: ddb54313dad9a7fa4299f4a062edd5042f234e9673d51872f7b7bad64dc494b86294918ab03b636df0ace59884400dcc3974ca02696d5cd98cd45e2f2f6a85d0
6
+ metadata.gz: 2868b757d5cea6de7ca3a25e9d4f3131f029954f07a2f995b674c858fb6602b00c041a1877176d1c9f106a8d2e543ef2cbd608129e116caa9b9fd01f3f2703ee
7
+ data.tar.gz: 54ef4814eb47f06e8d7148b4f6eaf1d68b0ba89d64b38d56b2af169b957e7962a2009cfffaf203c6ad918e6088eb1cd78507b043c4bdf12e5149f60033fbe26e
data/README.md CHANGED
@@ -20,6 +20,7 @@ rails db:migrate
20
20
  ```
21
21
 
22
22
  ### Configuration
23
+
23
24
  Create an initializer in your rails application:
24
25
 
25
26
  ```
@@ -115,14 +116,7 @@ gem("stellar-sdk", {
115
116
 
116
117
  ## Development
117
118
 
118
- ```ruby
119
- cp spec/config.yml{.sample,}
120
- ```
121
-
122
- Edit the `spec/config.yml` file.
123
-
124
119
  ```sh
125
- cd spec/dummy
126
120
  rails db:migrate
127
121
  rails db:migrate RAILS_ENV=test
128
122
  ```
@@ -54,6 +54,8 @@ module StellarBase
54
54
  options["model"].min_amount = 0.0
55
55
 
56
56
  # Make Deposits free unless we want it configured
57
+ #
58
+ # TODO: this should come from `CallFeeFrom.(details[:fee_from)`
57
59
  options["model"].fee_fixed = details[:fee_fixed] || 0.0
58
60
  options["model"].fee_percent = details[:fee_percent] || 0.0
59
61
  end
@@ -0,0 +1,50 @@
1
+ module StellarBase
2
+ module FeeRequests
3
+ module Contracts
4
+ class Show < ApplicationContract
5
+
6
+ property :operation
7
+ property :type
8
+ property :asset_code
9
+ property :amount
10
+
11
+ OPERATION_TYPES = %w[deposit withdraw].freeze
12
+
13
+ validates(
14
+ :operation,
15
+ presence: true,
16
+ inclusion: {
17
+ in: OPERATION_TYPES,
18
+ message: "%{value} is not a valid operation",
19
+ },
20
+ )
21
+ validates(:asset_code, :amount, presence: true)
22
+
23
+ validate :check_valid_asset_code
24
+
25
+ def check_valid_asset_code
26
+ return if operation.blank?
27
+ asset_codes = if operation == "deposit"
28
+ StellarBase.configuration.depositable_assets
29
+ &.map do |asset|
30
+ asset[:asset_code]
31
+ end
32
+ else
33
+ StellarBase.configuration.withdrawable_assets
34
+ &.map do |asset|
35
+ asset[:asset_code]
36
+ end
37
+ end
38
+
39
+ return if asset_codes.include? asset_code
40
+
41
+ errors.add(
42
+ :asset_code,
43
+ "invalid asset_code. Valid asset_codes: #{asset_codes.join(', ')}",
44
+ )
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ module StellarBase
2
+ module FeeRequests
3
+ class FeeRequestPolicy
4
+
5
+ def initialize(_, _)
6
+ end
7
+
8
+ def show?
9
+ StellarBase.included_module?(:fees) &&
10
+ (
11
+ StellarBase.included_module?(:withdraw) ||
12
+ StellarBase.included_module?(:deposit)
13
+ )
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module StellarBase
2
+ module FeeRequests
3
+ module Operations
4
+ class Show < ApplicationOperation
5
+
6
+ POLICY_ERROR_MESSAGE = "You are unauthorized to request fee details".freeze
7
+
8
+ step self::Policy::Pundit(FeeRequestPolicy, :show?)
9
+ failure :set_policy_error!
10
+ step Model(FeeRequest, :new)
11
+ step Contract::Build(constant: Contracts::Show)
12
+ step Contract::Validate(key: :fee_request)
13
+ step Contract::Persist(method: :sync)
14
+ step :find_asset_details!
15
+ success :set_fee_response!
16
+
17
+ private
18
+
19
+ def set_policy_error!(options, params:, **)
20
+ options["result.policy.message"] = POLICY_ERROR_MESSAGE
21
+ end
22
+
23
+ def find_asset_details!(options, params:, **)
24
+ details = FindAssetDetails.(
25
+ operation: params[:fee_request][:operation],
26
+ asset_code: params[:fee_request][:asset_code],
27
+ )
28
+ options[:asset_details] = details.presence || {}
29
+ end
30
+
31
+ def set_fee_response!(options, model:, asset_details:, params:, **)
32
+ model.fee_response = FeeRequests::CallFeeFrom.(
33
+ fee_request: model,
34
+ fee_from: asset_details[:fee_from],
35
+ )
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -52,8 +52,12 @@ module StellarBase
52
52
  options["model"].min_amount = 0.0
53
53
  options["model"].max_amount =
54
54
  DetermineMaxAmount.(withdrawal_asset_details[:max_amount_from])
55
+
56
+ # TODO: This should come from `CallFeeFrom.(details[:fee_from])`
57
+ # or dest_extra
55
58
  options["model"].fee_fixed =
56
59
  DetermineFee.(withdrawal_asset_details[:fee_fixed])
60
+
57
61
  options["model"].fee_percent =
58
62
  DetermineFee.(withdrawal_asset_details[:fee_percent])
59
63
  options["model"].fee_network = fee_network
@@ -1,5 +1,18 @@
1
1
  module StellarBase
2
2
  class ApplicationController < ActionController::Base
3
3
  protect_from_forgery with: :null_session
4
+
5
+ private
6
+
7
+ def errors_from(op)
8
+ unless op["result.policy.default"].success?
9
+ return { error: op["result.policy.message"] }
10
+ end
11
+
12
+ if op["contract.default"].errors.any?
13
+ return { error: op["contract.default"].errors }
14
+ end
15
+ end
16
+
4
17
  end
5
18
  end
@@ -21,16 +21,6 @@ module StellarBase
21
21
 
22
22
  private
23
23
 
24
- def errors_from(op)
25
- unless op["result.policy.default"].success?
26
- return { errors: op["result.policy.message"] }
27
- end
28
-
29
- if op["contract.default"].errors.any?
30
- return { errors: op["contract.default"].errors }
31
- end
32
- end
33
-
34
24
  def balance_params
35
25
  params.permit(:asset_code).to_hash.with_indifferent_access
36
26
  end
@@ -12,10 +12,7 @@ module StellarBase
12
12
  representer = DepositRequestRepresenter.new(twin)
13
13
  render json: representer
14
14
  else
15
- render(
16
- json: { error: op["contract.default"].errors },
17
- status: :unprocessable_entity,
18
- )
15
+ render json: errors_from(op), status: :unprocessable_entity
19
16
  end
20
17
  end
21
18
  end
@@ -0,0 +1,31 @@
1
+ module StellarBase
2
+ class FeeRequestsController < ApplicationController
3
+
4
+ def show
5
+ op = FeeRequests::Operations::Show.(fee_request: fee_request_params)
6
+
7
+ respond_to do |f|
8
+ f.json do
9
+ if op.success?
10
+ representer = FeeRequestRepresenter.new(op["model"])
11
+ render json: representer
12
+ else
13
+ render json: errors_from(op), status: :unprocessable_entity
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def fee_request_params
22
+ params.permit(*%i[
23
+ asset_code
24
+ amount
25
+ type
26
+ operation
27
+ ]).to_hash.with_indifferent_access
28
+ end
29
+
30
+ end
31
+ end
@@ -12,10 +12,7 @@ module StellarBase
12
12
  representer = WithdrawalRequestRepresenter.new(twin)
13
13
  render json: representer
14
14
  else
15
- render(
16
- json: { error: op["result.policy.message"] || op["contract.default"].errors },
17
- status: :unprocessable_entity,
18
- )
15
+ render json: errors_from(op), status: :unprocessable_entity
19
16
  end
20
17
  end
21
18
  end
@@ -0,0 +1,13 @@
1
+ module StellarBase
2
+ class FeeRequest
3
+
4
+ include Virtus.model
5
+
6
+ attribute :operation, String
7
+ attribute :amount, BigDecimal
8
+ attribute :asset_code, BigDecimal
9
+ attribute :type, String
10
+ attribute :fee_response, BigDecimal
11
+
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module StellarBase
2
+ class FeeRequestRepresenter < ApplicationRepresenter
3
+
4
+ property :fee_response, as: :fee
5
+
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ module StellarBase
2
+ module FeeRequests
3
+ class CallFeeFrom
4
+
5
+ FEE_FROM_ERROR = "`fee_from` must be a string of an object " \
6
+ "that responds to `.call` or the object itself".freeze
7
+
8
+ def self.call(fee_request:, fee_from:)
9
+ return 0 if fee_from.nil?
10
+ callback = callback_from(fee_from)
11
+ callback.(fee_request)
12
+ end
13
+
14
+ def self.callback_from(fee_from)
15
+ if fee_from.respond_to?(:constantize)
16
+ fee_from = fee_from.constantize
17
+ end
18
+
19
+ if !fee_from.respond_to?(:call)
20
+ fail ArgumentError, FEE_FROM_ERROR
21
+ end
22
+
23
+ fee_from
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module StellarBase
2
+ class FindAssetDetails
3
+
4
+ def self.call(operation:, asset_code:)
5
+ return nil if operation.blank?
6
+ return nil if asset_code.blank?
7
+
8
+ assets = if operation == "deposit"
9
+ StellarBase.configuration.depositable_assets
10
+ elsif operation == "withdraw"
11
+ StellarBase.configuration.withdrawable_assets
12
+ else
13
+ []
14
+ end
15
+ assets.find {|asset| asset[:asset_code] == asset_code }
16
+ end
17
+
18
+ end
19
+ end
data/config/routes.rb CHANGED
@@ -3,5 +3,6 @@ StellarBase::Engine.routes.draw do
3
3
  get "/withdraw" => "withdraw#create", as: :withdraw
4
4
  get "/deposit" => "deposit#create", as: :deposit
5
5
  get "/balance/:asset_code" => "balances#show", as: :balance
6
+ get "/fee" => "fee_requests#show", as: :fee
6
7
  end
7
8
 
@@ -0,0 +1,11 @@
1
+ FactoryBot.define do
2
+
3
+ factory :stellar_base_fee_request, class: "StellarBase::FeeRequest" do
4
+ asset_code { "BTCT" }
5
+ amount { 0.001 }
6
+ operation { "deposit" }
7
+ type { nil }
8
+ fee_response { nil }
9
+ end
10
+
11
+ end
@@ -1,3 +1,3 @@
1
1
  module StellarBase
2
- VERSION = "3.0.1".freeze
2
+ VERSION = "3.1.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: 3.0.1
4
+ version: 3.1.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: 2019-03-07 00:00:00.000000000 Z
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: disposable
@@ -426,6 +426,9 @@ files:
426
426
  - app/concepts/stellar_base/deposit_requests/contracts/create.rb
427
427
  - app/concepts/stellar_base/deposit_requests/deposit_request_policy.rb
428
428
  - app/concepts/stellar_base/deposit_requests/operations/create.rb
429
+ - app/concepts/stellar_base/fee_requests/contracts/show.rb
430
+ - app/concepts/stellar_base/fee_requests/fee_request_policy.rb
431
+ - app/concepts/stellar_base/fee_requests/operations/show.rb
429
432
  - app/concepts/stellar_base/withdrawal_requests/contracts/create.rb
430
433
  - app/concepts/stellar_base/withdrawal_requests/operations/create.rb
431
434
  - app/concepts/stellar_base/withdrawal_requests/withdrawal_request_policy.rb
@@ -433,6 +436,7 @@ files:
433
436
  - app/controllers/stellar_base/balances_controller.rb
434
437
  - app/controllers/stellar_base/bridge_callbacks_controller.rb
435
438
  - app/controllers/stellar_base/deposit_controller.rb
439
+ - app/controllers/stellar_base/fee_requests_controller.rb
436
440
  - app/controllers/stellar_base/home_controller.rb
437
441
  - app/controllers/stellar_base/withdraw_controller.rb
438
442
  - app/errors/stellar_base/not_found_error.rb
@@ -447,6 +451,7 @@ files:
447
451
  - app/models/stellar_base/bridge_callback.rb
448
452
  - app/models/stellar_base/deposit.rb
449
453
  - app/models/stellar_base/deposit_request.rb
454
+ - app/models/stellar_base/fee_request.rb
450
455
  - app/models/stellar_base/stellar_operation.rb
451
456
  - app/models/stellar_base/stellar_payment.rb
452
457
  - app/models/stellar_base/stellar_toml.rb
@@ -455,6 +460,7 @@ files:
455
460
  - app/representers/stellar_base/application_representer.rb
456
461
  - app/representers/stellar_base/balance_representer.rb
457
462
  - app/representers/stellar_base/deposit_request_representer.rb
463
+ - app/representers/stellar_base/fee_request_representer.rb
458
464
  - app/representers/stellar_base/withdrawal_request_representer.rb
459
465
  - app/services/stellar_base/account_subscriptions/execute_account_subscription_callback.rb
460
466
  - app/services/stellar_base/account_subscriptions/find_or_create_operation.rb
@@ -490,6 +496,8 @@ files:
490
496
  - app/services/stellar_base/deposit_requests/send_asset.rb
491
497
  - app/services/stellar_base/deposit_requests/trigger.rb
492
498
  - app/services/stellar_base/deposit_requests/update_deposit.rb
499
+ - app/services/stellar_base/fee_requests/call_fee_from.rb
500
+ - app/services/stellar_base/find_asset_details.rb
493
501
  - app/services/stellar_base/gen_memo_for.rb
494
502
  - app/services/stellar_base/gen_random_string.rb
495
503
  - app/services/stellar_base/init_asset_sending_client.rb
@@ -523,6 +531,7 @@ files:
523
531
  - lib/stellar_base/factories/bridge_callbacks.rb
524
532
  - lib/stellar_base/factories/deposit_requests.rb
525
533
  - lib/stellar_base/factories/deposits.rb
534
+ - lib/stellar_base/factories/fee_requests.rb
526
535
  - lib/stellar_base/factories/stellar_operations.rb
527
536
  - lib/stellar_base/factories/stellar_transactions.rb
528
537
  - lib/stellar_base/factories/withdrawal_requests.rb