Skip to content

Commit

Permalink
fix(senate): update to new api (#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimakorzhovnik authored Nov 2, 2024
1 parent ebb7acf commit 5a98611
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 300 deletions.
32 changes: 13 additions & 19 deletions src/constants/defaultNetworks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NetworkConfig, Networks } from 'src/types/networks';

type NetworksList = {
[key in Networks.BOSTROM | Networks.SPACE_PUSSY]: NetworkConfig;
[key in Networks]: NetworkConfig;
};

const defaultNetworks: NetworksList = {
Expand All @@ -17,24 +17,18 @@ const defaultNetworks: NetworksList = {
BECH32_PREFIX: 'bostrom',
MEMO_KEPLR: '[bostrom] cyb.ai, using keplr',
},
// localbostrom: {
// CHAIN_ID: 'localbostrom',
// BASE_DENOM: 'boot',
// DENOM_LIQUID: 'hydrogen',
// RPC_URL: 'https://rpc.bostrom.cybernode.ai',
// LCD_URL: 'https://lcd.bostrom.cybernode.ai',
// WEBSOCKET_URL: 'wss://rpc.bostrom.cybernode.ai/websocket',
// INDEX_HTTPS: 'https://index.bostrom.cybernode.ai/v1/graphql',
// INDEX_WEBSOCKET: 'wss://index.bostrom.cybernode.ai/v1/graphql',
// BECH32_PREFIX: 'bostrom',
// MEMO_KEPLR: '[bostrom] cyb.ai, using keplr',
// },
// RPC_URL=https://rpc.bostrom.moon.cybernode.ai
// LCD_URL=https://lcd.bostrom.moon.cybernode.ai
// WEBSOCKET_URL=wss://rpc.bostrom.moon.cybernode.ai/websocket
// INDEX_HTTPS=https://index.bostrom.moon.cybernode.ai/v1/graphql
// INDEX_WEBSOCKET = wss://index.bostrom.moon.cybernode.ai/v1/graphql
// CHAIN_ID = localbostrom
localbostrom: {
CHAIN_ID: Networks.LOCAL_BOSTROM,
BASE_DENOM: 'boot',
DENOM_LIQUID: 'hydrogen',
RPC_URL: 'https://rpc.bostrom.moon.cybernode.ai',
LCD_URL: 'https://lcd.bostrom.moon.cybernode.ai',
WEBSOCKET_URL: 'wss://rpc.bostrom.moon.cybernode.ai/websocket',
INDEX_HTTPS: 'https://index.bostrom.moon.cybernode.ai/v1/graphql',
INDEX_WEBSOCKET: 'wss://index.bostrom.moon.cybernode.ai/v1/graphql',
BECH32_PREFIX: 'bostrom',
MEMO_KEPLR: '[bostrom] cyb.ai, using keplr',
},

'space-pussy': {
CHAIN_ID: Networks.SPACE_PUSSY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const findSelectAppByUrl = (
const isRobot =
url.includes('@') || url.includes('neuron/') || url.includes('robot');
const isOracle = url.includes('oracle');
const isSenate = url.includes('senate');
const isCyberver = url.includes('cyberver');

const itemsMenuObj = reduceRobotSubItems(passport, address);
Expand All @@ -25,6 +26,10 @@ const findSelectAppByUrl = (
pathname = routes.oracle.path;
}

if (isSenate) {
pathname = routes.senate.path;
}

if (isCyberver) {
pathname = '/cyberver';
}
Expand Down
5 changes: 3 additions & 2 deletions src/containers/governance/actionBarDatail.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import BigNumber from 'bignumber.js';
import { NumericFormat } from 'react-number-format';
import { ActionBar, Pane } from '@cybercongress/gravity';
import { Pane } from '@cybercongress/gravity';
import { coins } from '@cosmjs/launchpad';
import { useSigningClient } from 'src/contexts/signerClient';
import {
Expand All @@ -27,6 +27,7 @@ import {
BtnGrd,
DenomArr,
Select,
ActionBar,
} from '../../components';

// import styles from './ActionBarDetail.module.scss';
Expand All @@ -46,7 +47,7 @@ const {

type Props = {
proposals: any;
id: number;
id: string;
update: () => void;
};

Expand Down
15 changes: 5 additions & 10 deletions src/containers/governance/components/card.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Pane } from '@cybercongress/gravity';
import {
Votes,
IconStatus,
Tooltip,
ContainerGradientText,
} from '../../../components';
import Display from 'src/components/containerGradient/Display/Display';
import { ProposalStatus } from 'cosmjs-types/cosmos/gov/v1beta1/gov';
import { Votes, IconStatus, Tooltip } from '../../../components';

const textPropsImg = require('../../../image/reader-outline.svg');
const paramChangePropsImg = require('../../../image/cog-outline.svg');
Expand All @@ -20,10 +15,10 @@ const finalTallyResult = (item) => {
noWithVeto: 0,
};
let finalTotalVotes = 0;
const yes = parseFloat(item.yes);
const abstain = parseFloat(item.abstain);
const no = parseFloat(item.no);
const noWithVeto = parseFloat(item.no_with_veto);
const yes = parseFloat(item.yes_count);
const abstain = parseFloat(item.abstain_count);
const no = parseFloat(item.no_count);
const noWithVeto = parseFloat(item.no_with_veto_count);

finalTotalVotes = yes + abstain + no + noWithVeto;
if (finalTotalVotes !== 0) {
Expand Down
18 changes: 12 additions & 6 deletions src/containers/governance/governance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { useQuery } from '@tanstack/react-query';
import Loader2 from 'src/components/ui/Loader2';
import { routes } from 'src/routes';
import { getProposals } from '../../utils/governance';
import Columns from './components/columns';
import { AcceptedCard, ActiveCard, RejectedCard } from './components/card';
import { ActionBar, CardStatisics } from '../../components';
import { formatNumber, coinDecimals } from '../../utils/utils';
import styles from './components/styles.module.scss';
import Columns from './components/columns';

type KeyOfProposalStatus = keyof typeof ProposalStatus;

Expand Down Expand Up @@ -74,17 +74,23 @@ function ProposalWrapper({
// ProposalsData['proposals'] extends (infer U)[] ? U : never
const mapProposalToCard = (proposal: any) => {
const {
proposal_id,
content,
id,
total_deposit,
status,
deposit_end_time,
voting_end_time,
final_tally_result,
title,
messages,
} = proposal;

const type = messages[0].content
? messages[0].content['@type']
: messages[0]['@type'];

return {
proposalId: proposal_id,
title: content.title || '<not set>',
proposalId: id,
title: title || '<not set>',
totalDeposit: total_deposit,
state: ProposalStatus[status as KeyOfProposalStatus],
timeEndDeposit: deposit_end_time
Expand All @@ -95,7 +101,7 @@ const mapProposalToCard = (proposal: any) => {
: undefined,
amounts: total_deposit[0] || undefined,
votes: final_tally_result || undefined,
type: content['@type'] || undefined,
type,
};
};

Expand Down
87 changes: 87 additions & 0 deletions src/containers/governance/hooks/useGetPropById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Coin } from '@cosmjs/proto-signing';
import { ProposalStatus } from '@cybercongress/cyber-ts/cosmos/gov/v1/gov';
import { useQuery } from '@tanstack/react-query';
import { getProposalsDetail } from 'src/utils/governance';

type KeyOfProposalStatus = keyof typeof ProposalStatus;

const reduceChanges = (messages: any) => {
if (messages.params) {
return messages.params;
}

if (messages.content && messages.content.changes) {
const { changes } = messages.content;
return changes.reduce((acc, item) => {
return { ...acc, [item.key]: item.value };
}, {});
}

return undefined;
};

const decodeProps = (proposal: any) => {
const { title, summary, status, messages } = proposal;

const type = messages[0].content
? messages[0].content['@type']
: messages[0]['@type'];

const proposer = !proposal.proposer.length
? messages[0].authority
: proposal.proposer;

const plan = messages[0].content ? messages[0].content.plan : undefined;
const recipient = messages[0].content
? messages[0].content.recipient
: undefined;

const amount =
messages[0].content && messages[0].content.amount
? messages[0].content.amount[0]
: undefined;

return {
type,
status: ProposalStatus[status as KeyOfProposalStatus],
title: title || '<not set>',
summary,
totalDeposit: proposal.total_deposit[0],
proposer,
changes: reduceChanges(messages[0]),
plan,
recipient,
amount,
};
};

type ProposalsById = {
status: ProposalStatus;
type: string;
title: string;
summary: string;
totalDeposit: Coin;
proposer: string;
recipient?: string;
amount?: Coin;
changes?: string;
plan?: any;
};

function useGetPropById(proposalId?: string): {
isLoading: boolean;
data?: ProposalsById;
refetch: () => void;
} {
const { data, isLoading, refetch } = useQuery(
['getProposalsDetail', proposalId],
async () => {
return getProposalsDetail(proposalId);
},
{ enabled: Boolean(proposalId) }
);

return { isLoading, data: data ? decodeProps(data) : undefined, refetch };
}

export default useGetPropById;
Empty file.
88 changes: 88 additions & 0 deletions src/containers/governance/hooks/useTallyResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import BigNumber from 'bignumber.js';
// import { QueryTallyResultResponse } from 'cosmjs-types/cosmos/gov/v1beta1/query';
import { useMemo } from 'react';
import { useCyberClient } from 'src/contexts/queryCyberClient';

const finalTallyResult = (item) => {
const finalVotes = {
yes: 0,
no: 0,
abstain: 0,
noWithVeto: 0,
finalTotalVotes: 0,
};
let finalTotalVotes = 0;
const yes = parseInt(item.yesCount, 10);
const abstain = parseInt(item.abstainCount, 10);
const no = parseInt(item.noCount, 10);
const noWithVeto = parseInt(item.noWithVetoCount, 10);

finalTotalVotes = yes + abstain + no + noWithVeto;
if (finalTotalVotes !== 0) {
finalVotes.yes = (yes / finalTotalVotes) * 100;
finalVotes.no = (no / finalTotalVotes) * 100;
finalVotes.abstain = (abstain / finalTotalVotes) * 100;
finalVotes.noWithVeto = (noWithVeto / finalTotalVotes) * 100;
finalVotes.finalTotalVotes = finalTotalVotes;
}

return finalVotes;
};

function useTallyResult(id?: string) {
const { hooks } = useCyberClient();

const { data: tallyResponse, refetch } = hooks.cosmos.gov.v1.useTallyResult({
request: { proposalId: id },
options: { enabled: Boolean(id) },
});
const { data: stakingPool } = hooks.cosmos.staking.v1beta1.usePool({
request: {},
});
const { data: tallyingResponse } = hooks.cosmos.gov.v1.useParams({
request: { paramsType: 'tallying' },
});

const tallyResultData = useMemo(() => {
let tally = {
participation: 0,
yes: 0,
abstain: 0,
no: 0,
noWithVeto: 0,
};

if (!tallyResponse || !stakingPool) {
return tally;
}

const tallyTemp = finalTallyResult(tallyResponse.tally);

const participation = new BigNumber(tallyTemp.finalTotalVotes)
.dividedBy(stakingPool.pool.bondedTokens)
.multipliedBy(100)
.toNumber();

tally = { ...tally, ...tallyTemp, participation };

return tally;
}, [tallyResponse, stakingPool]);

const tallyingData = useMemo(() => {
let tallying = { quorum: '0', threshold: '0', vetoThreshold: '0' };

if (tallyingResponse) {
tallying = { ...tallying, ...tallyingResponse.tallyParams };
}

return tallying;
}, [tallyingResponse]);

return {
refetch,
tallyResult: tallyResultData,
tallying: tallyingData,
};
}

export default useTallyResult;
Loading

0 comments on commit 5a98611

Please sign in to comment.