From 40d4ef9cae0220cbfab7c81637bf080b4513d540 Mon Sep 17 00:00:00 2001 From: snoopy1412 Date: Wed, 25 Sep 2024 19:19:21 +0800 Subject: [PATCH 1/2] Refactor useAssetsToVotes and useService hooks --- src/hooks/useAssetsToVotes.ts | 16 +++++++++ src/hooks/useService.ts | 24 ++++++++++++++ src/hooks/useStakeOldAndNewPrev.ts | 52 ++++++++++++++++-------------- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/hooks/useAssetsToVotes.ts b/src/hooks/useAssetsToVotes.ts index 2273d93..c1bc65c 100644 --- a/src/hooks/useAssetsToVotes.ts +++ b/src/hooks/useAssetsToVotes.ts @@ -1,5 +1,7 @@ import { useReadContract } from 'wagmi'; import { abi, address } from '@/config/abi/hub'; +import { readContract } from '@wagmi/core'; +import { config } from '@/config/wagmi'; export type Operation = 'add' | 'subtract'; @@ -24,6 +26,20 @@ function useAssetsToVotes({ commission, totalAmount, inputAmount, operation }: A return result; } +export async function assetsToVotes( + commission: bigint, + totalAmount: bigint, + inputAmount: bigint, + operation: Operation +): Promise { + return await readContract(config, { + abi, + address, + functionName: 'assetsToVotes', + args: [commission, calculateAssets(totalAmount, inputAmount, operation)] + }); +} + function calculateAssets(totalAmount: bigint, inputAmount: bigint, operation: Operation): bigint { if (operation === 'add') { return (totalAmount || 0n) + (inputAmount || 0n); diff --git a/src/hooks/useService.ts b/src/hooks/useService.ts index 84388c1..c9d1836 100644 --- a/src/hooks/useService.ts +++ b/src/hooks/useService.ts @@ -175,6 +175,30 @@ export function useCollatorByAddress({ address, enabled = true }: CollatorByAddr }); } +export const fetchCollatorByAddress = async ({ + address, + currentChainId +}: { + address: `0x${string}`; + currentChainId: number; +}) => { + const params: CollatorSetQueryParams = { + where: { + ...(address ? { address: toLowerCase(address) } : {}), + inset: 1 + }, + orderBy: 'key', + orderDirection: 'desc', + skip: 0, + first: 1 + }; + const result = await fetchCollatorSet(params, currentChainId!); + if (result === null) { + throw new Error('Failed to fetch collator set'); + } + return result; +}; + type CollatorSetByAccountsParams = { accounts: `0x${string}`[]; enabled?: boolean; diff --git a/src/hooks/useStakeOldAndNewPrev.ts b/src/hooks/useStakeOldAndNewPrev.ts index 9e23f55..755fa0e 100644 --- a/src/hooks/useStakeOldAndNewPrev.ts +++ b/src/hooks/useStakeOldAndNewPrev.ts @@ -1,6 +1,10 @@ import { DEFAULT_PREV } from '@/utils/getPrevNew'; -import useAssetsToVotes, { Operation } from './useAssetsToVotes'; -import { fetchCollatorSetNewPrev, fetchCollatorSetPrev } from './useService'; +import { assetsToVotes, Operation } from './useAssetsToVotes'; +import { + fetchCollatorByAddress, + fetchCollatorSetNewPrev, + fetchCollatorSetPrev +} from './useService'; import { genKey } from '@/utils'; import { CollatorSet } from '@/service/type'; import { useCallback, useState } from 'react'; @@ -22,41 +26,41 @@ export function useStakeOldAndNewPrev({ operation = 'add' }: UseOldAndNewPrevProps) { const { currentChainId } = useWalletStatus(); - const commission = BigInt(collator?.commission || 0); - const totalAmount = BigInt(collator?.assets || 0); const collatorAddress = collator?.address || ''; - const oldKey = collator?.key || ''; const [isLoading, setIsLoading] = useState(false); - const { refetch: refetchAssetsToVotes, data } = useAssetsToVotes({ - commission, - totalAmount, - inputAmount, - operation - }); - - console.log('data', data); const getPrevAndNewPrev: () => Promise = useCallback(async () => { - setIsLoading(true); - - if (!oldKey || !collatorAddress) { - setIsLoading(true); + if (!collatorAddress) { return { oldPrev: DEFAULT_PREV, newPrev: DEFAULT_PREV }; } + setIsLoading(true); + const newCollator = await fetchCollatorByAddress({ + address: collatorAddress as `0x${string}`, + currentChainId: currentChainId! + }); + if (!newCollator) { + setIsLoading(false); + return { oldPrev: DEFAULT_PREV, newPrev: DEFAULT_PREV }; + } + + const commission = BigInt(newCollator?.[0]?.commission || 0); + const totalAmount = BigInt(newCollator?.[0]?.assets || 0); + const oldKey = newCollator?.[0]?.key || ''; const [assetsToVotesResult, collatorSetPrev] = await Promise.all([ - refetchAssetsToVotes(), - fetchCollatorSetPrev({ key: oldKey, collatorAddress, currentChainId: currentChainId! }) + assetsToVotes(commission, totalAmount, inputAmount, operation), + fetchCollatorSetPrev({ + key: oldKey, + collatorAddress, + currentChainId: currentChainId! + }) ]); - console.log('assetsToVotesResult?.data ', assetsToVotesResult?.data); - - const newKey = genKey({ address: collatorAddress, votes: assetsToVotesResult?.data ?? 0n }); - console.log('newKey', newKey); + const newKey = genKey({ address: collatorAddress, votes: assetsToVotesResult ?? 0n }); if (!newKey) { setIsLoading(false); @@ -78,7 +82,7 @@ export function useStakeOldAndNewPrev({ ? (collatorSetNewPrev?.[0]?.address as `0x${string}`) : DEFAULT_PREV }; - }, [refetchAssetsToVotes, collatorAddress, oldKey, currentChainId]); + }, [collatorAddress, currentChainId, inputAmount, operation]); return { getPrevAndNewPrev, From d9efc640b9c758e58a5dfe849da3f02ae6a9199c Mon Sep 17 00:00:00 2001 From: snoopy1412 Date: Wed, 25 Sep 2024 19:36:11 +0800 Subject: [PATCH 2/2] Refactor useService and useStakingAccount hooks, add orderBy and orderDirection parameters --- src/hooks/useService.ts | 4 +++- src/service/services.ts | 3 --- src/service/type.ts | 8 +++++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/hooks/useService.ts b/src/hooks/useService.ts index c9d1836..4262eeb 100644 --- a/src/hooks/useService.ts +++ b/src/hooks/useService.ts @@ -239,7 +239,9 @@ export function useStakingAccount({ enabled = true }: StakingAccountParams) { const params: StakingAccountQueryParams = { where: { account: toLowerCase(address) - } + }, + orderBy: 'latestChangeTimestamp', + orderDirection: 'desc' }; const queryKey = ['stakingAccounts', params]; const result = useQuery({ diff --git a/src/service/services.ts b/src/service/services.ts index b4c5385..c574378 100644 --- a/src/service/services.ts +++ b/src/service/services.ts @@ -46,10 +46,7 @@ export async function fetchStakingAccount( params: StakingAccountQueryParams, chainId: ChainId ): Promise { - console.log('fetchStakingAccount', chainId); - const client = getClient(chainId); - console.log('client', client); try { const response = await client.request<{ stakingAccounts: StakingAccount[] }>( GET_STAKING_ACCOUNT, diff --git a/src/service/type.ts b/src/service/type.ts index d528c51..7b6f520 100644 --- a/src/service/type.ts +++ b/src/service/type.ts @@ -210,7 +210,13 @@ export interface CollatorSetQueryParams { export type CollatorSetQueryFunction = (params: CollatorSetQueryParams) => Promise; -export type StakingAccountOrderByField = 'id' | 'pool' | 'collator' | 'account' | 'assets'; +export type StakingAccountOrderByField = + | 'id' + | 'pool' + | 'collator' + | 'account' + | 'assets' + | 'latestChangeTimestamp'; export interface StakingAccount { account: `0x${string}`; assets: string;