-
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 #12 from CityOfZion/CU-86a0cw8d9
CU-86a0cw8d9 - Add support to CryptoCompare exchange
- Loading branch information
Showing
4 changed files
with
70 additions
and
3 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
27 changes: 27 additions & 0 deletions
27
packages/bs-neo-legacy/src/__tests__/CryptoCompareExchange.spec.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,27 @@ | ||
import { Currency } from '@cityofzion/blockchain-service' | ||
import { BSNeoLegacy } from '../BSNeoLegacy' | ||
|
||
let bsNeoLegacy: BSNeoLegacy | ||
|
||
describe('CryptoCompare', () => { | ||
beforeAll(() => { | ||
bsNeoLegacy = new BSNeoLegacy('neoLegacy', { type: 'mainnet', url: '"https://mainnet1.neo2.coz.io:443' }) | ||
}) | ||
it('Should return a list with prices of tokens using USD', async () => { | ||
const currency: Currency = 'USD' | ||
const tokenPriceList = await bsNeoLegacy.exchange.getTokenPrices(currency) | ||
expect(tokenPriceList.length).toBeGreaterThan(0) | ||
}) | ||
|
||
it('Should return a list with prices of tokens using BRL', async () => { | ||
const currency: Currency = 'BRL' | ||
const tokenPriceList = await bsNeoLegacy.exchange.getTokenPrices(currency) | ||
expect(tokenPriceList.length).toBeGreaterThan(0) | ||
}) | ||
|
||
it('Should return a list with prices of tokens using EUR', async () => { | ||
const currency: Currency = 'EUR' | ||
const tokenPriceList = await bsNeoLegacy.exchange.getTokenPrices(currency) | ||
expect(tokenPriceList.length).toBeGreaterThan(0) | ||
}) | ||
}) |
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 | ||
})) | ||
} | ||
|
||
} |