Skip to content

Commit

Permalink
Merge branch 'master' into rails_version
Browse files Browse the repository at this point in the history
Conflicts:
	app/models/account_operation.rb
	test/unit/trade_order_test.rb
  • Loading branch information
David FRANCOIS committed Dec 21, 2011
2 parents ee11885 + d3ef321 commit ee3d9c9
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 121 deletions.
5 changes: 0 additions & 5 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,6 @@ img#price-graph {
margin: 2em auto;
}

/* Trade orders */
table#trade-orders tr.inactive {
color: gray;
}

/* Trade order book */
div#purchase-orders {
width: 45%;
Expand Down
2 changes: 1 addition & 1 deletion app/models/account_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class AccountOperation < ActiveRecord::Base
CURRENCIES = %w{ EUR USD LREUR LRUSD BTC PGAU INR CAD }
MIN_BTC_CONFIRMATIONS = 5
MIN_BTC_CONFIRMATIONS = 4

default_scope order('`account_operations`.`created_at` DESC')

Expand Down
8 changes: 0 additions & 8 deletions app/models/limit_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class LimitOrder < TradeOrder

validate :amount do
if new_record?
if amount and (amount < MIN_AMOUNT) and !skip_min_amount
errors[:amount] << (I18n.t "errors.must_be_greater", :min=>MIN_AMOUNT)
end

if amount and (amount > user.balance(:btc)) and selling?
errors[:amount] << (I18n.t "errors.greater_than_balance", :balance=>("%.4f" % user.balance(:btc)), :currency=>"BTC")
end
Expand All @@ -18,10 +14,6 @@ class LimitOrder < TradeOrder
errors[:amount] << (I18n.t "errors.greater_than_capacity", :capacity=>("%.4f" % (user.balance(currency) / ppc)), :ppc=>ppc, :currency=>currency)
end
end

if dark_pool? and amount < MIN_DARK_POOL_AMOUNT
errors[:dark_pool] << (I18n.t "errors.minimum_dark_pool_order")
end
end
end

Expand Down
22 changes: 12 additions & 10 deletions app/models/trade.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Trade < Operation
DEFAULT_COMMISSION_RATE = BigDecimal("0.199")
DEFAULT_COMMISSION_RATE = BigDecimal("0")

default_scope order("created_at DESC")

Expand Down Expand Up @@ -72,10 +72,12 @@ def self.plottable(currency)

def execute

sell_com = sale_order.user.commission_rate || DEFAULT_COMMISSION_RATE

buy_com = purchase_order.user.commission_rate || DEFAULT_COMMISSION_RATE
seller_commission_rate = sale_order.user.commission_rate || DEFAULT_COMMISSION_RATE
buyer_commission_rate = purchase_order.user.commission_rate || DEFAULT_COMMISSION_RATE

btc_fee = traded_btc * buyer_commission_rate
currency_fee = traded_currency * seller_commission_rate

account_operations << AccountOperation.new do |it|
it.currency = currency
it.amount = -traded_currency
Expand All @@ -84,7 +86,7 @@ def execute

account_operations << AccountOperation.new do |it|
it.currency = currency
it.amount = traded_currency * ((100.0 - sell_com) / 100.0)
it.amount = traded_currency - currency_fee
it.account_id = sale_order.user_id
end

Expand All @@ -97,20 +99,20 @@ def execute

account_operations << AccountOperation.new do |bt|
bt.currency = "BTC"
bt.amount = traded_btc * ((100.0 - buy_com) / 100.0)
bt.amount = traded_btc - btc_fee
bt.account_id = purchase_order.user_id
end

account_operations << AccountOperation.new do |fee|
fee.currency = "BTC"
fee.amount = traded_btc * buy_com / 100.0
fee.account = Account.storage_account_for("BTC_fees".to_sym)
fee.amount = btc_fee
fee.account = Account.storage_account_for(:fees)
end

account_operations << AccountOperation.new do |fee|
fee.currency = currency
fee.amount = traded_currency * sell_com / 100.0
fee.account = Account.storage_account_for((currency + "_fees").to_sym)
fee.amount = currency_fee
fee.account = Account.storage_account_for(:fees)
end

save!
Expand Down
17 changes: 17 additions & 0 deletions app/models/trade_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ class TradeOrder < ActiveRecord::Base
:presence => true,
:inclusion => { :in => ["buy", "sell"] }

validate :amount do
if new_record?
if amount and (amount < MIN_AMOUNT) and !skip_min_amount
errors[:amount] << (I18n.t "errors.must_be_greater", :min=>MIN_AMOUNT)
end

if dark_pool? and amount < MIN_DARK_POOL_AMOUNT
errors[:dark_pool] << (I18n.t "errors.minimum_dark_pool_order")
end
end
end

def buying?
category == "buy"
end
Expand Down Expand Up @@ -68,6 +80,10 @@ def self.active_with_category(category)
def self.active
where(:active => true)
end

def self.inactive
where(:active => false)
end

def self.visible(user)
if user
Expand Down Expand Up @@ -154,6 +170,7 @@ def self.get_orders(category, options = {})
select("currency").
select("dark_pool").
active.
where("`type` <> 'MarketOrder'").
visible(options[:user]).
with_currency(options[:currency] || :all).
group("#{options[:separated] ? "id" : "ppc"}").
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class User < Account

# Setup accessible (or protected) attributes for your model
attr_accessible :password, :password_confirmation, :remember_me, :time_zone,
:merchant, :require_ga_otp, :require_yk_otp, :full_name, :address, :commission_rate
:merchant, :require_ga_otp, :require_yk_otp, :full_name, :address

attr_accessor :captcha,
:skip_captcha,
Expand Down
34 changes: 34 additions & 0 deletions app/views/trade_orders/_detailed_list.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
%table#trade-orders.default
%tr
%th= t("activerecord.attributes.trade_order.updated_at")
%th= t("activerecord.attributes.trade_order.category")
%th= t("activerecord.attributes.trade_order.type")
%th= t("activerecord.attributes.trade_order.amount")
%th= t("activerecord.attributes.trade_order.ppc")
%th= t("activerecord.attributes.trade_order.total")
%th.auto-sized
%th.auto-sized
%th.auto-sized
%th.auto-sized

- unless trade_orders.blank?
- trade_orders.each do |to|
%tr{ :class => cycle('even', 'odd', :name => 'trade_orders') }
%td= l(to.updated_at)
%td= t(".#{to.category}")
%td= t(".#{to.type.gsub(/Order/, "").downcase}")
%td.amount= number_to_bitcoins to.amount, :precision => 4
- if to.is_a?(LimitOrder)
%td.amount= format_amount(to.ppc, to[:currency], 5)
%td.amount= format_amount(to.ppc * to.amount, to[:currency], 4)
- else
%td= t(".market_order")
%td= t(".market_order")
%td.auto-sized= dark_pool_icon_for(to)
%td.auto-sized= currency_icon_for(to[:currency])
%td.auto-sized= activate_link_for(to) if to.activable?
%td.auto-sized= delete_link_for(to)

- reset_cycle 'trade_orders'
- else
= render :partial => "layouts/no_data"
40 changes: 4 additions & 36 deletions app/views/trade_orders/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
= creation_link :account_trade_order, t(".create")

%h1= t :trade_orders_title
%h1= t(".active_orders", :count => @trade_orders.active.count)
= render :partial => "trade_orders/detailed_list", :locals => { :trade_orders => @trade_orders.active }

%table#trade-orders.default
%tr
%th= t :created_at
%th= t :type
%th= t :amount
%th= t :price
%th= t :total
%th.auto-sized
%th.auto-sized
%th.auto-sized
%th.auto-sized

- unless @trade_orders.blank?
- @trade_orders.each do |to|
%tr{ :class => [(to.active? ? "" : "inactive"), cycle('even', 'odd')] }
%td= l to.created_at
%td= t(".#{to.category}")
%td.amount= number_to_bitcoins to.amount, :precision => 4
- if to.is_a?(LimitOrder)
%td.amount= format_amount(to.ppc, to[:currency], 5)
%td.amount= format_amount(to.ppc * to.amount, to[:currency], 4)
- else
%td= t(".market_order")
%td= t(".market_order")
%td.auto-sized= dark_pool_icon_for(to)
%td.auto-sized= currency_icon_for(to[:currency])
- if to.activable?
%td.auto-sized= activate_link_for(to)
- else
%td.auto-sized= nil
%td.auto-sized= delete_link_for(to)
- else
= render :partial => "layouts/no_data"

= will_paginate(@trade_orders)
%h1= t(".inactive_orders", :count => @trade_orders.inactive.count)
= render :partial => "trade_orders/detailed_list", :locals => { :trade_orders => @trade_orders.inactive }
2 changes: 1 addition & 1 deletion app/views/yubikeys/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%h1= t(".associate")

= form_for [current_user, @yubikey] do |f|
= form_for @yubikey, :url => user_yubikeys_path(@yubikey) do |f|
= errors_for @yubikey, :message => "yubikeys.index.error"

.form-field
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ en:
type: Matching
currency: Currency
total: Total
created_at: Created at
updated_at: Updated at
transfer:
id: ID
created_at: Created at
Expand Down
9 changes: 0 additions & 9 deletions config/locales/en/other.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ en:
no_buying_orders_found: No buying orders have been found.
no_selling_orders_found: No selling orders have been found.

# views/trade_orders/index.html
trade_orders_title: Trade orders
created_at: Created at
type: Type
price: Price
total: Total
dark_pool_order: Dark pool order
no_orders_found: No orders have been found.

# views/trades/index.html
trade_history_title: Trade history
no_trades_found: No trades were found
Expand Down
15 changes: 10 additions & 5 deletions config/locales/en/views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ en:
trade_orders:
index:
create: Create an order
market_order: Market Order
sell: Sell
buy: Buy
active_orders: "Active orders (%{count})"
inactive_orders: "Inactive orders (%{count})"
detailed_list:
na: N/A
sell: Ask
buy: Bid
market: Market
limit: Limit
delete_order: Delete this trade order
delete_order_confirm: Are you sure you want to delete this trade order ?
delete_order_confirm: Are you sure you want to delete this trade order ?
activate_order: Activate this trade order and execute it
activate_order_confirm: Are you sure you want to active this trade order ?
activate_order_confirm: Are you sure you want to activate this trade order ?
book:
bids: Bids
asks: Asks
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ fr:
type: Matching
currency: Devise
total: Total
created_at: Création
updated_at: Mise à jour
transfer:
id: ID
created_at: Date
Expand Down
8 changes: 0 additions & 8 deletions config/locales/fr/other.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ fr:
no_selling_orders_found: "Aucun ordre de vente n'a été trouvé"
total: Total

# views/trade_orders/index.html
trade_orders_title: Vos ordres
created_at: Création
type: Type
price: Prix
dark_pool_order: Ordre privé
no_orders_found: "Aucun ordre n'a été trouvé"

# views/trades/index.html
trade_history_title: Historique
no_trades_found: Aucune transaction trouvée
Expand Down
7 changes: 6 additions & 1 deletion config/locales/fr/views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ fr:
trade_orders:
index:
create: Passer un ordre
market_order: Prix du marché
active_orders: "Ordres actifs (%{count})"
inactive_orders: "Ordres inactifs (%{count})"
detailed_list:
na: N/A
sell: Vente
buy: Achat
market: Marché
limit: Limite
delete_order: Supprimer cet ordre
delete_order_confirm: Êtes-vous sûr(e) de vouloir supprimer cet ordre ?
activate_order: Activer l'ordre et l'exécuter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddComToUsers < ActiveRecord::Migration
class AddCommissionRateToUsers < ActiveRecord::Migration
def self.up
add_column :accounts, :commission_rate, :decimal,
:precision => 16,
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
t.boolean "notify_on_trade", :default => true
t.integer "last_notified_trade_id", :default => 0, :null => false
t.integer "max_read_tx_id", :default => 0, :null => false
t.decimal "commission_rate", :precision => 16, :scale => 8, :default => 0.0
t.decimal "commission_rate", :precision => 16, :scale => 8
end

add_index "accounts", ["email"], :name => "index_users_on_email", :unique => true
Expand Down
2 changes: 1 addition & 1 deletion test/unit/account_operation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class AccountOperationTest < ActiveSupport::TestCase
u = Factory(:user)

assert_no_difference ("u.balance(:eur)") do
add_money(u, -1000, :eur)
add_money(u, -100, :eur)
end
end
end
Loading

0 comments on commit ee3d9c9

Please sign in to comment.