Skip to content

Commit

Permalink
Fix status error (#29)
Browse files Browse the repository at this point in the history
* fix collatorSet get method

* Optimize the useCollatorByAddress hook by adding the inset parameter and adjusting the query logic; dynamically control the display of the Stake button in StakeManagementModal based on the collator's status.

* remove koi chain

* support unstakeRINGFromInactiveCollator and unstakeDepositsFromInactiveCollator

* fix ts error

* fix abi

* fix types error
  • Loading branch information
snoopy1412 authored Nov 19, 2024
1 parent 181b65a commit 900c76e
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 139 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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-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"

2 changes: 1 addition & 1 deletion src/components/collator/_hooks/collator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const useCreateCollator = ({

const { writeContractAsync, ...rest } = useWriteContract();

const oldKey = genKey({ address: address as `0x${string}`, votes: votes ?? 0n });
const oldKey = genKey({ address: address as `0x${string}`, votes: (votes as bigint) ?? 0n });

const createCollator = useCallback(
async ({ commission }: CreateAndCollatorProps) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/collator/_hooks/commissionLocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const useCommissionLocks = () => {
});

const isLockPeriod = useMemo(() => {
const locked = result?.data ?? 0n;
const locked = (result?.data as bigint) ?? 0n;
const now = BigInt(Math.floor(Date.now() / 1000));

return locked > now;
Expand Down
3 changes: 1 addition & 2 deletions src/components/collator/_hooks/set-session-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const getData = (sessionKey: string, chainId: ChainId) => {
case ChainId.DARWINIA:
case ChainId.CRAB:
return `0x0d00${sessionKey}00`;
case ChainId.KOI:
return `0x0a00${sessionKey}00`;

default:
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/collator/_hooks/update-commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const useUpdateCommission = ({
}
});

const newKey = genKey({ address: collatorAddress, votes: votes ?? 0n });
const newKey = genKey({ address: collatorAddress, votes: (votes as bigint) ?? 0n });

const { writeContractAsync, isPending } = useWriteContract();

Expand Down
55 changes: 55 additions & 0 deletions src/config/abi/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ export const abi = [
stateMutability: 'view',
type: 'function'
},
{
inputs: [
{
internalType: 'address',
name: 'c',
type: 'address'
}
],
name: '_isInactiveCollator',
outputs: [
{
internalType: 'bool',
name: '',
type: 'bool'
}
],
stateMutability: 'view',
type: 'function'
},
{
inputs: [
{
Expand Down Expand Up @@ -813,6 +832,24 @@ export const abi = [
stateMutability: 'nonpayable',
type: 'function'
},
{
inputs: [
{
internalType: 'address',
name: 'collator',
type: 'address'
},
{
internalType: 'uint256[]',
name: 'depositIds',
type: 'uint256[]'
}
],
name: 'unstakeDepositsFromInactiveCollator',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
},
{
inputs: [
{
Expand Down Expand Up @@ -841,6 +878,24 @@ export const abi = [
stateMutability: 'nonpayable',
type: 'function'
},
{
inputs: [
{
internalType: 'address',
name: 'collator',
type: 'address'
},
{
internalType: 'uint256',
name: 'assets',
type: 'uint256'
}
],
name: 'unstakeRINGFromInactiveCollator',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
},
{
inputs: [
{
Expand Down
32 changes: 2 additions & 30 deletions src/config/chains/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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';
export { chain as koi, nativeTokenIcon as koiNativeTokenIcon } from './koi';

export const KTON_TOKEN_MAP = new Map<ChainId, string>([
[ChainId.CRAB, 'CKTON'],
[ChainId.DARWINIA, 'KTON'],
[ChainId.KOI, 'PKTON']
[ChainId.DARWINIA, 'KTON']
]);

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

export const KTON_TOKEN_INFO_MAP = new Map<
Expand All @@ -40,15 +36,6 @@ export const KTON_TOKEN_INFO_MAP = new Map<
address: '0x0000000000000000000000000000000000000402',
governanceName: 'KtonDAO'
}
],
[
ChainId.KOI,
{
symbol: 'PKTON',
decimals: 18,
address: '0x0000000000000000000000000000000000000402',
governanceName: 'PktonDAO'
}
]
]);

Expand All @@ -72,13 +59,6 @@ export const RING_DAO_GOVERNANCE_MAP = new Map<
url: "'http://vote.ringdao.com'",
name: 'RingDAO governance'
}
],
[
ChainId.KOI,
{
url: "'http://koi-vote.ringdao.com'",
name: 'RingDAO test version governance'
}
]
]);

Expand All @@ -105,13 +85,5 @@ export const GRING_TOKEN_ADDRESS_MAP = new Map<
symbol: 'gRING',
decimals: 18
}
],
[
ChainId.KOI,
{
address: '0xdafa555e2785DC8834F4Ea9D1ED88B6049142999',
symbol: 'gKRING',
decimals: 18
}
]
]);
38 changes: 0 additions & 38 deletions src/config/chains/koi.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/hooks/useService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ export const fetchCollatorSetNewPrev = async ({
type CollatorByAddressParams = {
address: `0x${string}`;
enabled?: boolean;
inset?: number;
};
export function useCollatorByAddress({ address, enabled = true }: CollatorByAddressParams) {
export function useCollatorByAddress({ address, enabled = true, inset }: CollatorByAddressParams) {
const { currentChainId, isEnabled } = useWalletStatus();

const params: CollatorSetQueryParams = {
where: {
...(address ? { address: toLowerCase(address) } : {}),
inset: 1
...(inset !== undefined ? { inset } : {})
},
orderBy: 'key',
orderDirection: 'desc',
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useStakingAccountWithStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function useStakingAccountWithStatus() {

return data.map((account) => {
const collatorAddress = account.collator.toLowerCase().trim();
const collatorSet = collatorSetByAccounts?.[collatorAddress];
const collatorSet = collatorSetByAccounts?.find(
(c) => c.address?.toLowerCase() === collatorAddress
);

let status: CollatorStatus | undefined = undefined;
if (isLoadingActiveCollators || isRefetchingActiveCollators) {
Expand Down
3 changes: 0 additions & 3 deletions src/service/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { ChainId } from '@/types/chains';
import { GraphQLClient } from 'graphql-request';

export const koiClient = new GraphQLClient(import.meta.env.VITE_KOI_GRAPHQL_API_URL);
export const crabClient = new GraphQLClient(import.meta.env.VITE_CRAB_GRAPHQL_API_URL);
export const darwiniaClient = new GraphQLClient(import.meta.env.VITE_DARWINIA_GRAPHQL_API_URL);

export function getClient(chainId: ChainId) {
switch (chainId) {
case ChainId.KOI:
return koiClient;
case ChainId.CRAB:
return crabClient;
case ChainId.DARWINIA:
Expand Down
6 changes: 2 additions & 4 deletions src/types/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
*/
export enum ChainId {
CRAB = 44,
DARWINIA = 46,
KOI = 701
DARWINIA = 46
}

export const ktonToken = {
[ChainId.CRAB]: 'CKTON',
[ChainId.DARWINIA]: 'KTON',
[ChainId.KOI]: 'PKTON'
[ChainId.DARWINIA]: 'KTON'
};
30 changes: 8 additions & 22 deletions src/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { darwinia, crab, koi } from "@/config/chains";
import { DEPLOYMENT_MODE } from "@/types/deploy";
import { ChainId } from "@/types/chains";
import { darwinia, crab } from '@/config/chains';
import { ChainId } from '@/types/chains';

import type { Chain } from "@rainbow-me/rainbowkit";

const chainDeployMode: DEPLOYMENT_MODE =
import.meta.env.VITE_DEPLOYMENT_MODE === "mainnet"
? DEPLOYMENT_MODE.MAINNET
: DEPLOYMENT_MODE.TESTNET;

const defaultChainForMode: Record<DEPLOYMENT_MODE, ChainId> = {
[DEPLOYMENT_MODE.MAINNET]: ChainId.DARWINIA,
[DEPLOYMENT_MODE.TESTNET]: ChainId.KOI,
};
import type { Chain } from '@rainbow-me/rainbowkit';

const chainConfigMap: Record<ChainId, Chain> = {
[ChainId.DARWINIA]: darwinia,
[ChainId.CRAB]: crab,
[ChainId.KOI]: koi,
[ChainId.CRAB]: crab
};

// Helper function to filter chains based on deployment mode
Expand All @@ -32,7 +20,7 @@ function filterChainsByDeploymentMode(chains: Record<ChainId, Chain>): Chain[] {
export function getChains(): [Chain, ...Chain[]] {
const filteredChains: Chain[] = filterChainsByDeploymentMode(chainConfigMap);
if (filteredChains.length === 0) {
throw new Error("No suitable chain configurations are available.");
throw new Error('No suitable chain configurations are available.');
}
return filteredChains as [Chain, ...Chain[]];
}
Expand All @@ -47,14 +35,12 @@ export function getDefaultChain(): Chain {
const filteredChains = filterChainsByDeploymentMode(chainConfigMap);
if (filteredChains.length === 0) {
throw new Error(
"No suitable chain configurations are available for the current deployment mode."
'No suitable chain configurations are available for the current deployment mode.'
);
}

const defaultChainId = defaultChainForMode[chainDeployMode];
const defaultChain = filteredChains.find(
(chain) => chain.id === defaultChainId
);
const defaultChainId = ChainId.DARWINIA;
const defaultChain = filteredChains.find((chain) => chain.id === defaultChainId);

return defaultChain || filteredChains[0];
}
Expand Down
Loading

0 comments on commit 900c76e

Please sign in to comment.