Skip to content

Commit

Permalink
Feature/add yield chart (#84)
Browse files Browse the repository at this point in the history
* feature/merge dev to main (#80)

* h

* fix datetime field

* Update yarn.lock

* feature/edit app tab links (#71)

* edit app tab links

* fixes for build

* Fix/time view (#74)

* fixes for build

* fix time view

* fix datetime autofill (#76)

* fix datetime autofill (#77)

* Fix/datetime and multiplier (#78)

* fix datetime autofill

* fix datetime autofill

* Fix/datetime and multiplier (#79)

* fix datetime autofill

* fix datetime autofill

* merge dev

* Dev (#82)

* h

* fix datetime field

* Update yarn.lock

* feature/edit app tab links (#71)

* edit app tab links

* fixes for build

* Fix/time view (#74)

* fixes for build

* fix time view

* fix datetime autofill (#76)

* fix datetime autofill (#77)

* Fix/datetime and multiplier (#78)

* fix datetime autofill

* fix datetime autofill

* Fix/datetime and multiplier (#79)

* fix datetime autofill

* fix datetime autofill

* merge dev

* fix total deposited view (#81)

* add base yield chart logic

* fix calculation proccess

* add chart data

* yarn lock

---------

Co-authored-by: Oleksandr Fedorenko <[email protected]>
  • Loading branch information
Sorizen and FedokDL authored Aug 16, 2024
1 parent fd2e249 commit 0f68a0f
Show file tree
Hide file tree
Showing 7 changed files with 11,316 additions and 7,561 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"dependencies": {
"@apollo/client": "^3.9.10",
"@distributedlab/tools": "^1.0.0-rc.13",
"@ethersproject/abi": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@vuelidate/core": "^2.0.0",
"@vuelidate/validators": "^2.0.0",
"@vueuse/core": "^10.1.2",
Expand Down Expand Up @@ -85,6 +87,6 @@
"yorkie": "^2.0.0"
},
"engines": {
"node": "18"
"node": "22"
}
}
141 changes: 139 additions & 2 deletions src/common/InfoDashboard/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import { mapKeys, mapValues } from 'lodash'
type ChartData = Record<number, BigNumber>

const ONE_DAY_TIMESTAMP = 24 * 60 * 60
const DECIMAL = BigNumber.from(10).pow(25)

export async function getChartData(
poolId: number,
poolStartedAt: BigNumber,
month: number,
): Promise<ChartData> {
type QueryData = Record<`r${number}`, { totalStaked: string }[]>
type QueryData = Record<`r${number}`, { totalStaked?: string }[]>
const { data } = await config.apolloClient.query<QueryData>({
query: _generateTotalStakedPerDayGraphqlQuery(poolId, poolStartedAt, month),
})

return mapValues(
mapKeys(data, (_, key) => key.slice(1)),
value => BigNumber.from(value[0].totalStaked),
value => BigNumber.from(value[0]?.totalStaked ?? 0),
)
}

Expand Down Expand Up @@ -67,3 +68,139 @@ function _generateTotalStakedPerDayGraphqlQuery(
${'{\n' + requests.join('\n') + '\n}'}
`
}

export async function getUserYieldPerDayChartData(
poolId: number,
user: string,
month: number,
): Promise<ChartData> {
type PoolIntercation = {
timestamp: string
rate: string
}

type QueryData = {
userInteractions: {
timestamp: string
rate: string
deposited: string
claimedRewards: string
pendingRewards: string
}[]
poolInteractions: PoolIntercation[]
}

// Get data from TheGraph
const { data } = await config.apolloClient.query<QueryData>({
query: _generateUserYieldPerDayGraphqlQuery(poolId, user, month),
})

// START leave yields only at the end of the calculation day
const poolInteractionsMap = new Map<string, PoolIntercation>()
data.poolInteractions.forEach(interaction => {
const { timestamp } = interaction
const date = new Date(Number(timestamp) * 1000)
const day = date.toISOString().split('T')[0]

if (
!poolInteractionsMap.has(day) ||
Number(timestamp) > Number(poolInteractionsMap.get(day)!.timestamp)
) {
poolInteractionsMap.set(day, interaction)
}
})
const poolInteractions = Array.from(poolInteractionsMap.values())
// END

// START calculate rewards
const yields: ChartData = {}
for (let i = 0; i < data.userInteractions.length; i++) {
const ui = data.userInteractions[i]
const nextUserIntercation =
i < data.userInteractions.length - 1
? data.userInteractions[i + 1]
: undefined

// Get `poolInteractions` periods between `userIntercationsYield`
// When `userInteraction` is last, get all periods that greater then current
const periodPoolInteractions = poolInteractions.filter(e => {
return (
Number(e.timestamp) > Number(ui.timestamp) &&
(nextUserIntercation
? Number(e.timestamp) < Number(nextUserIntercation.timestamp)
: true)
)
})

// Calculate current yield from the `userIntercations` and push
const uiv = BigNumber.from(ui.claimedRewards).add(ui.pendingRewards)
yields[Number(ui.timestamp)] = uiv

// Calculate nex period yields from the `poolIntercations` and push
periodPoolInteractions.forEach(pi => {
const rateDiff = BigNumber.from(pi.rate).sub(ui.rate)
const periodReward = BigNumber.from(ui.deposited)
.mul(rateDiff)
.div(DECIMAL)

const value = uiv.add(periodReward)

yields[Number(pi.timestamp)] = value
})
}
// END

return yields
}

function _generateUserYieldPerDayGraphqlQuery(
poolId: number,
user: string,
// TODO: add month
month: number,
) {
const fromTimestamp =
new Time(String(month + 1), 'M').toDate().getTime() / 1000
const toTimestamp = new Time(String(month + 2), 'M').toDate().getTime() / 1000

const REQUEST_PATTERN = `
userInteractions (
orderBy: timestamp
orderDirection: asc
where: {
user: "${user}"
poolId: "${poolId.toString()}"
timestamp_gt: ${fromTimestamp}
timestamp_lt: ${toTimestamp}
}
) {
timestamp
rate
deposited
claimedRewards
pendingRewards
}
poolInteractions (
orderBy: timestamp
orderDirection: asc
where: {
rate_gt: 0
timestamp_gt: ${fromTimestamp}
timestamp_lt: ${toTimestamp}
pool_: {
id: "${hexlify(poolId)}"
}
}
first: 1000
) {
timestamp
rate
}
`

const requests = [REQUEST_PATTERN]

return gql`
${'{\n' + requests.join('\n') + '\n}'}
`
}
Loading

0 comments on commit 0f68a0f

Please sign in to comment.