Skip to content

Commit

Permalink
Earn protocol WIP #18 (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinciarka authored Nov 4, 2024
1 parent dd76cbb commit 0210b9a
Show file tree
Hide file tree
Showing 131 changed files with 3,360 additions and 746 deletions.
2 changes: 1 addition & 1 deletion apps/earn-protocol-landing-page/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVaultsList } from '@/app/server-handlers/sdk/getVaultsList'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { LandingPageContent } from '@/components/layout/LandingPageContent/LandingPageContent'

export const revalidate = 60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ if (!process.env.SDK_API_URL) {
}

export const backendSDK = makeSDK({
apiURL: process.env.SDK_API_URL,
apiURL: `${process.env.SDK_API_URL}/api/sdk`,
})
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const NavigationWrapper: FC = () => {
iconSize: 18,
},
{
url: '/rebalancing-activity',
url: '/earn/rebalance-activity',
id: 'rebalancing-activity',
title: 'Rebalancing Activity',
description: 'Text for rebalancing activity',
Expand Down
27 changes: 18 additions & 9 deletions apps/earn-protocol/account-kit/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { alchemy, base } from '@account-kit/infra'
import { alchemy, arbitrum, base } from '@account-kit/infra'
import { type AlchemyAccountsUIConfig, cookieStorage, createConfig } from '@account-kit/react'
import { SDKChainId, SDKSupportedNetworkIdsEnum } from '@summerfi/app-types'
import { type Chain } from 'viem'

import { NetworkIds } from '@/constants/networks-list'

type SupportedNetworkIds = NetworkIds.MAINNET | NetworkIds.BASEMAINNET
export const SDKChainIdToAAChainMap = {
[SDKChainId.ARBITRUM]: arbitrum,
[SDKChainId.BASE]: base,
}

const uiConfig: AlchemyAccountsUIConfig = {
illustrationStyle: 'outline',
Expand Down Expand Up @@ -33,7 +36,7 @@ export const getAccountKitConfig = ({
chainId,
}: {
forkRpcUrl?: string
chainId?: SupportedNetworkIds
chainId?: SDKSupportedNetworkIdsEnum
}) => {
return createConfig(
{
Expand All @@ -43,10 +46,13 @@ export const getAccountKitConfig = ({
rpcUrl: '/api/rpc',
},
chain: {
[NetworkIds.MAINNET]: defaultChain,
[NetworkIds.BASEMAINNET]: base,
}[chainId ?? (defaultChain.id as SupportedNetworkIds)],
ssr: true,
[SDKSupportedNetworkIdsEnum.ARBITRUM]: arbitrum,
[SDKSupportedNetworkIdsEnum.BASE]: base,
}[chainId ?? defaultChain.id] as Chain,
chains: Object.values(SDKChainIdToAAChainMap).map((chain) => ({
chain,
})),
ssr: false,
storage: cookieStorage,
},
uiConfig,
Expand All @@ -61,6 +67,9 @@ export const staticConfig = createConfig(
rpcUrl: '/api/rpc',
},
chain: defaultChain,
chains: Object.values(SDKChainIdToAAChainMap).map((chain) => ({
chain,
})),
ssr: true,
storage: cookieStorage,
},
Expand Down
22 changes: 14 additions & 8 deletions apps/earn-protocol/app/earn/[network]/details/[vaultId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { subgraphNetworkToId, VaultGridDetails } from '@summerfi/app-earn-ui'
import { Text, VaultGridDetails } from '@summerfi/app-earn-ui'
import { type SDKNetwork } from '@summerfi/app-types'

import { getVaultDetails } from '@/app/server-handlers/sdk/getVaultDetails'
import { getVaultsList } from '@/app/server-handlers/sdk/getVaultsList'
import { getVaultDetails } from '@/app/server-handlers/sdk/get-vault-details'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { VaultDetailsView } from '@/components/layout/VaultDetailsView/VaultDetailsView'

type EarnVaultDetailsPageProps = {
Expand All @@ -15,18 +15,24 @@ type EarnVaultDetailsPageProps = {
export const revalidate = 60

const EarnVaultDetailsPage = async ({ params }: EarnVaultDetailsPageProps) => {
const networkId = subgraphNetworkToId(params.network)

const [selectedVault, { vaults }] = await Promise.all([
const [vault, { vaults }] = await Promise.all([
getVaultDetails({
vaultAddress: params.vaultId,
chainId: networkId,
network: params.network,
}),
getVaultsList(),
])

if (!vault) {
return (
<Text>
No vault found with the id {params.vaultId} on the network {params.network}
</Text>
)
}

return (
<VaultGridDetails vault={selectedVault} vaults={vaults}>
<VaultGridDetails vault={vault} vaults={vaults}>
<VaultDetailsView />
</VaultGridDetails>
)
Expand Down
2 changes: 1 addition & 1 deletion apps/earn-protocol/app/earn/[network]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type SDKNetwork } from '@summerfi/app-types'

import { getVaultsList } from '@/app/server-handlers/sdk/getVaultsList'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { VaultsListView } from '@/components/layout/VaultsListView/VaultsListView'

type EarnNetworkVaultsPageProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Text } from '@summerfi/app-earn-ui'
import { type SDKNetwork } from '@summerfi/app-types'
import { parseServerResponseToClient } from '@summerfi/app-utils'
import { type IArmadaPosition } from '@summerfi/sdk-client-react'

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'
import { VaultManageView } from '@/components/layout/VaultManageView/VaultManageView'

type EarnVaultManagePageProps = {
params: {
vaultId: string
network: SDKNetwork
walletAddress: string
}
}

export const revalidate = 60

const EarnVaultManagePage = async ({ params }: EarnVaultManagePageProps) => {
const [vault, { vaults }, position] = await Promise.all([
getVaultDetails({
vaultAddress: params.vaultId,
network: params.network,
}),
getVaultsList(),
getUserPosition({
vaultAddress: params.vaultId,
network: params.network,
walletAddress: params.walletAddress,
}),
])

if (!vault) {
return (
<Text>
No vault found with the id {params.vaultId} on the network {params.network}
</Text>
)
}

if (!position) {
return (
<Text>
No position found on {params.walletAddress} on the network {params.network}
</Text>
)
}

const positionJsonSafe = parseServerResponseToClient<IArmadaPosition>(position)

return (
<VaultManageView
vault={vault}
vaults={vaults}
position={positionJsonSafe}
viewWalletAddress={params.walletAddress}
/>
)
}

export default EarnVaultManagePage

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { subgraphNetworkToId, Text } from '@summerfi/app-earn-ui'
import { Text } from '@summerfi/app-earn-ui'
import { type SDKNetwork } from '@summerfi/app-types'

import { getVaultDetails } from '@/app/server-handlers/sdk/getVaultDetails'
import { getVaultsList } from '@/app/server-handlers/sdk/getVaultsList'
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'

type EarnVaultOpenManagePageProps = {
type EarnVaultOpenPageProps = {
params: {
vaultId: string
network: SDKNetwork
Expand All @@ -14,13 +14,11 @@ type EarnVaultOpenManagePageProps = {

export const revalidate = 60

const EarnVaultOpenManagePage = async ({ params }: EarnVaultOpenManagePageProps) => {
const networkId = subgraphNetworkToId(params.network)

const EarnVaultOpenPage = async ({ params }: EarnVaultOpenPageProps) => {
const [vault, { vaults }] = await Promise.all([
getVaultDetails({
vaultAddress: params.vaultId,
chainId: networkId,
network: params.network,
}),
getVaultsList(),
])
Expand All @@ -36,4 +34,4 @@ const EarnVaultOpenManagePage = async ({ params }: EarnVaultOpenManagePageProps)
return <VaultOpenView vault={vault} vaults={vaults} />
}

export default EarnVaultOpenManagePage
export default EarnVaultOpenPage
2 changes: 1 addition & 1 deletion apps/earn-protocol/app/earn/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVaultsList } from '@/app/server-handlers/sdk/getVaultsList'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { VaultsListView } from '@/components/layout/VaultsListView/VaultsListView'

export const revalidate = 60
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { portfolioRewardsHandler } from '@/app/server-handlers/portfolio/portfolio-rewards-handler'
import { portfolioWalletAssetsHandler } from '@/app/server-handlers/portfolio/portfolio-wallet-assets-handler'
import { getVaultsList } from '@/app/server-handlers/sdk/getVaultsList'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { PortfolioPageView } from '@/components/layout/PortfolioPageView/PortfolioPageView'

type PortfolioPageProps = {
Expand Down
25 changes: 22 additions & 3 deletions apps/earn-protocol/app/earn/rebalance-activity/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { type FC } from 'react'
import { parseQueryStringServerSide } from '@summerfi/app-utils'
import { type ReadonlyURLSearchParams } from 'next/navigation'

import { getGlobalRebalances } from '@/app/server-handlers/sdk/get-global-rebalances'
import { getVaultsList } from '@/app/server-handlers/sdk/get-vaults-list'
import { RebalanceActivityView } from '@/features/rebalance-activity/components/RebalanceActivityView/RebalanceActivityView'
import { rebalancingActivityRawData } from '@/features/rebalance-activity/table/dummyData'

const RebalanceActivityPage = () => {
return <RebalanceActivityView rawData={rebalancingActivityRawData} />
export const revalidate = 60

interface RebalanceActivityPageProps {
searchParams: ReadonlyURLSearchParams
}

const RebalanceActivityPage: FC<RebalanceActivityPageProps> = async ({ searchParams }) => {
const [{ vaults }, { rebalances }] = await Promise.all([getVaultsList(), getGlobalRebalances()])

return (
<RebalanceActivityView
vaultsList={vaults}
rebalancesList={rebalances}
searchParams={parseQueryStringServerSide({ searchParams })}
/>
)
}

export default RebalanceActivityPage
4 changes: 2 additions & 2 deletions apps/earn-protocol/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function RootLayout({ children }: { children: React.ReactNo
const chainId: number | undefined = accountKitState.state?.chainId
const forkRpcUrl: string | undefined = chainId ? forks[chainId] : undefined

const accountKitInitialState = cookieToInitialState(
const accountKitInitializedState = cookieToInitialState(
getAccountKitConfig({ forkRpcUrl, chainId }),
cookie ?? undefined,
)
Expand All @@ -42,7 +42,7 @@ export default async function RootLayout({ children }: { children: React.ReactNo
<GlobalStyles />
</head>
<body className={` ${fontInter.variable}`} style={{ backgroundColor: '#1B1B1B' }}>
<AlchemyAccountsProvider initialState={accountKitInitialState}>
<AlchemyAccountsProvider initialState={accountKitInitializedState}>
<NextIntlClientProvider messages={messages}>
<MasterPage>{children}</MasterPage>
</NextIntlClientProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { sdkSupportedNetworks } 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 async function getGlobalRebalances() {
const rebalancesByNetwork = await Promise.all(
sdkSupportedNetworks.map((networkId) => {
const chainInfo = getChainInfoByChainId(networkId)

return backendSDK.armada.users.getGlobalRebalancesRaw({
chainInfo,
})
}),
)

// flatten the list
const rebalancesList = rebalancesByNetwork
.reduce<(typeof rebalancesByNetwork)[number]['rebalances']>(
(acc, { rebalances }) => [...acc, ...rebalances],
[],
)
// initially sorted by timestamp since it combines data from independent subgraphs
.sort((a, b) => simpleSort({ a: a.timestamp, b: b.timestamp, direction: SortDirection.DESC }))

return {
rebalances: rebalancesList,
callDataTimestamp: Date.now(),
}
}
43 changes: 43 additions & 0 deletions apps/earn-protocol/app/server-handlers/sdk/get-user-position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { type SDKNetwork } from '@summerfi/app-types'
import { type IArmadaPosition } from '@summerfi/sdk-client-react'
import { Address, getChainInfoByChainId, User, Wallet } from '@summerfi/sdk-common'

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

export async function getUserPosition({
network,
vaultAddress,
walletAddress,
}: {
network: SDKNetwork
vaultAddress: string
walletAddress: string
}) {
try {
const chainId = subgraphNetworkToId(network)
const chainInfo = getChainInfoByChainId(chainId)

const fleetAddress = Address.createFromEthereum({
value: vaultAddress,
})

const wallet = Wallet.createFrom({
address: Address.createFromEthereum({
value: walletAddress.toLowerCase(),
}),
})
const user = User.createFrom({
chainInfo,
wallet,
})
const position = await backendSDK.armada.users.getUserPosition({
fleetAddress,
user,
})

return position as IArmadaPosition | undefined
} catch (error) {
throw new Error(`Failed to get users position: ${error}`)
}
}
Loading

0 comments on commit 0210b9a

Please sign in to comment.