Skip to content

Commit

Permalink
CU-86a0cw8d9 - Add support to CryptoCompare exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
endkeyCoder committed Aug 14, 2023
1 parent dc28518 commit 9bcc6a7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/bs-neo-legacy/src/BSNeoLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
BDSClaimable,
BlockchainDataService,
BlockchainService,
CalculateTransferFeeResponse,
Claimable,
Exchange,
Token,
Expand All @@ -15,12 +14,12 @@ import {
import { api, sc, u, wallet } from '@cityofzion/neon-js'
import { DEFAULT_URL_BY_NETWORK_TYPE, LEGACY_NETWORK_BY_NETWORK_TYPE, NATIVE_ASSETS, TOKENS } from './constants'
import { DoraBDSNeoLegacy } from './DoraBDSNeoLegacy'
import { CryptoCompareExchange } from './exchange/CryptoCompareExchange'

export class BSNeoLegacy<BSCustomName extends string = string> implements BlockchainService, Claimable {
dataService!: BlockchainDataService & BDSClaimable
blockchainName: BSCustomName
feeToken: Token
// Implement an Exchange interface for the legacy blockchain
exchange!: Exchange
tokenClaim: Token
tokens: Token[]
Expand Down Expand Up @@ -49,6 +48,7 @@ export class BSNeoLegacy<BSCustomName extends string = string> implements Blockc
}
this.network = network
this.dataService = new DoraBDSNeoLegacy(network)
this.exchange = new CryptoCompareExchange(network)
}

validateAddress(address: string): boolean {
Expand Down
22 changes: 22 additions & 0 deletions packages/bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Currency } from '@cityofzion/blockchain-service'
import { BSNeoLegacy } from '../BSNeoLegacy'
import { wallet } from '@cityofzion/neon-js'

Expand Down Expand Up @@ -62,6 +63,27 @@ describe('BSNeoLegacy', () => {
const encryptedKey = await wallet.encrypt(account.wif, password)
const decryptedAccount = await bsNeoLegacy.decryptKey(encryptedKey, password)
expect(decryptedAccount).toEqual(account)
}, 10000)

it('Should return a list with prices of tokens using USD', async () => {
bsNeoLegacy.network.type = 'mainnet'
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 () => {
bsNeoLegacy.network.type = 'mainnet'
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 () => {
bsNeoLegacy.network.type = 'mainnet'
const currency: Currency = 'EUR'
const tokenPriceList = await bsNeoLegacy.exchange.getTokenPrices(currency)
expect(tokenPriceList.length).toBeGreaterThan(0)
})

it.skip('Should be able to transfer a native asset', async () => {
Expand Down
39 changes: 39 additions & 0 deletions packages/bs-neo-legacy/src/exchange/CryptoCompareExchange.ts
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
}))
}

}

0 comments on commit 9bcc6a7

Please sign in to comment.