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

[limit] add limit to vault & user stats #241

Merged
merged 6 commits into from
Jan 27, 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
4 changes: 2 additions & 2 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { CodegenConfig } from '@graphql-codegen/cli'
import { Network } from './src/utils/enums'
import configs from './src/utils/configs'

// TODO change
let network: Network = Network.Mainnet

let network: Network = Network.Holesky

if (process.env.NETWORK === 'mainnet') {
network = Network.Mainnet
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/subgraph/stats/fiatRatesQuery.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query FiatRates {
networks {
exchangeRates {
assetsUsdRate
usdToEurRate
usdToGbpRate
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/subgraph/vault/userRewardsQuery.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
query UserRewards(
$where: AllocatorStats_filter
$limit: Int
) {
allocator: allocatorStats_collection(
interval: day
first: $limit
where: $where
) {
apy
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/subgraph/vault/vaultStatsQuery.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
query VaultStats($vaultAddress: String! $timestamp: Timestamp!) {
query VaultStats($vaultAddress: String! $timestamp: Timestamp!, $limit: Int) {
vaultStats: vaultStats_collection(
interval: day
first: $limit
where: {
vault: $vaultAddress
timestamp_gte: $timestamp,
Expand Down
4 changes: 2 additions & 2 deletions src/methods/utils/getFiatRates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type GetFiatRatesInput = {

const getGnoRate = () => graphql.subgraph.stats.fetchFiatRatesQuery({
url: configs[Network.Gnosis].api.subgraph,
modifyResult: (data) => Number(data.networks[0].assetsUsdRate),
modifyResult: (data) => Number(data.exchangeRates[0].assetsUsdRate),
})

const getFiatRates = (values: GetFiatRatesInput) => {
Expand All @@ -26,7 +26,7 @@ const getFiatRates = (values: GetFiatRatesInput) => {
usdToKrwRate,
usdToAudRate,
swiseUsdRate,
} = data.networks[0]
} = data.exchangeRates[0]

let assetUsd = Number(assetsUsdRate)

Expand Down
9 changes: 9 additions & 0 deletions src/methods/vault/requests/getUserRewards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type GetUserRewardsInput = {
vaultAddress: string
}

const calculateLimit = (dateTo: number, dateFrom: number): number => {
const millisecondsInDay = 1000 * 60 * 60 * 24

return Math.floor((dateTo - dateFrom) / millisecondsInDay)
}

const getUserRewards = async (input: GetUserRewardsInput): Promise<MergedReward[]> => {
const { options, vaultAddress, userAddress, dateFrom, dateTo } = input

Expand All @@ -29,13 +35,16 @@ const getUserRewards = async (input: GetUserRewardsInput): Promise<MergedReward[
? configs[Network.Mainnet].api.subgraph
: subgraphUrl

const limit = calculateLimit(dateTo, dateFrom)

const timestampTo = String(dateTo * 1_000)
const timestampFrom = String(dateFrom * 1_000)

const [ rewards, networkFiatRates, gnosisFiatRates ] = await Promise.all([
graphql.subgraph.vault.fetchUserRewardsQuery({
url: subgraphUrl,
variables: {
limit,
where: {
timestamp_lte: timestampTo,
timestamp_gte: timestampFrom,
Expand Down
2 changes: 1 addition & 1 deletion src/methods/vault/requests/getUserStats/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { apiUrls, validateArgs, calculateUserStats, getTimestamp } from '../../../../utils'
import graphql from '../../../../graphql'
import { StakeWiseSubgraphGraph } from '../../../../types/graphql/subgraph'


type GetUserStatsInput = {
Expand All @@ -19,6 +18,7 @@ const getUserStats = (input: GetUserStatsInput) => {
return graphql.subgraph.vault.fetchUserRewardsQuery({
url: apiUrls.getSubgraphqlUrl(options),
variables: {
limit: daysCount,
where: {
timestamp_gte: String(getTimestamp(daysCount)),
allocator_: {
Expand Down
1 change: 1 addition & 0 deletions src/methods/vault/requests/getVaultStats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const getVaultStats = (input: GetVaultStatsInput) => {
url: apiUrls.getSubgraphqlUrl(options),
variables: {
timestamp,
limit: daysCount,
vaultAddress: vaultAddress.toLowerCase(),
},
modifyResult: modifyVaultStats,
Expand Down
Loading