diff --git a/apps/website/src/app/demos/dashboard-trade/utils/getPrices.ts b/apps/website/src/app/demos/dashboard-trade/utils/getPrices.ts deleted file mode 100644 index 8fe3ce3..0000000 --- a/apps/website/src/app/demos/dashboard-trade/utils/getPrices.ts +++ /dev/null @@ -1,49 +0,0 @@ -import axios from 'axios' - -import { - CoingeckoSimplePriceRequest, - CoingeckoSimplePriceResponse, -} from '@/types/coingecko/simplePrices' - -import generateRequestParams from '@/utils/coingecko/simplePrices/generateRequestParams' - -function getPrices( - params: CoingeckoSimplePriceRequest, -): Promise> { - const options = generateRequestParams(params) - - const promise: Promise> = new Promise( - (resolve, reject) => { - axios - .request(options) - .then(function (response) { - const keys = Object.keys(response.data) - - const coingeckoPrices: Array = keys.map( - key => { - const item = response.data[key] - return { - cryptoCurrency: key, - btc: item.btc, - usd: item.usd, - eth: item.eth, - btc_24h_change: item.btc_24h_change, - eth_24h_change: item.eth_24h_change, - usd_24h_change: item.usd_24h_change, - } - }, - ) - - resolve(coingeckoPrices) - }) - .catch(function (error) { - console.error('prices ...', error) - reject(error) - }) - }, - ) - - return promise -} - -export default getPrices diff --git a/apps/website/src/classes/coingecko/SimplePriceRequest.ts b/apps/website/src/classes/coingecko/SimplePriceRequest.ts new file mode 100644 index 0000000..a4632a6 --- /dev/null +++ b/apps/website/src/classes/coingecko/SimplePriceRequest.ts @@ -0,0 +1,62 @@ +import axios, {AxiosResponse} from 'axios' + +import { + CoingeckoSimplePriceRequest, + CoingeckoSimplePriceResponse, +} from '@/types/coingecko/simplePrices' +import generateRequestParams from '@/utils/coingecko/simplePrices/generateRequestParams' + +class SimplePriceRequest { + params: CoingeckoSimplePriceRequest + + constructor(params: CoingeckoSimplePriceRequest) { + this.params = params + } + + async execute(): Promise> { + const options = generateRequestParams(this.params) + + const promise: Promise> = new Promise( + (resolve, reject) => { + axios + .request(options) + .then(function (response: AxiosResponse) { + const keys = Object.keys(response.data) + + const coingeckoPrices: Array = + keys.map(key => { + const item = response.data[key] + const cryptoCurrency = key + + const btc = item.btc || 0 + const eth = item.eth || 0 + const usd = item.usd || 0 + const btc_24h_change = item.btc_24h_change || 0 + const eth_24h_change = item.eth_24h_change || 0 + const usd_24h_change = item.usd_24h_change || 0 + + return { + cryptoCurrency, + btc: btc, + usd: usd, + eth: eth, + btc_24h_change, + eth_24h_change, + usd_24h_change, + } + }) + + resolve(coingeckoPrices) + }) + .catch(function (error) { + console.error('prices ...', error) + reject(error) + }) + }, + ) + + return promise + } +} + +export default SimplePriceRequest