peatio icon indicating copy to clipboard operation
peatio copied to clipboard

CoinRPC#sendtoaddress requires real number with maximum 8 decimal places but Peatio may send more (withdraw)

Open yivo opened this issue 8 years ago • 10 comments

  • https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
  • https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)

To reproduce:

  • Put binding.pry at 33-th line at app/models/worker/withdraw_coin.rb.
  • Edit config/currencies.yml and set quick_withdraw_max to 1 or something greater.
  • Run app & daemons, manually stop amqp:withdraw_coin: god stop amqp:withdraw_coin and run manually RAILS_ROOT=$(pwd) bundle exec ruby lib/daemons/amqp_daemon.rb withdraw_coin (this is required to make REPL work).
  • Deposit BTC.
  • Generate Bitcoin Testnet address outside of Peatio hot wallet.
  • Create withdraw for 0.5 BTC to that address.
  • Wait a minute than back to shell – you should be in REPL shell.
  • Examine variables balance, fee, withdraw.amount.

yivo avatar Jan 17 '18 11:01 yivo

Result of execution of this line of code Withdraws::Satoshi.new("fund_source"=>3, "member_id"=>7, "currency"=>"btc", "sum"=>0.5) Is

 id: nil,
 sn: nil,
 account_id: nil,
 member_id: 7,
 currency: 2,
 amount: nil,
 fee: nil,
 fund_uid: nil,
 fund_extra: nil,
 created_at: nil,
 updated_at: nil,
 done_at: nil,
 txid: nil,
 aasm_state: nil,
 sum: 0.5000000000000001e0,
 type: "Withdraws::Satoshi">

Here we can find that sum is 0.5000000000000001e0 instead of 0.5

ysv avatar Jan 17 '18 13:01 ysv

@yssavchuk So we have some issues with numbers since 0.5 becomes 0.5000000000000001

yivo avatar Jan 17 '18 13:01 yivo

Finally I found this method to be responsible for withdraw precision

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

That's why we can add this line to currencies.yml

precision: 8 #or less

This will solve our problem until we have withdraw fee hardcoded

def set_fee
      self.fee = "0.0001".to_d
end

ysv avatar Jan 17 '18 14:01 ysv

What is more I think we need to provide input validation of amount because user can input 0.0010000000000001 @yivo ping

ysv avatar Jan 17 '18 14:01 ysv

@yssavchuk What is the default value for precision in currencies.yml?

yivo avatar Jan 17 '18 14:01 yivo

it is not provided for coins

ysv avatar Jan 17 '18 14:01 ysv

@yssavchuk We can check user input like so:

n = 0.0010000000000001
valid = n.to_s.split('.').last == n.to_s.split('.').last[0...8]

yivo avatar Jan 17 '18 14:01 yivo

@yssavchuk Can you set precision to 8 in currencies.yml and test?

yivo avatar Jan 17 '18 14:01 yivo

Progress in #315.

yivo avatar Jan 17 '18 14:01 yivo

this will help https://github.com/amarian12/nlexch.com/commit/be2f7bdb30e48686ec9472ed6dfb7def6152e993

amarian12 avatar Jan 18 '18 12:01 amarian12