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

Change getting eth price source to coingecko #397

Merged
merged 4 commits into from
Jan 22, 2025
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
13 changes: 1 addition & 12 deletions src/containers/HeaderWrapper/HeaderWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ThankYouModal } from '@components/Modals/ThankYouModal/ThankYouModal'
import { changeToNightlyAdapter, connectStaticWallet, getEclipseWallet } from '@utils/web3/wallet'
import { sleep } from '@invariant-labs/sdk-eclipse'
import { getLeaderboardQueryParams } from '@store/selectors/leaderboard'
import { generateHash } from '@utils/utils'

export const HeaderWrapper: React.FC = () => {
const dispatch = useDispatch()
Expand Down Expand Up @@ -91,18 +92,6 @@ export const HeaderWrapper: React.FC = () => {
}, [])

const shouldResetRpc = useMemo(() => {
const generateHash = (str: string): string => {
let hash = 0

for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash
}

return Math.abs(hash).toString(16).padStart(8, '0')
}

const STORAGE_KEY = 'INVARIANT_RPC_HASH'

const currentAddresses =
Expand Down
15 changes: 9 additions & 6 deletions src/store/consts/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const WETH_MAIN: Token = {
name: 'Ethereum',
logoURI:
'https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/2FPyTwcZLUg1MDrwsyoP4D6s1tM7hAkHYRjkNb5w6Pxk/logo.png',
coingeckoId: 'ethereum'
coingeckoId: 'bridged-wrapped-ether-eclipse'
}

export const LAIKA_MAIN: Token = {
Expand Down Expand Up @@ -779,7 +779,14 @@ export const DEFAULT_TOKEN_DECIMAL = 6

export const COINGECKO_QUERY_COOLDOWN = 20 * 60 * 1000

export const DEFAULT_TOKENS = ['solana', 'dogwifcoin', 'turbo-eth', 'laika-3', 'mooncoin-2']
export const DEFAULT_TOKENS = [
'solana',
'dogwifcoin',
'turbo-eth',
'laika-3',
'mooncoin-2',
'bridged-wrapped-ether-eclipse'
]

export const TIMEOUT_ERROR_MESSAGE =
'Transaction has timed out. Check the details to confirm success.'
Expand Down Expand Up @@ -826,10 +833,6 @@ export const getPopularPools = (network: NetworkType) => {
}

export const TOKENS_PRICES_FROM_JUP: { coingeckoId: string; solanaAddress: string }[] = [
{
coingeckoId: 'ethereum',
solanaAddress: '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs'
},
{
coingeckoId: 'usd-coin',
solanaAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'
Expand Down
30 changes: 27 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,9 @@ export const getMockedTokenPrice = (symbol: string, network: NetworkType): Token
let isCoinGeckoQueryRunning = false

export const getCoinGeckoTokenPrice = async (id: string): Promise<number | undefined> => {
const defaultTokensHash = generateHash(JSON.stringify(DEFAULT_TOKENS))
const cachedHash = localStorage.getItem('COINGECKO_DEFAULT_TOKEN_LIST_CHANGED')

while (isCoinGeckoQueryRunning) {
await sleep(100)
}
Expand All @@ -1664,23 +1667,32 @@ export const getCoinGeckoTokenPrice = async (id: string): Promise<number | undef
lastQueryTimestamp = Number(cachedLastQueryTimestamp)
}

const isHashOutdated = cachedHash !== defaultTokensHash

const cachedPriceData = localStorage.getItem('COINGECKO_PRICE_DATA')
let priceData: CoinGeckoAPIData = []
if (cachedPriceData && Number(lastQueryTimestamp) + COINGECKO_QUERY_COOLDOWN > Date.now()) {
priceData = JSON.parse(cachedPriceData)
} else {

if (
isHashOutdated ||
!cachedPriceData ||
Number(lastQueryTimestamp) + COINGECKO_QUERY_COOLDOWN <= Date.now()
) {
try {
const { data } = await axios.get<CoinGeckoAPIData>(
`https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=${DEFAULT_TOKENS}`
)
priceData = data

localStorage.setItem('COINGECKO_PRICE_DATA', JSON.stringify(priceData))
localStorage.setItem('COINGECKO_LAST_QUERY_TIMESTAMP', String(Date.now()))
localStorage.setItem('COINGECKO_DEFAULT_TOKEN_LIST_CHANGED', defaultTokensHash)
} catch (e) {
localStorage.removeItem('COINGECKO_LAST_QUERY_TIMESTAMP')
localStorage.removeItem('COINGECKO_PRICE_DATA')
console.log(e)
}
} else {
priceData = JSON.parse(cachedPriceData)
}

isCoinGeckoQueryRunning = false
Expand Down Expand Up @@ -1861,3 +1873,15 @@ export const checkDataDelay = (date: string | Date, timeInMinutes: number): bool

return differenceInMinutes > timeInMinutes
}

export const generateHash = (str: string): string => {
let hash = 0

for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash
}

return Math.abs(hash).toString(16).padStart(8, '0')
}
Loading