Skip to content

Commit

Permalink
Merge pull request #250 from EveripediaNetwork/update-cmc-api
Browse files Browse the repository at this point in the history
Update cmc api
  • Loading branch information
Aliiiu authored Apr 11, 2024
2 parents 7fba84d + 256c523 commit 6389231
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/components/bridge/originInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const OriginInfo = ({
setTokenInputAmount,
getSpecificBalance,
}: OriginInfoType) => {
const { data } = useGetIqPriceQuery()
const exchangeRate = data?.response || 0.0
const { data } = useGetIqPriceQuery('IQ')
const exchangeRate = +data?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0

return (
<Flex p="3" pr="5" rounded="lg" border="solid 1px" borderColor="divider">
Expand Down
6 changes: 3 additions & 3 deletions src/components/client/BridgePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const BridgePage = () => {
const { accountName = '' } = activeUser ?? {}
const [selectedToken, setSelectedToken] = useState(TOKENS[0])
const [selectedTokenIcon, setSelectedTokenIcon] = useState(<IQEosLogo />)
const [tokenInputAmount, setTokenInputAmount] = useState<string>()
const [tokenInputAmount, setTokenInputAmount] = useState<string>('0')
const [inputAddress, setInputAddress] = useState<string>()
const [inputAccount, setInputAccount] = useState<string>(
activeUser ? accountName : '',
Expand All @@ -55,8 +55,8 @@ const BridgePage = () => {
const { switchNetwork, isSuccess } = useSwitchNetwork()
const { chain } = useNetwork()
const chainId = parseInt(config.chainId)
const { data } = useGetIqPriceQuery()
const exchangeRate = data?.response || 0.0
const { data } = useGetIqPriceQuery('IQ')
const exchangeRate = data?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0
const inputRef = useRef<HTMLInputElement>(null)
const {
iqBalanceOnEth,
Expand Down
4 changes: 2 additions & 2 deletions src/components/client/StakePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const StakePage = () => {
const { chain } = useNetwork()
const chainId = parseInt(config.chainId)
const { switchNetwork, isSuccess } = useSwitchNetwork()
const { data: iqData } = useGetIqPriceQuery()
const exchangeRate = iqData?.response || 0.0
const { data: iqData } = useGetIqPriceQuery('IQ')
const exchangeRate = iqData?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0
const resetValues = () => {
setIsProcessingUnlock(false)
setTrxHash(undefined)
Expand Down
5 changes: 3 additions & 2 deletions src/components/dashboard/IQButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { useGetIqPriceQuery } from '@/services/iqPrice'

export const IQButton = (props: ButtonProps) => {
const SIG_FIGS = 4
const { data, isLoading, error } = useGetIqPriceQuery()
const iqPrice = data?.response
const { data, isLoading, error } = useGetIqPriceQuery('IQ')

const iqPrice = data?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0
return (
<>
<Button
Expand Down
4 changes: 2 additions & 2 deletions src/components/dashboard/TreasuryGraphTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const TreasuryGraphTable = ({
const { userTotalIQLocked, totalHiiqSupply } = useLockOverview(
config.treasuryHiIQAddress,
)
const { data } = useGetIqPriceQuery()
const rate = data?.response || 0.0
const { data } = useGetIqPriceQuery('IQ')
const rate = data?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0
const [tokenData, setTokenData] = useState<TreasuryTokenType[]>([])
const [tokenDataToShow, setTokenDataToShow] = useState<TreasuryTokenType[]>(
[],
Expand Down
4 changes: 2 additions & 2 deletions src/components/lock/LockOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const LockOverview = () => {
const { tvl } = useErc20()
const [holders, setHolders] = useState(0)
const [averageLockTime, setAverageLockTime] = useState(0)
const { data } = useGetIqPriceQuery()
const rate = data?.response || 0.0
const { data } = useGetIqPriceQuery('IQ')
const rate = data?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0

useEffect(() => {
const getHiIQHolders = async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/lock/LockedDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const LockedDetails = ({
const [trxHash, setTrxHash] = useState()
const { data } = useWaitForTransaction({ hash: trxHash })
const { isConnected, address } = useAccount()
const { data: iqData } = useGetIqPriceQuery()
const price = iqData?.response || 0.0
const { data: iqData } = useGetIqPriceQuery('IQ')
const price = iqData?.response?.data?.IQ[0]?.quote?.USD?.price || 0.0
const { showToast } = useReusableToast()

useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/wallet/use-hiiiq-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const useHiIQBalance = (address: string | undefined | null) => {
functionName: 'locked',
args: [address as `0x${string}`],
})
const { data } = useGetIqPriceQuery()
const { data } = useGetIqPriceQuery('IQ')

useEffect(() => {
const getBalance = async () => {
Expand All @@ -66,7 +66,9 @@ export const useHiIQBalance = (address: string | undefined | null) => {
iqLocked: Number(formatUnits(locked[0], 18)),
end: new Date(Number(locked[1]) * 1000),
}
const coinGeckoIqPrice = data?.response ? +data?.response : 0
const coinGeckoIqPrice = data?.response
? +data?.response?.data?.IQ[0]?.quote?.USD?.price
: 0
updateHiIQDetails({
hiiqBalance,
iqBalance: lockInfo.iqLocked,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/cmc-token-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import config from '@/config'
import type { NextApiRequest, NextApiResponse } from 'next'
import axios from 'axios'

type ResponseData = {
export type ResponseData = {
status: boolean
message: string
response?: any
Expand All @@ -15,7 +15,7 @@ export default async function handler(
try {
const { tokenName } = req.query
const response = await axios.get(
`https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${tokenName}`,
`https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=${tokenName}`,
{
headers: {
'X-CMC_PRO_API_KEY': config.cmcApiKey as string,
Expand Down
10 changes: 6 additions & 4 deletions src/services/iqPrice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ export const iqPriceApi = createApi({
return null
},
baseQuery: fetchBaseQuery({
baseUrl: '/api/iq-price',
baseUrl: '/api/cmc-token-details',
}),
refetchOnMountOrArgChange: 60,
refetchOnFocus: true,
endpoints: (builder) => ({
getIqPrice: builder.query<
{
response?: number
response?: any
status: boolean
message: string
},
void
string
>({
query: () => '',
query: (tokenName: string) => `?tokenName=${tokenName}`,
}),
}),
})
export const { useGetIqPriceQuery } = iqPriceApi

export const { getIqPrice } = iqPriceApi.endpoints
16 changes: 10 additions & 6 deletions src/utils/dashboard-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios'
import { getIqPrice } from '@/services/iqPrice'
import { store } from '@/store/store'

export const numFormatter = Intl.NumberFormat('en', {
notation: 'compact',
Expand All @@ -22,12 +23,15 @@ export const fetchPrices = async () => {

export const fetchTokenData = async (symbol: string) => {
try {
const res = await axios.get('/api/cmc-token-details', {
params: { tokenName: symbol },
})
const res = await store.dispatch(getIqPrice.initiate('IQ'))

const response = res.data
if (!response) {
throw new Error('Error fetching data')
}

const { data } = response.response
const tokenDetails = data[symbol]
const tokenDetails = data[symbol][0]

if (!tokenDetails) {
throw new Error(`No data found for symbol ${symbol}`)
Expand All @@ -53,7 +57,7 @@ export const fetchTokenData = async (symbol: string) => {
percent_change_1y,
}
} catch (error) {
console.error('Error fetching data:', error)
console.error(error)
return null
}
}
Expand Down

0 comments on commit 6389231

Please sign in to comment.