Skip to content

Commit

Permalink
Get publish dates of all vaults using one API call(vaults query)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexharvest committed Apr 24, 2024
1 parent f798c68 commit 6058dfe
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 71 deletions.
55 changes: 30 additions & 25 deletions src/components/VaultComponents/VaultList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,35 +476,40 @@ const VaultList = () => {
useEffect(() => {
const getCreatedAtData = async () => {
if (groupOfVaults) {
const vaultsKey = Object.keys(groupOfVaults)
vaultsKey.map(async symbol => {
const token = groupOfVaults[symbol]
const tokenChainId = token.chain || token.data.chain
const isSpecialVault = token.liquidityPoolVault || token.poolVault
const paramAddress = isSpecialVault
? token.data.collateralAddress
: token.vaultAddress || token.tokenAddress
const vaultIds = vaultsKey.filter(
vaultId =>
groupOfVaults[vaultId].vaultAddress === paramAddress ||
groupOfVaults[vaultId].tokenAddress === paramAddress,
)
const id = vaultIds[0]
const tokenVault = get(vaultsData, token.hodlVaultId || id)

const vaultPool = isSpecialVault
? token.data
: find(totalPools, pool => pool.collateralAddress === get(tokenVault, `vaultAddress`))
const address =
token.vaultAddress || vaultPool.autoStakePoolAddress || vaultPool.contractAddress
const { data, flag } = await getPublishDate(address, tokenChainId)
groupOfVaults[symbol].publishDate = flag ? Number(data[data.length - 1].timestamp) : null
})
const { data, flag } = await getPublishDate()
if (flag) {
const vaultsKey = Object.keys(groupOfVaults)
vaultsKey.map(async symbol => {
const token = groupOfVaults[symbol]
const isSpecialVault = token.liquidityPoolVault || token.poolVault
const paramAddress = isSpecialVault
? token.data.collateralAddress
: token.vaultAddress || token.tokenAddress
const vaultIds = vaultsKey.filter(
vaultId =>
groupOfVaults[vaultId].vaultAddress === paramAddress ||
groupOfVaults[vaultId].tokenAddress === paramAddress,
)
const id = vaultIds[0]
const tokenVault = get(vaultsData, token.hodlVaultId || id)
const vaultPool = isSpecialVault
? token.data
: find(totalPools, pool => pool.collateralAddress === get(tokenVault, `vaultAddress`))
const address =
token.vaultAddress || vaultPool.autoStakePoolAddress || vaultPool.contractAddress
for (let i = 0; i < data.length; i += 1) {
if (address.toLowerCase() === data[i].id) {
groupOfVaults[symbol].publishDate = data[i].timestamp
return
}
}
})
}
}
}

getCreatedAtData()
}, [pools, vaultsData]) // eslint-disable-line react-hooks/exhaustive-deps
}, []) // eslint-disable-line react-hooks/exhaustive-deps

const vaultsSymbol = useMemo(
() =>
Expand Down
122 changes: 76 additions & 46 deletions src/utilities/apiCalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,65 +94,95 @@ export const getLastHarvestInfo = async (address, chainId) => {
return result
}

export const getPublishDate = async (address, chainId) => {
let data = {},
flag = true
export const getPublishDate = async () => {
const allData = [],
allFlags = []

const myHeaders = new Headers()
myHeaders.append('Content-Type', 'application/json')

address = address.toLowerCase()
const farm = '0xa0246c9032bc3a600820415ae600c6388619a14d'
const ifarm = '0x1571ed0bed4d987fe2b498ddbae7dfa19519f651'

const graphql = JSON.stringify({
const graphqlQueries = [
{
url: GRAPH_URL_MAINNET,
query: `{
vaultHistories(
where: {
vault: "${address === farm ? ifarm : address}",
},
orderBy: timestamp,
vaults(
first: 1000,
orderBy: createAtBlock,
orderDirection: desc
) {
sharePrice, timestamp
id, timestamp
}
}`,
variables: {},
}),
requestOptions = {
method: 'POST',
headers: myHeaders,
body: graphql,
redirect: 'follow',
}

const url =
chainId === CHAIN_IDS.ETH_MAINNET
? GRAPH_URL_MAINNET
: chainId === CHAIN_IDS.POLYGON_MAINNET
? GRAPH_URL_POLYGON
: chainId === CHAIN_IDS.BASE
? GRAPH_URL_BASE
: GRAPH_URL_ARBITRUM
},
{
url: GRAPH_URL_POLYGON,
query: `{
vaults(
first: 1000,
orderBy: createAtBlock,
orderDirection: desc
) {
id, timestamp
}
}`,
},
{
url: GRAPH_URL_BASE,
query: `{
vaults(
first: 1000,
orderBy: createAtBlock,
orderDirection: desc
) {
id, timestamp
}
}`,
},
{
url: GRAPH_URL_ARBITRUM,
query: `{
vaults(
first: 1000,
orderBy: createAtBlock,
orderDirection: desc
) {
id, timestamp
}
}`,
},
]

try {
await fetch(url, requestOptions)
.then(response => response.json())
.then(res => {
data = res.data.vaultHistories
if (data.length === 0) {
flag = false
}
})
.catch(error => {
console.log('error', error)
flag = false
})
const requests = graphqlQueries.map(({ url, query }) => {
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: JSON.stringify({ query }),
redirect: 'follow',
}
return fetch(url, requestOptions)
.then(response => response.json())
.then(res => {
const data = res.data.vaults || []
const flag = data.length > 0
allData.push(data)
allFlags.push(flag)
})
.catch(error => {
console.log('Error fetching data from URL:', url, error)
allFlags.push(false)
})
})

await Promise.all(requests)
} catch (err) {
console.log('Fetch data about price feed: ', err)
flag = false
console.log('Error fetching data:', err)
return { data: [], flags: [] }
}
return { data, flag }

const combinedData = allData.reduce((acc, curr) => acc.concat(curr), [])
const combinedFlags = allFlags.every(flag => flag)
return { data: combinedData, flag: combinedFlags }
}

export const getVaultHistories = async (address, chainId) => {
Expand Down

0 comments on commit 6058dfe

Please sign in to comment.