Skip to content

Commit

Permalink
elvis change uniswap api url
Browse files Browse the repository at this point in the history
  • Loading branch information
ElvisLYC committed May 8, 2019
1 parent 84ace1a commit e75d971
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 159 deletions.
3 changes: 2 additions & 1 deletion lib/cryptoexchange/exchanges/uniswap/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Cryptoexchange::Exchanges
module Uniswap
class Market < Cryptoexchange::Models::Market
NAME = 'uniswap'
API_URL = 'https://uniswap-analytics.appspot.com/api/v1'
API_KEY = HashHelper.dig(Cryptoexchange::Credentials.get(@exchange), 'api_key')
API_URL = "https://api-test-238309.appspot.com/v0/exchanges?key=#{API_KEY}"
end
end
end
33 changes: 20 additions & 13 deletions lib/cryptoexchange/exchanges/uniswap/services/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@ module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
false
end
end

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

def ticker_url(market_pair)
pairs = Cryptoexchange::Exchanges::Uniswap::Services::Pairs.new.fetch
pair = pairs.select { |s| s.base == market_pair.base }.first
exchangeAddress = pair.inst_id
"#{Cryptoexchange::Exchanges::Uniswap::Market::API_URL}/ticker?exchangeAddress=#{exchangeAddress}"
def ticker_url
"#{Cryptoexchange::Exchanges::Uniswap::Market::API_URL}"
end

def adapt_all(output)
output.map do |pair|
base = pair["tokenSymbol"]
market_pair = Cryptoexchange::Models::MarketPair.new(
base: base,
target: "ETH",
market: Uniswap::Market::NAME
)
adapt(pair, market_pair)
end
end

def adapt(output, market_pair)
ticker = Cryptoexchange::Models::Ticker.new
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = Uniswap::Market::NAME
ticker.last = NumericHelper.divide(1, NumericHelper.to_d(output['lastTradePrice']))
ticker.high = NumericHelper.divide(1, NumericHelper.to_d(output['highPrice']))
ticker.low = NumericHelper.divide(1, NumericHelper.to_d(output['lowPrice']))
ticker.volume = (NumericHelper.to_d(output['tradeVolume']) / 10**18) / ticker.last
ticker.last = NumericHelper.divide(1, NumericHelper.to_d(output['price']))
ticker.volume = NumericHelper.to_d(output['tokenLiquidity'])
ticker.timestamp = nil
ticker.payload = output
ticker
Expand Down
7 changes: 2 additions & 5 deletions lib/cryptoexchange/exchanges/uniswap/services/pairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Cryptoexchange::Exchanges
module Uniswap
module Services
class Pairs < Cryptoexchange::Services::Pairs
PAIRS_URL = "#{Cryptoexchange::Exchanges::Uniswap::Market::API_URL}/directory"
PAIRS_URL = "#{Cryptoexchange::Exchanges::Uniswap::Market::API_URL}"

def fetch
output = super
Expand All @@ -12,14 +12,11 @@ def fetch
def adapt(output)
market_pairs = []
output.each do |pair|
base = pair["symbol"]
base = pair["tokenSymbol"]
target = "ETH"
inst_id = pair["exchangeAddress"]
next unless base && target
market_pairs << Cryptoexchange::Models::MarketPair.new(
base: base,
target: target,
inst_id: inst_id,
market: Uniswap::Market::NAME
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cryptoexchange/services/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def fetch(endpoint)
begin
response = http_get(endpoint)
if response.code == 200
response.parse :json
JSON.parse response, allow_nan: true
elsif response.code == 400
raise Cryptoexchange::HttpBadRequestError, { response: response }
else
Expand Down
2 changes: 1 addition & 1 deletion lib/cryptoexchange/services/pairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def fetch_via_api(endpoint = self.class::PAIRS_URL, params = self.class::POST_PA
begin
fetch_response = self.class::HTTP_METHOD == 'POST' ? http_post(endpoint, params) : http_get(endpoint)
if fetch_response.code == 200
fetch_response.parse :json
JSON.parse fetch_response, allow_nan: true
elsif fetch_response.code == 400
raise Cryptoexchange::HttpBadRequestError, { response: fetch_response }
else
Expand Down

Large diffs are not rendered by default.

This file was deleted.

4 changes: 1 addition & 3 deletions spec/exchanges/uniswap/integration/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

expect(ticker.volume).to be_a Numeric
expect(ticker.last).to be_a Numeric
expect(ticker.high).to be_a Numeric
expect(ticker.low).to be_a Numeric


expect(ticker.payload).to_not be nil
end
end
2 changes: 1 addition & 1 deletion spec/exchanges/uniswap/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

RSpec.describe Cryptoexchange::Exchanges::Uniswap::Market do
it { expect(described_class::NAME).to eq 'uniswap' }
it { expect(described_class::API_URL).to eq 'https://uniswap-analytics.appspot.com/api/v1' }
it { expect(described_class::API_URL).to eq 'https://api-test-238309.appspot.com/v0/exchanges?key=#{YOUR_API_KEY_HERE}' }
end

0 comments on commit e75d971

Please sign in to comment.