import { Form , FormInstance , InputNumber } from 'antd' ; import { ExplorerLink , useMint , useWallet } from '@oyster/common' ; import { Governance , Realm } from '@solana/spl-governance' ; import { TransactionInstruction } from '@solana/web3.js' ; import React from 'react' ; import { formDefaults } from '../../../../tools/forms' ; import { useAnchorIdlAddress } from '../../../../tools/anchor/anchorHooks' ; import { useRpcContext } from '../../../../hooks/useRpcContext' ; import { createSetRealmConfig } from '@solana/spl-governance' ; import Checkbox from 'antd/lib/checkbox/Checkbox' ; import { parseMintSupplyFraction , RealmMintSupplyConfigFormItem , RealmMintSupplyConfigValues , } from '../../../../components/realmMintSupplyConfigFormItem/realmMintSupplyConfigFormItem' ; import BN from 'bn.js' ; import { getMintDecimalAmountFromNatural , getMintMinAmountAsDecimal , } from '../../../../tools/units' ; import { parseMinTokensToCreate } from '../../../../components/governanceConfigFormItem/governanceConfigFormItem' ; import { ProgramAccount } from '@solana/spl-governance' ; export interface RealmConfigValues { minCommunityTokensToCreateGovernance : number | string ; communityMintDecimals : number ; } class RealmConfigFormModel { minCommunityTokensToCreateGovernance : BN ; constructor ( args ) { this . minCommunityTokensToCreateGovernance = args . minCommunityTokensToCreateGovernance ; } } export function getRealmConfig ( values ) { return new RealmConfigFormModel ( { minCommunityTokensToCreateGovernance : new BN ( values . minCommunityTokensToCreateGovernance ) , } ) ; } export const RealmConfigForm = ( { form , realm , governance , onCreateInstruction , } ) => { const idlAddress = useAnchorIdlAddress ( governance . account . governedAccount ) ; const { programId , programVersion } = useRpcContext ( ) ; const wallet = useWallet ( ) ; const communityMintInfo = useMint ( realm ?. account . communityMint ) ; if ( ! wallet ?. publicKey ) { return < div > Wallet not connected < /div>; } const onCreate = async ( values ) => { const minCommunityTokensToCreateGovernance = parseMinTokensToCreate ( values . minCommunityTokensToCreateGovernance , values . communityMintDecimals ) ; const setRealmConfigIx = await createSetRealmConfig ( programId , programVersion , realm . pubkey , governance . pubkey , values . removeCouncil === true ? undefined : realm . account . config . councilMint , parseMintSupplyFraction ( values . communityMintMaxVoteWeightFraction ) , new BN ( minCommunityTokensToCreateGovernance . toString ( ) ) , undefined , wallet . publicKey ! ) ; onCreateInstruction ( setRealmConfigIx ) ; } ; const minCommunityTokenAmount = communityMintInfo ? getMintMinAmountAsDecimal ( communityMintInfo ) : 0 ; const minCommunityTokensToCreateGovernance = communityMintInfo ? getMintDecimalAmountFromNatural ( communityMintInfo , realm . account . config . minCommunityTokensToCreateGovernance ) . toNumber ( ) : 0 ; let mintDecimals = communityMintInfo ? communityMintInfo . decimals : 0 ; return ( < Form { ... formDefaults } form = { form } onFinish = { onCreate } initialValues = { { idlAccount : idlAddress } } > < Form . Item label = "program id" > < ExplorerLink address = { programId } type = "address" / > < /Form.Item> < Form . Item label = "realm" > < ExplorerLink address = { realm . pubkey } type = "address" / > < /Form.Item> < Form . Item label = "realm authority (governance account)" > < ExplorerLink address = { governance . pubkey } type = "address" / > < /Form.Item> { communityMintInfo && ( < > < Form . Item label = "min community tokens to create governance" name = "minCommunityTokensToCreateGovernance" rules = { [ { required : true } ] } initialValue = { minCommunityTokensToCreateGovernance } > < InputNumber min = { minCommunityTokenAmount } step = { minCommunityTokenAmount } style = { { width : 200 } } stringMode = { mintDecimals !== 0 } /> < /Form.Item> < Form . Item hidden name = "communityMintDecimals" initialValue = { communityMintInfo . decimals } > < /Form.Item> < />)} { realm . account . config . councilMint && ( < Form . Item label = "remove council" name = "removeCouncil" valuePropName = "checked" > < Checkbox > < /Checkbox> < /Form.Item>)} < RealmMintSupplyConfigFormItem communityMintAddress = { realm . account . communityMint } maxVoteWeightSource = { realm . account . config . communityMintMaxVoteWeightSource } > < /RealmMintSupplyConfigFormItem> < /Form>); } ;