Skip to content

Commit

Permalink
Fixed calculating APY and render loading, when no data available
Browse files Browse the repository at this point in the history
  • Loading branch information
askulikov authored and esaminu committed Mar 21, 2022
1 parent 4e87421 commit 3d7892a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Translate } from 'react-localize-redux';
import { useDispatch, useSelector } from 'react-redux';
import styled from 'styled-components';

import { Mixpanel } from '../../../mixpanel/index';
import { redirectTo } from '../../../redux/actions/account';
import { getValidatorFarmData } from '../../../redux/actions/staking';
import { selectFarmValidatorAPY } from '../../../redux/slices/staking';
import { FARMING_VALIDATOR_VERSION, ValidatorVersion } from '../../../utils/constants';
import Balance from '../../common/balance/Balance';
Expand Down Expand Up @@ -141,6 +142,12 @@ export default function ValidatorBox({
const dispatch = useDispatch();
const farmAPY = useSelector(state => selectFarmValidatorAPY(state, {validatorId: validator?.accountId}));
const { accountId: validatorId, active } = validator;
const isFarmingValidator = validator.version === ValidatorVersion[FARMING_VALIDATOR_VERSION];

useEffect(() => {
if (!isFarmingValidator) return;
dispatch(getValidatorFarmData(validatorId));
}, []);

const fee = validator.fee && validator.fee.percentage;
const cta = amount ? (
Expand All @@ -161,7 +168,7 @@ export default function ValidatorBox({
dispatch(redirectTo(`/staking/${validatorId}${stakeAction ? `/${stakeAction}` : ''}`));
}
};
const isFarmingValidator = validator.version === ValidatorVersion[FARMING_VALIDATOR_VERSION];

return (
<Container
className='validator-box'
Expand All @@ -184,7 +191,11 @@ export default function ValidatorBox({
{
isFarmingValidator && <>
<span><Translate id='staking.validator.apy'/>&nbsp;</span>
<span>{farmAPY}%&nbsp;-&nbsp;</span>
{farmAPY === null
? <span className="animated-dots" style={{width: 16}}/>
: <span>{farmAPY}</span>
}
<span>%&nbsp;-&nbsp;</span>
</>
}
<span>{fee}% <Translate id='staking.validatorBox.fee' /> -&nbsp;</span>
Expand Down
1 change: 0 additions & 1 deletion packages/frontend/src/redux/actions/staking.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ export const getValidatorFarmData = (validatorId) => async (dispatch, getState)
const accountId = selectStakingMainAccountId(getState());
const currentAccountId = selectStakingCurrentAccountAccountId(getState());
const isLockup = currentAccountId !== accountId;

const account_id = isLockup ? selectStakingLockupId(getState()) : accountId;

const list = await Promise.all(farms.map(({ token_id, farm_id }) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/frontend/src/redux/slices/staking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export const selectValidatorFarmDataByValidatorID = createSelector(
export const selectFarmValidatorAPY = createSelector(
[selectValidatorFarmDataByValidatorID, selectTokensFiatValueUSD],
(farmData, tokenPrices) => {
if (!farmData.poolSummary || !tokenPrices) return 0;

if (!farmData.poolSummary || !tokenPrices) return null;
return calculateAPY(farmData.poolSummary, tokenPrices);
}
);
2 changes: 1 addition & 1 deletion packages/frontend/src/utils/staking.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ export const calculateAPY = (poolSummary, tokenPrices) => {
}
catch (e) {
console.error('Error during calculating APY', e);
return 0;
return '-';
}
};

0 comments on commit 3d7892a

Please sign in to comment.