Skip to content

Commit

Permalink
fix(sigma, keys ): owner balance, double add (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimakorzhovnik authored Dec 19, 2023
1 parent 530a6c6 commit f32d51f
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useMemo, useRef } from 'react';
import cx from 'classnames';
import { Link } from 'react-router-dom';
import { Link, useLocation } from 'react-router-dom';
import { usePopperTooltip } from 'react-popper-tooltip';
import { Transition } from 'react-transition-group';

Expand Down Expand Up @@ -28,6 +28,7 @@ function AccountItem({
link,
}) {
const address = data?.cyber?.bech32;
const location = useLocation();

const { passport } = usePassportByAddress(address);

Expand All @@ -41,13 +42,15 @@ function AccountItem({
setControlledVisible(false);
}

const robotPath = nickname
? routes.robotPassport.getLink(nickname)
: routes.robot.path;

const linkToRobot = location.pathname.includes('@') ? robotPath : undefined;

return (
<Link
to={
link ||
(nickname && routes.robotPassport.getLink(nickname)) ||
routes.robot.path
}
to={link || linkToRobot}
onClick={handleClick}
className={cx(
styles.containerSwichAccount,
Expand Down
25 changes: 13 additions & 12 deletions src/containers/sigma/hooks/useGetBalanceBostrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { useEffect, useState, useMemo, useCallback } from 'react';
import BigNumber from 'bignumber.js';
import { useIbcDenom } from 'src/contexts/ibcDenom';
import { useAppData } from 'src/contexts/appData';
import { Nullable } from 'src/types';
import useGetBalanceMainToken from './useGetBalanceMainToken';
import useBalanceToken from './useBalanceToken';
import { convertAmount } from '../../../utils/utils';
import { CYBER } from '../../../utils/config';

function useGetBalanceBostrom(address) {
function useGetBalanceBostrom(address: Nullable<string>) {
const { marketData } = useAppData();
const { traseDenom } = useIbcDenom();
const { balance: balanceMainToken, loading: loadingMalin } =
Expand All @@ -24,22 +25,23 @@ function useGetBalanceBostrom(address) {
const [balances, setBalances] = useState({});

useEffect(() => {
if (address !== null) {
const { bech32 } = address;
const keyLs = `lastBalances-${bech32}`;
if (address) {
const keyLs = `lastBalances-${address}`;
const lastBalancesLs = localStorage.getItem(keyLs);

if (!loadingToken && !loadingMalin) {
if (!loadingMalin && !loadingToken) {
let dataResult = {};
const mainToken = { [CYBER.DENOM_CYBER]: { ...balanceMainToken } };
const mainToken = {
[CYBER.DENOM_CYBER]: { ...balanceMainToken },
};
const dataResultTemp = { ...mainToken, ...balanceToken };
const tempData = getBalanceMarket(dataResultTemp);
dataResult = { ...tempData };
setBalances(dataResult);
if (Object.keys(dataResult).length > 0) {
localStorage.setItem(keyLs, JSON.stringify(dataResult));
}
} else if (lastBalancesLs !== null) {
} else if (lastBalancesLs) {
const dataLs = JSON.parse(lastBalancesLs);
setBalances(dataLs);
}
Expand Down Expand Up @@ -105,12 +107,11 @@ function useGetBalanceBostrom(address) {
}, [balances]);

useEffect(() => {
if (address !== null) {
const { bech32 } = address;
const keyLs = `lastCap-${bech32}`;
if (address) {
const keyLs = `lastCap-${address}`;
const lastCapLs = localStorage.getItem(keyLs);
let lastCap = new BigNumber(0);
if (lastCapLs !== null) {
if (lastCapLs) {
lastCap = lastCap.plus(JSON.parse(lastCapLs));
}

Expand All @@ -136,7 +137,7 @@ function useGetBalanceBostrom(address) {
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [useGetCapTokens]);
}, [useGetCapTokens, address]);

return {
totalAmountInLiquid,
Expand Down
28 changes: 1 addition & 27 deletions src/containers/sigma/hooks/useGetBalanceMainToken.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,12 @@
import BigNumber from 'bignumber.js';
import { useEffect, useState } from 'react';
import { useQueryClient } from 'src/contexts/queryClient';
import { useGetBalance, initValueMainToken } from './utils';

function useGetBalanceMainToken(address) {
const queryClient = useQueryClient();
const addressActive = address?.bech32 || address;
const [balance, setBalance] = useState({ ...initValueMainToken });
const data = useGetBalance(queryClient, addressActive);
const [loading, setLoading] = useState(true);

useEffect(() => {
setLoading(true);
if (data !== undefined) {
setBalance({ ...initValueMainToken });
Object.keys(data).forEach((key) => {
if (data[key] && data[key].amount > 0) {
setBalance((item) => ({
...item,
[key]: { ...data[key] },
total: {
...item.total,
amount: new BigNumber(item.total.amount)
.plus(data[key].amount)
.toNumber(),
},
}));
}
});
setLoading(false);
}
}, [data]);

return { balance, loading };
return { balance: data || { ...initValueMainToken }, loading: !data };
}

export default useGetBalanceMainToken;
19 changes: 17 additions & 2 deletions src/containers/sigma/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,27 @@ export const useGetBalance = (client, addressBech32) => {
responsevalidatorCommission
);

return {
const resultBalance = {
liquid: responsegetBalance,
frozen: delegationsAmount,
melting: unbondingAmount,
growth: rewardsAmount,
commission: commissionAmount,
};

if (commissionAmount.amount > 0) {
resultBalance.commission = commissionAmount;
}

const total = Object.values(resultBalance).reduce((acc, item) => {
return new BigNumber(acc).plus(item.amount).toString();
}, 0);

return {
...resultBalance,
total: {
denom: DENOM_CYBER,
amount: total,
},
};
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/containers/sigma/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function Sigma() {
const updateDataCap = (newData) => {
setValue((item) => ({
...item,
dataCap: { ...item.dataCap, ...newData },
dataCap: superSigma ? { ...item.dataCap, ...newData } : { ...newData },
}));
};

Expand Down
3 changes: 3 additions & 0 deletions src/features/adviser/AdviserContainer.module.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
.wrapper {
position: sticky;
top: 75px;
width: 60%;

z-index: 3;
min-height: 90px; // 3 lines

margin-top: -45px;
margin-bottom: 15px;
margin-left: auto;
margin-right: auto;

display: flex;
justify-content: center;
Expand Down
Loading

0 comments on commit f32d51f

Please sign in to comment.