diff --git a/Strategies/components/MangoDepositComponent.tsx b/Strategies/components/MangoDepositComponent.tsx index 92863879db..7f9de69478 100644 --- a/Strategies/components/MangoDepositComponent.tsx +++ b/Strategies/components/MangoDepositComponent.tsx @@ -72,6 +72,7 @@ const MangoDepositComponent = ({ mint, councilMint, symbol, + config, } = useRealm() const [isDepositing, setIsDepositing] = useState(false) const [ @@ -204,7 +205,7 @@ const MangoDepositComponent = ({ const defaultProposalMint = voteByCouncil ? realm?.account.config.councilMint : !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/Strategies/components/everlend/EverlendDeposit.tsx b/Strategies/components/everlend/EverlendDeposit.tsx index 45d3ef2ee9..defdc55a08 100644 --- a/Strategies/components/everlend/EverlendDeposit.tsx +++ b/Strategies/components/everlend/EverlendDeposit.tsx @@ -65,6 +65,7 @@ const EverlendDeposit = ({ councilMint, ownVoterWeight, symbol, + config, } = useRealm() const [voteByCouncil, setVoteByCouncil] = useState(false) const client = useVotePluginsClientStore( @@ -106,7 +107,7 @@ const EverlendDeposit = ({ const defaultProposalMint = voteByCouncil ? realm?.account.config.councilMint : !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/Strategies/components/everlend/EverlendWithdraw.tsx b/Strategies/components/everlend/EverlendWithdraw.tsx index 282e4ff4fc..6c96ce5670 100644 --- a/Strategies/components/everlend/EverlendWithdraw.tsx +++ b/Strategies/components/everlend/EverlendWithdraw.tsx @@ -53,6 +53,7 @@ const EverlendWithdraw = ({ councilMint, ownVoterWeight, symbol, + config, } = useRealm() const { canUseTransferInstruction } = useGovernanceAssets() const [voteByCouncil, setVoteByCouncil] = useState(false) @@ -101,7 +102,7 @@ const EverlendWithdraw = ({ const defaultProposalMint = voteByCouncil ? realm?.account.config.councilMint : !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/Strategies/components/solend/SolendDeposit.tsx b/Strategies/components/solend/SolendDeposit.tsx index 4313edd034..80d3569233 100644 --- a/Strategies/components/solend/SolendDeposit.tsx +++ b/Strategies/components/solend/SolendDeposit.tsx @@ -57,6 +57,7 @@ const SolendDeposit = ({ mint, councilMint, symbol, + config, } = useRealm() const [isDepositing, setIsDepositing] = useState(false) const [deposits, setDeposits] = useState<{ @@ -199,7 +200,7 @@ const SolendDeposit = ({ const defaultProposalMint = voteByCouncil ? realm?.account.config.councilMint : !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/Strategies/components/solend/SolendWithdraw.tsx b/Strategies/components/solend/SolendWithdraw.tsx index e2d8010be1..4ad4f87234 100644 --- a/Strategies/components/solend/SolendWithdraw.tsx +++ b/Strategies/components/solend/SolendWithdraw.tsx @@ -59,6 +59,7 @@ const SolendWithdraw = ({ mint, councilMint, symbol, + config, } = useRealm() const [isWithdrawing, setIsWithdrawing] = useState(false) const [voteByCouncil, setVoteByCouncil] = useState(false) @@ -198,7 +199,7 @@ const SolendWithdraw = ({ const defaultProposalMint = voteByCouncil ? realm?.account.config.councilMint : !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/VoteStakeRegistry/components/Account/LockTokensAccount.tsx b/VoteStakeRegistry/components/Account/LockTokensAccount.tsx index 2f60258a30..475d858d6b 100644 --- a/VoteStakeRegistry/components/Account/LockTokensAccount.tsx +++ b/VoteStakeRegistry/components/Account/LockTokensAccount.tsx @@ -41,7 +41,14 @@ interface DepositBox { const unlockedTypes = ['none'] const LockTokensAccount = ({ tokenOwnerRecordPk }) => { - const { realm, realmInfo, mint, tokenRecords, councilMint } = useRealm() + const { + realm, + realmInfo, + mint, + tokenRecords, + councilMint, + config, + } = useRealm() const [isLockModalOpen, setIsLockModalOpen] = useState(false) const client = useVotePluginsClientStore((s) => s.state.vsrClient) const [reducedDeposits, setReducedDeposits] = useState([]) @@ -74,8 +81,8 @@ const LockTokensAccount = ({ tokenOwnerRecordPk }) => { setIsLoading(true) try { if ( - realm?.account.config.useCommunityVoterWeightAddin && - realm.pubkey && + config?.account.communityTokenConfig.voterWeightAddin && + realm!.pubkey && wallet?.publicKey && client ) { @@ -155,7 +162,7 @@ const LockTokensAccount = ({ tokenOwnerRecordPk }) => { const getTokenOwnerRecord = async () => { const defaultMint = !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/VoteStakeRegistry/components/TokenBalance/LockPluginTokenBalanceCard.tsx b/VoteStakeRegistry/components/TokenBalance/LockPluginTokenBalanceCard.tsx index fd315a9566..d219991090 100644 --- a/VoteStakeRegistry/components/TokenBalance/LockPluginTokenBalanceCard.tsx +++ b/VoteStakeRegistry/components/TokenBalance/LockPluginTokenBalanceCard.tsx @@ -26,7 +26,7 @@ const LockPluginTokenBalanceCard = ({ proposal?: Option }) => { const { fmtUrlWithCluster } = useQueryContext() - const { councilMint, mint, realm, symbol } = useRealm() + const { councilMint, mint, realm, symbol, config } = useRealm() const [tokenOwnerRecordPk, setTokenOwneRecordPk] = useState('') const connected = useWalletStore((s) => s.connected) const wallet = useWalletStore((s) => s.current) @@ -53,7 +53,7 @@ const LockPluginTokenBalanceCard = ({ const getTokenOwnerRecord = async () => { const defaultMint = !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/components/Members/useMembers.tsx b/components/Members/useMembers.tsx index 792f66d150..318cdd7d83 100644 --- a/components/Members/useMembers.tsx +++ b/components/Members/useMembers.tsx @@ -21,7 +21,7 @@ import { usePrevious } from '@hooks/usePrevious' import { capitalize } from '@utils/helpers' import useMembersStore from 'stores/useMembersStore' export default function useMembers() { - const { tokenRecords, councilTokenOwnerRecords, realm } = useRealm() + const { tokenRecords, councilTokenOwnerRecords, realm, config } = useRealm() const connection = useWalletStore((s) => s.connection) const previousRealmPubKey = usePrevious(realm?.pubkey.toBase58()) as string const setMembers = useMembersStore((s) => s.setMembers) @@ -285,14 +285,14 @@ export default function useMembers() { if ( realm?.pubkey && previousRealmPubKey !== realm?.pubkey.toBase58() && - !realm?.account.config.useCommunityVoterWeightAddin + !config?.account.communityTokenConfig.voterWeightAddin ) { handleSetMembers() getDelegates() } if ( !realm?.pubkey || - (realm.pubkey && realm?.account.config.useCommunityVoterWeightAddin) + (realm.pubkey && config?.account.communityTokenConfig.voterWeightAddin) ) { getDelegates() setMembers([]) diff --git a/components/RealmHeader.tsx b/components/RealmHeader.tsx index 65c94cee94..500f375c09 100644 --- a/components/RealmHeader.tsx +++ b/components/RealmHeader.tsx @@ -65,7 +65,7 @@ const RealmHeader = () => {
)}
- {!realm?.account.config.useCommunityVoterWeightAddin && ( + {!config?.account.communityTokenConfig.voterWeightAddin && ( diff --git a/components/TokenBalance/NftBalanceCard.tsx b/components/TokenBalance/NftBalanceCard.tsx index 758bfa9bae..ecac552ee2 100644 --- a/components/TokenBalance/NftBalanceCard.tsx +++ b/components/TokenBalance/NftBalanceCard.tsx @@ -30,7 +30,7 @@ const NftBalanceCard = () => { const isLoading = useNftPluginStore((s) => s.state.isLoadingNfts) const connection = useWalletStore((s) => s.connection) const [tokenOwnerRecordPk, setTokenOwneRecordPk] = useState('') - const { tokenRecords, realm, symbol, mint, councilMint } = useRealm() + const { tokenRecords, realm, symbol, mint, councilMint, config } = useRealm() const { fetchRealm } = useWalletStore((s) => s.actions) const ownTokenRecord = wallet?.publicKey ? tokenRecords[wallet.publicKey!.toBase58()] @@ -81,7 +81,7 @@ const NftBalanceCard = () => { const getTokenOwnerRecord = async () => { const defaultMint = !mint?.supply.isZero() || - realm?.account.config.useMaxCommunityVoterWeightAddin + config?.account.communityTokenConfig.maxVoterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/hooks/useCreateProposal.ts b/hooks/useCreateProposal.ts index 615356537c..909e936d57 100644 --- a/hooks/useCreateProposal.ts +++ b/hooks/useCreateProposal.ts @@ -21,6 +21,7 @@ export default function useCreateProposal() { mint, councilMint, canChooseWhoVote, + config, } = useRealm() const { getRpcContext } = useRpcContext() const handleCreateProposal = async ({ @@ -45,7 +46,7 @@ export default function useCreateProposal() { const defaultProposalMint = !mint?.supply.isZero() || - realm?.account.config.useCommunityVoterWeightAddin + config?.account.communityTokenConfig.voterWeightAddin ? realm!.account.communityMint : !councilMint?.supply.isZero() ? realm!.account.config.councilMint diff --git a/hooks/useRealm.tsx b/hooks/useRealm.tsx index 71c64793fb..1e3601db0d 100644 --- a/hooks/useRealm.tsx +++ b/hooks/useRealm.tsx @@ -194,7 +194,7 @@ export default function useRealm() { const canChooseWhoVote = realm?.account.communityMint && (!mint?.supply.isZero() || - realm.account.config.useCommunityVoterWeightAddin) && + config?.account.communityTokenConfig.voterWeightAddin) && realm.account.config.councilMint && !councilMint?.supply.isZero() diff --git a/hooks/useTreasuryInfo/assembleWallets.tsx b/hooks/useTreasuryInfo/assembleWallets.tsx index 4ecb3189d5..5ee4dfab49 100644 --- a/hooks/useTreasuryInfo/assembleWallets.tsx +++ b/hooks/useTreasuryInfo/assembleWallets.tsx @@ -5,6 +5,7 @@ import { getProgramDataAccount, ProgramAccount, Realm, + RealmConfigAccount, } from '@solana/spl-governance' import { Connection, PublicKey } from '@solana/web3.js' import { SparklesIcon } from '@heroicons/react/outline' @@ -41,6 +42,7 @@ export const assembleWallets = async ( councilMint?: MintInfo, communityMint?: MintInfo, realm?: ProgramAccount, + realmConfig?: ProgramAccount, realmInfo?: RealmInfo ) => { const walletMap: { [address: string]: Wallet } = {} @@ -277,9 +279,10 @@ export const assembleWallets = async ( minCommunityTokensToCreateGovernance: new BigNumber( config.minCommunityTokensToCreateGovernance.toString() ).shiftedBy(communityMint ? -communityMint.decimals : 0), - useCommunityVoterWeightAddin: config.useCommunityVoterWeightAddin, - useMaxCommunityVoterWeightAddin: - config.useMaxCommunityVoterWeightAddin, + useCommunityVoterWeightAddin: !!realmConfig?.account + .communityTokenConfig.voterWeightAddin, + useMaxCommunityVoterWeightAddin: !!realmConfig?.account + .communityTokenConfig.maxVoterWeightAddin, }, icon: realmInfo?.ogImage ? ( diff --git a/hooks/useTreasuryInfo/index.tsx b/hooks/useTreasuryInfo/index.tsx index a31b0a3b7d..8b1fd33945 100644 --- a/hooks/useTreasuryInfo/index.tsx +++ b/hooks/useTreasuryInfo/index.tsx @@ -25,7 +25,7 @@ interface Data { } export default function useTreasuryInfo(): Result { - const { realmInfo, realm, mint, councilMint } = useRealm() + const { realmInfo, realm, mint, councilMint, config } = useRealm() const connection = useWalletStore((s) => s.connection.current) const accounts = useGovernanceAssetsStore((s) => s.assetAccounts) const loadingGovernedAccounts = useGovernanceAssetsStore( @@ -79,6 +79,7 @@ export default function useTreasuryInfo(): Result { councilMint, mint, realm, + config, realmInfo ) } diff --git a/pages/api/daoStatistics.ts b/pages/api/daoStatistics.ts index 04852cdf30..18bee1ed6c 100644 --- a/pages/api/daoStatistics.ts +++ b/pages/api/daoStatistics.ts @@ -4,7 +4,6 @@ import { getRealms, ProgramAccount, Realm, - tryGetRealmConfig, } from '@solana/spl-governance' import { Connection, PublicKey } from '@solana/web3.js' import tokenService from '@utils/services/token' @@ -15,6 +14,7 @@ import BigNumber from 'bignumber.js' import BN from 'bn.js' import { WSOL_MINT_PK } from '@components/instructions/tools' import { withSentry } from '@sentry/nextjs' +import { getRealmConfigAccountOrDefault } from '@tools/governance/configs' const handler = async (req: NextApiRequest, res: NextApiResponse) => { const conn = new Connection( @@ -61,17 +61,20 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { ) const programId = realm.owner + const realmConfig = await getRealmConfigAccountOrDefault( + conn, + programId, + realm.pubkey + ) // Get NFT DAOs - if (realm.account.config.useCommunityVoterWeightAddin) { - const realmConfig = await tryGetRealmConfig(conn, programId, realm.pubkey) - if ( - realmConfig.account.communityTokenConfig.voterWeightAddin?.equals( - new PublicKey('GnftV5kLjd67tvHpNGyodwWveEKivz3ZWvvE3Z4xi2iw') - ) - ) { - nftRealms.push(realm) - } + + if ( + realmConfig.account.communityTokenConfig.voterWeightAddin?.equals( + new PublicKey('GnftV5kLjd67tvHpNGyodwWveEKivz3ZWvvE3Z4xi2iw') + ) + ) { + nftRealms.push(realm) } // Get Governances diff --git a/pages/dao/[symbol]/members/index.tsx b/pages/dao/[symbol]/members/index.tsx index 387da64d6f..239d7a8cb1 100644 --- a/pages/dao/[symbol]/members/index.tsx +++ b/pages/dao/[symbol]/members/index.tsx @@ -1,10 +1,12 @@ import useRealm from '@hooks/useRealm' import Members from './Members' const MembersPage = () => { - const { realm } = useRealm() + const { config } = useRealm() return (
- {!realm?.account.config.useCommunityVoterWeightAddin ? : null} + {!config?.account.communityTokenConfig.voterWeightAddin ? ( + + ) : null}
) } diff --git a/pages/dao/[symbol]/params/index.tsx b/pages/dao/[symbol]/params/index.tsx index 03202e763e..fbc590f485 100644 --- a/pages/dao/[symbol]/params/index.tsx +++ b/pages/dao/[symbol]/params/index.tsx @@ -27,7 +27,7 @@ import Tooltip from '@components/Tooltip' import { AccountType } from '@utils/uiTypes/assets' const Params = () => { - const { realm, mint } = useRealm() + const { realm, mint, config } = useRealm() const wallet = useWalletStore((s) => s.current) const { canUseAuthorityInstruction, @@ -248,14 +248,14 @@ const Params = () => { padding label="Use community voter weight add-in" val={getYesNoString( - realmConfig?.useCommunityVoterWeightAddin + config?.account.communityTokenConfig.voterWeightAddin )} />
diff --git a/stores/useWalletStore.tsx b/stores/useWalletStore.tsx index b4c650ab49..2d5e398f76 100644 --- a/stores/useWalletStore.tsx +++ b/stores/useWalletStore.tsx @@ -22,7 +22,6 @@ import { RealmConfigAccount, SignatoryRecord, TokenOwnerRecord, - tryGetRealmConfig, VoteRecord, } from '@solana/spl-governance' import { ProgramAccount } from '@solana/spl-governance' @@ -43,6 +42,7 @@ import { import { accountsToPubkeyMap } from '@tools/sdk/accounts' import { HIDDEN_PROPOSALS } from '@components/instructions/tools' import { sleep } from '@blockworks-foundation/mango-client' +import { getRealmConfigAccountOrDefault } from '@tools/governance/configs' interface WalletStore extends State { connected: boolean @@ -363,7 +363,7 @@ const useWalletStore = create((set, get) => ({ realmId, realmCouncilMintPk ), - getRealmConfig(connection, programId, realmId), + getRealmConfigAccountOrDefault(connection, programId, realmId), ]) const governancesMap = accountsToPubkeyMap(governances) @@ -569,11 +569,3 @@ const useWalletStore = create((set, get) => ({ })) export default useWalletStore - -const getRealmConfig = async (connection, programId, realmId) => { - try { - return await tryGetRealmConfig(connection, programId, realmId) - } catch (e) { - return null - } -} diff --git a/tools/governance/configs.ts b/tools/governance/configs.ts index 9357e6d657..3ef62e6191 100644 --- a/tools/governance/configs.ts +++ b/tools/governance/configs.ts @@ -1,8 +1,15 @@ import { + getRealmConfigAddress, + GovernanceAccountParser, + GoverningTokenConfig, + GoverningTokenType, + ProgramAccount, PROGRAM_VERSION_V3, + RealmConfigAccount, VoteThreshold, VoteThresholdType, } from '@solana/spl-governance' +import { Connection, PublicKey } from '@solana/web3.js' export function createGovernanceThresholds( programVersion: number, @@ -40,3 +47,37 @@ export function createGovernanceThresholds( councilVetoVoteThreshold, } } + +export function createDefaultRealmConfigAccount(realmPk: PublicKey) { + const defaultTokenConfig = new GoverningTokenConfig({ + voterWeightAddin: undefined, + maxVoterWeightAddin: undefined, + tokenType: GoverningTokenType.Liquid, + reserved: new Uint8Array(), + }) + + return new RealmConfigAccount({ + realm: realmPk, + communityTokenConfig: defaultTokenConfig, + councilTokenConfig: defaultTokenConfig, + reserved: new Uint8Array(), + }) +} + +export async function getRealmConfigAccountOrDefault( + connection: Connection, + programId: PublicKey, + realmPk: PublicKey +) { + const realmConfigPk = await getRealmConfigAddress(programId, realmPk) + const accountInfo = await connection.getAccountInfo(realmConfigPk) + + return (accountInfo + ? GovernanceAccountParser(RealmConfigAccount)(realmConfigPk, accountInfo) + : // If the account doesn't exist then create a default instance + { + pubkey: realmConfigPk, + owner: programId, + account: createDefaultRealmConfigAccount(realmPk), + }) as ProgramAccount +}