-
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.
CU-86a0cw8d9 - Add support to CryptoCompare exchange
- Loading branch information
1 parent
dc28518
commit 9bcc6a7
Showing
3 changed files
with
63 additions
and
2 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
39 changes: 39 additions & 0 deletions
39
packages/bs-neo-legacy/src/exchange/CryptoCompareExchange.ts
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,39 @@ | ||
import { Currency, Exchange, Network, TokenPricesResponse } from '@cityofzion/blockchain-service' | ||
import axios, { AxiosInstance } from 'axios' | ||
import {TOKENS} from '../constants' | ||
|
||
type CryptoCompareDataResponse = { | ||
RAW: { | ||
[symbol: string]: { | ||
[currency: string]: { | ||
PRICE: number | ||
} | ||
} | ||
} | ||
} | ||
|
||
export class CryptoCompareExchange implements Exchange { | ||
network: Network; | ||
private axiosInstance: AxiosInstance | ||
|
||
constructor(network: Network) { | ||
this.network = network | ||
this.axiosInstance = axios.create({ baseURL: "https://min-api.cryptocompare.com" }) | ||
} | ||
|
||
async getTokenPrices(currency: Currency): Promise<TokenPricesResponse[]> { | ||
if (this.network.type !== 'mainnet') throw new Error('Exchange is only available on mainnet') | ||
const tokenSymbols = TOKENS[this.network.type].map(token => token.symbol) | ||
const { data: prices } = await this.axiosInstance.get<CryptoCompareDataResponse>("/data/pricemultifull", { | ||
params: { | ||
fsyms: tokenSymbols.join(','), | ||
tsyms: currency | ||
} | ||
}) | ||
return Object.entries(prices.RAW).map(([symbol, price]) => ({ | ||
symbol, | ||
amount: price[currency].PRICE | ||
})) | ||
} | ||
|
||
} |