-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
79 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
53 changes: 45 additions & 8 deletions
53
packages/plugins/Snapshot/src/SiteAdaptor/hooks/usePower.ts
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 |
---|---|---|
@@ -1,17 +1,54 @@ | ||
import { useAsyncRetry } from 'react-use' | ||
import { useChainContext } from '@masknet/web3-hooks-base' | ||
import type { NetworkPluginID } from '@masknet/shared-base' | ||
import type { ProposalIdentifier } from '../../types.js' | ||
import { useProposal } from './useProposal.js' | ||
import { find, sumBy } from 'lodash-es' | ||
import { useChainContext } from '@masknet/web3-hooks-base' | ||
import { fetchJSON } from '@masknet/web3-providers/helpers' | ||
import { isSameAddress } from '@masknet/web3-shared-base' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { find, omit, sumBy } from 'lodash-es' | ||
import { useMemo } from 'react' | ||
import type { Proposal, ProposalIdentifier, ScoreResponse } from '../../types.js' | ||
import { getScores } from '../../utils.js' | ||
import { useProposal } from './useProposal.js' | ||
|
||
export function useMyScore(account: string, proposal: Proposal) { | ||
const { network, snapshot, space, strategies } = proposal | ||
const { data: score } = useQuery({ | ||
queryKey: ['snapshot', 'user-score', account, network, snapshot], | ||
queryFn: async () => { | ||
const res = await fetchJSON<ScoreResponse>('https://score.snapshot.org/api/scores', { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
params: { | ||
addresses: [account], | ||
space: space.id, | ||
network, | ||
snapshot, | ||
strategies: strategies.map((x) => omit(x, ['network', '__typename'])), | ||
}, | ||
}), | ||
}) | ||
return res.result.scores | ||
}, | ||
select(scores) { | ||
return scores.reduce((total, record) => { | ||
const entry = Object.entries(record).find(([key]) => isSameAddress(key, account)) | ||
return entry ? entry[1] + total : total | ||
}, 0) | ||
}, | ||
}) | ||
return score | ||
} | ||
|
||
export function usePower(identifier: ProposalIdentifier) { | ||
const proposal = useProposal(identifier.id) | ||
const { account } = useChainContext<NetworkPluginID.PLUGIN_EVM>() | ||
return useAsyncRetry(async () => { | ||
if (!account) return 0 | ||
const score = useMyScore(account, proposal) || 0 | ||
const votedScore = useMemo(() => { | ||
if (!account || !proposal) return 0 | ||
const scores = getScores(proposal) | ||
return sumBy(scores, (score) => find(score, (_, key) => key.toLowerCase() === account.toLowerCase()) ?? 0) | ||
return sumBy(scores, (score) => find(score, (_, key) => isSameAddress(key, account)) ?? 0) | ||
}, [account, proposal]) | ||
return score + votedScore | ||
} |
9 changes: 5 additions & 4 deletions
9
packages/plugins/Snapshot/src/SiteAdaptor/hooks/useProposal.ts
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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import { PluginSnapshotRPC } from '../../messages.js' | ||
import { useSuspenseQuery } from '@tanstack/react-query' | ||
import { PluginSnapshotRPC } from '../../messages.js' | ||
import type { Proposal } from '../../types.js' | ||
|
||
export function useProposal(id: string) { | ||
export function useProposal(id: string): Proposal { | ||
return useSuspenseQuery({ | ||
queryKey: ['plugin', 'snapshot', 'fetchProposal', id], | ||
queryFn: () => PluginSnapshotRPC.fetchProposal(id), | ||
select(proposal) { | ||
proposal.status = | ||
const status = | ||
!proposal.isStart ? 'Pending' | ||
: proposal.isEnd ? 'Closed' | ||
: 'Active' | ||
return proposal | ||
return { ...proposal, status } satisfies Proposal | ||
}, | ||
}).data | ||
} |
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