Skip to content

Commit

Permalink
bugfix: fix lock token amounts (#383)
Browse files Browse the repository at this point in the history
Signed-off-by: maryjanyes <[email protected]>
  • Loading branch information
maryjanyes authored Dec 13, 2024
1 parent 0271fe2 commit f3bfb89
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
25 changes: 16 additions & 9 deletions src/dex/hooks/governance/useFetchLockedGovToken.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { DexService, DEX_TOKEN_PRECISION_VALUE } from "@dex/services";
import { DEX_TOKEN_PRECISION_VALUE, DexService } from "@dex/services";
import { useQuery } from "react-query";
import { GovernanceQueries } from "./types";
import { isNil, isNotNil } from "ramda";
import { isNil } from "ramda";
import BigNumber from "bignumber.js";
import { AccountId } from "@hashgraph/sdk";
import { solidityAddressToAccountIdString } from "@shared/utils";

type UseFetchGODTokenQueryKey = [GovernanceQueries.FetchLockGODToken, string | undefined, string | undefined];

export function useFetchLockedGovToken(accountId: string | undefined, tokenHolderAddress: string) {
export function useFetchLockedGovToken(
accountId: string | undefined,
tokenHolderAddress: string,
tokenPrecision?: number
) {
function formatBalance(events: Map<string, any[]> | undefined) {
if (isNil(events)) return 0;
const amountEventArray = events.get("UpdatedAmount") ?? [];
const accountAddress = AccountId.fromString(accountId ?? "").toSolidityAddress();
const lockTokenDetails = amountEventArray.find(
(item) => solidityAddressToAccountIdString(item.user) === solidityAddressToAccountIdString(accountAddress)
);
return isNotNil(lockTokenDetails?.idOrAmount)
? BigNumber(lockTokenDetails.idOrAmount).shiftedBy(-DEX_TOKEN_PRECISION_VALUE).toNumber()
: 0;
const lockTokenTotalAmount = amountEventArray
.filter(
(item) => solidityAddressToAccountIdString(item.user) === solidityAddressToAccountIdString(accountAddress)
)
.reduce((total, item) => {
return total.plus(new BigNumber(item.idOrAmount).shiftedBy(tokenPrecision ?? DEX_TOKEN_PRECISION_VALUE));
}, new BigNumber(0));

return (lockTokenTotalAmount || new BigNumber(0)).toNumber();
}

return useQuery<Map<string, any[]> | undefined, Error, number, UseFetchGODTokenQueryKey>(
Expand Down
2 changes: 1 addition & 1 deletion src/dex/hooks/governance/useUnlockGODToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface UseUnLockGODTokenParams {
export function useUnlockGODToken(
accountId: string | undefined,
tokenHolderAddress: string | undefined,
tokenDecimals: string,
tokenDecimals: string = "0",
handleOnSuccess: HandleOnSuccess
) {
const queryClient = useQueryClient();
Expand Down
4 changes: 2 additions & 2 deletions src/dex/pages/Governance/VotingPower/useManageVotingPower.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export function useManageVotingPower(governanceTokenId: string, tokenHolderAddre
const { data: token } = useToken(governanceTokenId);
const govTokenBalance = useTokenBalance({ tokenId: governanceTokenId });

const lockedGOVToken = useFetchLockedGovToken(walletId, tokenHolderAddress);
const lockedGOVToken = useFetchLockedGovToken(walletId, tokenHolderAddress, Number(token?.data.decimals));
const canClaimGODTokens = useCanUserUnlockGODToken(walletId, tokenHolderAddress);
const lockGODTokenSubmit = useLockGODToken(walletId, tokenHolderAddress, handleLockedGODTokenSuccess);
const unLockGODTokenSubmit = useUnlockGODToken(
walletId,
tokenHolderAddress,
token?.data.decimals ?? "0",
token?.data.decimals,
handleUnLockedGODTokenSuccess
);

Expand Down

0 comments on commit f3bfb89

Please sign in to comment.