Skip to content

Commit

Permalink
Refactor package.json and pnpm-lock.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopy1412 committed Sep 12, 2024
1 parent aaa24d5 commit 80f1abe
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 181 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"type-check": "tsc --b",
"lint": "eslint .",
"preview": "vite preview"
},
Expand Down
16 changes: 6 additions & 10 deletions src/components/collator/_hooks/collator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ type CreateAndCollatorProps = {
commission: bigint;
};
export const useCreateAndCollator = ({ enabled }: { enabled: boolean }) => {
const { address, isEnabled } = useWalletStatus();
const { writeContractAsync, ...rest } = useWriteContract();
const { currentChainId, address } = useWalletStatus();
const oldKey = genKey({ address: address as `0x${string}`, votes: 0n });
const {
data: collatorSetPrev,
isLoading: isLoadingPrev,
isRefetching: isRefetchingPrev
} = useCollatorSetPrev({
key: oldKey,
currentChainId,
enabled: !!oldKey && enabled
enabled: isEnabled && !!oldKey && enabled
});

const prev = (
Expand All @@ -46,16 +45,14 @@ export const useCreateAndCollator = ({ enabled }: { enabled: boolean }) => {
};
};

export default useCreateAndCollator;

export const useCreateCollator = ({
commission,
enabled
}: {
commission: bigint;
enabled: boolean;
}) => {
const { address, currentChainId } = useWalletStatus();
const { address, isEnabled } = useWalletStatus();
const {
data: stakedOf,
isLoading: isLoadingStakedOf,
Expand All @@ -66,7 +63,7 @@ export const useCreateCollator = ({
functionName: 'stakedOf',
args: [address as `0x${string}`],
query: {
enabled: !!address && enabled
enabled: isEnabled && enabled
}
});

Expand All @@ -80,7 +77,7 @@ export const useCreateCollator = ({
functionName: 'assetsToVotes',
args: [stakedOf ?? 0n, commission],
query: {
enabled: !!commission && !!stakedOf && enabled
enabled: isEnabled && !!commission && !!stakedOf && enabled
}
});

Expand All @@ -94,8 +91,7 @@ export const useCreateCollator = ({
isRefetching: isRefetchingPrev
} = useCollatorSetPrev({
key: oldKey,
currentChainId,
enabled: !!oldKey && enabled
enabled: isEnabled && !!oldKey && enabled
});

const prev = (
Expand Down
8 changes: 2 additions & 6 deletions src/components/collator/_hooks/commissionLockInfo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { useMemo } from 'react';
import { useCommissionLocks } from './commissionLocks';

export function useCommissionLockInfo(address?: `0x${string}`) {
const {
isLockPeriod,
isLoading: isLockPeriodLoading,
lockEndTime
} = useCommissionLocks(address as `0x${string}`);
export function useCommissionLockInfo() {
const { isLockPeriod, isLoading: isLockPeriodLoading, lockEndTime } = useCommissionLocks();

const remainingLockTime = useMemo(() => {
if (!lockEndTime) return '';
Expand Down
8 changes: 5 additions & 3 deletions src/components/collator/_hooks/commissionLocks.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useMemo } from 'react';
import { useReadContract } from 'wagmi';
import { address as hubAddress, abi as hubAbi } from '@/config/abi/hub';
import { useMemo } from 'react';
import useWalletStatus from '@/hooks/useWalletStatus';

export const useCommissionLocks = (address: `0x${string}`) => {
export const useCommissionLocks = () => {
const { isEnabled, address } = useWalletStatus();
const result = useReadContract({
address: hubAddress,
abi: hubAbi,
functionName: 'commissionLocks',
args: [address as `0x${string}`],
query: {
enabled: !!address
enabled: isEnabled
}
});

Expand Down
4 changes: 0 additions & 4 deletions src/components/collator/_hooks/update-commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { isNil } from 'lodash-es';
import { useCallback } from 'react';
import { DEFAULT_PREV } from '@/utils/getPrevNew';
import { useCollatorSetNewPrev } from '@/hooks/useService';
import useWalletStatus from '@/hooks/useWalletStatus';
import { genKey } from '@/utils';

type UpdateCommissionProps = {
Expand All @@ -22,8 +21,6 @@ const useUpdateCommission = ({
collatorAddress,
totalAssets
}: UpdateCommissionProps) => {
const { currentChainId } = useWalletStatus();

const { data: votes, isLoading: isLoadingVotes } = useReadContract({
abi: hubAbi,
address: hubAddress,
Expand All @@ -43,7 +40,6 @@ const useUpdateCommission = ({
} = useCollatorSetNewPrev({
key: newKey,
newKey,
currentChainId,
enabled: !!collatorAddress && !!newKey && !!oldKey
});

Expand Down
6 changes: 1 addition & 5 deletions src/components/collator/join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import TransactionStatus from '../transaction-status';
import { useSetSessionKey } from './_hooks/set-session-key';
import { useCreateCollator, useCreateAndCollator } from './_hooks/collator';
import { validSessionKey } from '@/utils';
import useWalletStatus from '@/hooks/useWalletStatus';
import { useCommissionLockInfo } from './_hooks/commissionLockInfo';
import { error } from '../toast';

Expand All @@ -18,7 +17,6 @@ interface CollatorJoinProps {
}

const CollatorJoin = ({ hasSessionKey, sessionKey, hasPool, refetch }: CollatorJoinProps) => {
const { address } = useWalletStatus();
const [isValidSessionKey, setIsValidSessionKey] = useState(true);
const [sessionKeyHash, setSessionKeyHash] = useState('');
const [commissionHash, setCommissionHash] = useState('');
Expand All @@ -31,9 +29,7 @@ const CollatorJoin = ({ hasSessionKey, sessionKey, hasPool, refetch }: CollatorJ
return commissionValue ? BigInt(commissionValue) : 0n;
}, [commissionValue]);

const { isLockPeriod, isLockPeriodLoading, remainingLockTime } = useCommissionLockInfo(
address as `0x${string}`
);
const { isLockPeriod, isLockPeriodLoading, remainingLockTime } = useCommissionLockInfo();

const {
createCollator,
Expand Down
16 changes: 6 additions & 10 deletions src/components/collator/manage.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Button, Tooltip } from '@nextui-org/react';
import { CircleHelp } from 'lucide-react';
import { validSessionKey } from '@/utils';
import { useCollatorByAddress, useCollatorSetPrev } from '@/hooks/useService';
import useWalletStatus from '@/hooks/useWalletStatus';
import { DEFAULT_PREV } from '@/utils/getPrevNew';
import TransactionStatus from '../transaction-status';
import StopCollation from './stop-collation';
import useStop from './_hooks/stop';
import { useSetSessionKey } from './_hooks/set-session-key';
import useUpdateCommission from './_hooks/update-commission';
import { validSessionKey } from '@/utils';
import { useCollatorByAddress, useCollatorSetPrev } from '@/hooks/useService';
import { DEFAULT_PREV } from '@/utils/getPrevNew';
import { useCommissionLockInfo } from './_hooks/commissionLockInfo';
import { error } from '../toast';
interface CollatorManagementProps {
Expand All @@ -24,7 +24,7 @@ const CollatorManagement = ({
refetch,
onStopSuccess
}: CollatorManagementProps) => {
const { address, currentChainId } = useWalletStatus();
const { address } = useWalletStatus();
const [open, setOpen] = useState(false);
const [isValidSessionKey, setIsValidSessionKey] = useState(true);
const [sessionKeyHash, setSessionKeyHash] = useState('');
Expand All @@ -34,9 +34,8 @@ const CollatorManagement = ({
const [commissionValue, setCommissionValue] = useState('');

const { data: collatorByAddress, isLoading: isLoadingCollatorByAddress } = useCollatorByAddress({
currentChainId,
address: address as `0x${string}`,
enabled: !!address
enabled: true
});
const currentCollator = collatorByAddress?.[0];
const oldKey = currentCollator?.key || '';
Expand All @@ -52,17 +51,14 @@ const CollatorManagement = ({
isRefetching: isRefetchingPrev
} = useCollatorSetPrev({
key: oldKey,
currentChainId,
enabled: !!oldKey
});

const oldPrev = (
collatorSetPrev?.[0] ? collatorSetPrev?.[0]?.address : DEFAULT_PREV
) as `0x${string}`;

const { isLockPeriod, isLockPeriodLoading, remainingLockTime } = useCommissionLockInfo(
address as `0x${string}`
);
const { isLockPeriod, isLockPeriodLoading, remainingLockTime } = useCommissionLockInfo();

const { setSessionKey, isPending: isPendingSetSessionKey } = useSetSessionKey();

Expand Down
5 changes: 1 addition & 4 deletions src/components/reserved-staking-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { useStakingAccount } from '@/hooks/useService';
import TooltipFormattedNumber from './tooltip-formatter-number';
import useWalletStatus from '@/hooks/useWalletStatus';
import { formatEther } from 'viem';
import { Skeleton } from '@nextui-org/react';

const ReservedStakingPanel = () => {
const { address, currentChainId } = useWalletStatus();
const { data, isLoading } = useStakingAccount({
address,
currentChainId
enabled: true
});
const totalAmount =
data?.reduce((acc, item) => acc + (item.assets ? BigInt(item.assets) : 0n), 0n) ?? 0n;
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useActiveCollators.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useReadContract } from 'wagmi';
import { address, abi } from '@/config/abi/collator';
import useWalletStatus from './useWalletStatus';

const useActiveCollators = ({ enabled }: { enabled: boolean }) => {
const { isEnabled } = useWalletStatus();
const result = useReadContract({
address: address,
abi: abi,
functionName: 'getActiveCollators',
args: [],
query: {
enabled,
refetchOnMount: true,
staleTime: 0
enabled: isEnabled && enabled
}
});

Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useAssetsToVotes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useReadContract } from 'wagmi';
import { abi, address } from '@/config/abi/hub';
import { isNil } from 'lodash-es';
import useWalletStatus from './useWalletStatus';

export type Operation = 'add' | 'subtract';

Expand All @@ -12,14 +13,14 @@ interface AssetsToVotesProps {
}

function useAssetsToVotes({ commission, totalAmount, inputAmount, operation }: AssetsToVotesProps) {
const { isEnabled } = useWalletStatus();
const result = useReadContract({
abi,
address,
functionName: 'assetsToVotes',
args: [commission, calculateAssets(totalAmount, inputAmount, operation)],
query: {
enabled: !isNil(totalAmount) && !isNil(inputAmount) && !!commission,
staleTime: 0
enabled: isEnabled && !isNil(totalAmount) && !isNil(inputAmount) && !!commission
}
});

Expand Down
13 changes: 4 additions & 9 deletions src/hooks/useCollatorList.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { useCallback } from 'react';
import useActiveCollators from './useActiveCollators';
import { useCollatorSet } from './useService';
import useWalletStatus from './useWalletStatus';

export const useActiveCollatorList = ({ enabled }: { enabled: boolean }) => {
const { currentChainId } = useWalletStatus();
const {
data: activeCollators,
isLoading: isActiveCollatorsLoading,
isRefetching: isActiveCollatorsRefetching,
refetch: refetchActiveCollators
} = useActiveCollators({
enabled: !!currentChainId && !!enabled
enabled
});
const {
data: list,
isLoading: isListLoading,
isRefetching: isListRefetching,
refetch: refetchList
} = useCollatorSet({
currentChainId: currentChainId,
offset: 0,
limit: activeCollators?.length || 0,
enabled: !!currentChainId && !!activeCollators && !!enabled
enabled: !!activeCollators && enabled
});

const refetch = useCallback(() => {
Expand All @@ -49,14 +46,13 @@ export const useWaitingCollatorList = ({
pageSize: number;
searchedAddress?: string;
}) => {
const { currentChainId } = useWalletStatus();
const {
data: activeCollators,
isLoading: isActiveCollatorsLoading,
isRefetching: isActiveCollatorsRefetching,
refetch: refetchActiveCollators
} = useActiveCollators({
enabled: !!currentChainId && enabled
enabled
});

const activeCollatorsCount = activeCollators?.length || 0;
Expand All @@ -68,11 +64,10 @@ export const useWaitingCollatorList = ({
isRefetching: isWaitingCollatorsRefetching,
refetch: refetchWaitingCollators
} = useCollatorSet({
currentChainId,
searchedAddress,
offset,
limit: pageSize,
enabled: !!currentChainId && !!activeCollators && enabled
enabled: !!activeCollators && enabled
});

const refetch = useCallback(() => {
Expand Down
23 changes: 0 additions & 23 deletions src/hooks/useDebounceState.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/hooks/useDebouncedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import { useDebounce } from 'react-use';

interface UseDebouncedStateProps<T> {
initialValue: T;
delay: number;
delay?: number;
}

interface UseDebouncedStateReturn<T> {
value: T;
debouncedValue: T;
setValue: (value: T) => void;
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
reset: () => void;
}

export function useDebouncedState<T>({
initialValue,
delay
delay = 500
}: UseDebouncedStateProps<T>): UseDebouncedStateReturn<T> {
const [value, setValue] = useState<T>(initialValue);
const [debouncedValue, setDebouncedValue] = useState<T>(initialValue);
Expand All @@ -40,5 +41,5 @@ export function useDebouncedState<T>({
setValue(initialValue);
setDebouncedValue(initialValue);
}, [initialValue]);
return { value, debouncedValue, handleChange, reset };
return { value, debouncedValue, setValue, handleChange, reset };
}
Loading

0 comments on commit 80f1abe

Please sign in to comment.