Skip to content

Commit

Permalink
feat: support claiming rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Feb 28, 2024
1 parent 052d4a1 commit 77f81cb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
33 changes: 25 additions & 8 deletions src/features/staking/components/logged-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import Link from "next/link";
import { memo, useState } from "react";

import type { StakingState } from "../context";
import { stakeValidator, unstakeValidator } from "../context/actions";
import {
claimRewardsAction,
stakeValidator,
unstakeValidator,
} from "../context/actions";
import { useStaking } from "../context/hooks";
import type { StakeAddresses } from "../lib/core/base";
import { formatCoin } from "../lib/core/coins";
Expand Down Expand Up @@ -99,8 +103,6 @@ function StakingPage() {
<Button
disabled={isLoading}
onClick={() => {
setIsLoading(true);

if (!client || !validator) return;

setIsLoading(true);
Expand All @@ -110,9 +112,11 @@ function StakingPage() {
validator: validator.operatorAddress,
};

unstakeValidator(addresses, client, staking).then(() => {
setIsLoading(false);
});
unstakeValidator(addresses, client, staking).finally(
() => {
setIsLoading(false);
},
);
}}
>
Undelegate
Expand All @@ -121,7 +125,20 @@ function StakingPage() {
<Button
disabled={isLoading || delegation.rewards.amount === "0"}
onClick={() => {
// @TODO
if (!client) return;

setIsLoading(true);

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

claimRewardsAction(addresses, client, staking).finally(
() => {
setIsLoading(false);
},
);
}}
>
Claim rewards
Expand Down Expand Up @@ -191,7 +208,7 @@ function StakingPage() {
validator: validator.operatorAddress,
};

stakeValidator(addresses, client, staking).then(() => {
stakeValidator(addresses, client, staking).finally(() => {
setIsLoading(false);
});
}}
Expand Down
26 changes: 20 additions & 6 deletions src/features/staking/context/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { StakingContextType, Unbonding } from ".";
import type { SigningClient, StakeAddresses } from "../lib/core/base";
import {
claimRewards,
getBalance,
getDelegations,
getRewards,
Expand Down Expand Up @@ -105,30 +106,43 @@ export const fetchStakingData = async (
};

export const stakeValidator = async (
addressess: StakeAddresses,
addresses: StakeAddresses,
client: SigningClient,
staking: StakingContextType,
) => {
await stakeAmount(addressess, client, {
await stakeAmount(addresses, client, {
amount: "1000",
denom: "uxion",
});

await fetchStakingData(addressess.delegator, staking);
await fetchStakingData(addresses.delegator, staking);
};

export const unstakeValidator = async (
addressess: StakeAddresses,
addresses: StakeAddresses,
client: SigningClient,
staking: StakingContextType,
) => {
const result = await unstakeAmount(addressess, client, {
const result = await unstakeAmount(addresses, client, {
amount: "1000",
denom: "uxion",
});

// eslint-disable-next-line no-console
console.log("debug: actions.ts: result", result);

await fetchStakingData(addressess.delegator, staking);
await fetchStakingData(addresses.delegator, staking);
};

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

// eslint-disable-next-line no-console
console.log("debug: actions.ts: result", result);

await fetchStakingData(addresses.delegator, staking);
};
42 changes: 32 additions & 10 deletions src/features/staking/lib/core/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Coin,
MsgDelegateEncodeObject,
MsgUndelegateEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
StdFee,
} from "@cosmjs/stargate";
import {
Expand All @@ -15,6 +16,7 @@ import {
} from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import BigNumber from "bignumber.js";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import {
MsgDelegate,
MsgUndelegate,
Expand All @@ -40,19 +42,20 @@ const getStakingQueryClient = async () => {

type FeeOpts = {
address: string;
amount: Coin[];
amount?: Coin[];
client: SigningClient;
gasLimit: string;
memo: string;
gasLimit?: string;
memo?: string;
msgs: EncodeObject[];
};

const getCosmosFee = async ({
address,
amount,
// @TODO: review
amount = [{ amount: "1000", denom: "uxion" }],
client,
gasLimit,
memo,
gasLimit = "400000",
memo = "",
msgs,
}: FeeOpts) => {
// @TODO: create signer from local account
Expand Down Expand Up @@ -188,11 +191,7 @@ export const unstakeAmount = async (

const fee: StdFee = await getCosmosFee({
address: addresses.delegator,
// @TODO: review
amount: [{ amount: "1000", denom: "uxion" }],
client,
gasLimit: "400000",
memo: "",
msgs: [msgAny],
});

Expand All @@ -207,3 +206,26 @@ export const getUnbonding = async (

return queryClient.staking.unbondingDelegation(address, validatorAddress);
};

export const claimRewards = async (
addresses: StakeAddresses,
client: NonNullable<SigningClient>,
) => {
const msg = MsgWithdrawDelegatorReward.fromPartial({
delegatorAddress: addresses.delegator,
validatorAddress: addresses.validator,
});

const msgAny: MsgWithdrawDelegatorRewardEncodeObject = {
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
value: msg,
};

const fee: StdFee = await getCosmosFee({
address: addresses.delegator,
client,
msgs: [msgAny],
});

return await client.signAndBroadcast(addresses.delegator, [msgAny], fee);
};

0 comments on commit 77f81cb

Please sign in to comment.