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

Earn protocol WIP #20 #559

Merged
merged 11 commits into from
Nov 6, 2024
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
Loading