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

Fix: Wallet links update. Deposit spread wallet recalculation #2687

Merged
merged 1 commit into from Sep 11, 2020
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/models/currency.rb
Expand Up @@ -145,9 +145,9 @@ def initialize_defaults
def link_wallets
if parent_id.present?
# Iterate through active deposit/withdraw wallets
Wallet.active.deposit.withdraw.each do |wallet|
Wallet.active.where.not(kind: :fee).with_currency(parent_id).each do |wallet|
# Link parent currency with wallet
CurrencyWallet.create(currency_id: parent_id, wallet_id: wallet.id)
CurrencyWallet.create(currency_id: id, wallet_id: wallet.id)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/wallet_service.rb
Expand Up @@ -31,9 +31,9 @@ def spread_deposit(deposit)
# NOTE: Consider min_collection_amount is defined per wallet.
# For now min_collection_amount is currency config.
{ address: w.address,
balance: w.current_balance(deposit.currency) * deposit.currency.get_price.to_d,
balance: w.current_balance(deposit.currency),
# Wallet max_balance will be in the platform currency
max_balance: w.max_balance,
max_balance: (w.max_balance / deposit.currency.get_price.to_d).round(deposit.currency.precision, BigDecimal::ROUND_DOWN),
min_collection_amount: deposit.currency.min_collection_amount,
skip_deposit_collection: w.service.skip_deposit_collection? }
end
Expand Down Expand Up @@ -126,7 +126,7 @@ def skip_deposit_collection?
# @return [Array<Peatio::Transaction>] result of spread in form of
# transactions array with amount and to_address defined.
def spread_between_wallets(deposit, destination_wallets)
original_amount = deposit.amount * deposit.currency.get_price.to_d
original_amount = deposit.amount
if original_amount < destination_wallets.pluck(:min_collection_amount).min
return []
end
Expand Down
11 changes: 7 additions & 4 deletions spec/models/currency_spec.rb
Expand Up @@ -247,20 +247,23 @@
end

context 'link_wallets' do
let!(:coin) { Currency.find(:eth) }
let!(:wallet) { Wallet.deposit_wallet(:eth) }

context 'without parent id' do
it 'should not create currency wallet' do
currency = Currency.create(code: 'test')
expect(CurrencyWallet.find_by(currency_id: currency.parent_id)).to eq nil
expect(CurrencyWallet.find_by(currency_id: currency.id, wallet_id: wallet.id)).to eq nil
end
end

context 'with parent id' do
it 'should create currency wallet' do
currency = Currency.create(code: 'test', parent_id: coin.id)
wallet = CurrencyWallet.find_by(currency_id: currency.parent_id)
c_w = CurrencyWallet.find_by(currency_id: currency.id, wallet_id: wallet.id)

expect(wallet.present?).to eq true
expect(wallet.currency_id).to eq currency.parent_id
expect(c_w.present?).to eq true
expect(c_w.currency_id).to eq currency.id
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/services/wallet_service_spec.rb
Expand Up @@ -579,7 +579,7 @@
let(:expected_spread) do
[{ to_address: 'fake-hot',
status: 'pending',
amount: (deposit.amount * deposit.currency.price).to_s,
amount: deposit.amount.to_s,
currency_id: currency.id }]
end

Expand All @@ -602,7 +602,7 @@
let(:expected_spread) do
[{ to_address: 'fake-cold',
status: 'pending',
amount: (deposit.amount * deposit.currency.price).to_s,
amount: deposit.amount.to_s,
currency_id: currency.id }]
end

Expand All @@ -623,11 +623,11 @@
let(:expected_spread) do
[{ to_address: 'fake-hot',
status: 'pending',
amount: '58.0',
amount: '1.38095238',
currency_id: currency.id },
{ to_address: 'fake-cold',
status: 'pending',
amount: '26.0',
amount: '0.61904762',
currency_id: currency.id }]
end

Expand Down