Initial commit

This commit is contained in:
Yaser
2026-08-13 19:42:08 +03:30
commit d59bf30ef0
173 changed files with 11702 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
email = ENV.fetch('EMAIL', 'admin@barong.io')
member = Member.find_by(email: email)
unless member
puts 'STATUS=NO_MEMBER'
exit 1
end
puts "STATUS=OK uid=#{member.uid} id=#{member.id}"
%w[usdt btc cad].each do |currency|
account = member.accounts.find_by(currency_id: currency)
next unless account
puts "ACCOUNT #{currency} balance=#{account.balance} locked=#{account.locked}"
end

4
bin/check_order.rb Normal file
View File

@@ -0,0 +1,4 @@
# frozen_string_literal: true
order = Order.find(ENV.fetch('ORDER_ID', '1'))
puts "ORDER id=#{order.id} state=#{order.state} price=#{order.price} volume=#{order.volume} origin_volume=#{order.origin_volume}"

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
puts '--- currencies ---'
%w[irt cad usdt].each do |cid|
c = Currency.find_by(id: cid)
puts "#{cid} visible=#{c&.visible} type=#{c&.type}" if c
end
%w[user@test.com bot@test.com admin@test.com].each do |email|
member = Member.find_by(email: email)
unless member
puts "MISSING #{email}"
next
end
sample = member.accounts.where('balance > 0').limit(3).map { |a| "#{a.currency_id}=#{a.balance}" }.join(', ')
fiat = %w[irt cad].map { |c| a = member.accounts.find_by(currency_id: c); "#{c}=#{a&.balance || 0}" }.join(' ')
total = member.accounts.where('balance > 0').count
puts "#{email} uid=#{member.uid} nonzero=#{total} fiat=[#{fiat}] sample=#{sample}"
end

23
bin/create_admin_user.rb Normal file
View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
email = ENV.fetch('ADMIN_EMAIL', 'admin@test.com')
password = ENV.fetch('ADMIN_PASSWORD', '123qwe!@#QWE')
user = User.find_or_initialize_by(email: email)
user.password = password
user.role = 'superadmin'
user.state = 'active'
user.level = 3
user.otp = false
if user.save
Level.where(id: 1..Level::LEVEL_ID_BOUNDS[user.level.to_s].to_i).find_each do |level|
user.labels.find_or_create_by!(key: level.key, scope: 'private') do |label|
label.value = level.value
end
end
puts "OK email=#{user.email} uid=#{user.uid} state=#{user.state} level=#{user.level}"
else
puts "ERR #{user.errors.full_messages.join(', ')}"
exit 1
end

56
bin/install.sh Normal file
View File

@@ -0,0 +1,56 @@
#!/bin/bash -x
COMPOSE_VERSION="1.23.2"
COMPOSE_URL="https://github.com/docker/compose/releases/download/$COMPOSE_VERSION/docker-compose-$(uname -s)-$(uname -m)"
# Opendax bootstrap script
install_core() {
sudo bash <<EOS
apt-get update
apt-get install -y -q git tmux gnupg2 dirmngr dbus htop curl libmariadbclient-dev-compat build-essential
EOS
}
log_rotation() {
sudo bash <<EOS
mkdir -p /etc/docker
echo '
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "10"
}
}' > /etc/docker/daemon.json
EOS
}
# Docker installation
install_docker() {
curl -fsSL https://get.docker.com/ | bash
sudo bash <<EOS
usermod -a -G docker $USER
curl -L "$COMPOSE_URL" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
EOS
}
activate_gcloud() {
sudo -u deploy bash <<EOS
gcloud auth configure-docker --quiet
EOS
}
install_ruby() {
sudo -u deploy bash <<EOS
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
EOS
}
install_core
log_rotation
install_docker
activate_gcloud
install_ruby

10
bin/install_webhook Normal file
View File

@@ -0,0 +1,10 @@
SECRET=$(ruby -rsecurerandom -e 'puts SecureRandom.hex(20)')
sed s/GENERATED_HMAC_SECRET/${SECRET}/g templates/webhook.service > webhook.service
sed -i s#OPENDAX_DIRECTORY#${PWD}#g webhook.service
echo "Generated Secret: ${SECRET}"
sudo mv ./webhook.service /etc/systemd/system/webhook.service
sudo systemctl daemon-reload
sudo systemctl start webhook

7
bin/quick-test.sh Normal file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
set -e
http --session barong POST http://www.app.local/api/v2/barong/identity/sessions email='iron@man.io' password='0lDHd9ufs9t@'
http --session barong GET http://www.app.local/api/v2/barong/resource/users/me
http --session barong GET http://www.app.local/api/v2/peatio/account/balances

110
bin/seed_test_balances.rb Normal file
View 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

99
bin/seed_test_users.rb Normal file
View File

@@ -0,0 +1,99 @@
# frozen_string_literal: true
# Creates / updates Fibitex test users in Barong (Dalan).
# Run: bundle exec rails runner lib/seed_test_users.rb
# Guard: set FIBITEX_SEED_TEST_USERS=false to skip.
require 'yaml'
require 'json'
def test_seed_enabled?
ENV.fetch('FIBITEX_SEED_TEST_USERS', 'true') != 'false'
end
unless test_seed_enabled?
puts 'SKIP seed_test_users (FIBITEX_SEED_TEST_USERS=false)'
exit 0
end
config_path = ENV.fetch(
'TEST_USERS_CONFIG',
File.expand_path('../config/test_users.yml', __dir__)
)
unless File.exist?(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 apply_kyc_labels!(user, kyc_mode)
Label.where(user_id: user.id, scope: 'private').delete_all
case kyc_mode.to_s
when 'zero'
bound = Level::LEVEL_ID_BOUNDS['0'].to_i
when 'full'
bound = Level::LEVEL_ID_BOUNDS[user.level.to_s].to_i
else
bound = Level::LEVEL_ID_BOUNDS[user.level.to_s].to_i
end
Level.where(id: 1..bound).find_each do |level|
user.labels.create!(key: level.key, value: level.value, scope: 'private')
end
user.update!(level: Level.what_level(user.kyc_info[:points])) if kyc_mode.to_s == 'full'
end
def sync_user_level!(user, target_level)
return if target_level.nil?
user.update!(level: target_level.to_i)
end
def remove_user!(email)
user = User.find_by(email: email)
return unless user
Label.where(user_id: user.id).delete_all
User.where(id: user.id).delete_all
puts "REMOVED #{email}"
end
(config['remove_emails'] || []).each do |email|
remove_user!(email)
end
(config['users'] || []).each do |entry|
email = entry['email']
user = User.find_or_initialize_by(email: email)
user.uid = entry['uid'] if entry['uid'].present? && user.uid.blank?
user.password = entry['password']
user.role = entry['role']
user.state = entry['state'] || 'active'
user.level = entry['level'] || 0
user.otp = false
if user.save
apply_kyc_labels!(user, entry['kyc'])
sync_user_level!(user, entry['level'])
puts "OK #{email} uid=#{user.uid} role=#{user.role} level=#{user.level} kyc=#{entry['kyc']}"
else
puts "ERR #{email}: #{user.errors.full_messages.join(', ')}"
exit 1
end
end
File.write(
File.join(Dir.tmpdir, 'fibitex_test_user_uids.json'),
JSON.pretty_generate(
(config['users'] || []).map { |u| { 'email' => u['email'], 'uid' => User.find_by!(email: u['email']).uid } }
)
)
puts 'DONE seed_test_users'

3
bin/set-env.sh Normal file
View File

@@ -0,0 +1,3 @@
export UID=$(id -u)
export GID=$(id -g)
alias dc=docker-compose

31
bin/start.sh Normal file
View File

@@ -0,0 +1,31 @@
#!/bin/bash -x
COMPOSE_VERSION="1.23.2"
COMPOSE_URL="https://github.com/docker/compose/releases/download/$COMPOSE_VERSION/docker-compose-$(uname -s)-$(uname -m)"
install_opendax() {
sudo -u deploy bash <<EOS
cd /home/deploy
source /home/deploy/.rvm/scripts/rvm
rvm install --quiet-curl 2.6.1
rvm use --default 2.6.1
gem install bundler
cd opendax
bundle install
rake render:config
rake service:cryptonodes && \
until rake wallet:create['deposit','http://127.0.0.1:8545','changeme']; do sleep 15; done && \
rake wallet:create['hot','http://127.0.0.1:8545','changeme'] && \
rake wallet:create['warm','http://127.0.0.1:8545','changeme'] && \
rake render:config && \
rake service:all && \
rake service:daemons && \
chmod +x bin/install_webhook
./bin/install_webhook
EOS
}
install_opendax

45
bin/sync_peatio_seed.rb Normal file
View File

@@ -0,0 +1,45 @@
# frozen_string_literal: true
# Sync Alvand-P config/peatio/seed into a running Peatio DB (insert missing rows, refresh IRT).
# Usage: bundle exec rails runner ./bin/sync_peatio_seed.rb
require 'yaml'
def seed_file(name)
Rails.root.join('config/seed', name)
end
puts '--- sync currencies ---'
YAML.load_file(seed_file('currencies.yml')).each do |hash|
id = hash.fetch('id')
currency = Currency.find_by(id: id)
if currency
currency.update!(hash.except('id'))
puts " updated currency #{id}"
else
Currency.create!(hash)
puts " created currency #{id}"
end
end
puts '--- sync markets ---'
engine = Engine.find_by!(name: 'peatio-default-engine')
YAML.load_file(seed_file('markets.yml')).map(&:symbolize_keys).each do |hash|
id = hash.fetch(:id)
enabled = hash.delete(:enabled)
hash[:state] ||= enabled ? :enabled : :disabled
hash.delete(:engine_name)
hash[:engine_id] = engine.id
market = Market.find_by(id: id)
if market
market.update!(hash.except(:id))
puts " updated market #{id} (#{hash[:state]})"
else
Market.create!(hash.except(:id).merge(id: id))
puts " created market #{id} (#{hash[:state]})"
end
end
puts '--- done ---'
puts "Markets by quote: #{Market.group(:quote_unit).count}"

33
bin/test_limit_order.rb Normal file
View File

@@ -0,0 +1,33 @@
# frozen_string_literal: true
email = ENV.fetch('EMAIL', 'admin@barong.io')
currency_id = ENV.fetch('CURRENCY', 'usdt')
amount = BigDecimal(ENV.fetch('AMOUNT', '10000'))
member = Member.find_by(email: email)
abort('NO_MEMBER') unless member
account = member.get_account(currency_id)
account.plus_funds!(amount)
puts "CREDITED #{currency_id}=#{amount} new_balance=#{account.balance}"
market = Market.find('btcusdt')
order = OrderBid.new(
state: Order::PENDING,
bid: market.quote_unit,
ask: market.base_unit,
market: market,
price: BigDecimal('50000'),
volume: BigDecimal('0.0001'),
origin_volume: BigDecimal('0.0001'),
member: member,
ord_type: 'limit'
)
begin
order.submit_order
puts "ORDER_OK id=#{order.id} state=#{order.state} price=#{order.price} volume=#{order.volume} locked=#{order.locked}"
rescue StandardError => e
puts "ORDER_FAIL #{e.class}: #{e.message}"
exit 1
end