Skip to content
This repository was archived by the owner on Jun 22, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/cryptoexchange/exchanges/trader_one/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cryptoexchange::Exchanges
module TraderOne
class Market < Cryptoexchange::Models::Market
NAME = 'trader_one'
API_URL = 'https://api.traderone.exchange'
end
end
end
39 changes: 39 additions & 0 deletions lib/cryptoexchange/exchanges/trader_one/services/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Cryptoexchange::Exchanges
module TraderOne
module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output, market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::TraderOne::Market::API_URL}/markets/tickers?#{market_pair.base}-#{market_pair.target}"
end

def adapt(output, market_pair)
name = "#{market_pair.base}-#{market_pair.target}".upcase
market = output[name]

ticker = Cryptoexchange::Models::Ticker.new
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = TraderOne::Market::NAME
ticker.last = NumericHelper.to_d(market['lastPrice'])
ticker.ask = NumericHelper.to_d(market['ask'])
ticker.bid = NumericHelper.to_d(market['bid'])
ticker.volume = NumericHelper.to_d(market['baseVolume'])
ticker.timestamp = nil
ticker.payload = output
ticker
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/cryptoexchange/exchanges/trader_one/services/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Cryptoexchange::Exchanges
module TraderOne
module Services
class OrderBook < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output['result'], market_pair)
end

def ticker_url(market_pair)
market = "#{market_pair.base}-#{market_pair.target}"
"#{Cryptoexchange::Exchanges::TraderOne::Market::API_URL}/markets/listOrderBook?market=#{market}"
end

def adapt(output, market_pair)
order_book = Cryptoexchange::Models::OrderBook.new
order_book.base = market_pair.base
order_book.target = market_pair.target
order_book.market = TraderOne::Market::NAME
order_book.asks = adapt_orders(output['sell'])
order_book.bids = adapt_orders(output['buy'])
order_book.payload = output
order_book

end

def adapt_orders(orders)
orders.map do |order_entry|
unitPrice = order_entry[0]
totalAmount = order_entry[1]
rateUSDC = order_entry[2]
flag = order_entry[3]

Cryptoexchange::Models::Order.new(price: unitPrice,
amount: totalAmount,
timestamp: nil)
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/cryptoexchange/exchanges/trader_one/services/pair.s.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Cryptoexchange::Exchanges
module TraderOne
module Services
class Pairs < Cryptoexchange::Services::Pairs
PAIRS_URL = "#{Cryptoexchange::Exchanges::TraderOne::Market::API_URL}/markets/tickers?quoteCurrency"

def fetch
output = super
adapt(output)
end

def adapt(output)
output.map do |pair|
base, target = pair.first.split('-')
Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
market: TraderOne::Market::NAME
)
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/cryptoexchange/exchanges/trader_one/services/trades.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Cryptoexchange::Exchanges
module TraderOne
module Services
class Trades < Cryptoexchange::Services::Market
def fetch(market_pair)
output = super(ticker_url(market_pair))
trades = output['result']
adapt(trades['orders'], market_pair)
end

def ticker_url(market_pair)
market = "#{market_pair.base}-#{market_pair.target}"
"#{Cryptoexchange::Exchanges::TraderOne::Market::API_URL}/orders/listOrders?market=#{market}&status=filled"
end

def adapt(trades, market_pair)
trades.collect do |trade|
tr = Cryptoexchange::Models::Trade.new
tr.base = market_pair.base
tr.target = market_pair.target
tr.type = trade['side']
tr.price = trade['price'].to_f
tr.amount = trade['amount'].to_f
tr.timestamp = trade['updatedAt'].to_i / 1000
tr.payload = trade
tr.market = TraderOne::Market::NAME
tr
end
end
end
end
end
end

62 changes: 62 additions & 0 deletions spec/exchanges/trader_one/integration/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'

RSpec.describe 'TraderOne integration specs' do
let(:client) { Cryptoexchange::Client.new }
let(:btc_usdc_pair) { Cryptoexchange::Models::MarketPair.new(base: 'BTC', target: 'USDC', market: 'trader_one') }

it 'fetch ticker' do
ticker = client.ticker(btc_usdc_pair)

expect(ticker.base).to eq 'BTC'
expect(ticker.target).to eq 'USDC'
expect(ticker.market).to eq 'trader_one'
expect(ticker.last).to be_a Numeric
expect(ticker.ask).to be_a Numeric
expect(ticker.bid).to be_a Numeric
expect(ticker.volume).to be_a Numeric
expect(ticker.timestamp).to be nil
expect(ticker.payload).to_not be nil
end

it 'fetch order book' do
order_book = client.order_book(btc_usdc_pair)

expect(order_book.base).to eq 'BTC'
expect(order_book.target).to eq 'USDC'
expect(order_book.market).to eq 'trader_one'
expect(order_book.asks).to_not be_empty
expect(order_book.bids).to_not be_empty
expect(order_book.asks.first.price).to_not be_nil
expect(order_book.bids.first.amount).to_not be_nil
expect(order_book.bids.first.timestamp).to be_nil
expect(order_book.asks.count).to be > 0
expect(order_book.bids.count).to be > 0
expect(order_book.timestamp).to be nil
expect(order_book.payload).to_not be nil
end

it 'fetch pairs' do
pairs = client.pairs('trader_one')
expect(pairs).not_to be_empty

pair = pairs.first
expect(pair.base).to_not be nil
expect(pair.target).to_not be nil
expect(pair.market).to eq 'trader_one'
end

it 'fetch trade' do
trades = client.trades(btc_usdc_pair)
trade = trades.sample
expect(trades).to_not be_empty
expect(trade.base).to eq 'BTC'
expect(trade.target).to eq 'USDC'
expect(trade.market).to eq 'trader_one'
expect(trade.trade_id).to be_nil
expect(trade.price).to_not be_nil
expect(trade.amount).to_not be_nil
expect(trade.timestamp).to be_a Numeric
expect(trade.payload).to_not be nil
end

end
6 changes: 6 additions & 0 deletions spec/exchanges/trader_one/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

RSpec.describe Cryptoexchange::Exchanges::TraderOne::Market do
it { expect(described_class::NAME).to eq 'trader_one' }
it { expect(described_class::API_URL).to eq 'https://api.traderone.exchange' }
end