Skip to content

Commit

Permalink
chore: Remove unused 'trades' related code (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvtaveras authored Jul 24, 2023
1 parent b98ff6b commit b2ed396
Show file tree
Hide file tree
Showing 28 changed files with 99 additions and 744 deletions.
39 changes: 5 additions & 34 deletions src/exchange_adapters/base.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { CeloContract } from '@celo/contractkit'
import { Currency, Exchange, ExternalCurrency, megabytesToBytes, requireVariables } from '../utils'
import { ExchangeApiRequestError, MetricCollector } from '../metric_collector'
import fetch, { Response } from 'node-fetch'

import BigNumber from 'bignumber.js'
import { CeloContract } from '@celo/contractkit'
import Logger from 'bunyan'
import https from 'https'
import fetch, { Response } from 'node-fetch'
import tls from 'tls'
import { ExchangeApiRequestError, MetricCollector } from '../metric_collector'
import { Currency, Exchange, ExternalCurrency, megabytesToBytes, requireVariables } from '../utils'

export enum DataType {
TICKER = 'Ticker',
TRADE = 'Trade',
}

export type RawPriceData = Ticker | Trade

type PriceMetadata = {
/** the exchange this price data object came from */
source: Exchange
Expand Down Expand Up @@ -53,25 +51,14 @@ export type Ticker = PriceMetadata & {
timestamp: number
}

export type Trade = PriceMetadata & {
amount: BigNumber
cost: BigNumber
id: string
price: BigNumber
side?: string
timestamp: number
}

export enum ExchangeDataType {
ORDERBOOK_STATUS = 'orderbook_status',
TRADE = 'trade',
TICKER = 'ticker',
}

export interface ExchangeAdapter {
exchangeName: Exchange
pairSymbol: string
fetchTrades: () => Promise<Trade[]>
fetchTicker: () => Promise<Ticker>
}

Expand Down Expand Up @@ -192,13 +179,6 @@ export abstract class BaseExchangeAdapter {
*/
abstract fetchTicker(): Promise<Ticker>

/**
* Fetches trades from the exchange, normalizes their format, and returns them
* in chronological order.
* It's not currently used by any BaseExchangeAdapter client.
*/
abstract fetchTrades(): Promise<Trade[]>

/**
* Fetches from an exchange api endpoint and returns the json-parsed result.
* Unless this is the fetch to get the orderbook status, it will confirm that
Expand Down Expand Up @@ -314,15 +294,6 @@ export abstract class BaseExchangeAdapter {
requireVariables({ timestamp, bid, ask, lastPrice, baseVolume })
}

/**
* Protect against bad or missing values from the api
* @param trade
*/
protected verifyTrade(trade: Partial<Trade>): void {
const { id, timestamp, price, amount, cost } = trade
requireVariables({ id, timestamp, price, amount, cost })
}

/**
* Parses a value as a BigNumber and avoids the NaN or infinite case by
* returning undefined instead
Expand Down
47 changes: 2 additions & 45 deletions src/exchange_adapters/binance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker } from './base'

import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'

export class BinanceAdapter extends BaseExchangeAdapter implements ExchangeAdapter {
baseApiUrl = 'https://api.binance.com/api/v3'
Expand All @@ -24,15 +25,6 @@ export class BinanceAdapter extends BaseExchangeAdapter implements ExchangeAdapt
return this.parseTicker(tickerJson)
}

async fetchTrades(): Promise<Trade[]> {
const tradesJson = await this.fetchFromApi(
ExchangeDataType.TRADE,
`aggTrades?symbol=${this.pairSymbol}`
)
// sort order from API is chronological
return this.parseTrades(tradesJson)
}

/**
*
* @param json parsed response from Binance's ticker endpoint
Expand Down Expand Up @@ -78,41 +70,6 @@ export class BinanceAdapter extends BaseExchangeAdapter implements ExchangeAdapt
return ticker
}

/**
*
* @param json response from Binance's trades endpoint
*
* [
* {
* "a": 683882, // Aggregate tradeId
* "p": "0.00008185", // Price
* "q": "5.30000000", // Quantity
* "f": 854278, // First tradeId
* "l": 854278, // Last tradeId
* "T": 1614665220705, // Timestamp
* "m": true, // Was the buyer the maker?
* "M": true // Was the trade the best price match?
* }
* ]
*/
parseTrades(json: any): Trade[] {
return json.map((trade: any) => {
const price = this.safeBigNumberParse(trade.p)
const amount = this.safeBigNumberParse(trade.q)
const normalizedTrade = {
...this.priceObjectMetadata,
amount,
cost: amount ? price?.times(amount) : undefined,
id: trade.a,
price,
side: trade.m ? 'sell' : 'buy',
timestamp: this.safeBigNumberParse(trade.T)?.toNumber()!,
}
this.verifyTrade(normalizedTrade)
return normalizedTrade
})
}

async isOrderbookLive(): Promise<boolean> {
const res = await this.fetchFromApi(ExchangeDataType.ORDERBOOK_STATUS, 'exchangeInfo')

Expand Down
13 changes: 3 additions & 10 deletions src/exchange_adapters/bitget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseExchangeAdapter, ExchangeDataType, Ticker } from './base'

import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'

export class BitgetAdapter extends BaseExchangeAdapter {
baseApiUrl = 'https://api.bitget.com/api'
Expand All @@ -18,14 +19,6 @@ export class BitgetAdapter extends BaseExchangeAdapter {
)
}

async fetchTrades(): Promise<Trade[]> {
/**
* Trades are cool, but empty arrays are cooler.
* @bogdan, 01/2023
*/
return []
}

protected generatePairSymbol(): string {
const base = BaseExchangeAdapter.standardTokenSymbolMap.get(this.config.baseCurrency)
const quote = BaseExchangeAdapter.standardTokenSymbolMap.get(this.config.quoteCurrency)
Expand Down Expand Up @@ -75,7 +68,7 @@ export class BitgetAdapter extends BaseExchangeAdapter {

/**
* Checks if the orderbook for the relevant pair is live. If it's not, the price
* data from Ticker + Trade endpoints may be inaccurate.
* data from Ticker endpoint may be inaccurate.
*
* https://api.bitget.com/api/spot/v1/public/product?symbol=BTCBRL_SPBL
*
Expand Down
11 changes: 2 additions & 9 deletions src/exchange_adapters/bitmart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker } from './base'

import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'

export class BitMartAdapter extends BaseExchangeAdapter implements ExchangeAdapter {
baseApiUrl = 'https://api-cloud.bitmart.com'
Expand All @@ -22,14 +23,6 @@ export class BitMartAdapter extends BaseExchangeAdapter implements ExchangeAdapt
return this.parseTicker(tickerJson)
}

async fetchTrades(): Promise<Trade[]> {
// Trade data is not needed by oracle but is required by the parent class.
// This function along with all other functions that are not needed by the oracle will
// be removed in a future PR.
// -- @bayological ;) --
return []
}

/**
*
* @param json parsed response from bitmart's ticker endpoint
Expand Down
44 changes: 2 additions & 42 deletions src/exchange_adapters/bitso.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker } from './base'

import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'

export class BitsoAdapter extends BaseExchangeAdapter implements ExchangeAdapter {
baseApiUrl = 'https://api.bitso.com/api/v3'
Expand All @@ -24,14 +25,6 @@ export class BitsoAdapter extends BaseExchangeAdapter implements ExchangeAdapter
return this.parseTicker(tickerJson.payload)
}

async fetchTrades(): Promise<Trade[]> {
const tradesJson = await this.fetchFromApi(
ExchangeDataType.TRADE,
`trades?book=${this.pairSymbol}`
)
return this.parseTrades(tradesJson.payload).sort((a, b) => a.timestamp - b.timestamp)
}

/**
*
* @param json parsed response from Bitso's ticker endpoint
Expand Down Expand Up @@ -70,39 +63,6 @@ export class BitsoAdapter extends BaseExchangeAdapter implements ExchangeAdapter
return ticker
}

/**
*
* @param json response from Bitso's trades endpoint
*
* [
* {
* "book": "btc_mxn",
* "created_at": "2021-07-02T05:54:45+0000",
* "amount": "0.00127843",
* "maker_side": "buy",
* "price": "659436.40",
* "tid": 41827090
* }
* ]
*/
parseTrades(json: any): Trade[] {
return json.map((trade: any) => {
const price = this.safeBigNumberParse(trade.price)
const amount = this.safeBigNumberParse(trade.amount)
const normalizedTrade = {
...this.priceObjectMetadata,
amount,
cost: amount ? price?.times(amount) : undefined,
id: trade.tid,
price,
side: trade.maker_side,
timestamp: this.safeDateParse(trade.created_at)!,
}
this.verifyTrade(normalizedTrade)
return normalizedTrade
})
}

/**
* No endpoint available to check this from Bitso.
* @returns bool
Expand Down
11 changes: 2 additions & 9 deletions src/exchange_adapters/bitstamp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker } from './base'

import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'

export class BitstampAdapter extends BaseExchangeAdapter implements ExchangeAdapter {
baseApiUrl = 'https://www.bitstamp.net/api/v2'
Expand All @@ -20,14 +21,6 @@ export class BitstampAdapter extends BaseExchangeAdapter implements ExchangeAdap
return this.parseTicker(tickerJson)
}

async fetchTrades(): Promise<Trade[]> {
// Trade data is not needed by oracle but is required by the parent class.
// This function along with all other functions that are not needed by the oracle will
// be removed in a future PR.
// -- @bayological ;) --
return []
}

/**
*
* @param json parsed response from bitstamps's ticker endpoint
Expand Down
46 changes: 2 additions & 44 deletions src/exchange_adapters/bittrex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseExchangeAdapter, ExchangeDataType, Ticker } from './base'

import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'

export class BittrexAdapter extends BaseExchangeAdapter {
baseApiUrl = 'https://api.bittrex.com/v3'
Expand All @@ -19,14 +20,6 @@ export class BittrexAdapter extends BaseExchangeAdapter {
return this.parseTicker(tickerJson, summaryJson)
}

async fetchTrades(): Promise<Trade[]> {
const tradeJson = await this.fetchFromApi(
ExchangeDataType.TRADE,
`markets/${this.pairSymbol}/trades`
)
return this.parseTrades(tradeJson)
}

protected generatePairSymbol(): string {
return `${BittrexAdapter.tokenSymbolMap.get(
this.config.baseCurrency
Expand Down Expand Up @@ -78,41 +71,6 @@ export class BittrexAdapter extends BaseExchangeAdapter {
return ticker
}

/**
* Translates the json response from the Bittrex API into the standard Trade object
*
* @param json response from the trades endpoint
* https://api.bittrex.com/v3/markets/{marketSymbol}/trades
*
* [
* {
* id: "string (uuid)",
* executedAt: "string (date-time)",
* quantity: "number (double)",
* rate: "number (double)",
* takerSide: "string"
* }
* ]
*/
parseTrades(json: any): Trade[] {
return json.map((trade: any) => {
const price = this.safeBigNumberParse(trade.rate)
const amount = this.safeBigNumberParse(trade.quantity)
const cost = amount ? price?.times(amount) : undefined
const normalizedTrade = {
...this.priceObjectMetadata,
amount,
cost,
id: trade.id,
price,
side: trade.takerSide,
timestamp: this.safeDateParse(trade.executedAt)!,
}
this.verifyTrade(normalizedTrade)
return normalizedTrade
})
}

/**
* https://bittrex.github.io/api/v3#/definitions/Market
*
Expand Down
Loading

0 comments on commit b2ed396

Please sign in to comment.