Skip to content

Commit

Permalink
fix: rewards flow
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Mar 6, 2024
1 parent 530d13f commit a6ce1fc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
14 changes: 9 additions & 5 deletions src/features/staking/components/modals/rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import { toast } from "react-toastify";
import { Button, HeroText } from "@/features/core/components/base";
import CommonModal from "@/features/core/components/common-modal";

import { claimRewardsAction } from "../../context/actions";
import { fetchUserDataAction } from "../../context/actions";
import { useStaking } from "../../context/hooks";
import { setModalOpened } from "../../context/reducer";
import { claimRewards } from "../../lib/core/tx";

type Step = "completed" | "loading";

const initialStep: Step = "loading";

const claimRewards = async (
const claimRewardsLoop = async (
stakingRef: ReturnType<typeof useStaking>,
setStep: (step: Step) => void,
) => {
Expand All @@ -23,17 +24,20 @@ const claimRewards = async (

if (!client || !delegations?.length) return;

const delegatorAddress = stakingRef.account.bech32Address;

delegations
.reduce(async (promise, delegation) => {
await promise;

const addresses = {
delegator: stakingRef.account.bech32Address,
delegator: delegatorAddress,
validator: delegation.validatorAddress,
};

return claimRewardsAction(addresses, client, stakingRef.staking);
await claimRewards(addresses, client);
}, Promise.resolve())
.then(() => fetchUserDataAction(delegatorAddress, staking))
.then(() => {
setStep("completed");
})
Expand Down Expand Up @@ -62,7 +66,7 @@ const RewardsModal = () => {
if (requested.current) return;

requested.current = true;
claimRewards(stakingRef, setStep);
claimRewardsLoop(stakingRef, setStep);
}
}, [isOpen, stakingRef]);

Expand Down
12 changes: 1 addition & 11 deletions src/features/staking/context/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import type { AbstraxionSigningClient } from "../lib/core/client";
import { sumAllCoins } from "../lib/core/coins";
import type { StakeAddresses } from "../lib/core/tx";
import { claimRewards, stakeAmount, unstakeAmount } from "../lib/core/tx";
import { stakeAmount, unstakeAmount } from "../lib/core/tx";
import {
addDelegations,
addUnbondings,
Expand Down Expand Up @@ -182,16 +182,6 @@ export const unstakeValidatorAction = async (
};
};

export const claimRewardsAction = async (
addresses: StakeAddresses,
client: AbstraxionSigningClient,
staking: StakingContextType,
) => {
await claimRewards(addresses, client);

await fetchUserDataAction(addresses.delegator, staking);
};

export const getValidatorDetailsAction = async (
validatorAddress: string,
staking: StakingContextType,
Expand Down
2 changes: 1 addition & 1 deletion src/features/staking/lib/core/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const claimRewards = async (
) => {
const msg = MsgWithdrawDelegatorReward.fromPartial({
delegatorAddress: addresses.delegator,
// validatorAddress: addresses.validator,
validatorAddress: addresses.validator,
});

const messageWrapper = [
Expand Down

0 comments on commit a6ce1fc

Please sign in to comment.