Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Currency#enabled (aka #visible, closes #818) #855

Merged
merged 1 commit into from May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/api/api_v2/deposits.rb
Expand Up @@ -12,7 +12,7 @@ class Deposits < Grape::API

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018-05-23--16-06-06

2018-05-23--15-59-29

2018-05-23--15-59-24

2018-05-23--15-57-53

2018-05-23--15-57-44

2018-05-23--15-57-34

2018-05-23--15-57-16

2018-05-23--15-55-37

desc 'Get your deposits history.'
params do
optional :currency, type: String, values: -> { Currency.codes(bothcase: true) }, desc: -> { "Currency value contains #{Currency.codes(bothcase: true).join(',')}" }
optional :currency, type: String, values: -> { Currency.enabled.codes(bothcase: true) }, desc: -> { "Currency value contains #{Currency.enabled.codes(bothcase: true).join(',')}" }
optional :limit, type: Integer, range: 1..100, default: 3, desc: "Set result limit."
optional :state, type: String, values: -> { Deposit::STATES.map(&:to_s) }
end
Expand All @@ -36,7 +36,7 @@ class Deposits < Grape::API

desc 'Where to deposit. The address field could be empty when a new address is generating (e.g. for bitcoin), you should try again later in that case.'
params do
requires :currency, type: String, values: -> { Currency.coins.codes(bothcase: true) }, desc: -> { "The account to which you want to deposit. Available values: #{Currency.coins.codes(bothcase: true).join(', ')}" }
requires :currency, type: String, values: -> { Currency.coins.enabled.codes(bothcase: true) }, desc: -> { "The account to which you want to deposit. Available values: #{Currency.coins.enabled.codes(bothcase: true).join(', ')}" }
end
get "/deposit_address" do
current_user.ac(params[:currency]).payment_address.yield_self do |pa|
Expand Down
2 changes: 1 addition & 1 deletion app/api/api_v2/entities/member.rb
Expand Up @@ -6,7 +6,7 @@ module Entities
class Member < Base
expose :sn
expose :email
expose :accounts, using: ::APIv2::Entities::Account
expose(:accounts, using: ::APIv2::Entities::Account) { |m| m.accounts.enabled }
end
end
end
6 changes: 3 additions & 3 deletions app/api/api_v2/fees.rb
Expand Up @@ -10,7 +10,7 @@ class Fees < Grape::API

desc 'Returns withdraw fees for currencies.'
get '/fees/withdraw' do
withdraw_fees = Currency.visible.map do |c|
withdraw_fees = Currency.enabled.map do |c|
fee = Fee.new(:fixed, c.withdraw_fee)
WithdrawFee.new(c.code, c.type, fee)
end
Expand All @@ -19,7 +19,7 @@ class Fees < Grape::API

desc 'Returns deposit fees for currencies.'
get '/fees/deposit' do
deposit_fees = Currency.visible.map do |c|
deposit_fees = Currency.enabled.map do |c|
fee = Fee.new(:fixed, c.deposit_fee)
DepositFee.new(c.code, c.type, fee)
end
Expand All @@ -28,7 +28,7 @@ class Fees < Grape::API

desc 'Returns trading fees for markets.'
get '/fees/trading' do
trading_fees = Market.visible.map do |m|
trading_fees = Market.enabled.map do |m|
ask_fee = Fee.new(:relative, m.ask_fee)
bid_fee = Fee.new(:relative, m.bid_fee)
TradingFee.new(m.id, ask_fee, bid_fee)
Expand Down
4 changes: 2 additions & 2 deletions app/api/api_v2/helpers.rb
Expand Up @@ -50,8 +50,8 @@ def build_order(attrs)
order = klass.new(
state: ::Order::WAIT,
member_id: current_user.id,
ask: Currency.find_by!(code: current_market.base_unit).id,
bid: Currency.find_by!(code: current_market.quote_unit).id,
ask: Currency.enabled.find_by!(code: current_market.base_unit).id,
bid: Currency.enabled.find_by!(code: current_market.quote_unit).id,
market_id: current_market.id,
ord_type: attrs[:ord_type] || 'limit',
price: attrs[:price],
Expand Down
2 changes: 1 addition & 1 deletion app/api/api_v2/markets.rb
Expand Up @@ -6,7 +6,7 @@ class Markets < Grape::API

desc 'Get all available markets.'
get "/markets" do
present Market.visible, with: APIv2::Entities::Market
present Market.enabled, with: APIv2::Entities::Market
end

end
Expand Down
4 changes: 2 additions & 2 deletions app/api/api_v2/solvency.rb
Expand Up @@ -8,7 +8,7 @@ class Solvency < Grape::API

desc 'Returns newest liability proof record for given currency.'
params do
requires :currency, type: String, values: -> { Currency.coin_codes(bothcase: true) }, desc: "The code of any currency with type 'coin'."
requires :currency, type: String, values: -> { Currency.enabled.coin_codes(bothcase: true) }, desc: "The code of any currency with type 'coin'."
end
get '/solvency/liability_proofs/latest' do
Proof
Expand All @@ -18,7 +18,7 @@ class Solvency < Grape::API

desc 'Returns newest partial tree record for member account of specified currency.'
params do
requires :currency, type: String, values: -> { Currency.coin_codes(bothcase: true) }, desc: "The code of any currency with type 'coin'."
requires :currency, type: String, values: -> { Currency.enabled.coin_codes(bothcase: true) }, desc: "The code of any currency with type 'coin'."
end
get '/solvency/liability_proofs/partial_tree/mine' do
current_user
Expand Down
4 changes: 2 additions & 2 deletions app/api/api_v2/withdraws.rb
Expand Up @@ -10,7 +10,7 @@ class Withdraws < Grape::API

desc 'List your withdraws as paginated collection.', scopes: %w[ history ]
params do
optional :currency, type: String, values: -> { Currency.codes(bothcase: true) }, desc: -> { "Any supported currencies: #{Currency.codes(bothcase: true).join(',')}." }
optional :currency, type: String, values: -> { Currency.enabled.codes(bothcase: true) }, desc: -> { "Any supported currencies: #{Currency.enabled.codes(bothcase: true).join(',')}." }
optional :page, type: Integer, default: 1, integer_gt_zero: true, desc: 'Page number (defaults to 1).'
optional :limit, type: Integer, default: 100, range: 1..1000, desc: 'Number of withdraws per page (defaults to 100, maximum is 1000).'
end
Expand All @@ -30,7 +30,7 @@ class Withdraws < Grape::API

desc '[DEPRECATED] Create withdraw.', scopes: %w[ withdraw ]
params do
requires :currency, type: String, values: -> { Currency.codes(bothcase: true) }, desc: -> { "Any supported currency: #{Currency.codes(bothcase: true).join(',')}." }
requires :currency, type: String, values: -> { Currency.enabled.codes(bothcase: true) }, desc: -> { "Any supported currency: #{Currency.enabled.codes(bothcase: true).join(',')}." }
requires :amount, type: BigDecimal, desc: 'Withdraw amount without fees.'
requires :rid, type: String, desc: 'The shared recipient ID.'
end
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/layouts/_header.scss
Expand Up @@ -32,7 +32,7 @@ header {
}

.dropdown-menu li:hover .sub-menu {
visibility: visible;
visibility: Avisible;
}

.dropdown:hover .dropdown-menu {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/currencies_controller.rb
Expand Up @@ -57,7 +57,7 @@ def permitted_currency_attributes
:withdraw_fee,
:deposit_fee,
:deposit_confirmations,
:visible,
:enabled,
:base_factor,
:precision,
:api_client,
Expand All @@ -77,7 +77,7 @@ def permitted_currency_attributes
end

def boolean_currency_attributes
%i[ visible
%i[ enabled
case_sensitive
supports_cash_addr_format
bitgo_test_net ]
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/markets_controller.rb
Expand Up @@ -56,12 +56,12 @@ def permitted_market_attributes
:ask_unit,
:ask_fee,
:ask_precision,
:visible,
:enabled,
:position ]
end

def boolean_market_attributes
%i[ visible ]
%i[ enabled ]
end
end
end
8 changes: 4 additions & 4 deletions app/controllers/application_controller.rb
Expand Up @@ -123,7 +123,7 @@ def set_gon
}
}

gon.currencies = Currency.visible.inject({}) do |memo, currency|
gon.currencies = Currency.enabled.inject({}) do |memo, currency|
memo[currency.code] = {
code: currency.code,
symbol: currency.symbol,
Expand All @@ -132,7 +132,7 @@ def set_gon
memo
end
gon.display_currency = ENV.fetch('DISPLAY_CURRENCY')
gon.fiat_currencies = Currency.fiats.pluck(:code)
gon.fiat_currencies = Currency.enabled.fiats.pluck(:code)

gon.tickers = {}
Market.all.each do |market|
Expand All @@ -141,12 +141,12 @@ def set_gon

if current_user
gon.user = { sn: current_user.sn }
gon.accounts = current_user.accounts.inject({}) do |memo, account|
gon.accounts = current_user.accounts.enabled.inject({}) do |memo, account|
memo[account.currency.code] = {
currency: account.currency.code,
balance: account.balance,
locked: account.locked
} if account.currency.try(:visible)
} if account.currency.try(:enabled)
memo
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/order_creation.rb
Expand Up @@ -6,8 +6,8 @@ module OrderCreation
extend ActiveSupport::Concern

def order_params(order)
params[order][:bid] = Currency.find_by(code: params[:bid])&.id
params[order][:ask] = Currency.find_by(code: params[:ask])&.id
params[order][:bid] = Currency.enabled.find_by(code: params[:bid])&.id
params[order][:ask] = Currency.enabled.find_by(code: params[:ask])&.id
params[order][:state] = Order::WAIT
params[order][:market_id] = params[:market]
params[order][:member_id] = current_user.id
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/private/assets_controller.rb
Expand Up @@ -6,12 +6,12 @@ class AssetsController < BaseController
skip_before_action :auth_member!, only: [:index]

def index
Currency.all.each do |ccy|
Currency.enabled.each do |ccy|
name = ccy.fiat? ? :fiat : ccy.code.to_sym
instance_variable_set :"@#{name}_proof", Proof.current(ccy.code.to_sym)
if current_user
instance_variable_set :"@#{name}_account", \
current_user.accounts.with_currency(ccy.code.to_sym).first
current_user.accounts.enabled.with_currency(ccy.code.to_sym).first
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/private/deposits_controller.rb
Expand Up @@ -22,7 +22,7 @@ def destroy
private

def currency
@currency ||= Currency.find_by_code!(params[:currency])
@currency ||= Currency.enabled.find_by_code!(params[:currency])
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/private/funds_controller.rb
Expand Up @@ -10,7 +10,7 @@ class FundsController < BaseController
before_action :auth_verified!

def index
@currencies = Currency.all.sort
@currencies = Currency.enabled.sort
@deposits = current_user.deposits
@accounts = current_user.accounts.enabled
@withdraws = current_user.withdraws
Expand All @@ -21,7 +21,7 @@ def index
helper_method :currency_icon_url

def gen_address
current_user.accounts.each(&:payment_address)
current_user.accounts.enabled.each(&:payment_address)
render nothing: true
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/private/markets_controller.rb
Expand Up @@ -7,7 +7,7 @@ class MarketsController < BaseController
include CurrencyHelper

skip_before_action :auth_member!, only: [:show]
before_action :visible_market?
before_action :enabled_market?
after_action :set_default_market

layout false
Expand Down Expand Up @@ -35,8 +35,8 @@ def show

private

def visible_market?
redirect_to trading_path(Market.first) unless current_market.visible?
def enabled_market?
redirect_to trading_path(Market.first) unless current_market.enabled?
end

def set_default_market
Expand All @@ -50,7 +50,7 @@ def set_member_data
end

def trading_ui_variables
accounts = @member&.accounts&.map do |x|
accounts = @member&.accounts&.enabled&.map do |x|
{ id: x.id,
locked: x.locked,
amount: x.amount,
Expand All @@ -64,7 +64,7 @@ def trading_ui_variables
{ current_market: @market.as_json,
gon_variables: gon.all_variables,
market_groups: @market_groups,
currencies: Currency.order(id: :asc).map { |c| { code: c.code, type: c.type } },
currencies: Currency.enabled.order(id: :asc).map { |c| { code: c.code, type: c.type } },
current_member: @member,
markets: @markets.map { |m| m.as_json.merge!(ticker: Global[m].ticker) },
my_accounts: accounts,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/private/withdraws_controller.rb
Expand Up @@ -28,7 +28,7 @@ def destroy
private

def currency
@currency ||= Currency.find_by_code!(params[:currency])
@currency ||= Currency.enabled.find_by_code!(params[:currency])
end

def withdraw_class
Expand Down
2 changes: 1 addition & 1 deletion app/models/account.rb
Expand Up @@ -17,7 +17,7 @@ class AccountError < StandardError
validates :member_id, uniqueness: { scope: :currency_id }
validates :balance, :locked, numericality: { greater_than_or_equal_to: 0.to_d }

scope :enabled, -> { joins(:currency).merge(Currency.where(visible: true)) }
scope :enabled, -> { joins(:currency).merge(Currency.where(enabled: true)) }

after_commit :trigger, :sync_update

Expand Down
11 changes: 5 additions & 6 deletions app/models/currency.rb
Expand Up @@ -36,15 +36,14 @@ class Currency < ActiveRecord::Base

after_create { Member.find_each(&:touch_accounts) }

scope :visible, -> { where(visible: true) }
scope :all_with_invisible, -> { all }
scope :enabled, -> { where(enabled: true) }

scope :coins, -> { where(type: :coin) }
scope :fiats, -> { where(type: :fiat) }

class << self
def codes(options = {})
visible.pluck(:code).yield_self do |downcase_codes|
pluck(:code).yield_self do |downcase_codes|
case
when options.fetch(:bothcase, false)
downcase_codes + downcase_codes.map(&:upcase)
Expand Down Expand Up @@ -166,7 +165,7 @@ def case_insensitive?
end

# == Schema Information
# Schema version: 20180425224307
# Schema version: 20180517084245
#
# Table name: currencies
#
Expand All @@ -178,7 +177,7 @@ def case_insensitive?
# quick_withdraw_limit :decimal(32, 16) default(0.0), not null
# withdraw_fee :decimal(32, 16) default(0.0), not null
# options :string(1000) default({}), not null
# visible :boolean default(TRUE), not null
# enabled :boolean default(TRUE), not null
# base_factor :integer default(1), not null
# precision :integer default(8), not null
# created_at :datetime not null
Expand All @@ -187,5 +186,5 @@ def case_insensitive?
# Indexes
#
# index_currencies_on_code (code) UNIQUE
# index_currencies_on_visible (visible)
# index_currencies_on_enabled (enabled)
#
8 changes: 4 additions & 4 deletions app/models/market.rb
Expand Up @@ -23,7 +23,7 @@ class Market < ActiveRecord::Base
# TODO: Don't use default_scope. Refactor to scopes!
default_scope { order(position: :asc) }

scope :visible, -> { where(visible: true) }
scope :enabled, -> { where(enabled: true) }

validate { errors.add(:ask_unit, :invalid) if ask_unit == bid_unit }
validates :id, uniqueness: { case_sensitive: false }, presence: true
Expand Down Expand Up @@ -103,7 +103,7 @@ def global
end

# == Schema Information
# Schema version: 20180522165830
# Schema version: 20180522121046
#
# Table name: markets
#
Expand All @@ -115,7 +115,7 @@ def global
# ask_precision :integer default(4), not null
# bid_precision :integer default(4), not null
# position :integer default(0), not null
# visible :boolean default(TRUE), not null
# enabled :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
Expand All @@ -124,6 +124,6 @@ def global
# index_markets_on_ask_unit (ask_unit)
# index_markets_on_ask_unit_and_bid_unit (ask_unit,bid_unit) UNIQUE
# index_markets_on_bid_unit (bid_unit)
# index_markets_on_enabled (enabled)
# index_markets_on_position (position)
# index_markets_on_visible (visible)
#
4 changes: 2 additions & 2 deletions app/views/admin/currencies/index.html.erb
Expand Up @@ -25,8 +25,8 @@
<%= l(x.created_at, format: :long) %>
<% end %>

<% t.column 'Visible' do |x| %>
<%= x.visible? ? 'Yes' : 'No' %>
<% t.column 'Enabled' do |x| %>
<%= x.enabled? ? 'Yes' : 'No' %>
<% end %>

<% t.column 'Actions' do |x| %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/currencies/show.html.erb
Expand Up @@ -35,8 +35,8 @@
<dt>ERC20 contract address</dt>
<dd><%= f.text_field :erc20_contract_address, class: 'form-control' %></dd>
<% end %>
<dt>Visible?</dt>
<dd><%= f.check_box :visible %></dd>
<dt>Enabled?</dt>
<dd><%= f.check_box :enabled %></dd>
<dt>Supports CashAddr format?</dt>
<dd><%= f.check_box :supports_cash_addr_format %></dd>
<dt>Base factor</dt>
Expand Down