Skip to content

Commit

Permalink
Earn protocol WIP #20 (#559)
Browse files Browse the repository at this point in the history
# 20 of N pull requests.

This pull request introduces several enhancements to the
`apps/earn-protocol` application, focusing on incorporating user
activity data and top depositors into various pages and components. The
most important changes include updates to the `EarnVaultManagePage`,
`EarnVaultOpenPage`, and `UserActivityPage` to fetch and display this
new data, as well as modifications to the server handlers and components
to support these updates.

### Enhancements to data fetching and display:

*
`apps/earn-protocol/app/earn/[network]/position/[vaultId]/[walletAddress]/page.tsx`:
Added `getUserActivity` to fetch user activity and top depositors, and
updated `EarnVaultManagePage` to include this data in the component
props.
([apps/earn-protocol/app/earn/[network]/position/[vaultId]/[walletAddress]/page.tsxR6](diffhunk://#diff-aec0650e1d631148c7236a9124bdf58ab593e3e089754ba25c6ca8b08b32ed61R6),
[apps/earn-protocol/app/earn/[network]/position/[vaultId]/[walletAddress]/page.tsxL22-R23](diffhunk://#diff-aec0650e1d631148c7236a9124bdf58ab593e3e089754ba25c6ca8b08b32ed61L22-R23),
[apps/earn-protocol/app/earn/[network]/position/[vaultId]/[walletAddress]/page.tsxR34-R38](diffhunk://#diff-aec0650e1d631148c7236a9124bdf58ab593e3e089754ba25c6ca8b08b32ed61R34-R38),
[apps/earn-protocol/app/earn/[network]/position/[vaultId]/[walletAddress]/page.tsxR65-R66](diffhunk://#diff-aec0650e1d631148c7236a9124bdf58ab593e3e089754ba25c6ca8b08b32ed61R65-R66))
* `apps/earn-protocol/app/earn/[network]/position/[vaultId]/page.tsx`:
Updated `EarnVaultOpenPage` to fetch user activity and top depositors
and pass this data to the `VaultOpenView` component.
([apps/earn-protocol/app/earn/[network]/position/[vaultId]/page.tsxR4](diffhunk://#diff-eaafa83dbaa20354e007ae0e12db9f7a702c4eda62f13e8ac7f97562a32e633cR4),
[apps/earn-protocol/app/earn/[network]/position/[vaultId]/page.tsxL18-R25](diffhunk://#diff-eaafa83dbaa20354e007ae0e12db9f7a702c4eda62f13e8ac7f97562a32e633cL18-R25),
[apps/earn-protocol/app/earn/[network]/position/[vaultId]/page.tsxL34-R43](diffhunk://#diff-eaafa83dbaa20354e007ae0e12db9f7a702c4eda62f13e8ac7f97562a32e633cL34-R43))
*
[`apps/earn-protocol/app/earn/user-activity/page.tsx`](diffhunk://#diff-00a406f55b8254b1f7ebb56bdffb224cf8036f0c3b2bf475a55323c66fe4bf24L16-R16):
Modified `UserActivityPage` to include top depositors in the fetched
data and pass it to the `UserActivityView` component.
[[1]](diffhunk://#diff-00a406f55b8254b1f7ebb56bdffb224cf8036f0c3b2bf475a55323c66fe4bf24L16-R16)
[[2]](diffhunk://#diff-00a406f55b8254b1f7ebb56bdffb224cf8036f0c3b2bf475a55323c66fe4bf24R25)

### Updates to server handlers:

*
[`apps/earn-protocol/app/server-handlers/sdk/get-user-activity.ts`](diffhunk://#diff-410dafcceab060e8269aee988d0fec5ba9126561247c8a7181761fe1f2b6b48cR1-R74):
Created a new function `getUserActivity` to fetch user activity and top
depositors.
*
[`apps/earn-protocol/app/server-handlers/sdk/get-users-activity.ts`](diffhunk://#diff-e5a3f2bcbd2b174bc55aaec59dde8bca644433d0ab0ef25de8ec779eb53c10d4L3-R14):
Updated `getUsersActivity` to include top depositors in the returned
data.
[[1]](diffhunk://#diff-e5a3f2bcbd2b174bc55aaec59dde8bca644433d0ab0ef25de8ec779eb53c10d4L3-R14)
[[2]](diffhunk://#diff-e5a3f2bcbd2b174bc55aaec59dde8bca644433d0ab0ef25de8ec779eb53c10d4L41-R39)
[[3]](diffhunk://#diff-e5a3f2bcbd2b174bc55aaec59dde8bca644433d0ab0ef25de8ec779eb53c10d4R58-R66)

### Component modifications:

*
[`apps/earn-protocol/components/layout/VaultManageView/VaultManageView.tsx`](diffhunk://#diff-180e4453ecde0323c93bedc67a55441498ddf30fa5f732446fd12cc2dc575ba9L3-R8):
Updated `VaultManageView` to accept and pass user activity and top
depositors to the `VaultManageViewComponent`.
[[1]](diffhunk://#diff-180e4453ecde0323c93bedc67a55441498ddf30fa5f732446fd12cc2dc575ba9L3-R8)
[[2]](diffhunk://#diff-180e4453ecde0323c93bedc67a55441498ddf30fa5f732446fd12cc2dc575ba9R18-R26)
[[3]](diffhunk://#diff-180e4453ecde0323c93bedc67a55441498ddf30fa5f732446fd12cc2dc575ba9R35-R36)
*
[`apps/earn-protocol/components/layout/VaultManageView/VaultManageViewComponent.tsx`](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccR1):
Enhanced `VaultManageViewComponent` to display user activity, top
depositors, and other related data.
[[1]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccR1)
[[2]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccR11-R35)
[[3]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccR44-R75)
[[4]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccL71-R94)
[[5]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccL82-R117)
[[6]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccL101-R127)
[[7]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccL148-R174)
[[8]](diffhunk://#diff-093401e3ee1390c1d36183263365baea6f0df465c9bc2cdafe8c6205c965caccL178-R209)
*
[`apps/earn-protocol/components/layout/VaultOpenView/VaultOpenView.tsx`](diffhunk://#diff-a40498ddbf1bf5830448800c7d93442c21fc90a4f6c4eed0b672478679ce71a9L3-R8):
Updated `VaultOpenView` to accept and pass user activity and top
depositors to the `VaultOpenViewComponent`.
[[1]](diffhunk://#diff-a40498ddbf1bf5830448800c7d93442c21fc90a4f6c4eed0b672478679ce71a9L3-R8)
[[2]](diffhunk://#diff-a40498ddbf1bf5830448800c7d93442c21fc90a4f6c4eed0b672478679ce71a9R17-R32)

---------

Co-authored-by: sebastianPiekarczyk <sebastian@oazoapps.com>
  • Loading branch information
marcinciarka and piekczyk authored Nov 6, 2024
1 parent 10e18a8 commit 5c4be81
Show file tree
Hide file tree
Showing 44 changed files with 1,129 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type SDKNetwork } from '@summerfi/app-types'
import { parseServerResponseToClient } from '@summerfi/app-utils'
import { type IArmadaPosition } from '@summerfi/sdk-client-react'

import { getUserActivity } from '@/app/server-handlers/sdk/get-user-activity'
import { getUserPosition } from '@/app/server-handlers/sdk/get-user-position'
import { getVaultDetails } from '@/app/server-handlers/sdk/get-vault-details'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
Expand All @@ -19,7 +20,7 @@ type EarnVaultManagePageProps = {
export const revalidate = 60

const EarnVaultManagePage = async ({ params }: EarnVaultManagePageProps) => {
const [vault, { vaults }, position] = await Promise.all([
const [vault, { vaults }, position, { userActivity, topDepositors }] = await Promise.all([
getVaultDetails({
vaultAddress: params.vaultId,
network: params.network,
Expand All @@ -30,6 +31,11 @@ const EarnVaultManagePage = async ({ params }: EarnVaultManagePageProps) => {
network: params.network,
walletAddress: params.walletAddress,
}),
getUserActivity({
vaultAddress: params.vaultId,
network: params.network,
walletAddress: params.walletAddress,
}),
])

if (!vault) {
Expand All @@ -56,6 +62,8 @@ const EarnVaultManagePage = async ({ params }: EarnVaultManagePageProps) => {
vaults={vaults}
position={positionJsonSafe}
viewWalletAddress={params.walletAddress}
userActivity={userActivity}
topDepositors={topDepositors}
/>
)
}
Expand Down
13 changes: 11 additions & 2 deletions apps/earn-protocol/app/earn/[network]/position/[vaultId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Text } from '@summerfi/app-earn-ui'
import { type SDKNetwork } from '@summerfi/app-types'

import { getUserActivity } from '@/app/server-handlers/sdk/get-user-activity'
import { getVaultDetails } from '@/app/server-handlers/sdk/get-vault-details'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { VaultOpenView } from '@/components/layout/VaultOpenView/VaultOpenView'
Expand All @@ -15,12 +16,13 @@ type EarnVaultOpenPageProps = {
export const revalidate = 60

const EarnVaultOpenPage = async ({ params }: EarnVaultOpenPageProps) => {
const [vault, { vaults }] = await Promise.all([
const [vault, { vaults }, { userActivity, topDepositors }] = await Promise.all([
getVaultDetails({
vaultAddress: params.vaultId,
network: params.network,
}),
getVaultsList(),
getUserActivity({ vaultAddress: params.vaultId, network: params.network }),
])

if (!vault) {
Expand All @@ -31,7 +33,14 @@ const EarnVaultOpenPage = async ({ params }: EarnVaultOpenPageProps) => {
)
}

return <VaultOpenView vault={vault} vaults={vaults} />
return (
<VaultOpenView
vault={vault}
vaults={vaults}
userActivity={userActivity}
topDepositors={topDepositors}
/>
)
}

export default EarnVaultOpenPage
3 changes: 2 additions & 1 deletion apps/earn-protocol/app/earn/user-activity/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface UserActivityPageProps {
}

const UserActivityPage: FC<UserActivityPageProps> = async ({ searchParams }) => {
const [{ vaults }, { usersActivity, totalUsers }] = await Promise.all([
const [{ vaults }, { usersActivity, totalUsers, topDepositors }] = await Promise.all([
getVaultsList(),
getUsersActivity(),
])
Expand All @@ -22,6 +22,7 @@ const UserActivityPage: FC<UserActivityPageProps> = async ({ searchParams }) =>
<UserActivityView
vaultsList={vaults}
usersActivity={usersActivity}
topDepositors={topDepositors}
totalUsers={totalUsers}
searchParams={parseQueryStringServerSide({ searchParams })}
/>
Expand Down
74 changes: 74 additions & 0 deletions apps/earn-protocol/app/server-handlers/sdk/get-user-activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
type SDKNetwork,
type SDKUsersActivityType,
UserActivityType,
type UsersActivity,
} from '@summerfi/app-types'
import { simpleSort, SortDirection } from '@summerfi/app-utils'
import { ArmadaVaultId } from '@summerfi/armada-protocol-service'
import { Address, getChainInfoByChainId } from '@summerfi/sdk-common'

import { backendSDK } from '@/app/server-handlers/sdk/sdk-backend-client'
import { subgraphNetworkToId } from '@/helpers/network-helpers'

export async function getUserActivity({
network,
vaultAddress,
walletAddress,
}: {
network: SDKNetwork
vaultAddress: string
walletAddress?: string
}): Promise<{
userActivity: UsersActivity
topDepositors: SDKUsersActivityType
callDataTimestamp: number
}> {
const chainId = subgraphNetworkToId(network)
const chainInfo = getChainInfoByChainId(chainId)

const fleetAddress = Address.createFromEthereum({
value: vaultAddress,
})
const poolId = ArmadaVaultId.createFrom({
chainInfo,
fleetAddress,
})

const { positions } = await backendSDK.armada.users.getUserActivityRaw({
poolId,
})

const userActivityList = positions
.filter((position) =>
walletAddress ? position.account.id === walletAddress.toLowerCase() : true,
)
.flatMap((position) => [
...position.deposits.map((deposit) => ({
...deposit,
balance: position.inputTokenBalance,
vault: position.vault,
account: position.account.id,
activity: UserActivityType.DEPOSIT,
})),
...position.withdrawals.map((deposit) => ({
...deposit,
balance: position.inputTokenBalance,
vault: position.vault,
account: position.account.id,
activity: UserActivityType.WITHDRAW,
})),
])

const topDepositors = positions
.sort((a, b) =>
simpleSort({ a: a.inputTokenBalance, b: b.inputTokenBalance, direction: SortDirection.DESC }),
)
.filter((item) => item.deposits.length > 0)

return {
userActivity: userActivityList,
topDepositors,
callDataTimestamp: Date.now(),
}
}
31 changes: 18 additions & 13 deletions apps/earn-protocol/app/server-handlers/sdk/get-users-activity.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import {
sdkSupportedNetworks,
type SDKUserActivityType,
type SDKUsersActivityType,
UserActivityType,
type UsersActivity,
} from '@summerfi/app-types'
import { simpleSort, SortDirection } from '@summerfi/app-utils'
import { getChainInfoByChainId } from '@summerfi/sdk-common'

import { backendSDK } from '@/app/server-handlers/sdk/sdk-backend-client'

export interface UserActivity {
timestamp: SDKUserActivityType['deposits'][0]['timestamp']
amount: SDKUserActivityType['deposits'][0]['amount']
balance: SDKUserActivityType['inputTokenBalance']
vault: SDKUserActivityType['vault']
account: SDKUserActivityType['account']['id']
activity: UserActivityType
}

export type UsersActivity = UserActivity[]

export async function getUsersActivity(): Promise<{
usersActivity: UsersActivity
topDepositors: SDKUsersActivityType
totalUsers: number
callDataTimestamp: number
}> {
Expand All @@ -38,7 +30,13 @@ export async function getUsersActivity(): Promise<{
(typeof usersActivityByNetwork)[number]['positions']
>((acc, { positions }) => [...acc, ...positions], [])

const totalUsers = usersActivityListRaw.filter((position) => position.deposits.length > 0).length
const totalUsers = [
...new Set(
usersActivityListRaw
.filter((position) => position.deposits.length > 0)
.map((position) => position.account.id),
),
].length

const usersActivityList = usersActivityListRaw.flatMap((position) => [
...position.deposits.map((deposit) => ({
Expand All @@ -57,8 +55,15 @@ export async function getUsersActivity(): Promise<{
})),
])

const topDepositors = usersActivityListRaw
.sort((a, b) =>
simpleSort({ a: a.inputTokenBalance, b: b.inputTokenBalance, direction: SortDirection.DESC }),
)
.filter((item) => item.deposits.length > 0)

return {
usersActivity: usersActivityList,
topDepositors,
totalUsers,
callDataTimestamp: Date.now(),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use client'

import { type SDKVaultishType, type SDKVaultsListType } from '@summerfi/app-types'
import {
type SDKUsersActivityType,
type SDKVaultishType,
type SDKVaultsListType,
type UsersActivity,
} from '@summerfi/app-types'
import { type IArmadaPosition, SDKContextProvider } from '@summerfi/sdk-client-react'

import { VaultManageViewComponent } from '@/components/layout/VaultManageView/VaultManageViewComponent'
Expand All @@ -10,11 +15,15 @@ export const VaultManageView = ({
vault,
vaults,
position,
userActivity,
topDepositors,
viewWalletAddress,
}: {
vault: SDKVaultishType
vaults: SDKVaultsListType
position: IArmadaPosition
userActivity: UsersActivity
topDepositors: SDKUsersActivityType
viewWalletAddress: string
}) => {
return (
Expand All @@ -23,6 +32,8 @@ export const VaultManageView = ({
vault={vault}
vaults={vaults}
position={position}
userActivity={userActivity}
topDepositors={topDepositors}
viewWalletAddress={viewWalletAddress}
/>
</SDKContextProvider>
Expand Down
Loading

0 comments on commit 5c4be81

Please sign in to comment.