From 07bca3c8d328d73ec252511fdc592c821a49ef5e Mon Sep 17 00:00:00 2001 From: retroboy Date: Wed, 13 Dec 2023 19:59:33 +0100 Subject: [PATCH 1/2] tokens fetching fixes --- package.json | 3 +- src/apollo/queries.js | 2 +- src/contexts/LpContestData.js | 2 +- src/contexts/TokenData.js | 94 +++++++++++++++-------------------- yarn.lock | 16 ++++++ 5 files changed, 60 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index b2cc64d..af37d30 100755 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "bignumber.js": "^9.0.0", "color-contrast-checker": "^1.5.0", "copy-to-clipboard": "^3.3.1", + "cross-env": "^7.0.3", "customize-cra": "^1.0.0", "dayjs": "^1.8.16", "decimal.js-light": "^2.5.0", @@ -75,7 +76,7 @@ }, "scripts": { "start": "react-app-rewired start", - "build": "NODE_ENV=production GENERATE_SOURCEMAP=false react-app-rewired build", + "build": "cross-env NODE_ENV=production GENERATE_SOURCEMAP=false react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject" }, diff --git a/src/apollo/queries.js b/src/apollo/queries.js index dc49d16..fe289ff 100755 --- a/src/apollo/queries.js +++ b/src/apollo/queries.js @@ -752,7 +752,7 @@ const TokenFields = ` // used for getting top tokens by daily volume export const TOKEN_TOP_DAY_DATAS = gql` query tokenDayDatas($date: Int) { - tokenDayDatas(first: 20, orderByDirection: "desc", orderBy: "day_id", where: { dateGt: $date }) { + tokenDayDatas(first: 100, orderByDirection: "desc", orderBy: "day_id", where: { dateGt: $date }) { tokenId date } diff --git a/src/contexts/LpContestData.js b/src/contexts/LpContestData.js index 3d9edf9..b96a9a6 100644 --- a/src/contexts/LpContestData.js +++ b/src/contexts/LpContestData.js @@ -142,7 +142,7 @@ export function Updater() { } const convertedAddressed = convertIdsToDecimal(userIds) try { - const response = await fetch('https://app.starknet.id/api/indexer/addrs_to_domains', { + const response = await fetch('https://api.starknet.id/addrs_to_domains', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/contexts/TokenData.js b/src/contexts/TokenData.js index d7cb320..06f5268 100755 --- a/src/contexts/TokenData.js +++ b/src/contexts/TokenData.js @@ -1,20 +1,22 @@ import React, { createContext, useContext, useReducer, useMemo, useCallback, useEffect } from 'react' +import { useWhitelistedTokens } from './Application' -import {jediSwapClient} from '../apollo/client' -import { - TOKEN_DATA, - FILTERED_TRANSACTIONS, - TOKEN_CHART, - TOKEN_TOP_DAY_DATAS, - TOKENS_HISTORICAL_BULK, -} from '../apollo/queries' +import { jediSwapClient } from '../apollo/client' +import { TOKEN_DATA, FILTERED_TRANSACTIONS, TOKEN_CHART, TOKEN_TOP_DAY_DATAS, TOKENS_HISTORICAL_BULK } from '../apollo/queries' import { useEthPrice } from './GlobalData' import dayjs from 'dayjs' import utc from 'dayjs/plugin/utc' -import { get2DayPercentChange, getPercentChange, getBlockFromTimestamp, getBlocksFromTimestamps,isStarknetAddress, convertDateToUnixFormat } from '../utils' +import { + get2DayPercentChange, + getPercentChange, + getBlockFromTimestamp, + getBlocksFromTimestamps, + isStarknetAddress, + convertDateToUnixFormat, +} from '../utils' import { timeframeOptions } from '../constants' import { useLatestBlocks } from './Application' import { updateNameData } from '../utils/data' @@ -124,7 +126,9 @@ function reducer(state, { type, payload }) { export default function Provider({ children }) { const [state, dispatch] = useReducer(reducer, {}) const update = useCallback((tokenAddress, data) => { - if (!tokenAddress) { return } + if (!tokenAddress) { + return + } dispatch({ type: UPDATE, payload: { @@ -195,16 +199,7 @@ export default function Provider({ children }) { updateCombinedVolume, }, ], - [ - state, - update, - updateTokenTxns, - updateCombinedVolume, - updateChartData, - updateTopTokens, - updateAllPairs, - updatePriceData, - ] + [state, update, updateTokenTxns, updateCombinedVolume, updateChartData, updateTopTokens, updateAllPairs, updatePriceData] )} > {children} @@ -212,7 +207,7 @@ export default function Provider({ children }) { ) } -const getTopTokens = async (ethPrice, ethPriceOld) => { +const getTopTokens = async (ethPrice, ethPriceOld, whitelistedTokens = []) => { const utcCurrentTime = dayjs() const utcOneDayBack = utcCurrentTime.subtract(1, 'day').unix() const utcTwoDaysBack = utcCurrentTime.subtract(2, 'day').unix() @@ -226,13 +221,16 @@ const getTopTokens = async (ethPrice, ethPriceOld) => { let tokenids = await jediSwapClient.query({ query: TOKEN_TOP_DAY_DATAS, fetchPolicy: 'network-only', - variables: { date: (currentDate - 1000000) }, + variables: { date: currentDate - 1000000 }, }) - const ids = tokenids?.data?.tokenDayDatas?.reduce((accum, entry) => { - accum.push(entry.tokenId) - return accum - }, []).filter((v, i, self) => self.indexOf(v) === i); + const ids = tokenids?.data?.tokenDayDatas + ?.reduce((accum, entry) => { + accum.push(entry.tokenId) + return accum + }, []) + .filter((v, i, self) => self.indexOf(v) === i) + .filter((key) => whitelistedTokens[key]) let current = await jediSwapClient.query({ query: TOKENS_HISTORICAL_BULK(ids), @@ -289,11 +287,7 @@ const getTopTokens = async (ethPrice, ethPriceOld) => { oneDayHistory?.tradeVolumeUSD ?? 0, twoDayHistory?.tradeVolumeUSD ?? 0 ) - const [oneDayTxns, txnChange] = get2DayPercentChange( - data.txCount, - oneDayHistory?.txCount ?? 0, - twoDayHistory?.txCount ?? 0 - ) + const [oneDayTxns, txnChange] = get2DayPercentChange(data.txCount, oneDayHistory?.txCount ?? 0, twoDayHistory?.txCount ?? 0) const currentLiquidityUSD = data?.totalLiquidity * ethPrice * data?.derivedETH const oldLiquidityUSD = oneDayHistory?.totalLiquidity * ethPriceOld * oneDayHistory?.derivedETH @@ -303,7 +297,6 @@ const getTopTokens = async (ethPrice, ethPriceOld) => { data?.derivedETH * ethPrice, oneDayHistory?.derivedETH ? oneDayHistory?.derivedETH * ethPriceOld : 0 ) - // set data data.priceUSD = data?.derivedETH * ethPrice data.totalLiquidityUSD = currentLiquidityUSD @@ -406,16 +399,9 @@ const getTokenData = async (address, ethPrice, ethPriceOld) => { ) // calculate percentage changes and daily changes - const [oneDayTxns, txnChange] = get2DayPercentChange( - data.txCount, - oneDayData?.txCount ?? 0, - twoDayData?.txCount ?? 0 - ) + const [oneDayTxns, txnChange] = get2DayPercentChange(data.txCount, oneDayData?.txCount ?? 0, twoDayData?.txCount ?? 0) - const priceChangeUSD = getPercentChange( - data?.derivedETH * ethPrice, - parseFloat(oneDayData?.derivedETH ?? 0) * ethPriceOld - ) + const priceChangeUSD = getPercentChange(data?.derivedETH * ethPrice, parseFloat(oneDayData?.derivedETH ?? 0) * ethPriceOld) const currentLiquidityUSD = data?.totalLiquidity * ethPrice * data?.derivedETH const oldLiquidityUSD = oneDayData?.totalLiquidity * ethPriceOld * oneDayData?.derivedETH @@ -521,7 +507,7 @@ const getIntervalTokenData = async (tokenAddress, startTime, interval = 3600, la // let result = await splitQuery(PRICES_BY_BLOCK, jediSwapClient, [tokenAddress], blocks, 50) - let result = {}; + let result = {} // format token ETH price results let values = [] for (var row in result) { @@ -587,14 +573,14 @@ const getTokenChartData = async (tokenAddress) => { } let tokenDayDatas = result.data.tokenDayDatas.map((item) => { - item.id = item.dayId; - item.date = convertDateToUnixFormat(item.date); - item.totalLiquidityETH = parseFloat(item.totalLiquidityETH); - item.totalLiquidityUSD = parseFloat(item.totalLiquidityUSD); - item.priceUSD = parseFloat(item.priceUSD); - item.dailyVolumeETH = parseFloat(item.dailyVolumeETH); - item.dailyVolumeUSD = parseFloat(item.dailyVolumeUSD); - return item; + item.id = item.dayId + item.date = convertDateToUnixFormat(item.date) + item.totalLiquidityETH = parseFloat(item.totalLiquidityETH) + item.totalLiquidityUSD = parseFloat(item.totalLiquidityUSD) + item.priceUSD = parseFloat(item.priceUSD) + item.dailyVolumeETH = parseFloat(item.dailyVolumeETH) + item.dailyVolumeUSD = parseFloat(item.dailyVolumeUSD) + return item }) skip += 1000 @@ -647,11 +633,12 @@ const getTokenChartData = async (tokenAddress) => { export function Updater() { const [, { updateTopTokens }] = useTokenDataContext() const [ethPrice, ethPriceOld] = useEthPrice() + const whitelistedTokens = useWhitelistedTokens() useEffect(() => { async function getData() { // get top pairs for overview list - let topTokens = await getTopTokens(ethPrice, ethPriceOld) - topTokens && updateTopTokens(topTokens); + let topTokens = await getTopTokens(ethPrice, ethPriceOld, whitelistedTokens) + topTokens && updateTopTokens(topTokens) } ethPrice && ethPriceOld && getData() }, [ethPrice, ethPriceOld, updateTopTokens]) @@ -837,8 +824,7 @@ export function useTokenPriceData(tokenAddress, timeWindow, interval = 3600) { useEffect(() => { const currentTime = dayjs.utc() const windowSize = timeWindow === timeframeOptions.MONTH ? 'month' : 'week' - const startTime = - timeWindow === timeframeOptions.ALL_TIME ? 1589760000 : currentTime.subtract(1, windowSize).startOf('hour').unix() + const startTime = timeWindow === timeframeOptions.ALL_TIME ? 1589760000 : currentTime.subtract(1, windowSize).startOf('hour').unix() async function fetch() { let data = await getIntervalTokenData(tokenAddress, startTime, interval, latestBlock.number) diff --git a/yarn.lock b/yarn.lock index 1b15a99..f07e597 100755 --- a/yarn.lock +++ b/yarn.lock @@ -4189,6 +4189,13 @@ create-react-context@^0.1.5: resolved "https://registry.yarnpkg.com/create-react-context/-/create-react-context-0.1.6.tgz#0f425931d907741127acc6e31acb4f9015dd9fdc" integrity sha512-eCnYYEUEc5i32LHwpE/W7NlddOB9oHwsPaWtWzYtflNkkwa3IfindIcoXdVWs12zCbwaMCavKNu84EXogVIWHw== +cross-env@^7.0.3: + version "7.0.3" + resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf" + integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw== + dependencies: + cross-spawn "^7.0.1" + cross-spawn@7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14" @@ -4209,6 +4216,15 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5: shebang-command "^1.2.0" which "^1.2.9" +cross-spawn@^7.0.1: + version "7.0.3" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6" + integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w== + dependencies: + path-key "^3.1.0" + shebang-command "^2.0.0" + which "^2.0.1" + crypto-browserify@^3.11.0: version "3.12.0" resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec" From ea135cc2b4e8a49522a476a6a8d58abc30141bd4 Mon Sep 17 00:00:00 2001 From: retroboy Date: Wed, 13 Dec 2023 20:18:20 +0100 Subject: [PATCH 2/2] fixed starknet id fetching --- src/contexts/LpContestData.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/contexts/LpContestData.js b/src/contexts/LpContestData.js index b96a9a6..1208154 100644 --- a/src/contexts/LpContestData.js +++ b/src/contexts/LpContestData.js @@ -6,6 +6,7 @@ import { LP_CONTEST_DATA, LP_CONTEST_NFT_RANK } from '../apollo/queries' import dayjs from 'dayjs' import utc from 'dayjs/plugin/utc' import { convertHexToDecimal } from '../utils' +import { number as starknetNumberModule } from 'starknet' const UPDATE = 'UPDATE' const UPDATE_PLAYERS_DATA = 'UPDATE_PLAYERS_DATA' @@ -154,13 +155,13 @@ export function Updater() { const processedResponse = await response.json() const domains = processedResponse.reduce((acc, userData) => { if (userData.domain && userData.address) { - acc[userData.address] = userData.domain + acc[starknetNumberModule.cleanHex(userData.address)] = userData.domain } return acc }, {}) payloadData.forEach((data) => { - data.starknetIdDomain = domains?.[convertedAddressed?.[data?.user?.id]] || '' // Здесь добавляется новое поле + data.starknetIdDomain = domains?.[data?.user?.id] || '' }) } catch (e) { } finally {