forked from coingecko/cryptoexchange
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request coingecko#2099 from coingecko/dydx-derivative-inte…
…gration introduce contract stat for dydx derivatives.
- Loading branch information
Showing
13 changed files
with
420 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
40 changes: 40 additions & 0 deletions
40
lib/cryptoexchange/exchanges/dydx_perpetual/services/contract_stat.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
lib/cryptoexchange/exchanges/dydx_perpetual/services/market.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
lib/cryptoexchange/exchanges/dydx_perpetual/services/order_book.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
lib/cryptoexchange/exchanges/dydx_perpetual/services/pairs.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
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.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
spec/cassettes/vcr_cassettes/DydxPerpetual/integration_specs_fetch_contract_stat.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.