-
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 #69 from ASparton/feat/23-get-one-crypto-history
feat/23-get-one-crypto-history
- Loading branch information
Showing
4 changed files
with
92 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,71 @@ | ||
import { binance } from 'ccxt'; | ||
import HttpStatusCode from '#types/HttpStatusCode'; | ||
import ApiErrors, { APIError } from '~apiErrors'; | ||
|
||
import { binance } from 'ccxt'; | ||
const _exchange = new binance(); | ||
|
||
async function getAllCrypto(cryptos: string[]) { | ||
return await _exchange.fetchTickers(cryptos); | ||
/** | ||
* Return the period and limit associated with a given period | ||
* | ||
* @param period - either 'daily', 'hourly' or 'minute' | ||
* | ||
* @returns a tuple of string and number | ||
*/ | ||
function _getPeriodAndLimit(period: string): [string, number] { | ||
switch (period) { | ||
case 'daily': | ||
return ['1d', 60]; | ||
|
||
case 'hourly': | ||
return ['1h', 48]; | ||
|
||
case 'minute': | ||
return ['1m', 120]; | ||
|
||
default: | ||
throw new APIError(ApiErrors.BAD_REQUEST, HttpStatusCode.BAD_REQUEST_400); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the tickers associated with the given symbols. | ||
* | ||
* @param symbols - symbols to fetch | ||
* | ||
* @returns a Promise of Ticker array | ||
*/ | ||
async function getAllCrypto(symbols: string[]) { | ||
return await _exchange.fetchTickers(symbols); | ||
} | ||
|
||
/** | ||
* Returns the ticker associated with the given symbol. | ||
* | ||
* @param symbol - symbol to fetch | ||
* | ||
* @returns a Promise of Ticker | ||
*/ | ||
async function getCrypto(symbol: string) { | ||
return await _exchange.fetchTicker(symbol); | ||
} | ||
|
||
async function getCrypto(crypto: string) { | ||
return await _exchange.fetchTicker(crypto); | ||
/** | ||
* Retrieves the history of the symbol using the given period. | ||
* | ||
* @param symbol - symbol to fetch | ||
* @param period - period to use | ||
* | ||
* @returns a Promise of an OHLCV array | ||
*/ | ||
async function getHistory(symbol: string, period: string) { | ||
const [_period, limit] = _getPeriodAndLimit(period); | ||
return await _exchange.fetchOHLCV(symbol, _period, undefined, limit); | ||
} | ||
|
||
const useCrypto = () => ({ | ||
getAllCrypto, | ||
getCrypto, | ||
getHistory, | ||
}); | ||
|
||
export default useCrypto; |
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,8 @@ | ||
import { z } from 'zod'; | ||
|
||
const HistoryParamDTO = z.object({ | ||
id: z.coerce.number(), | ||
period: z.enum(['daily', 'hourly', 'minute']), | ||
}); | ||
|
||
export default HistoryParamDTO; |