-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(senate): update to new api (#1343)
- Loading branch information
1 parent
ebb7acf
commit 5a98611
Showing
12 changed files
with
331 additions
and
300 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
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
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
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
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
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,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.
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,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; |
Oops, something went wrong.