Initial commit
This commit is contained in:
110
bin/seed_test_balances.rb
Normal file
110
bin/seed_test_balances.rb
Normal file
@@ -0,0 +1,110 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Credits test user balances in Peatio (Dena).
|
||||
# Run: bundle exec rails runner lib/seed_test_balances.rb
|
||||
|
||||
require 'yaml'
|
||||
require 'json'
|
||||
require 'bigdecimal'
|
||||
|
||||
def test_seed_enabled?
|
||||
ENV.fetch('FIBITEX_SEED_TEST_USERS', 'true') != 'false'
|
||||
end
|
||||
|
||||
unless test_seed_enabled?
|
||||
puts 'SKIP seed_test_balances (FIBITEX_SEED_TEST_USERS=false)'
|
||||
exit 0
|
||||
end
|
||||
|
||||
config_path = [
|
||||
ENV['TEST_USERS_CONFIG'],
|
||||
File.expand_path('../config/test_users.yml', __dir__),
|
||||
'/opt/peatio/config/test_users.yml'
|
||||
].compact.find { |p| File.exist?(p) }
|
||||
|
||||
unless config_path
|
||||
puts "ERR missing config: #{config_path}"
|
||||
exit 1
|
||||
end
|
||||
|
||||
config = YAML.safe_load(File.read(config_path))
|
||||
fiat_ids = %w[irt cad]
|
||||
|
||||
def min_order_amounts_by_currency
|
||||
mins = {}
|
||||
Market.find_each do |market|
|
||||
next unless market.state == 'enabled'
|
||||
|
||||
amount = market.min_amount.to_d
|
||||
next if amount <= 0
|
||||
|
||||
cur = market.base_unit
|
||||
mins[cur] = mins[cur] ? [mins[cur], amount].min : amount
|
||||
end
|
||||
mins
|
||||
end
|
||||
|
||||
def ensure_member!(entry, barong_role: 'member')
|
||||
email = entry['email']
|
||||
uid = entry['uid']
|
||||
member = Member.find_or_initialize_by(email: email)
|
||||
member.uid = uid if uid.present?
|
||||
member.role = barong_role
|
||||
member.level = entry['level'] || 0
|
||||
member.state = 'active'
|
||||
member.group = entry['peatio_group'] || 'vip-0'
|
||||
member.save!
|
||||
member
|
||||
end
|
||||
|
||||
def sync_account_balance!(account, target)
|
||||
target = target.to_d
|
||||
current = account.balance.to_d
|
||||
return if current == target
|
||||
|
||||
diff = target - current
|
||||
if diff > 0
|
||||
account.plus_funds!(diff)
|
||||
puts "CREDIT #{account.member.email} #{account.currency_id}=#{target} (added #{diff})"
|
||||
elsif diff < 0 && account.respond_to?(:sub_funds!) && account.balance.to_d >= -diff
|
||||
account.sub_funds!(-diff)
|
||||
puts "DEBIT #{account.member.email} #{account.currency_id}=#{target} (removed #{-diff})"
|
||||
else
|
||||
account.update!(balance: target)
|
||||
puts "SET #{account.member.email} #{account.currency_id}=#{target} (was #{current})"
|
||||
end
|
||||
end
|
||||
|
||||
min_orders = min_order_amounts_by_currency
|
||||
|
||||
(config['users'] || []).each do |entry|
|
||||
email = entry['email']
|
||||
member = ensure_member!(entry, barong_role: entry['role'] == 'superadmin' ? 'superadmin' : 'member')
|
||||
coin_fixed = (entry['coin_fixed'] || {}).transform_keys(&:to_s)
|
||||
|
||||
Currency.find_each do |currency|
|
||||
amount =
|
||||
if fiat_ids.include?(currency.id)
|
||||
BigDecimal(entry['fiat_balance'].to_s)
|
||||
elsif coin_fixed.key?(currency.id)
|
||||
BigDecimal(coin_fixed[currency.id].to_s)
|
||||
else
|
||||
base_min = min_orders[currency.id] || currency.min_deposit_amount.to_d
|
||||
base_min = BigDecimal('1') if base_min <= 0
|
||||
base_min * BigDecimal(entry['coin_multiplier'].to_s)
|
||||
end
|
||||
|
||||
account = member.get_account(currency)
|
||||
sync_account_balance!(account, amount)
|
||||
end
|
||||
end
|
||||
|
||||
puts 'DONE seed_test_balances'
|
||||
|
||||
(config['users'] || []).each do |entry|
|
||||
member = Member.find_by(email: entry['email'])
|
||||
next unless member
|
||||
|
||||
nonzero = member.accounts.where('balance > 0').count
|
||||
puts "SUMMARY #{entry['email']} currencies_with_balance=#{nonzero}"
|
||||
end
|
||||
Reference in New Issue
Block a user