diff --git a/apps/web-cheqd/src/models/gov_params/index.ts b/apps/web-cheqd/src/models/gov_params/index.ts new file mode 100644 index 0000000000..6f2f5bee25 --- /dev/null +++ b/apps/web-cheqd/src/models/gov_params/index.ts @@ -0,0 +1,51 @@ +interface Params { + quorum: string; + threshold: string; + min_deposit: { denom: string; amount: string }[]; + voting_period: number; + burn_vote_veto: boolean; + veto_threshold: string; + max_deposit_period: number; + min_initial_deposit_ratio: string; +} + +class GovParams { + public depositParams: { + minDeposit: Array<{ + denom: string; + amount: string; + }>; + maxDepositPeriod: number; + }; + + public tallyParams: { + quorum: string; + threshold: string; + vetoThreshold: string; + }; + + public votingParams: { + votingPeriod: number; + }; + + constructor(payload: Params) { + this.depositParams = { + minDeposit: payload.min_deposit, + maxDepositPeriod: payload.max_deposit_period, + }; + this.tallyParams = { + quorum: payload.quorum, + threshold: payload.threshold, + vetoThreshold: payload.veto_threshold, + }; + this.votingParams = { + votingPeriod: payload.voting_period, + }; + } + + static fromJson(data: any): GovParams { + return new GovParams(data.params); // Access 'params' object directly + } +} + +export default GovParams; diff --git a/apps/web-cheqd/src/screens/params/hooks.ts b/apps/web-cheqd/src/screens/params/hooks.ts new file mode 100644 index 0000000000..9d48a393fb --- /dev/null +++ b/apps/web-cheqd/src/screens/params/hooks.ts @@ -0,0 +1,166 @@ +import numeral from 'numeral'; +import * as R from 'ramda'; +import { useCallback, useState } from 'react'; +import chainConfig from '@/chainConfig'; +import { ParamsQuery, useParamsQuery } from '@/graphql/types/general_types'; +import { DistributionParams, GovParams, MintParams, SlashingParams, StakingParams } from '@/models'; +import type { ParamsState } from '@/screens/params/types'; +import { formatToken } from '@/utils/format_token'; + +const { primaryTokenUnit } = chainConfig(); + +const initialState: ParamsState = { + loading: true, + exists: true, + staking: null, + slashing: null, + minting: null, + distribution: null, + gov: null, +}; + +// ================================ +// staking +// ================================ +const formatStaking = (data: ParamsQuery) => { + if (data.stakingParams.length) { + const stakingParamsRaw = StakingParams.fromJson(data?.stakingParams?.[0]?.params ?? {}); + return { + bondDenom: stakingParamsRaw.bondDenom, + unbondingTime: stakingParamsRaw.unbondingTime, + maxEntries: stakingParamsRaw.maxEntries, + historicalEntries: stakingParamsRaw.historicalEntries, + maxValidators: stakingParamsRaw.maxValidators, + }; + } + + return null; +}; + +// ================================ +// slashing +// ================================ +const formatSlashing = (data: ParamsQuery) => { + if (data.slashingParams.length) { + const slashingParamsRaw = SlashingParams.fromJson(data?.slashingParams?.[0]?.params ?? {}); + return { + downtimeJailDuration: slashingParamsRaw.downtimeJailDuration, + minSignedPerWindow: slashingParamsRaw.minSignedPerWindow, + signedBlockWindow: slashingParamsRaw.signedBlockWindow, + slashFractionDoubleSign: slashingParamsRaw.slashFractionDoubleSign, + slashFractionDowntime: slashingParamsRaw.slashFractionDowntime, + }; + } + return null; +}; + +// ================================ +// minting +// ================================ +const formatMint = (data: ParamsQuery) => { + if (data.mintParams.length) { + const mintParamsRaw = MintParams.fromJson(data?.mintParams?.[0]?.params ?? {}); + + return { + blocksPerYear: mintParamsRaw.blocksPerYear, + goalBonded: mintParamsRaw.goalBonded, + inflationMax: mintParamsRaw.inflationMax, + inflationMin: mintParamsRaw.inflationMin, + inflationRateChange: mintParamsRaw.inflationRateChange, + mintDenom: mintParamsRaw.mintDenom, + }; + } + + return null; +}; + +// ================================ +// distribution +// ================================ + +const formatDistribution = (data: ParamsQuery) => { + if (data.distributionParams.length) { + const distributionParamsRaw = DistributionParams.fromJson( + data?.distributionParams?.[0]?.params ?? {} + ); + return { + baseProposerReward: distributionParamsRaw.baseProposerReward, + bonusProposerReward: distributionParamsRaw.bonusProposerReward, + communityTax: distributionParamsRaw.communityTax, + withdrawAddressEnabled: distributionParamsRaw.withdrawAddressEnabled, + }; + } + + return null; +}; + +// ================================ +// gov +// ================================ + +const formatGov = (data: ParamsQuery) => { + if (data.govParams.length) { + const govParamsRaw = GovParams.fromJson(data?.govParams?.[0] ?? {}); + return { + minDeposit: formatToken( + govParamsRaw.depositParams.minDeposit?.[0]?.amount ?? 0, + govParamsRaw.depositParams.minDeposit?.[0]?.denom ?? primaryTokenUnit + ), + maxDepositPeriod: govParamsRaw.depositParams.maxDepositPeriod, + quorum: numeral(numeral(govParamsRaw.tallyParams.quorum).format('0.[00]')).value() ?? 0, + threshold: numeral(numeral(govParamsRaw.tallyParams.threshold).format('0.[00]')).value() ?? 0, + vetoThreshold: + numeral(numeral(govParamsRaw.tallyParams.vetoThreshold).format('0.[00]')).value() ?? 0, + votingPeriod: govParamsRaw.votingParams.votingPeriod, + }; + } + + return null; +}; + +const formatParam = (data: ParamsQuery) => { + const results: Partial = {}; + + results.staking = formatStaking(data); + + results.slashing = formatSlashing(data); + + results.minting = formatMint(data); + + results.distribution = formatDistribution(data); + + results.gov = formatGov(data); + + return results; +}; + +export const useParams = () => { + const [state, setState] = useState(initialState); + + const handleSetState = useCallback((stateChange: (prevState: ParamsState) => ParamsState) => { + setState((prevState) => { + const newState = stateChange(prevState); + return R.equals(prevState, newState) ? prevState : newState; + }); + }, []); + + // ================================ + // param query + // ================================ + useParamsQuery({ + onCompleted: (data) => { + handleSetState((prevState) => ({ + ...prevState, + loading: false, + ...formatParam(data), + })); + }, + onError: () => { + handleSetState((prevState) => ({ ...prevState, loading: false })); + }, + }); + + return { + state, + }; +};