diff --git a/src/api/staking.ts b/src/api/staking.ts index 466756655..765d88200 100644 --- a/src/api/staking.ts +++ b/src/api/staking.ts @@ -1,6 +1,6 @@ //@ts-nocheck import { ApiPromise } from "@polkadot/api" -import { useQuery } from "@tanstack/react-query" +import { useMutation, useQuery } from "@tanstack/react-query" import { QUERY_KEYS } from "utils/queryKeys" import BN from "bignumber.js" import { getUniques } from "./uniques" @@ -8,6 +8,7 @@ import { getReferendumInfoOf } from "./democracy" import request, { gql } from "graphql-request" import { PROVIDERS, useProviderRpcUrlStore } from "./provider" import { useRpcProvider } from "providers/rpcProvider" +import { useAccount } from "sections/web3-connect/Web3Connect.utils" interface ISubscanData { code: number @@ -327,3 +328,39 @@ const getStakingPositionBalances = )), } } + +export const useProcessedVotesIds = () => { + const { account } = useAccount() + const { api } = useRpcProvider() + + return useMutation(async () => { + if (!account) { + return [] + } + + const processedVotesRes = await api.query.staking.processedVotes.entries( + account.address, + ) + + const ids = processedVotesRes.map(([processedVote]) => { + const [, id] = processedVote.toHuman() as string[] + + return id + }) + + return ids + }) +} + +export const usePositionVotesIds = () => { + const { api } = useRpcProvider() + + return useMutation(async (positionId: number) => { + const positionVotesRes = await api.query.staking.positionVotes(positionId) + const positionVotesIds = positionVotesRes.votes.map((position) => + position[0].toString(), + ) + + return positionVotesIds + }) +} diff --git a/src/sections/staking/sections/dashboard/components/StakingInputSection/Stake/Stake.tsx b/src/sections/staking/sections/dashboard/components/StakingInputSection/Stake/Stake.tsx index 648ddf774..8a27d1fd1 100644 --- a/src/sections/staking/sections/dashboard/components/StakingInputSection/Stake/Stake.tsx +++ b/src/sections/staking/sections/dashboard/components/StakingInputSection/Stake/Stake.tsx @@ -16,6 +16,7 @@ import { TOAST_MESSAGES } from "state/toasts" import { useRpcProvider } from "providers/rpcProvider" import { useAccount } from "sections/web3-connect/Web3Connect.utils" import { Web3ConnectModalButton } from "sections/web3-connect/modal/Web3ConnectModalButton" +import { useProcessedVotesIds } from "api/staking" export const Stake = ({ loading, @@ -36,6 +37,8 @@ export const Stake = ({ const { account } = useAccount() const form = useForm<{ amount: string }>() + const processedVotes = useProcessedVotesIds() + const onSubmit = async (values: FormValues) => { const amount = getFixedPointAmount(values.amount, 12).toString() @@ -62,9 +65,18 @@ export const Stake = ({ }, {} as ToastMessage) if (isStakePosition) { + const processedVoteIds = await processedVotes.mutateAsync() + transaction = await createTransaction( { - tx: api.tx.staking.increaseStake(positionId, amount), + tx: processedVoteIds.length + ? api.tx.utility.batchAll([ + ...processedVoteIds.map((id) => + api.tx.democracy.removeVote(id), + ), + api.tx.staking.increaseStake(positionId, amount), + ]) + : api.tx.staking.increaseStake(positionId, amount), }, { toast }, ) @@ -168,11 +180,7 @@ export const Stake = ({ {account ? ( -