Skip to content

Commit

Permalink
Merge pull request #66 from jediswaplabs/retroboy/fixes
Browse files Browse the repository at this point in the history
fixed starknet id fetching
  • Loading branch information
retroboydev authored Dec 13, 2023
2 parents 6757f63 + ea135cc commit 4c57d4f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 59 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/apollo/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions src/contexts/LpContestData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -142,7 +143,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',
Expand All @@ -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 {
Expand Down
94 changes: 40 additions & 54 deletions src/contexts/TokenData.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -195,24 +199,15 @@ export default function Provider({ children }) {
updateCombinedVolume,
},
],
[
state,
update,
updateTokenTxns,
updateCombinedVolume,
updateChartData,
updateTopTokens,
updateAllPairs,
updatePriceData,
]
[state, update, updateTokenTxns, updateCombinedVolume, updateChartData, updateTopTokens, updateAllPairs, updatePriceData]
)}
>
{children}
</TokenDataContext.Provider>
)
}

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()
Expand All @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

[email protected]:
version "7.0.1"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14"
Expand All @@ -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"
Expand Down

0 comments on commit 4c57d4f

Please sign in to comment.