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,247 @@
# frozen_string_literal: true
describe API::V2::CoinGecko::HistoricalTrades, type: :request do
describe 'GET /api/v2/coingecko/historical_trades' do
before(:each) { delete_measurments('trades') }
after(:each) { delete_measurments('trades') }
context 'there is no market pair' do
it 'should return error' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'TEST_TEST' }
expect(response).to have_http_status 404
expect(response).to include_api_error('record.not_found')
end
end
context 'there is no trades in influx' do
let(:expected_response) do
{
'buy' => [],
'sell' => []
}
end
it 'should return recent trades' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD' }
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result).to eq expected_response
end
end
context 'there are trades in influx' do
let!(:trade1) { create(:trade, :btcusd, price: '5.0'.to_d, amount: '1.1'.to_d, total: '5.5'.to_d, created_at: Time.now) }
let!(:trade2) { create(:trade, :btcusd, price: '6.0'.to_d, amount: '0.9'.to_d, total: '5.4'.to_d, created_at: Time.now + 1.month) }
before do
trade1.write_to_influx
trade2.write_to_influx
end
context 'return all trades' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 0.9,
'price' => 6,
'target_volume' => 5.4,
'trade_id' => trade2.id,
'trade_timestamp' => trade2.created_at.to_i * 1000,
'type' => 'buy'},
{'base_volume' => 1.1,
'price' => 5,
'target_volume' => 5.5,
'trade_id' => trade1.id,
'trade_timestamp' => trade1.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
it do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD'}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
end
context 'return filtered trades' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 0.9,
'price' => 6,
'target_volume' => 5.4,
'trade_id' => trade2.id,
'trade_timestamp' => trade2.created_at.to_i * 1000,
'type' => 'buy'},
{'base_volume' => 1.1,
'price' => 5,
'target_volume' => 5.5,
'trade_id' => trade1.id,
'trade_timestamp' => trade1.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
context 'by taker_type' do
it 'buy' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD', type: 'buy'}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
it 'sell' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD', type: 'sell'}
expect(response).to be_successful
expect(response_body).to eq({'buy'=> [], 'sell' => []})
end
end
context 'by start time' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 0.9,
'price' => 6,
'target_volume' => 5.4,
'trade_id' => trade2.id,
'trade_timestamp' => trade2.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
it do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD', start_time: Time.now + 15.days}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
end
context 'by end time' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 1.1,
'price' => 5,
'target_volume' => 5.5,
'trade_id' => trade1.id,
'trade_timestamp' => trade1.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
it do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD', end_time: Time.now + 15.days}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
end
context 'by limit' do
context 'without specified limit' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 0.9,
'price' => 6,
'target_volume' => 5.4,
'trade_id' => trade2.id,
'trade_timestamp' => trade2.created_at.to_i * 1000,
'type' => 'buy'},
{'base_volume' => 1.1,
'price' => 5,
'target_volume' => 5.5,
'trade_id' => trade1.id,
'trade_timestamp' => trade1.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
it 'returns all trades' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD'}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
end
context 'with specified limit' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 0.9,
'price' => 6,
'target_volume' => 5.4,
'trade_id' => trade2.id,
'trade_timestamp' => trade2.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
it 'returns specific number of trades' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD', limit: 1}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
end
context 'with limit==0' do
let(:expected_response) do
{
'buy' => [
{'base_volume' => 0.9,
'price' => 6,
'target_volume' => 5.4,
'trade_id' => trade2.id,
'trade_timestamp' => trade2.created_at.to_i * 1000,
'type' => 'buy'},
{'base_volume' => 1.1,
'price' => 5,
'target_volume' => 5.5,
'trade_id' => trade1.id,
'trade_timestamp' => trade1.created_at.to_i * 1000,
'type' => 'buy'}
],
'sell' => []
}
end
it 'returns all trades' do
get '/api/v2/coingecko/historical_trades', params: { ticker_id: 'BTC_USD', limit: 0}
expect(response).to be_successful
expect(response_body).to eq expected_response
end
end
end
end
end
end
end

View File

@@ -0,0 +1,98 @@
# frozen_string_literal: true
describe API::V2::CoinGecko::Orderbook, type: :request do
describe 'GET /api/v2/coingecko/orderbook' do
before do
create_list(:order_bid, 5, :btcusd)
create_list(:order_bid, 5, :btcusd, price: 2)
create_list(:order_ask, 5, :btcusd)
create_list(:order_ask, 5, :btcusd, price: 3)
end
let(:asks) { [["1.0", "5.0"], ["3.0", "5.0"]] }
let(:bids) { [["2.0", "5.0"], ["1.0", "5.0"]] }
context 'valid market param' do
it 'sorts asks and bids from highest to lowest' do
get "/api/v2/coingecko/orderbook", params: { ticker_id: "BTC_USD"}
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result['asks'].size).to eq 2
expect(result['bids'].size).to eq 2
expect(result['asks']).to eq asks
expect(result['bids']).to eq bids
end
context 'with depth param' do
before do
create_list(:order_bid, 5, :btcusd)
create_list(:order_bid, 5, :btcusd, price: 4.1)
create_list(:order_ask, 5, :btcusd)
create_list(:order_ask, 5, :btcusd, price: 12.2)
end
it 'get asks and bids with depth param' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 2 }
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result['asks'].size).to eq 1
expect(result['bids'].size).to eq 1
end
it 'get asks and bids with depth param' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 4 }
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result['asks'].size).to eq 2
expect(result['bids'].size).to eq 2
end
it 'get asks and bids with depth param' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 1 }
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result['asks'].size).to eq 0
expect(result['bids'].size).to eq 0
end
it 'get asks and bids with depth param' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 3 }
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result['asks'].size).to eq 1
expect(result['bids'].size).to eq 1
end
it 'get asks and bids with depth param for all orderbook' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 0 }
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result['asks'].size).to eq 3
expect(result['bids'].size).to eq 3
end
context 'invalid depth params' do
it 'shoud return error' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 'test' }
expect(response).to have_http_status 422
expect(response).to include_api_error('coingecko.market_depth.non_integer_depth')
end
it 'shoud return error' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "BTC_USD", depth: 2000 }
expect(response).to have_http_status 422
expect(response).to include_api_error('coingecko.market_depth.invalid_depth')
end
end
end
end
context 'invalid market param' do
it 'validates market param' do
get '/api/v2/coingecko/orderbook', params: { ticker_id: "usdusd" }
expect(response).to have_http_status 404
expect(response).to include_api_error('record.not_found')
end
end
end
end

View File

@@ -0,0 +1,42 @@
# encoding: UTF-8
# frozen_string_literal: true
describe API::V2::CoinGecko::Pairs, type: :request do
describe 'GET /api/v2/coingecko/pairs' do
before(:each) { clear_redis }
let!(:market) do
::Market.enabled.ordered.sample
end
let!(:expected_response) {
{
"ticker_id" => market.underscore_name,
"base" => market[:base_unit].upcase,
"target" => market[:quote_unit].upcase
}
}
it 'lists visible currencies' do
get '/api/v2/coingecko/pairs'
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result.size).to eq Market.enabled.size
expect(result).to include(expected_response)
end
end
context 'There is no markets' do
before { DatabaseCleaner.clean }
it 'should return summary' do
get '/api/v2/coingecko/pairs'
expect(response).to be_successful
result = JSON.parse(response.body)
expect(result).to eq []
end
end
end

View File

@@ -0,0 +1,107 @@
# encoding: UTF-8
# frozen_string_literal: true
describe API::V2::CoinGecko::Tickers, type: :request do
describe 'GET /api/v2/coingecko/tickers' do
before(:each) { delete_measurments('trades') }
after(:each) { delete_measurments('trades') }
before do
create_list(:order_bid, 5, :btcusd)
create_list(:order_ask, 5, :btcusd)
end
context 'no trades executed yet' do
let(:expected_btcusd_ticker) do
{
'ticker_id' => 'BTC_USD',
'base_currency' => 'BTC',
'target_currency' => 'USD',
'last_price' => '0.0',
'target_volume' => '0.0', 'base_volume' => '0.0',
'bid' => '1.0', 'ask' => '1.0',
'high' => '0.0', 'low' => '0.0'
}
end
let(:expected_btceth_ticker) do
{
'ticker_id' => 'BTC_ETH',
'base_currency' => 'BTC',
'target_currency' => 'ETH',
'last_price' => '0.0',
'target_volume' => '0.0', 'base_volume' => '0.0',
'bid' => '0.0', 'ask' => '0.0',
'high' => '0.0', 'low' => '0.0'
}
end
it 'returns tickers of all markets' do
get '/api/v2/coingecko/tickers'
expect(response).to be_successful
btc_usd_ticker = response_body.find {|ticker| ticker['ticker_id'] == 'BTC_USD'}
btc_eth_ticker = response_body.find {|ticker| ticker['ticker_id'] == 'BTC_ETH'}
expect(btc_usd_ticker).to eq expected_btcusd_ticker
expect(btc_eth_ticker).to eq expected_btceth_ticker
end
end
context 'single trade was executed' do
let!(:trade) { create(:trade, :btcusd, price: '5.0'.to_d, amount: '1.1'.to_d, total: '5.5'.to_d)}
let(:expected_ticker) do
{
'ticker_id' => 'BTC_USD',
'base_currency' => 'BTC',
'target_currency' => 'USD',
'last_price' => '5.0',
'target_volume' => '5.5', 'base_volume' => '1.1',
'bid' => '1.0', 'ask' => '1.0',
'high' => '5.0', 'low' => '5.0'
}
end
before do
trade.write_to_influx
end
it 'returns tickers of all markets' do
get '/api/v2/coingecko/tickers'
expect(response).to be_successful
btc_usd_ticker = response_body.find {|ticker| ticker['ticker_id'] == 'BTC_USD'}
expect(btc_usd_ticker).to eq expected_ticker
end
end
context 'multiple trades were executed' do
let!(:trade1) { create(:trade, :btcusd, price: '5.0'.to_d, amount: '1.1'.to_d, total: '5.5'.to_d)}
let!(:trade2) { create(:trade, :btcusd, price: '6.0'.to_d, amount: '0.9'.to_d, total: '5.4'.to_d)}
let(:expected_ticker) do
{
'ticker_id' => 'BTC_USD',
'base_currency' => 'BTC',
'target_currency' => 'USD',
'last_price' => '6.0',
'target_volume' => '10.9', 'base_volume' => '2.0',
'bid' => '1.0', 'ask' => '1.0',
'high' => '6.0', 'low' => '5.0'
}
end
before do
trade1.write_to_influx
trade2.write_to_influx
end
it 'returns tickers of all markets' do
get '/api/v2/coingecko/tickers'
expect(response).to be_successful
btc_usd_ticker = response_body.find {|ticker| ticker['ticker_id'] == 'BTC_USD'}
expect(btc_usd_ticker).to eq expected_ticker
end
end
end
end