Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.getpara.com/llms.txt

Use this file to discover all available pages before exploring further.

Withdraw your accumulated staking rewards from validators on Cosmos chains using CosmJS in React Native.

Prerequisites

Claim Rewards

import { useMemo } from 'react';
import { View, Text, Button } from 'react-native';
import { useParaCosmjsProtoSigner, useParaCosmjsSignAndBroadcast } from '@getpara/react-native-wallet/cosmos';
import { SigningStargateClient, coins } from '@cosmjs/stargate';
import { MsgWithdrawDelegatorReward } from 'cosmjs-types/cosmos/distribution/v1beta1/tx';

const RPC_URL = 'https://rpc.cosmos.directory/cosmoshub';

function ClaimRewards() {
  const { protoSigner, isLoading } = useParaCosmjsProtoSigner();

  const address = protoSigner?.address;

  const client = useMemo(
    () => protoSigner ? SigningStargateClient.connectWithSigner(RPC_URL, protoSigner) : undefined,
    [protoSigner]
  );

  const { signAndBroadcastAsync, isPending } = useParaCosmjsSignAndBroadcast(protoSigner, client);

  const claimStakingRewards = async () => {
    if (!protoSigner || !address) return;

    const validator = 'cosmosvaloper1...';

    const msgWithdrawReward = {
      typeUrl: '/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward',
      value: MsgWithdrawDelegatorReward.fromPartial({
        delegatorAddress: address,
        validatorAddress: validator,
      }),
    };

    const fee = {
      amount: coins(5000, 'uatom'),
      gas: '200000',
    };

    try {
      const result = await signAndBroadcastAsync({
        messages: [msgWithdrawReward],
        fee,
        memo: 'Claiming rewards via Para',
      });

      console.log('Rewards claimed:', result.transactionHash);
      console.log('Gas used:', result.gasUsed);
    } catch (error) {
      console.error('Failed to claim rewards:', error);
    }
  };

  if (isLoading) return <Text>Loading...</Text>;

  return (
    <View>
      <Text>Delegator: {address}</Text>
      <Button
        title={isPending ? 'Claiming...' : 'Claim Rewards'}
        onPress={claimStakingRewards}
        disabled={isPending}
      />
    </View>
  );
}

Next Steps