Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gov params [web-cheqd] #1332

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions apps/web-cheqd/src/models/gov_params/index.ts
Original file line number Diff line number Diff line change
@@ -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;
166 changes: 166 additions & 0 deletions apps/web-cheqd/src/screens/params/hooks.ts
Original file line number Diff line number Diff line change
@@ -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<ParamsState> = {};

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<ParamsState>(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,
};
};
Loading