forked from forbole/big-dipper-2.0-cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: gov params [web-cheqd] (forbole#1332)
- Loading branch information
Showing
2 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |