Skip to content

Commit

Permalink
Use realm config account instead of flags (solana-labs#970)
Browse files Browse the repository at this point in the history
* chore: Use communityTokenConfig instead of useCommunityVoterWeightAddin

* chore: Use communityTokenConfig instead of useMaxCommunityVoterWeightAddin
  • Loading branch information
SebastianBor authored Aug 19, 2022
1 parent c842f97 commit 4edc99e
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Strategies/components/MangoDepositComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const MangoDepositComponent = ({
mint,
councilMint,
symbol,
config,
} = useRealm()
const [isDepositing, setIsDepositing] = useState(false)
const [
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Strategies/components/everlend/EverlendDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const EverlendDeposit = ({
councilMint,
ownVoterWeight,
symbol,
config,
} = useRealm()
const [voteByCouncil, setVoteByCouncil] = useState(false)
const client = useVotePluginsClientStore(
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Strategies/components/everlend/EverlendWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const EverlendWithdraw = ({
councilMint,
ownVoterWeight,
symbol,
config,
} = useRealm()
const { canUseTransferInstruction } = useGovernanceAssets()
const [voteByCouncil, setVoteByCouncil] = useState(false)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Strategies/components/solend/SolendDeposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const SolendDeposit = ({
mint,
councilMint,
symbol,
config,
} = useRealm()
const [isDepositing, setIsDepositing] = useState(false)
const [deposits, setDeposits] = useState<{
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Strategies/components/solend/SolendWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const SolendWithdraw = ({
mint,
councilMint,
symbol,
config,
} = useRealm()
const [isWithdrawing, setIsWithdrawing] = useState(false)
const [voteByCouncil, setVoteByCouncil] = useState(false)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions VoteStakeRegistry/components/Account/LockTokensAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DepositBox[]>([])
Expand Down Expand Up @@ -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
) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const LockPluginTokenBalanceCard = ({
proposal?: Option<Proposal>
}) => {
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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions components/Members/useMembers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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([])
Expand Down
2 changes: 1 addition & 1 deletion components/RealmHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const RealmHeader = () => {
<div className="w-40 h-10 rounded-md animate-pulse bg-bkg-3" />
)}
<div className="flex items-center space-x-4">
{!realm?.account.config.useCommunityVoterWeightAddin && (
{!config?.account.communityTokenConfig.voterWeightAddin && (
<Link href={fmtUrlWithCluster(`/dao/${symbol}/members`)}>
<a className="flex items-center text-sm cursor-pointer default-transition text-fgd-2 hover:text-fgd-3">
<UsersIcon className="flex-shrink-0 w-5 h-5 mr-1" />
Expand Down
4 changes: 2 additions & 2 deletions components/TokenBalance/NftBalanceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion hooks/useCreateProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function useCreateProposal() {
mint,
councilMint,
canChooseWhoVote,
config,
} = useRealm()
const { getRpcContext } = useRpcContext()
const handleCreateProposal = async ({
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hooks/useRealm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 6 additions & 3 deletions hooks/useTreasuryInfo/assembleWallets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -41,6 +42,7 @@ export const assembleWallets = async (
councilMint?: MintInfo,
communityMint?: MintInfo,
realm?: ProgramAccount<Realm>,
realmConfig?: ProgramAccount<RealmConfigAccount>,
realmInfo?: RealmInfo
) => {
const walletMap: { [address: string]: Wallet } = {}
Expand Down Expand Up @@ -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 ? (
<img src={realmInfo.ogImage} />
Expand Down
3 changes: 2 additions & 1 deletion hooks/useTreasuryInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Data {
}

export default function useTreasuryInfo(): Result<Data> {
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(
Expand Down Expand Up @@ -79,6 +79,7 @@ export default function useTreasuryInfo(): Result<Data> {
councilMint,
mint,
realm,
config,
realmInfo
)
}
Expand Down
23 changes: 13 additions & 10 deletions pages/api/daoStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pages/dao/[symbol]/members/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import useRealm from '@hooks/useRealm'
import Members from './Members'
const MembersPage = () => {
const { realm } = useRealm()
const { config } = useRealm()
return (
<div>
{!realm?.account.config.useCommunityVoterWeightAddin ? <Members /> : null}
{!config?.account.communityTokenConfig.voterWeightAddin ? (
<Members />
) : null}
</div>
)
}
Expand Down
6 changes: 3 additions & 3 deletions pages/dao/[symbol]/params/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -248,14 +248,14 @@ const Params = () => {
padding
label="Use community voter weight add-in"
val={getYesNoString(
realmConfig?.useCommunityVoterWeightAddin
config?.account.communityTokenConfig.voterWeightAddin
)}
/>
<AddressField
padding
label="Use max community voter weight add-in"
val={getYesNoString(
realmConfig?.useMaxCommunityVoterWeightAddin
config?.account.communityTokenConfig.maxVoterWeightAddin
)}
/>
<div className="flex">
Expand Down
12 changes: 2 additions & 10 deletions stores/useWalletStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
RealmConfigAccount,
SignatoryRecord,
TokenOwnerRecord,
tryGetRealmConfig,
VoteRecord,
} from '@solana/spl-governance'
import { ProgramAccount } from '@solana/spl-governance'
Expand All @@ -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
Expand Down Expand Up @@ -363,7 +363,7 @@ const useWalletStore = create<WalletStore>((set, get) => ({
realmId,
realmCouncilMintPk
),
getRealmConfig(connection, programId, realmId),
getRealmConfigAccountOrDefault(connection, programId, realmId),
])

const governancesMap = accountsToPubkeyMap(governances)
Expand Down Expand Up @@ -569,11 +569,3 @@ const useWalletStore = create<WalletStore>((set, get) => ({
}))

export default useWalletStore

const getRealmConfig = async (connection, programId, realmId) => {
try {
return await tryGetRealmConfig(connection, programId, realmId)
} catch (e) {
return null
}
}
Loading

0 comments on commit 4edc99e

Please sign in to comment.