Skip to content

Commit

Permalink
Merge pull request #12 from CityOfZion/CU-86a0cw8d9
Browse files Browse the repository at this point in the history
CU-86a0cw8d9 - Add support to CryptoCompare exchange
  • Loading branch information
raulduartep authored Aug 17, 2023
2 parents dc28518 + 09bfb74 commit 5b05047
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 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
3 changes: 2 additions & 1 deletion 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,7 +63,7 @@ describe('BSNeoLegacy', () => {
const encryptedKey = await wallet.encrypt(account.wif, password)
const decryptedAccount = await bsNeoLegacy.decryptKey(encryptedKey, password)
expect(decryptedAccount).toEqual(account)
})
}, 10000)

it.skip('Should be able to transfer a native asset', async () => {
const account = bsNeoLegacy.generateAccountFromWif(process.env.TESTNET_PRIVATE_KEY as string)
Expand Down
27 changes: 27 additions & 0 deletions packages/bs-neo-legacy/src/__tests__/CryptoCompareExchange.spec.ts
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 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 5b05047

Please sign in to comment.