diff --git a/packages/frontend/src/components/staking/components/Validator.js b/packages/frontend/src/components/staking/components/Validator.js index 6c23cef6ce..54c1fd99b8 100644 --- a/packages/frontend/src/components/staking/components/Validator.js +++ b/packages/frontend/src/components/staking/components/Validator.js @@ -13,6 +13,7 @@ import { selectValidatorsFarmData, selectFarmValidatorAPY, selectStakingCurrentA import { selectActionsPending } from '../../../redux/slices/status'; import { selectTokensFiatValueUSD, selectTokenWhiteList } from '../../../redux/slices/tokenFiatValues'; import { selectAllContractMetadata } from '../../../redux/slices/tokens'; +import StakingFarmContracts from '../../../services/StakingFarmContracts'; import { FARMING_VALIDATOR_VERSION } from '../../../utils/constants'; import FormButton from '../../common/FormButton'; import SafeTranslate from '../../SafeTranslate'; @@ -99,9 +100,14 @@ export default function Validator({ const handleStakeAction = async () => { if (showConfirmModal && !loading) { - await dispatch(getValidatorFarmData(validator, currentAccountId)).then((res) => + await StakingFarmContracts.getFarmListWithUnclaimedRewards({ + contractName: validator.contract.contractId, + account_id: currentAccountId, + from_index: 0, + limit: 300, + }).then((res) => Promise.all([ - (res?.farmRewards || []) + (res || []) .filter(({balance}) => !new BN(balance).isZero()) .map(({token_id}) => dispatch(claimFarmRewards(validator.accountId, token_id))) ]) diff --git a/packages/frontend/src/redux/actions/staking.js b/packages/frontend/src/redux/actions/staking.js index 26d3b2f52e..3addc0de99 100644 --- a/packages/frontend/src/redux/actions/staking.js +++ b/packages/frontend/src/redux/actions/staking.js @@ -10,6 +10,7 @@ import { FARMING_CLAIM_YOCTO, } from '../../config'; import { fungibleTokensService, FT_MINIMUM_STORAGE_BALANCE_LARGE } from '../../services/FungibleTokens'; +import StakingFarmContracts from '../../services/StakingFarmContracts'; import { getLockupAccountId, getLockupMinBalanceForStorage } from '../../utils/account-with-lockup'; import { showAlert } from '../../utils/alerts'; import { @@ -549,43 +550,25 @@ export const getValidatorFarmData = (validator, accountId) => async (dispatch, g if (validator?.version !== FARMING_VALIDATOR_VERSION || !accountId) return; const poolSummary = await validator.contract.get_pool_summary(); - const farms = await validator.contract.get_farms({ from_index: 0, limit: 300 }); - - const list = await Promise.all( - farms.map(({ token_id, farm_id, active }) => - dispatch(fetchToken({ contractName: token_id })) - .then(() => - validator.contract - .get_unclaimed_reward({ - account_id: accountId, - farm_id, - }) - .catch(() => '0') - .then((balance) => ({ - token_id, - balance, - farm_id, - active, - })) - ) - .catch((error) => { - console.error(error); - return { - token_id, - farm_id, - balance: 0, - active, - }; - }) - ) - ); + + const farmList = await StakingFarmContracts.getFarmListWithUnclaimedRewards({ + contractName: validator.contract.contractId, + account_id: accountId, + from_index: 0, + limit: 300, + }); + + try { + await Promise.all(farmList.map(({ token_id }) => dispatch(fetchToken({ contractName: token_id })))); + } catch (error) { + console.error(error); + } const farmData = { poolSummary: {...poolSummary}, - farmRewards: list, + farmRewards: farmList, }; await dispatch(staking.setValidatorFarmData(validator.accountId, farmData)); - return farmData; }; export const claimFarmRewards = (validatorId, token_id) => async (dispatch, getState) => { diff --git a/packages/frontend/src/services/StakingFarmContracts.js b/packages/frontend/src/services/StakingFarmContracts.js new file mode 100644 index 0000000000..e72e9a8ae9 --- /dev/null +++ b/packages/frontend/src/services/StakingFarmContracts.js @@ -0,0 +1,49 @@ +import { + wallet +} from '../utils/wallet'; + +// Staking Farm Contract +// https://github.com/referencedev/staking-farm/ +export default class StakingFarmContracts { + // View functions are not signed, so do not require a real account! + static viewFunctionAccount = wallet.getAccountBasic('dontcare'); + + static getFarms({ contractName, from_index, limit }) { + return this.viewFunctionAccount.viewFunction( + contractName, + 'get_farms', + { from_index, limit } + ); + } + + static getPoolSummary({ contractName }) { + return this.viewFunctionAccount.viewFunction( + contractName, + 'get_pool_summary' + ); + } + + static getUnclaimedRewards({ contractName, account_id, farm_id }) { + return this.viewFunctionAccount.viewFunction( + contractName, + 'get_unclaimed_reward', + { account_id, farm_id } + ); + } + + static getFarmListWithUnclaimedRewards = async ({ contractName, account_id, from_index, limit }) => { + const farms = await StakingFarmContracts.getFarms({ contractName, from_index, limit }); + return Promise.all( + farms.map(({ token_id, farm_id, active }) => + StakingFarmContracts.getUnclaimedRewards({ contractName, account_id, farm_id }) + .catch(() => '0') + .then((balance) => ({ + token_id, + balance, + farm_id, + active, + })) + ) + ); + } +}