Skip to content

Commit

Permalink
add currency precision config, fix withdraw precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Xie committed Mar 30, 2015
1 parent c6b962b commit 7592021
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/withdraw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Withdraw < ActiveRecord::Base
delegate :name, to: :member, prefix: true
delegate :coin?, :fiat?, to: :currency_obj

before_validation :fix_precision
before_validation :calc_fee
before_validation :set_account
after_create :generate_sn
Expand Down Expand Up @@ -207,6 +208,12 @@ def ensure_account_balance
end
end

def fix_precision
if sum && currency_obj.precision
self.sum = sum.round(currency_obj.precision, BigDecimal::ROUND_DOWN)
end
end

def calc_fee
if respond_to?(:set_fee)
set_fee
Expand Down
1 change: 1 addition & 0 deletions config/currencies.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
code: cny
symbol: "¥"
coin: false
precision: 2
assets:
accounts:
-
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/currencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
key: satoshi
code: btc
coin: true
precision: 8
rpc: http://bitcoinrpc:[email protected]:18332
blockchain: http://testnet.btclook.com/txn/#{txid}
address_url: https://blockchain.info/address/#{address}
Expand Down
7 changes: 7 additions & 0 deletions spec/models/withdraw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

describe Withdraw do

context '#fix_precision' do
it "should round down to max precision" do
withdraw = create(:satoshi_withdraw, sum: '0.123456789')
withdraw.sum.should == '0.12345678'.to_d
end
end

context 'fund source' do
it "should strip trailing spaces in fund_uid" do
fund_source = create(:btc_fund_source, uid: 'test ')
Expand Down

0 comments on commit 7592021

Please sign in to comment.