Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial batch claiming work #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions src/features/staking/components/modals/rewards.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { MsgWithdrawDelegatorRewardEncodeObject } from "@cosmjs/stargate";
import BigNumber from "bignumber.js";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { memo, useEffect, useRef, useState } from "react";
import { toast } from "react-toastify";

Expand All @@ -12,7 +14,7 @@ import { fetchUserDataAction } from "../../context/actions";
import { useStaking } from "../../context/hooks";
import { setModalOpened } from "../../context/reducer";
import { normaliseCoin } from "../../lib/core/coins";
import { claimRewards } from "../../lib/core/tx";
import { claimRewardsBatch } from "../../lib/core/tx";

type Step = "completed" | "loading";

Expand All @@ -33,23 +35,56 @@ const claimRewardsLoop = async (

const delegatorAddress = stakingRef.account.bech32Address;

delegations
.reduce(async (promise, delegation) => {
await promise;
// Collect the rewards claims in an array of messages
const rewardClaimMessages = delegations.reduce((messages, delegation) => {
const normalised = normaliseCoin(delegation.rewards);

const normalised = normaliseCoin(delegation.rewards);
if (new BigNumber(normalised.amount).lt(MIN_CLAIMABLE_XION)) {
return messages;
}

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

const msg = MsgWithdrawDelegatorReward.fromPartial({
delegatorAddress: addresses.delegator,
validatorAddress: addresses.validator,
});

messages.push({
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
value: msg,
} satisfies MsgWithdrawDelegatorRewardEncodeObject);

return messages;
}, [] as MsgWithdrawDelegatorRewardEncodeObject[]);

if (rewardClaimMessages.length === 0) {
toast("No claimable rewards found.", { type: "info" });

return;
}

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

// const normalised = normaliseCoin(delegation.rewards);

if (new BigNumber(normalised.amount).lt(MIN_CLAIMABLE_XION)) {
return;
}
// if (new BigNumber(normalised.amount).lt(MIN_CLAIMABLE_XION)) {
// return;
// }

const addresses = {
delegator: delegatorAddress,
validator: delegation.validatorAddress,
};
// const addresses = {
// delegator: delegatorAddress,
// validator: delegation.validatorAddress,
// };

await claimRewards(addresses, client);
}, Promise.resolve())
// await claimRewards(addresses, client);
// }, Promise.resolve())
claimRewardsBatch(rewardClaimMessages, client, delegatorAddress)
.then(() => fetchUserDataAction(delegatorAddress, staking))
.then(() => {
setStep("completed");
Expand Down
2 changes: 2 additions & 0 deletions src/features/staking/components/validators-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ const ValidatorsTable = () => {
!staking.state.validators.unbonding ||
!staking.state.validators.bonded;

console.log({staking});

const validators =
currentTab === "active" ? activeValidators : inactiveValidators;

Expand Down
39 changes: 24 additions & 15 deletions src/features/staking/lib/core/tx.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import type {
Coin,
DeliverTxResponse,
MsgBeginRedelegateEncodeObject,
MsgDelegateEncodeObject,
MsgUndelegateEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
} from "@cosmjs/stargate";
import type { Coin, DeliverTxResponse, MsgBeginRedelegateEncodeObject, MsgDelegateEncodeObject, MsgUndelegateEncodeObject, MsgWithdrawDelegatorRewardEncodeObject } from "@cosmjs/stargate";
import BigNumber from "bignumber.js";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import {
MsgBeginRedelegate,
MsgCancelUnbondingDelegation,
MsgDelegate,
MsgUndelegate,
} from "cosmjs-types/cosmos/staking/v1beta1/tx";
import { MsgBeginRedelegate, MsgCancelUnbondingDelegation, MsgDelegate, MsgUndelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx";



import { FAUCET_CONTRACT_ADDRESS } from "@/config";
import { MIN_CLAIMABLE_XION } from "@/constants";



import type { Unbonding } from "../../context/state";
import { type AbstraxionSigningClient } from "./client";
import { getUXionCoinFromXion, normaliseCoin } from "./coins";
import { getCosmosFee } from "./fee";


const getTxCoin = (coin: Coin) => ({
amount: coin.amount,
denom: ["UXION", "XION"].includes(coin.denom.toUpperCase())
Expand Down Expand Up @@ -198,6 +191,22 @@ export const claimRewards = async (
.catch(handleTxError);
};

export const claimRewardsBatch = async (
rewardClaimMessages: MsgWithdrawDelegatorRewardEncodeObject[],
client: NonNullable<AbstraxionSigningClient>,
delegatorAddress: string,
) => {
const fee = await getCosmosFee({
address: delegatorAddress,
msgs: rewardClaimMessages,
});

return await client
.signAndBroadcast(delegatorAddress, rewardClaimMessages, fee)
.then(getTxVerifier("withdraw_rewards"))
.catch(handleTxError);
};

export const getCanClaimRewards = (rewards?: Coin) => {
if (!rewards) {
return false;
Expand Down Expand Up @@ -309,4 +318,4 @@ export const faucetFunds = async (
return await client
.execute(address, FAUCET_CONTRACT_ADDRESS, msg, "auto")
.catch(handleTxError);
};
};
Loading