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

fix: properly handle vote accounts for multiple clusters #82

Merged
merged 6 commits into from
Nov 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export class ApiNetworkTokenDataService {
network: { connect: { cluster: network.cluster } },
type: NetworkTokenType.Validator,
account: hash,
name: `${network.cluster.replace('Solana', 'Solana ')} Genesis`,
name: `${network.cluster.replace('Solana', 'Solana ')}`,
description: `Genesis Hash for ${network.cluster.replace('Solana', 'Solana ')}`,
program: SystemProgram.programId.toBase58(),
}

await this.core.data.networkToken.create({ data })
this.logger.log(
`ensureGenesisHash: Created Genesis Block for cluster ${network.cluster} with genesis hash ${hash}`,
Expand Down Expand Up @@ -89,6 +89,12 @@ export class ApiNetworkTokenDataService {

async delete(networkTokenId: string) {
await this.findOne(networkTokenId)
const conditions = await this.core.data.roleCondition.findMany({
where: { token: { id: networkTokenId } },
})
if (conditions.length) {
throw new Error(`Network token ${networkTokenId} is used by ${conditions.length} role conditions`)
}
const deleted = await this.core.data.networkToken.delete({ where: { id: networkTokenId } })
return !!deleted
}
Expand Down
22 changes: 13 additions & 9 deletions libs/api/role/data-access/src/lib/api-role-resolver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ export class ApiRoleResolverService {
const startedAt = Date.now()

const conditions = await this.getRoleConditions({ community })
const voteIdentities = await this.getVoteIdentities({ conditions })

await this.syncCommunityMembers({ communityId, voteIdentities })
const clusterVoteIdentities = await this.getVoteIdentities({ conditions })
await this.syncCommunityMembers({ communityId, voteIdentities: Object.values(clusterVoteIdentities).flat() })

const [roleMap, users] = await Promise.all([
this.getRoleMap({ community }),
Expand All @@ -83,6 +82,7 @@ export class ApiRoleResolverService {

// Now we want to loop over each condition and check the assets
for (const condition of conditions) {
const voteIdentities = clusterVoteIdentities[condition.token.cluster]
if (condition.token?.type === NetworkTokenType.Validator && voteIdentities.length) {
if (voteIdentities.find((identity) => resolved.solanaIds.includes(identity))) {
resolved.conditions.push(condition)
Expand Down Expand Up @@ -128,29 +128,33 @@ export class ApiRoleResolverService {
return Promise.resolve(result)
}

async getVoteIdentities({ conditions }: { conditions: RoleCondition[] }) {
async getVoteIdentities({ conditions }: { conditions: RoleCondition[] }): Promise<Record<NetworkCluster, string[]>> {
const result: Record<NetworkCluster, string[]> = {
[NetworkCluster.SolanaCustom]: [],
[NetworkCluster.SolanaDevnet]: [],
[NetworkCluster.SolanaMainnet]: [],
[NetworkCluster.SolanaTestnet]: [],
}
const hasValidatorCondition: RoleCondition | undefined = conditions.find(
(c) => c.type === NetworkTokenType.Validator,
)
if (!hasValidatorCondition) {
this.logger.debug(`getVoteIdentities: No validator conditions found.`)
return []
return result
}
const clusters: NetworkCluster[] = conditions.map((c) => c.token.cluster)

const accounts: string[] = []

for (const cluster of clusters) {
try {
const accountsForCluster = await this.network.cluster.getVoteIdentities(cluster)
this.logger.debug(`[${cluster}] getVoteIdentities: Found ${accountsForCluster.length} identities.`)
accounts.push(...accountsForCluster)
result[cluster].push(...accountsForCluster)
} catch (e) {
this.logger.error(`[${cluster}] getVoteIdentities: Error getting vote identities for cluster.: ${e}`)
}
}

return accounts
return result
}

async syncCommunityMembers({ communityId, voteIdentities }: { communityId: string; voteIdentities: string[] }) {
Expand Down
14 changes: 10 additions & 4 deletions libs/web/bot/data-access/src/lib/use-admin-find-many-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ export function useAdminFindManyBot(props: Partial<AdminFindManyBotInput> & { co
return undefined
}),
deleteBot: (botId: string) =>
sdk.adminDeleteBot({ botId }).then(() => {
toastSuccess('Bot deleted')
return query.refetch()
}),
sdk
.adminDeleteBot({ botId })
.then(() => {
toastSuccess('Bot deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ export function useAdminFindManyCommunity(props?: Partial<AdminFindManyCommunity
return undefined
}),
deleteCommunity: (communityId: string) =>
sdk.adminDeleteCommunity({ communityId }).then(() => {
toastSuccess('Community deleted')
return query.refetch()
}),
sdk
.adminDeleteCommunity({ communityId })
.then(() => {
toastSuccess('Community deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AdminFindManyNetworkAssetInput, NetworkCluster, NetworkTokenType } from '@pubkey-link/sdk'
import { useSdk } from '@pubkey-link/web-core-data-access'
import { toastSuccess } from '@pubkey-ui/core'
import { toastError, toastSuccess } from '@pubkey-ui/core'
import { useQuery } from '@tanstack/react-query'
import { useState } from 'react'

Expand Down Expand Up @@ -35,9 +35,15 @@ export function useAdminFindManyNetworkAsset(
},
setSearch,
deleteNetworkAsset: (networkAssetId: string) =>
sdk.adminDeleteNetworkAsset({ networkAssetId }).then(() => {
toastSuccess('NetworkAsset deleted')
return query.refetch()
}),
sdk
.adminDeleteNetworkAsset({ networkAssetId })
.then(() => {
toastSuccess('NetworkAsset deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function UserNetworkAssetListFeature({

{query.isLoading ? (
<UiLoader />
) : items?.length ? (
) : groups?.length ? (
<UiStack gap="lg">
{groups.map((group) => (
<UiStack key={group.token.id}>
Expand All @@ -86,8 +86,19 @@ export default function UserNetworkAssetListFeature({
))}
</UiStack>
) : (
<UiInfo message={`No ${type ? type : ''} assets found${cluster ? ` on cluster ${cluster}` : ''}.`} />
<UiInfo message={`No ${type ? typeName(type) : 'assets'} found${cluster ? ` on cluster ${cluster}` : ''}.`} />
)}
</UiStack>
)
}

function typeName(type: NetworkTokenType) {
switch (type) {
case NetworkTokenType.Fungible:
return 'tokens'
case NetworkTokenType.NonFungible:
return 'collectibles'
default:
return type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ export function useAdminFindManyNetworkToken(
return undefined
}),
deleteNetworkToken: (networkTokenId: string) =>
sdk.adminDeleteNetworkToken({ networkTokenId }).then(() => {
toastSuccess('NetworkToken deleted')
return query.refetch()
}),
sdk
.adminDeleteNetworkToken({ networkTokenId })
.then(() => {
toastSuccess('NetworkToken deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ export function useAdminFindManyNetwork(props?: Partial<AdminFindManyNetworkInpu
return undefined
}),
deleteNetwork: (networkId: string) =>
sdk.adminDeleteNetwork({ networkId }).then(() => {
toastSuccess('Network deleted')
return query.refetch()
}),
sdk
.adminDeleteNetwork({ networkId })
.then(() => {
toastSuccess('Network deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
14 changes: 10 additions & 4 deletions libs/web/role/data-access/src/lib/use-admin-find-many-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ export function useAdminFindManyRole(props: Partial<AdminFindManyRoleInput> & {
return undefined
}),
deleteRole: (roleId: string) =>
sdk.adminDeleteRole({ roleId }).then(() => {
toastSuccess('Role deleted')
return query.refetch()
}),
sdk
.adminDeleteRole({ roleId })
.then(() => {
toastSuccess('Role deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ export function useAdminFindManySnapshot(props: Partial<AdminFindManySnapshotInp
return undefined
}),
deleteSnapshot: (snapshotId: string) =>
sdk.adminDeleteSnapshot({ snapshotId }).then(() => {
toastSuccess('Snapshot deleted')
return query.refetch()
}),
sdk
.adminDeleteSnapshot({ snapshotId })
.then(() => {
toastSuccess('Snapshot deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
16 changes: 11 additions & 5 deletions libs/web/user/data-access/src/lib/use-admin-find-many-user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AdminFindManyUserInput, UserRole, UserStatus } from '@pubkey-link/sdk'
import { useSdk } from '@pubkey-link/web-core-data-access'
import { toastSuccess } from '@pubkey-ui/core'
import { toastError, toastSuccess } from '@pubkey-ui/core'
import { useQuery } from '@tanstack/react-query'
import { useState } from 'react'

Expand Down Expand Up @@ -45,9 +45,15 @@ export function useAdminFindManyUser(props?: AdminFindManyUserInput) {
setStatus(status)
},
deleteUser: (userId: string) =>
sdk.adminDeleteUser({ userId }).then(() => {
toastSuccess('User deleted')
return query.refetch()
}),
sdk
.adminDeleteUser({ userId })
.then(() => {
toastSuccess('User deleted')
return query.refetch()
})
.catch((err) => {
toastError(err.message)
return undefined
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function UserProfileTabCommunities({ isAuthUser, username }: { is
) : (
<UiInfo
title="No communities found."
message={`${isAuthUser ? 'You have' : `${username} has`} is not a member of any communities`}
message={`${isAuthUser ? 'You are' : `${username} is`} not a member of any communities`}
/>
)
}
Loading