Skip to content

Commit

Permalink
fix(sigma): delegation (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimakorzhovnik authored Nov 24, 2024
1 parent b65f760 commit ee73e69
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/containers/Validators/Validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import { DenomArr, MainContainer, Loading } from 'src/components';
import { BASE_DENOM, DENOM_LIQUID } from 'src/constants/config';
import useStakingParams from 'src/features/staking/useStakingParams';

import { getDelegatorDelegations } from 'src/features/staking/getDelegatorDelegations';
import { useCyberClient } from 'src/contexts/queryCyberClient';
import { fromBech32, formatNumber, asyncForEach } from '../../utils/utils';
import ActionBarContainer from './ActionBarContainer';
import { TableHeroes, TableItem, InfoBalance } from './components';
import getHeroes from './getHeroesHook';
import { useGetBalance } from '../../pages/robot/_refactor/account/hooks';
import useSetActiveAddress from '../../hooks/useSetActiveAddress';
import styles from './Validators.module.scss';
import { getDelegatorDelegations } from 'src/features/staking/getDelegatorDelegations';

function Validators({ defaultAccount }) {
const { isMobile: mobile } = useDevice();
const { status = 'active' } = useParams();

const queryClient = useQueryClient();
const { rpc } = useCyberClient();
const [updatePage, setUpdatePage] = useState(0);
const { addressActive } = useSetActiveAddress(defaultAccount);
const { balance, loadingBalanceInfo, balanceToken } = useGetBalance(
Expand Down Expand Up @@ -102,7 +104,7 @@ function Validators({ defaultAccount }) {
let delegationsDataTemp = [];
if (addressActive !== null && queryClient) {
const responseDelegatorDelegations = await getDelegatorDelegations(
queryClient,
rpc,
addressActive.bech32
);
delegationsDataTemp = responseDelegatorDelegations;
Expand All @@ -114,7 +116,7 @@ function Validators({ defaultAccount }) {
console.log(`e`, e);
setDelegationsData([]);
}
}, [addressActive, queryClient, updatePage]);
}, [addressActive, rpc, updatePage]);

useEffect(() => {
if (validators.length > 0 && delegationsData.length > 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/containers/sigma/hooks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { useQueryClient } from 'src/contexts/queryClient';

import { isPussyChain } from 'src/utils/chains/pussy';

import { fromBech32 } from '../../../utils/utils';
import { getDelegatorDelegations } from 'src/features/staking/getDelegatorDelegations';
import { useCyberClient } from 'src/contexts/queryCyberClient';
import { fromBech32 } from '../../../utils/utils';

const initValue = {
denom: BASE_DENOM,
Expand Down Expand Up @@ -111,6 +112,7 @@ function useCyberverBalance({ address }) {

export const useGetBalance = (addressBech32) => {
const client = useQueryClient();
const { rpc } = useCyberClient();

const { data, isFetching, refetch } = useQuery(
['getBalance', addressBech32],
Expand All @@ -121,7 +123,7 @@ export const useGetBalance = (addressBech32) => {
);

const responsedelegatorDelegations = await getDelegatorDelegations(
client,
rpc,
addressBech32
);

Expand Down Expand Up @@ -166,7 +168,7 @@ export const useGetBalance = (addressBech32) => {
return resultBalance;
},
{
enabled: Boolean(client && addressBech32),
enabled: Boolean(client && addressBech32 && rpc),
}
);

Expand Down
20 changes: 11 additions & 9 deletions src/features/staking/getDelegatorDelegations.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { CyberClient } from '@cybercongress/cyber-js';
import { createPagination } from '@cosmjs/stargate';
import { cyber } from '@cybercongress/cyber-ts';
import { DelegationResponse } from 'cosmjs-types/cosmos/staking/v1beta1/staking';

// eslint-disable-next-line import/prefer-default-export
export const getDelegatorDelegations = async (
client: CyberClient,
client: Awaited<ReturnType<typeof cyber.ClientFactory.createRPCQueryClient>>,
addressBech32: string
): Promise<DelegationResponse[]> => {
let nextKey;
let nextKey: Uint8Array | undefined;
const delegationData: DelegationResponse[] = [];

let done = false;
while (!done) {
// eslint-disable-next-line no-await-in-loop
const responsedelegatorDelegations = await client.delegatorDelegations(
addressBech32,
nextKey
);
const responsedelegatorDelegations =
// eslint-disable-next-line no-await-in-loop
await client.cosmos.staking.v1beta1.delegatorDelegations({
delegatorAddr: addressBech32,
pagination: createPagination(nextKey),
});

delegationData.push(...responsedelegatorDelegations.delegationResponses);

const key = responsedelegatorDelegations?.pagination?.nextKey;

if (key) {
if (key?.byteLength) {
nextKey = key;
} else {
done = true;
Expand Down
8 changes: 5 additions & 3 deletions src/pages/robot/_refactor/account/hooks/useGetBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'src/constants/config';

import { getDelegatorDelegations } from 'src/features/staking/getDelegatorDelegations';
import { useCyberClient } from 'src/contexts/queryCyberClient';
import { coinDecimals, fromBech32 } from '../../../../../utils/utils';
import useGetSlots from '../../../../../containers/mint/useGetSlots';

Expand All @@ -32,6 +33,7 @@ const initValueToken = {

function useGetBalance(address, updateAddress) {
const queryClient = useQueryClient();
const { rpc } = useCyberClient();
const [addressActive, setAddressActive] = useState(null);
const [loadingBalanceInfo, setLoadingBalanceInfo] = useState(true);
const [loadingBalanceToken, setLoadingBalanceToken] = useState(true);
Expand All @@ -55,7 +57,7 @@ function useGetBalance(address, updateAddress) {
useEffect(() => {
const getBalance = async () => {
try {
if (queryClient && addressActive !== null) {
if (queryClient && addressActive !== null && rpc) {
setBalance(initValue);
setLoadingBalanceInfo(true);
const availablePromise = await queryClient.getBalance(
Expand All @@ -69,7 +71,7 @@ function useGetBalance(address, updateAddress) {
}));

const delegationResponses = await getDelegatorDelegations(
queryClient,
rpc,
addressActive
);
let delegationsAmount = 0;
Expand Down Expand Up @@ -150,7 +152,7 @@ function useGetBalance(address, updateAddress) {
}
};
getBalance();
}, [queryClient, addressActive, updateAddress]);
}, [queryClient, rpc, addressActive, updateAddress]);

useEffect(() => {
const getBalance = async () => {
Expand Down
9 changes: 4 additions & 5 deletions src/pages/robot/_refactor/account/hooks/useGetHeroes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { useEffect, useState } from 'react';
import { useQueryClient } from 'src/contexts/queryClient';

import { getDelegatorDelegations } from 'src/features/staking/getDelegatorDelegations';
import { useCyberClient } from 'src/contexts/queryCyberClient';
import { coinDecimals } from '../../../../../utils/utils';

function useGetHeroes(address, updateAddress) {
const queryClient = useQueryClient();
const { rpc } = useCyberClient();
const [staking, setStaking] = useState([]);
const [totalRewards, setTotalRewards] = useState([]);
const [loadingHeroesInfo, setLoadingHeroesInfo] = useState(true);
Expand All @@ -14,12 +16,9 @@ function useGetHeroes(address, updateAddress) {
setStaking([]);
setTotalRewards([]);
setLoadingHeroesInfo(true);
if (queryClient) {
if (queryClient && rpc) {
let delegations = [];
const delegatorDelegations = await getDelegatorDelegations(
queryClient,
address
);
const delegatorDelegations = await getDelegatorDelegations(rpc, address);

if (delegatorDelegations.length) {
delegations = delegatorDelegations.reduce(
Expand Down

0 comments on commit ee73e69

Please sign in to comment.