Skip to content

Commit

Permalink
Merge pull request #12 from blockcoders/change-dot-price
Browse files Browse the repository at this point in the history
change binance-api to fetch from gecko
  • Loading branch information
admin-blockcoders authored Dec 12, 2022
2 parents 94111f4 + adfafbb commit ebde9d3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 82 deletions.
27 changes: 16 additions & 11 deletions __test__/components/infoCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ jest.mock('../../generated', () => ({
}),
}))

jest.mock('binance-api-node', () => {
return {
__esModule: true,
default: () => ({
avgPrice: jest.fn().mockResolvedValue({
price,
}),
}),
}
})

jest.mock('next/router', () => ({
useRouter: jest.fn(() => ({
locale: 'en',
Expand All @@ -58,6 +47,10 @@ jest.mock('../../hooks/useFormatIntl', () => ({

describe('InfoCard', () => {
it('show render', async () => {
global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue({ polkadot: { usd: price } }),
})

const token = 'DOT'

const { container } = render(<InfoCard />)
Expand All @@ -74,4 +67,16 @@ describe('InfoCard', () => {
)
await waitFor(() => expect(sdkVersionElement.innerHTML).toContain(`SDK: ${version}`))
})

it('show price error', async () => {
global.fetch = jest.fn().mockRejectedValue(new Error('invalid url'))

const token = 'DOT'

const { container } = render(<InfoCard />)

const tokenElement = container?.getElementsByClassName('ink_infocard-dot')[0]

await waitFor(() => expect(tokenElement?.innerHTML).toContain(token + `: $0`))
})
})
22 changes: 11 additions & 11 deletions components/InfoCard/InfoCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Binance, { AvgPriceResult } from 'binance-api-node'
import { get } from 'lodash'
import Image from 'next/future/image'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -26,16 +25,17 @@ function InfoCard() {
const token = 'DOT'

useEffect(() => {
const client = Binance()
client
.avgPrice({ symbol: `${token}USDT` })
.then((res) => {
const result = Number((res as AvgPriceResult).price)
setPrice(result)
})
.catch((err) => {
console.log(err)
})
;(async () => {
try {
const data = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=polkadot&vs_currencies=usd')
const price = await data.json()

setPrice(price?.polkadot?.usd || 0)
} catch (error) {
console.error(error)
setPrice(0)
}
})()
}, [])

useEffect(() => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"@polkadot/util": "10.1.12",
"@popperjs/core": "^2.11.6",
"@types/lodash": "^4.14.189",
"binance-api-node": "^0.12.0",
"bn.js": "^5.2.1",
"bootstrap": "^5.2.2",
"graphql": "^16.6.0",
Expand Down
63 changes: 4 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebde9d3

Please sign in to comment.