Skip to content

Commit

Permalink
fix: bug when viewing earned token rewards on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
esaminu committed Mar 21, 2022
1 parent 07801d8 commit aa69f9f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
14 changes: 6 additions & 8 deletions packages/frontend/src/components/staking/components/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { redirectTo } from '../../../redux/actions/account';
import { claimFarmRewards, getValidatorFarmData } from '../../../redux/actions/staking';
import { showCustomAlert } from '../../../redux/actions/status';
import selectNEARAsTokenWithMetadata from '../../../redux/crossStateSelectors/selectNEARAsTokenWithMetadata';
import { selectValidatorsFarmData, selectFarmValidatorAPY } from '../../../redux/slices/staking';
import { selectValidatorsFarmData, selectFarmValidatorAPY, selectStakingCurrentAccountAccountId } from '../../../redux/slices/staking';
import { selectActionsPending } from '../../../redux/slices/status';
import { selectTokensFiatValueUSD, selectTokenWhiteList } from '../../../redux/slices/tokenFiatValues';
import { selectAllContractMetadata } from '../../../redux/slices/tokens';
Expand All @@ -28,7 +28,7 @@ const renderFarmUi = ({ farmList, contractMetadataByContractId, openModal, token
}

return farmList.map((farm, i) => {
const { token_id, balance } = farm;
const { token_id, balance, farm_id } = farm;
const currentTokenContractMetadata = contractMetadataByContractId[token_id];

if (!currentTokenContractMetadata) {
Expand All @@ -39,7 +39,7 @@ const renderFarmUi = ({ farmList, contractMetadataByContractId, openModal, token

return (
<BalanceBox
key={token_id}
key={farm_id}
token={{
onChainFTMetadata: currentTokenContractMetadata,
fiatValueMetadata,
Expand Down Expand Up @@ -79,6 +79,7 @@ export default function Validator({
const contractMetadataByContractId = useSelector(selectAllContractMetadata);
const tokenFiatValues = useSelector(selectTokensFiatValueUSD);
const tokenWhitelist = useSelector(selectTokenWhiteList);
const currentAccountId = useSelector(selectStakingCurrentAccountAccountId);

const dispatch = useDispatch();
const stakeNotAllowed = !!selectedValidator && selectedValidator !== match.params.validator && !!currentValidators.length;
Expand Down Expand Up @@ -126,11 +127,8 @@ export default function Validator({
const validatorFarmData = validatorsFarmData[validator?.accountId] || {};

useEffect(() => {
console.log(isFarmingValidator,'isfarmi', validator);
if (!isFarmingValidator || !validator?.accountId) return;

dispatch(getValidatorFarmData(validator.accountId));
}, [validator?.accountId, isFarmingValidator]);
dispatch(getValidatorFarmData(validator, currentAccountId));
}, [validator, currentAccountId]);

const farmList = validatorFarmData?.farmRewards || [];
const tokenPriceMetadata = { tokenFiatValues, tokenWhitelist };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 { selectFarmValidatorAPY, selectStakingCurrentAccountAccountId } from '../../../redux/slices/staking';
import { FARMING_VALIDATOR_VERSION, ValidatorVersion } from '../../../utils/constants';
import Balance from '../../common/balance/Balance';
import FormButton from '../../common/FormButton';
Expand Down Expand Up @@ -142,12 +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];
const isFarmingValidator = validator?.version === ValidatorVersion[FARMING_VALIDATOR_VERSION];
const currentAccountId = useSelector(selectStakingCurrentAccountAccountId);

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

const fee = validator.fee && validator.fee.percentage;
const cta = amount ? (
Expand Down
15 changes: 3 additions & 12 deletions packages/frontend/src/redux/actions/staking.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,27 +525,18 @@ export const handleUpdateCurrent = (accountId) => async (dispatch, getState) =>
dispatch(staking.updateCurrent({ currentAccount }));
};

export const getValidatorFarmData = (validatorId) => async (dispatch, getState) => {
export const getValidatorFarmData = (validator, accountId) => async (dispatch, getState) => {

const validators = selectStakingAllValidators(getState());
const validator = { ...validators.find((validator) => validator?.accountId === validatorId)};

if (validator?.version !== FARMING_VALIDATOR_VERSION) return;
if (validator?.version !== FARMING_VALIDATOR_VERSION || !accountId) return;

const poolSummary = await validator.contract.get_pool_summary();
console.log(poolSummary,'pools');
const farms = await validator.contract.get_farms({ from_index: 0, limit: 300 });

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 }) => {
try {
dispatch(fetchToken({ contractName: token_id }));
return validator.contract
.get_unclaimed_reward({ account_id, farm_id })
.get_unclaimed_reward({ account_id: accountId, farm_id })
.catch(() => '0')
.then((balance) => ({
token_id,
Expand Down

0 comments on commit aa69f9f

Please sign in to comment.