import { PublicKey } from '@solana/web3.js' ; import { Form , InputNumber , Space , Typography } from 'antd' ; import React , { useState } from 'react' ; import { contexts } from '@oyster/common' ; import { formatPercentage , getMintMinAmountAsDecimal , getMintSupplyAsDecimal , getMintSupplyFractionAsDecimalPercentage , getMintSupplyPercentageAsDecimal , } from '../../tools/units' ; import { parseMinTokensToCreate } from '../governanceConfigFormItem/governanceConfigFormItem' ; const { useMint } = contexts . Accounts ; const { Text } = Typography ; export function RealmMintTokensFormItem ( { communityMintAddress , } ) { const communityMintInfo = useMint ( communityMintAddress ) ; const [ minTokensPercentage , setMinTokensPercentage ] = useState < number | undefined > ( ) ; if ( ! communityMintInfo || ! communityMintAddress ) { if ( minTokensPercentage ) { setMinTokensPercentage ( undefined ) ; } return ( < Form . Item label = "min community tokens to create governance" > < InputNumber style = { { width : 200 } } disabled = { true } / > < /Form.Item>); } let mintDecimals = communityMintInfo . decimals ; let mintSupply1Percent = getMintSupplyPercentageAsDecimal ( communityMintInfo , 1 ) ; let minTokenAmount = getMintMinAmountAsDecimal ( communityMintInfo ) ; const maxTokenAmount = ! communityMintInfo . supply . isZero ( ) ? getMintSupplyAsDecimal ( communityMintInfo ) : undefined ; let minTokenStep = Math . max ( mintSupply1Percent , minTokenAmount ) ; let minTokensToCreateProposal = minTokenStep ; const getMinTokensPercentage = ( amount ) => getMintSupplyFractionAsDecimalPercentage ( communityMintInfo , amount ) ; const onMinTokensChange = ( minTokensToCreateProposal ) => { const minTokens = parseMinTokensToCreate ( minTokensToCreateProposal , mintDecimals ) ; setMinTokensPercentage ( getMinTokensPercentage ( minTokens ) ) ; } ; if ( ! minTokensPercentage ) { onMinTokensChange ( minTokensToCreateProposal ) ; } return ( < > < Form . Item label = "min community tokens to create governance" > < Space align = "end" > < Form . Item name = "minTokensToCreateGovernance" rules = { [ { required : true } ] } initialValue = { minTokensToCreateProposal } noStyle > < InputNumber min = { minTokenAmount } max = { maxTokenAmount } step = { minTokenStep } onChange = { onMinTokensChange } style = { { width : 200 } } stringMode = { mintDecimals !== 0 } /> < /Form.Item> { maxTokenAmount && minTokensPercentage && ( < Text type = "secondary" > { `${ formatPercentage ( minTokensPercentage ) } of token supply` } < /Text>)} < /Space> < /Form.Item> < Form . Item hidden name = "mintDecimals" initialValue = { mintDecimals } > < /Form.Item> < />); }