Skip to content

Commit

Permalink
Merge pull request coingecko#2099 from coingecko/dydx-derivative-inte…
Browse files Browse the repository at this point in the history
…gration

introduce contract stat for dydx derivatives.
  • Loading branch information
teekenl authored Jun 1, 2020
2 parents 06c4b02 + c0268f3 commit 4d917ac
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ Or install it yourself as:
| Coinflex Futures | N | N | Y | Y | N | Y* | N | | | N | N | | | | coinflex_futures |
| Deribit | Y | Y | Y | Y | Y | Y | Y | | | Y | Y | Y | Y | Y | deribit |
| Delta Futures | Y | Y | Y | Y | N | Y | Y | Y | Y | N | N | Y | N | N | delta_futures |
| Dydx (Perpetual) | Y | Y | Y | Y | Y | Y | N | N | N | N |
N | Y | N | N | dydx_perpetual |
| FTX | Y | Y | Y | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | N | ftx |
| Fmex | Y | Y | Y | Y | Y | Y | Y | | | N | N | Y | Y | Y | fmex |
| Gate (Futures) | Y | Y | Y | Y | Y | Y | Y | | | N | N | Y | Y | Y | gate_futures |
Expand Down
11 changes: 6 additions & 5 deletions lib/cryptoexchange/exchanges/dydx/services/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ def ticker_url


def adapt_all(output)
output['markets'].map do |ticker|
base, target = ticker[0].split('-')
output['markets'].map do |ticker, value|
next if value["type"] == "PERPETUAL"

base, target = ticker.split('-')
market_pair = Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
market: Dydx::Market::NAME
)
adapt(ticker, market_pair)
end
adapt(value, market_pair)
end.compact
end

def adapt(output, market_pair)
ticker = Cryptoexchange::Models::Ticker.new
output = output[1]
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = Dydx::Market::NAME
Expand Down
8 changes: 5 additions & 3 deletions lib/cryptoexchange/exchanges/dydx/services/pairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ def fetch
end

def adapt(output)
output['markets'].map do |pair|
base, target = pair[0].split("-")
output['markets'].map do |pair, value|
next if value["type"] == "PERPETUAL"

base, target = pair.split("-")
Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
market: Dydx::Market::NAME
)
end
end.compact
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/cryptoexchange/exchanges/dydx_perpetual/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Cryptoexchange::Exchanges
module DydxPerpetual
class Market < Cryptoexchange::Models::Market
NAME = 'dydx_perpetual'
API_URL = 'https://api.dydx.exchange/v1'

def self.trade_page_url(args={})
base = if args[:base] == "PBTC"
"BTC"
else
args[:base]
end

"https://trade.dydx.exchange/perpetual/#{base}-#{args[:target]}"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Cryptoexchange::Exchanges
module DydxPerpetual
module Services
class ContractStat < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
contract_info = super(contract_info_url(market_pair))
adapt(market_pair, contract_info['market'])
end

def contract_info_url(market_pair)
"#{Cryptoexchange::Exchanges::DydxPerpetual::Market::API_URL}/perpetual-markets/#{market_pair.base}-#{market_pair.target}"
end

def adapt(market_pair, contract_info)
contract_stat = Cryptoexchange::Models::ContractStat.new
contract_stat.base = market_pair.base
contract_stat.target = market_pair.target
contract_stat.market = DydxPerpetual::Market::NAME
contract_stat.index_identifier = nil
contract_stat.index_name = nil
contract_stat.funding_rate_percentage = contract_info['fundingRate'] ? contract_info['fundingRate'] * 100 : nil
contract_stat.open_interest = contract_info['openInterest'] ? contract_info['openInterest'].to_f : nil
# contract_stat.index
# contract_stat.payload
# contract_stat.next_funding_rate_timestamp
# contract_stat.funding_rate_percentage_predicted
contract_stat.contract_type = "perpetual"

contract_stat
end
end
end
end
end
50 changes: 50 additions & 0 deletions lib/cryptoexchange/exchanges/dydx_perpetual/services/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Cryptoexchange::Exchanges
module DydxPerpetual
module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
false
end
end

def fetch
output = super ticker_url
adapt_all(output)
end

def ticker_url
"#{Cryptoexchange::Exchanges::DydxPerpetual::Market::API_URL}/stats/markets"
end

def adapt_all(output)
output["markets"].map do |ticker, value|
next if value["type"] != "PERPETUAL"

base, target = ticker.split('-')
market_pair = Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
market: DydxPerpetual::Market::NAME
)
adapt(value, market_pair)
end.compact
end

def adapt(output, market_pair)
ticker = Cryptoexchange::Models::Ticker.new
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = DydxPerpetual::Market::NAME
ticker.last = NumericHelper.to_d(output['last'])
ticker.high = NumericHelper.to_d(output['high'])
ticker.low = NumericHelper.to_d(output['low'])
ticker.volume = NumericHelper.to_d(output['baseVolume'])
ticker.timestamp = nil
ticker.payload = output
ticker
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/cryptoexchange/exchanges/dydx_perpetual/services/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Cryptoexchange::Exchanges
module DydxPerpetual
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, market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Dydx::Market::API_URL}/orderbook/#{market_pair.base}-#{market_pair.target}"
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 = DydxPerpetual::Market::NAME
order_book.asks = adapt_orders(output['asks'], order_book.target)
order_book.bids = adapt_orders(output['bids'], order_book.target)
order_book.timestamp = nil
order_book.payload = output
order_book
end

def adapt_orders(orders, target)
orders.collect do |order_entry|
price = order_entry["price"].to_f
amount = order_entry["amount"].to_f / 1_000_000_000_000_000_000
if target == "USDC"
price = price * 1_000_000_000_000
end
Cryptoexchange::Models::Order.new(price: price,
amount: amount,
timestamp: nil)
end
end
end
end
end
end
27 changes: 27 additions & 0 deletions lib/cryptoexchange/exchanges/dydx_perpetual/services/pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Cryptoexchange::Exchanges
module DydxPerpetual
module Services
class Pairs < Cryptoexchange::Services::Pairs
PAIRS_URL = "#{Cryptoexchange::Exchanges::DydxPerpetual::Market::API_URL}/stats/markets"

def fetch
output = super
adapt(output)
end

def adapt(output)
output['markets'].map do |pair, value|
next if value["type"] != "PERPETUAL"

base, target = pair.split("-")
Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
market: DydxPerpetual::Market::NAME
)
end.compact
end
end
end
end
end
24 changes: 14 additions & 10 deletions spec/cassettes/vcr_cassettes/Dydx/integration_specs_fetch_pairs.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4d917ac

Please sign in to comment.