Initial commit

This commit is contained in:
Yaser
2026-08-13 19:50:53 +03:30
commit 38084458fe
879 changed files with 95198 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
# frozen_string_literal: true
describe 'job.rake' do
context "Rake::Task['job:liabilities:compact_orders']" do
let(:member1) { create(:member) }
let(:member2) { create(:member) }
let(:job) { Job.last }
let!(:liabilities) do
(0..9).to_a.reverse.each do |day|
100.times { create(:liability, member_id: [member1.id, member2.id].sample, created_at: Time.now - day.day, updated_at: Time.now - day.day) }
end
end
subject { Rake::Task['job:liabilities:compact_orders'] }
before(:each) { DatabaseCleaner.clean }
after(:each) do
DatabaseCleaner.strategy = :truncation
subject.reenable
end
it 'call rake task with default time range', clean_database_with_truncation: true do
# default values
min = (Time.now - 1.week).beginning_of_day.to_s(:db)
max = (Time.now - 6.day).beginning_of_day.to_s(:db)
counter = Operations::Liability.where("LOWER(reference_type) = LOWER('Order') AND created_at BETWEEN '#{min}' AND '#{max}'").count
result = ActiveRecord::Base.connection.query("SELECT NULL, code, currency_id, member_id, SUM(debit), SUM(credit) FROM liabilities WHERE (LOWER(reference_type) = LOWER('Order') AND created_at BETWEEN '#{min}' AND '#{max}') GROUP BY code, member_id, currency_id, DATE(created_at)")
subject.invoke
expect(job.name).to eq('compact_orders')
expect(job.counter).to eq(counter)
expect(job.error_code).to eq(0)
expect(Operations::Liability.where(reference_type: 'CompactOrders').count).to eq(result.count)
end
it 'call rake task with time range', clean_database_with_truncation: true do
min = (Time.now - 2.day).beginning_of_day.to_s(:db)
max = (Time.now - 1.day).beginning_of_day.to_s(:db)
counter = Operations::Liability.where("LOWER(reference_type) = LOWER('Order') AND created_at BETWEEN '#{min}' AND '#{max}'").count
result = ActiveRecord::Base.connection.query("SELECT NULL, code, currency_id, member_id, SUM(debit), SUM(credit) FROM liabilities WHERE (LOWER(reference_type) = LOWER('Order') AND created_at BETWEEN '#{min}' AND '#{max}') GROUP BY code, member_id, currency_id, DATE(created_at)")
subject.invoke(min, max)
expect(job.name).to eq('compact_orders')
expect(job.counter).to eq(counter)
expect(job.error_code).to eq(0)
expect(Operations::Liability.where(reference_type: 'CompactOrders').count).to eq(result.count)
end
end
end

View File

@@ -0,0 +1,123 @@
# frozen_string_literal: true
describe 'revert.rake' do
let(:bob) { create(:member, :level_3, email: 'bob@gmail.com') }
let(:alice) { create(:member, :level_3, email: 'alice@gmail.com') }
let(:bob_accounting_balance) { Operations::Liability.where(member_id: bob.id).sum(:credit) - Operations::Liability.where(member_id: bob.id).sum(:debit) }
let(:alice_accounting_balance) { Operations::Liability.where(member_id: alice.id).sum(:credit) - Operations::Liability.where(member_id: alice.id).sum(:debit) }
let(:bob_legacy_balance) { bob.get_account(:usd).balance }
let(:alice_legacy_balance) { alice.get_account(:btc).balance }
subject { Rake::Task['revert:trading_activity'] }
after(:each) { subject.reenable }
context 'simple case' do
let(:price) { 10.to_d }
let(:volume) { 5.to_d }
let(:ask) { ::Matching::LimitOrder.new create(:order_ask, :btcusd, price: price, volume: volume, member: alice).to_matching_attributes }
let(:bid) { ::Matching::LimitOrder.new create(:order_bid, :btcusd, price: price, volume: volume, member: bob).to_matching_attributes }
let(:executor) do
Matching::Executor.new(
action: 'execute',
trade: {
market_id: 'btcusd',
maker_order_id: ask.id,
taker_order_id: bid.id,
strike_price: price.to_s('F'),
amount: volume.to_s('F'),
total: (price * volume).to_s('F')
}
)
end
before do
# Deposit 5 btc to Alice
# Deposit 50 usd to Bob
deposit = create(:deposit_btc, member_id: alice.id, amount: 5)
deposit.accept!
deposit.process!
deposit.dispatch!
create(:deposit_usd, member_id: bob.id, amount: 50).accept!
alice.get_account(:btc).lock_funds(5)
bob.get_account(:usd).lock_funds(50)
executor.execute!
end
it 'revert trading activities' do
subject.invoke(bob.email)
expect(alice_legacy_balance).to eq(5)
expect(bob_legacy_balance).to eq(50)
expect(Operations.validate_accounting_equation(Operations::Liability.all +
Operations::Asset.all +
Operations::Revenue.all +
Operations::Expense.all)).to eq(true)
expect(alice_legacy_balance).to eq(alice_accounting_balance)
expect(bob_legacy_balance).to eq(bob_accounting_balance)
end
end
context 'several trades' do
let(:price) { 10.to_d }
let(:volume) { 5.to_d }
let(:volume1) { 3.to_d }
let(:volume2) { 2.to_d }
let(:ask) { ::Matching::LimitOrder.new create(:order_ask, :btcusd, price: price, volume: volume, member: alice).to_matching_attributes }
let(:bid) { ::Matching::LimitOrder.new create(:order_bid, :btcusd, price: price, volume: volume1, member: bob).to_matching_attributes }
let(:bid1) { ::Matching::LimitOrder.new create(:order_bid, :btcusd, price: price, volume: volume2, member: bob).to_matching_attributes }
let(:executor1) do
Matching::Executor.new(
action: 'execute',
trade: {
market_id: 'btcusd',
maker_order_id: ask.id,
taker_order_id: bid.id,
strike_price: price.to_s('F'),
amount: volume1.to_s('F'),
total: (price * volume1).to_s('F')
}
)
end
let(:executor2) do
Matching::Executor.new(
action: 'execute',
trade: {
market_id: 'btcusd',
maker_order_id: ask.id,
taker_order_id: bid1.id,
strike_price: price.to_s('F'),
amount: volume2.to_s('F'),
total: (price * volume2).to_s('F')
}
)
end
before do
# Deposit 5 btc to Alice
# Deposit 50 usd to Bob
deposit = create(:deposit_btc, member_id: alice.id, amount: 5)
deposit.accept!
deposit.process!
deposit.dispatch!
create(:deposit_usd, member_id: bob.id, amount: 50).accept!
alice.get_account(:btc).lock_funds(5)
bob.get_account(:usd).lock_funds(50)
executor1.execute!
executor2.execute!
end
it 'reverts trading activities' do
subject.invoke(bob.email)
expect(alice_legacy_balance).to eq(5)
expect(bob_legacy_balance).to eq(50)
expect(Operations.validate_accounting_equation(Operations::Liability.all +
Operations::Asset.all +
Operations::Revenue.all +
Operations::Expense.all)).to eq(true)
expect(alice_legacy_balance).to eq(alice_accounting_balance)
expect(bob_legacy_balance).to eq(bob_accounting_balance)
end
end
end