Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-86a0cw8d9 - Add support to CryptoCompare exchange #12

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
endkeyCoder marked this conversation as resolved.
Show resolved Hide resolved
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
}))
}

}