Skip to content

Commit

Permalink
behave properly when there is no realm config account + misc fixes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
asktree authored Jul 26, 2023
1 parent 5f17b75 commit 0ebce2e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 33 deletions.
5 changes: 3 additions & 2 deletions components/AboutRealm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react'
import useRealm from 'hooks/useRealm'
import useProgramVersion from '@hooks/useProgramVersion'

const AboutRealm = () => {
const { realmInfo, symbol } = useRealm()
const programVersion = useProgramVersion()

return (
<div className="pb-4 space-y-3">
Expand Down Expand Up @@ -44,7 +45,7 @@ const AboutRealm = () => {
) : null}
<div>
<p className="text-xs text-fgd-3">Program Version</p>
<p className="text-fgd-1">{realmInfo?.programVersion}</p>
<p className="text-fgd-1">{programVersion}</p>
</div>
</div>
)
Expand Down
4 changes: 2 additions & 2 deletions components/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { ReactNode } from 'react'
import { ReactNode } from 'react'
import Tippy from '@tippyjs/react'
import 'tippy.js/animations/scale.css'

type TooltipProps = {
content: ReactNode
content: ReactNode | undefined
placement?: any
className?: string
children?: ReactNode
Expand Down
10 changes: 5 additions & 5 deletions components/treasuryV2/Details/MintDetails/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import { useState } from 'react'
import cx from 'classnames'
import {
PencilIcon,
Expand Down Expand Up @@ -28,22 +28,22 @@ import { GoverningTokenType } from '@solana/spl-governance'
import useProgramVersion from '@hooks/useProgramVersion'
import useWalletOnePointOh from '@hooks/useWalletOnePointOh'
import { DEFAULT_GOVERNANCE_PROGRAM_VERSION } from '@components/instructions/tools'
import { useRealmConfigQuery } from '@hooks/queries/realmConfig'
import { useEffectiveRealmConfig } from '@hooks/queries/realmConfig'

interface Props {
className?: string
mint: Mint
}

const useTokenType = (govpop: 'community' | 'council' | undefined) => {
const config = useRealmConfigQuery().data?.result
const config = useEffectiveRealmConfig()
switch (govpop) {
case undefined:
return undefined
case 'community':
return config?.account.communityTokenConfig.tokenType
return config?.communityTokenConfig.tokenType
case 'council':
return config?.account.councilTokenConfig.tokenType
return config?.councilTokenConfig.tokenType
}
}

Expand Down
32 changes: 20 additions & 12 deletions components/treasuryV2/Details/RealmAuthorityDetails/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ export default function Config(props: Props) {
icon={<TokenIcon />}
name={'Token type'}
value={
{ 0: 'Liquid', 1: 'Membership', 2: 'Disabled' }[
props.realmAuthority.config.communityTokenConfig!.tokenType
]
props.realmAuthority.config.communityTokenConfig
? { 0: 'Liquid', 1: 'Membership', 2: 'Disabled' }[
props.realmAuthority.config.communityTokenConfig
.tokenType
]
: 'Liquid'
}
/>
)}
Expand Down Expand Up @@ -202,15 +205,20 @@ export default function Config(props: Props) {
<div className="font-bold">Council Rules</div>
</div>
<div className="grid grid-cols-1 gap-8">
<Section
icon={<TokenIcon />}
name={'Token type'}
value={
{ 0: 'Liquid', 1: 'Membership', 2: 'Disabled' }[
props.realmAuthority.config.councilTokenConfig!.tokenType
]
}
/>
{
<Section
icon={<TokenIcon />}
name={'Token type'}
value={
props.realmAuthority.config.councilTokenConfig
? { 0: 'Liquid', 1: 'Membership', 2: 'Disabled' }[
props.realmAuthority.config.councilTokenConfig
.tokenType
]
: 'Liquid'
}
/>
}
<Section
icon={<BeakerIcon />}
name={'Use council voter weight add‑in'}
Expand Down
40 changes: 39 additions & 1 deletion hooks/queries/realmConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { getRealmConfig, getRealmConfigAddress } from '@solana/spl-governance'
import {
GovernanceAccountType,
GoverningTokenType,
RealmConfigAccount,
getRealmConfig,
getRealmConfigAddress,
} from '@solana/spl-governance'
import { PublicKey } from '@solana/web3.js'
import { useQuery } from '@tanstack/react-query'
import asFindable from '@utils/queries/asFindable'
import { useRealmQuery } from './realm'
import useLegacyConnectionContext from '@hooks/useLegacyConnectionContext'
import useSelectedRealmPubkey from '@hooks/selectedRealm/useSelectedRealmPubkey'

export const realmConfigQueryKeys = {
all: (cluster: string) => [cluster, 'RealmConfig'],
Expand Down Expand Up @@ -39,3 +46,34 @@ export const useRealmConfigQuery = () => {

return query
}

const DEFAULT_CONFIG_FOR_REALM = (realm: PublicKey): RealmConfigAccount => ({
accountType: GovernanceAccountType.RealmConfig,
realm,
communityTokenConfig: {
voterWeightAddin: undefined,
maxVoterWeightAddin: undefined,
tokenType: GoverningTokenType.Liquid,
reserved: new Uint8Array(),
},
councilTokenConfig: {
voterWeightAddin: undefined,
maxVoterWeightAddin: undefined,
tokenType: GoverningTokenType.Liquid,
reserved: new Uint8Array(),
},
reserved: new Uint8Array(),
})

/** There may be no RealmConfigAccount for the DAO, in which case the program just uses defaults */
export const useEffectiveRealmConfig = () => {
const { data: configResult } = useRealmConfigQuery()
const realmPk = useSelectedRealmPubkey()
return configResult === undefined
? undefined
: configResult.result === undefined
? realmPk
? DEFAULT_CONFIG_FOR_REALM(realmPk)
: undefined
: configResult.result.account
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getMintNaturalAmountFromDecimalAsBN } from '@tools/sdk/units'
import { validateSolAddress } from '@utils/formValidation'
import { UiInstruction } from '@utils/uiTypes/proposalCreationTypes'
import { useRouter } from 'next/router'
import React, {
import {
FC,
useCallback,
useContext,
Expand All @@ -25,6 +25,7 @@ import React, {
import { NewProposalContext } from '../../../new'
import useMembershipTypes from './useMembershipTypes'
import { useRealmQuery } from '@hooks/queries/realm'
import Tooltip from '@components/Tooltip'

type Form = {
memberKey?: string
Expand Down Expand Up @@ -197,17 +198,26 @@ const RevokeGoverningTokens: FC<{

return (
<>
<Select
label="Membership Type"
value={selectedMembershipType}
onChange={(x) => setForm((p) => ({ ...p, membershipPopulation: x }))}
<Tooltip
content={
Object.keys(membershipTypes).length === 0
? 'Your DAO has no governance tokens with the Membership token type'
: undefined
}
>
{Object.keys(membershipTypes).map((x) => (
<Select.Option key={x} value={x}>
{capitalizeFirstLetter(x)}
</Select.Option>
))}
</Select>
<Select
label="Membership Token"
disabled={Object.keys(membershipTypes).length === 0}
value={selectedMembershipType}
onChange={(x) => setForm((p) => ({ ...p, membershipPopulation: x }))}
>
{Object.keys(membershipTypes).map((x) => (
<Select.Option key={x} value={x}>
{capitalizeFirstLetter(x)}
</Select.Option>
))}
</Select>
</Tooltip>
<Input
label="Member Public Key"
value={form.memberKey}
Expand Down

1 comment on commit 0ebce2e

@vercel
Copy link

@vercel vercel bot commented on 0ebce2e Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.