Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues mentioned in #9 #10

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ VITE_PROJECT_ID = 2719448e2ce94fdd269a3c8587123bcc
VITE_DEPLOYMENT_MODE = testnet
VITE_APP_NAME = "Collator Staking - RingDAO"
VITE_APP_DESCRIPTION = "Elevate your earnings and effortlessly manage your staking positions with the RingDAO collator staking page."
VITE_KOI_GRAPHQL_API_URL = "https://thegraph.darwinia.network/dip7/subgraphs/name/dip7index-koi"
VITE_KOI_GRAPHQL_API_URL = "https://thegraph-g2.darwinia.network/training/subgraphs/name/dip7index-koi-kv"
VITE_CRAB_GRAPHQL_API_URL = "https://thegraph.darwinia.network/dip7/subgraphs/name/dip7index-crab"
VITE_DARWINIA_GRAPHQL_API_URL = "https://thegraph.darwinia.network/dip7/subgraphs/name/dip7index-darwinia"

11 changes: 8 additions & 3 deletions src/components/collator/join.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button, Tooltip } from '@nextui-org/react';
import { CircleHelp } from 'lucide-react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import useCheckWaitingIndexing from '@/hooks/useWaitingIndexing';
import TransactionStatus from '../transaction-status';
import { useSetSessionKey } from './_hooks/set-session-key';
import { useCreateCollator, useCreateAndCollator } from './_hooks/collator';
Expand All @@ -21,7 +22,7 @@ const CollatorJoin = ({ hasSessionKey, sessionKey, hasPool, refetch }: CollatorJ
const [commissionHash, setCommissionHash] = useState('');
const [sessionKeyValue, setSessionKeyValue] = useState('');
const [commissionValue, setCommissionValue] = useState('');

const { checkWaitingIndexing, isLoading: isLoadingWaitingIndexing } = useCheckWaitingIndexing();
const { setSessionKey, isPending: isPendingSetSessionKey } = useSetSessionKey();

const commission = useMemo(() => {
Expand Down Expand Up @@ -84,6 +85,10 @@ const CollatorJoin = ({ hasSessionKey, sessionKey, hasPool, refetch }: CollatorJ
}, []);

const handleSetCommission = useCallback(async () => {
const { isDeployed } = await checkWaitingIndexing();
if (!isDeployed) {
return;
}
if (hasPool) {
const tx = await createCollator({
commission
Expand All @@ -103,7 +108,7 @@ const CollatorJoin = ({ hasSessionKey, sessionKey, hasPool, refetch }: CollatorJ
setCommissionHash(tx);
}
}
}, [hasPool, commission, createCollator, createAndCollator]);
}, [hasPool, commission, createCollator, createAndCollator, checkWaitingIndexing]);

const handleSetCommissionSuccess = useCallback(() => {
setCommissionHash('');
Expand Down Expand Up @@ -239,7 +244,7 @@ const CollatorJoin = ({ hasSessionKey, sessionKey, hasPool, refetch }: CollatorJ
className="h-[2.125rem] w-full"
isDisabled={!hasSessionKey || !commissionValue || commission < 0n}
onClick={handleSetCommission}
isLoading={isSetCommissionLoading || isLockPeriodLoading}
isLoading={isSetCommissionLoading || isLockPeriodLoading || isLoadingWaitingIndexing}
>
{hasPool ? 'Collate' : 'Create Nomination Pool & Collate'}
</Button>
Expand Down
20 changes: 15 additions & 5 deletions src/components/collator/manage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CircleHelp } from 'lucide-react';
import { validSessionKey } from '@/utils';
import { fetchCollatorSetPrev, useCollatorByAddress } from '@/hooks/useService';
import useWalletStatus from '@/hooks/useWalletStatus';
import useCheckWaitingIndexing from '@/hooks/useWaitingIndexing';
import { DEFAULT_PREV } from '@/utils/getPrevNew';
import TransactionStatus from '../transaction-status';
import StopCollation from './stop-collation';
Expand Down Expand Up @@ -34,7 +35,7 @@ const CollatorManagement = ({
const [sessionKeyValue, setSessionKeyValue] = useState('');
const [commissionValue, setCommissionValue] = useState('');
const [isLoadingPrev, setIsLoadingPrev] = useState(false);

const { checkWaitingIndexing, isLoading: isLoadingWaitingIndexing } = useCheckWaitingIndexing();
const { data: collatorByAddress, isLoading: isLoadingCollatorByAddress } = useCollatorByAddress({
address: address as `0x${string}`,
enabled: true
Expand Down Expand Up @@ -131,6 +132,10 @@ const CollatorManagement = ({
}, []);

const handleSetCommission = useCallback(async () => {
const { isDeployed } = await checkWaitingIndexing();
if (!isDeployed) {
return;
}
const oldPrev = await getPrevAddress();
if (!oldPrev) {
error('Previous key is missing. Please verify your collator information.');
Expand All @@ -142,7 +147,7 @@ const CollatorManagement = ({
if (tx) {
setCommissionHash(tx);
}
}, [updateCommission, getPrevAddress]);
}, [updateCommission, getPrevAddress, checkWaitingIndexing]);

const handleSetCommissionSuccess = useCallback(() => {
setCommissionHash('');
Expand All @@ -153,6 +158,10 @@ const CollatorManagement = ({
}, []);

const handleStop = useCallback(async () => {
const { isDeployed } = await checkWaitingIndexing();
if (!isDeployed) {
return;
}
const oldPrev = await getPrevAddress();
if (!oldPrev) {
error('Previous key is missing. Please verify your collator information.');
Expand All @@ -164,7 +173,7 @@ const CollatorManagement = ({
if (tx) {
setStopHash(tx);
}
}, [stop, getPrevAddress]);
}, [stop, getPrevAddress, checkWaitingIndexing]);

const handleStopSuccess = useCallback(() => {
setStopHash('');
Expand Down Expand Up @@ -272,7 +281,8 @@ const CollatorManagement = ({
isLockPeriodLoading ||
isLoadingUpdateCommission ||
isLoadingPrev ||
isLoadingCollatorByAddress
isLoadingCollatorByAddress ||
isLoadingWaitingIndexing
}
>
Update Commission
Expand All @@ -284,7 +294,7 @@ const CollatorManagement = ({
color="primary"
className="h-[2.125rem] w-full"
variant="light"
isLoading={isPendingStop || isLoadingPrev}
isLoading={isPendingStop || isLoadingPrev || isLoadingWaitingIndexing}
onClick={handleStop}
>
Stop Collation
Expand Down
14 changes: 7 additions & 7 deletions src/components/deposit-item.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Checkbox, Progress, Tooltip } from '@nextui-org/react';
import dayjs from 'dayjs';
import { formatEther } from 'viem';
import FormattedNumberTooltip from './formatted-number-tooltip';
import type { DepositInfo } from '@/hooks/useUserDepositDetails';
import { calculateDepositProgress } from '@/utils/date';

interface DepositItemProps {
item: DepositInfo;
Expand All @@ -13,12 +13,12 @@ interface DepositItemProps {

const DepositItem = ({ item, isChecked, symbol, onChange }: DepositItemProps) => {
const value = formatEther(item?.value || 0n);
const startAtDate = dayjs(item?.startAt * 1000).format('YYYY-MM-DD');
const endAtDate = dayjs(item?.endAt * 1000).format('YYYY-MM-DD');
const now = dayjs().unix();
const totalDuration = item?.endAt - item?.startAt;
const elapsedDuration = Math.max(0, Math.min(now - item?.startAt, totalDuration));
const progressValue = (elapsedDuration / totalDuration) * 100;

const { startAtDate, endAtDate, progressValue } = calculateDepositProgress(
item?.startAt,
item?.endAt
);

return (
<Tooltip closeDelay={0} content={`${startAtDate} - ${endAtDate}`} placement="bottom">
<div
Expand Down
2 changes: 1 addition & 1 deletion src/components/empty.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Empty = ({ label }: { label: string }) => {
return (
<div className="flex h-[10rem] flex-col items-center justify-center gap-[0.625rem] rounded-medium bg-secondary">
<div className="flex flex-col items-center gap-[0.625rem]">
<div className="flex flex-col items-center gap-[0.825rem]">
<img src="/images/common/empty.svg" alt="empty" className="h-[56px] w-[116px]" />
<p className="m-0 text-[0.75rem] font-normal text-foreground/50">{label}</p>
</div>
Expand Down
12 changes: 11 additions & 1 deletion src/components/reserved-staking-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { useStakingAccount } from '@/hooks/useService';
import TooltipFormattedNumber from './tooltip-formatter-number';
import { formatEther } from 'viem';
import { Skeleton } from '@nextui-org/react';
import useWalletStatus from '@/hooks/useWalletStatus';

const ReservedStakingPanel = () => {
const { nativeTokenIcon, currentChain } = useWalletStatus();
const { data, isLoading } = useStakingAccount({
enabled: true
});
Expand All @@ -14,7 +16,15 @@ const ReservedStakingPanel = () => {
return (
<div className="relative flex h-[11rem] w-full flex-col items-center justify-center gap-[0.625rem] rounded-medium bg-secondary p-5 md:w-[22.5rem]">
<div className="relative mt-[0.62rem] size-[3.75rem]">
<img src="/images/token/ring.svg" alt="ring" className="h-[60px] w-[60px]" />
{nativeTokenIcon ? (
<img
src={nativeTokenIcon}
alt={currentChain?.nativeCurrency?.name}
className="h-[60px] w-[60px]"
/>
) : (
<img src="/images/token/ring.svg" alt="ring" className="h-[60px] w-[60px]" />
)}
</div>
<p className="m-0 text-[0.875rem] font-normal text-foreground">Reserved in Staking</p>
{isLoading ? (
Expand Down
25 changes: 17 additions & 8 deletions src/components/unstake-deposit-item.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import { Checkbox, Progress, Tooltip } from '@nextui-org/react';
import { formatEther } from 'viem';
import dayjs from 'dayjs';
import FormattedNumberTooltip from './formatted-number-tooltip';

import type { StakedDepositInfo } from '@/view/stake/_hooks/staked';
import { calculateDepositProgress } from '@/utils/date';

interface UnstakeDepositItemProps {
item: StakedDepositInfo;
isChecked?: boolean;
isDisabled?: boolean;
symbol?: string;
onChange: (id: bigint) => void;
}

const UnstakeDepositItem = ({ item, isChecked, symbol, onChange }: UnstakeDepositItemProps) => {
const UnstakeDepositItem = ({
item,
isChecked,
isDisabled,
symbol,
onChange
}: UnstakeDepositItemProps) => {
const value = formatEther(item?.amount || 0n);

const startAtDate = dayjs(item?.startAt * 1000).format('YYYY-MM-DD');
const endAtDate = dayjs(item?.endAt * 1000).format('YYYY-MM-DD');
const now = dayjs().unix();
const totalDuration = item?.endAt - item?.startAt;
const elapsedDuration = Math.max(0, Math.min(now - item?.startAt, totalDuration));
const progressValue = (elapsedDuration / totalDuration) * 100;
const { startAtDate, endAtDate, progressValue } = calculateDepositProgress(
item?.startAt,
item?.endAt
);

return (
<Tooltip closeDelay={0} content={`${startAtDate} - ${endAtDate}`} placement="bottom">
<div
className="flex cursor-pointer items-start gap-[0.31rem]"
onClick={() => {
if (isDisabled) return;
onChange(item?.tokenId);
}}
>
<Checkbox
isSelected={isChecked}
isDisabled={isDisabled}
radius="full"
className="-mt-[3px]"
onValueChange={() => {
if (isDisabled) return;
onChange(item?.tokenId);
}}
classNames={{
Expand Down
4 changes: 3 additions & 1 deletion src/components/unstake-deposit-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ export type DepositListRef = {
reset: () => void;
};
interface DepositListProps {
isDisabled?: boolean;
maxCount?: number;
deposits: StakedDepositInfo[];
onCheckedDepositsChange: (deposits: StakedDepositInfo[]) => void;
}

const UnstakeDepositList = forwardRef<DepositListRef, DepositListProps>(
({ maxCount = 5, deposits, onCheckedDepositsChange }, ref) => {
({ maxCount = 5, deposits, onCheckedDepositsChange, isDisabled = false }, ref) => {
const [checkedDeposits, setCheckedDeposits] = useState<StakedDepositInfo[]>([]);

const { chain } = useWalletStatus();
Expand Down Expand Up @@ -52,6 +53,7 @@ const UnstakeDepositList = forwardRef<DepositListRef, DepositListProps>(
<UnstakeDepositItem
key={deposit.tokenId}
item={deposit}
isDisabled={isDisabled}
isChecked={checkedDeposits.includes(deposit)}
symbol={chain?.nativeCurrency?.symbol}
onChange={() => handleDepositChange(deposit)}
Expand Down
9 changes: 9 additions & 0 deletions src/config/chains/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ChainId } from '@/types/chains';
import { nativeTokenIcon as crabNativeTokenIcon } from './crab';
import { nativeTokenIcon as darwiniaNativeTokenIcon } from './darwinia';
import { nativeTokenIcon as koiNativeTokenIcon } from './koi';

export { chain as crab, nativeTokenIcon as crabNativeTokenIcon } from './crab';
export { chain as darwinia, nativeTokenIcon as darwiniaNativeTokenIcon } from './darwinia';
Expand All @@ -10,6 +13,12 @@ export const KTON_TOKEN_MAP = new Map<ChainId, string>([
[ChainId.KOI, 'PKTON']
]);

export const NATIVE_TOKEN_ICON_MAP = new Map<ChainId, string>([
[ChainId.CRAB, crabNativeTokenIcon],
[ChainId.DARWINIA, darwiniaNativeTokenIcon],
[ChainId.KOI, koiNativeTokenIcon]
]);

export const KTON_TOKEN_INFO_MAP = new Map<
ChainId,
{ symbol: string; decimals: number; address: `0x${string}` }
Expand Down
4 changes: 2 additions & 2 deletions src/config/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const defiTabs = [
}
];

export const stakeTabs = [
export const stakeTabs = (tokenName: string) => [
{
label: 'Stake RING',
label: `Stake ${tokenName}`,
key: 'stake-ring'
},
{
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function useCollatorSet({
};

return useQuery({
queryKey: ['collatorSet', params],
queryKey: ['collatorSet', params, currentChainId],
queryFn: async () => {
const result = await fetchCollatorSet(params, currentChainId!);

Expand Down Expand Up @@ -104,7 +104,7 @@ export function useCollatorSetNewPrev({
};

return useQuery({
queryKey: ['collatorSetNewPrev', params],
queryKey: ['collatorSetNewPrev', params, currentChainId],
queryFn: async () => {
const result = await fetchCollatorSet(params, currentChainId!);
if (result === null) {
Expand Down Expand Up @@ -162,7 +162,7 @@ export function useCollatorByAddress({ address, enabled = true }: CollatorByAddr
};

return useQuery({
queryKey: ['collatorByAddress', params],
queryKey: ['collatorByAddress', params, currentChainId],
queryFn: async () => {
const result = await fetchCollatorSet(params, currentChainId!);
if (result === null) {
Expand Down Expand Up @@ -218,7 +218,7 @@ export function useCollatorSetByAccounts({
first: 1000
};
return useQuery({
queryKey: ['collatorSetByAccounts', params],
queryKey: ['collatorSetByAccounts', params, currentChainId],
queryFn: async () => {
const result = await fetchCollatorSetByAccounts(params, currentChainId!);
if (result === null) {
Expand All @@ -243,7 +243,7 @@ export function useStakingAccount({ enabled = true }: StakingAccountParams) {
orderBy: 'latestChangeTimestamp',
orderDirection: 'desc'
};
const queryKey = ['stakingAccounts', params];
const queryKey = ['stakingAccounts', params, currentChainId];
const result = useQuery({
queryKey,
queryFn: async () => {
Expand Down
Loading
Loading