Skip to content

Commit

Permalink
update token record preference (solana-labs#2209)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShuk authored Mar 26, 2024
1 parent e8239be commit 28072a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
22 changes: 9 additions & 13 deletions hooks/useCreateProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ export default function useCreateProposal() {
const { result: selectedGovernance } = await fetchGovernanceByPubkey(
connection.current,
governance.pubkey
)
)
const ownTokenRecord = ownVoterWeight?.councilTokenRecord ?? ownVoterWeight?.communityTokenRecord

if (!ownTokenRecord) throw new Error('token owner record does not exist')
if (!selectedGovernance) throw new Error('governance not found')
if (!realm) throw new Error()

const ownTokenRecord = ownVoterWeight?.getTokenRecordToCreateProposal(
selectedGovernance.account.config,
voteByCouncil
) // TODO just get the token record the normal way

const defaultProposalMint =
!mint?.supply.isZero() ||
config?.account.communityTokenConfig.voterWeightAddin
Expand All @@ -88,7 +86,7 @@ export default function useCreateProposal() {
rpcContext,
realm,
governance.pubkey,
ownTokenRecord!,
ownTokenRecord,
title,
description,
proposalMint,
Expand Down Expand Up @@ -134,14 +132,12 @@ export default function useCreateProposal() {
connection.current,
governance
)
const ownTokenRecord = ownVoterWeight?.councilTokenRecord ?? ownVoterWeight?.communityTokenRecord

if (!ownTokenRecord) throw new Error('token owner record does not exist')
if (!selectedGovernance) throw new Error('governance not found')
if (!realm) throw new Error()

const ownTokenRecord = ownVoterWeight?.getTokenRecordToCreateProposal(
selectedGovernance.account.config,
voteByCouncil
)

const defaultProposalMint =
!mint?.supply.isZero() ||
config?.account.communityTokenConfig.voterWeightAddin
Expand All @@ -165,7 +161,7 @@ export default function useCreateProposal() {
rpcContext,
realm,
governance,
ownTokenRecord!,
ownTokenRecord,
title,
description,
proposalMint,
Expand Down
28 changes: 27 additions & 1 deletion hub/components/EditWalletRules/RulesDetailsInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NFT_PLUGINS_PKS } from '@constants/plugins';
import { BN } from '@coral-xyz/anchor';
import { BigNumber } from 'bignumber.js';
import { produce } from 'immer';
Expand All @@ -6,6 +7,7 @@ import {
useRealmCommunityMintInfoQuery,
useRealmCouncilMintInfoQuery,
} from '@hooks/queries/mintInfo';
import { useRealmConfigQuery } from '@hooks/queries/realmConfig';
import { ButtonToggle } from '@hub/components/controls/ButtonToggle';
import { Input } from '@hub/components/controls/Input';
import { Slider } from '@hub/components/controls/Slider';
Expand Down Expand Up @@ -120,6 +122,7 @@ export function CanVote(props: Props) {
export function VotingPowerToCreateProposals(props: Props) {
const communityTokenInfo = useRealmCommunityMintInfoQuery();
const councilTokenInfo = useRealmCouncilMintInfoQuery();
const config = useRealmConfigQuery().data?.result;

const tokenInfoQuery =
props.rules.tokenType === GovernanceTokenType.Community
Expand All @@ -140,11 +143,27 @@ export function VotingPowerToCreateProposals(props: Props) {
.multipliedBy(100)
: undefined;

const isNftPlugin =
config?.account.communityTokenConfig.voterWeightAddin &&
NFT_PLUGINS_PKS.includes(
config?.account.communityTokenConfig.voterWeightAddin?.toBase58(),
);

const isNftGovernanceWithoutCouncil =
isNftPlugin && props.govPop === 'community' && !councilTokenInfo.data;

return (
<ValueBlock
title={`What is the minimum amount of ${props.govPop} governance power required to create a proposal?`}
description={`A user must have this many ${props.govPop} governance power in order to create a proposal.`}
>
{isNftGovernanceWithoutCouncil ? (
<div className="text-sm dark:text-neutral-500 relative top-[-10px]">
The council is not added. Max tokens can be 5
</div>
) : (
''
)}
<div className="relative">
<Input
className="w-full pr-24"
Expand All @@ -158,7 +177,14 @@ export function VotingPowerToCreateProposals(props: Props) {
)}
onChange={(e) => {
const text = e.currentTarget.value.replaceAll(/[^\d.-]/g, '');
const value = text ? new BigNumber(text) : new BigNumber(0);
let value = text ? new BigNumber(text) : new BigNumber(0);

if (
isNftGovernanceWithoutCouncil &&
value.isGreaterThan(new BigNumber(5))
) {
value = new BigNumber(5);
}
const newRules = produce(props.rules, (data) => {
data.votingPowerToCreateProposals = value;
});
Expand Down

0 comments on commit 28072a0

Please sign in to comment.