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

bypass unlocking when lock period is zero #253

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Button, Fieldset, Input, RacetrackSpinnerIcon } from "@sifchain/ui";
import BigNumber from "bignumber.js";
import { clamp } from "rambda";
import { ChangeEventHandler, FormEventHandler, useCallback, useMemo } from "react";
import { ChangeEventHandler, FormEvent, FormEventHandler, useCallback, useMemo } from "react";
import { useLiquidityProviderQuery } from "~/domains/clp";
import useUnlockLiquidity from "~/domains/clp/formHooks/useUnlockLiquidity";
import useUnlockLiquidityMutation from "~/domains/clp/hooks/useUnlockLiquidityMutation";
import { useTokenRegistryQuery } from "~/domains/tokenRegistry";
import type { ManageLiquidityModalProps } from "./types";
import {useRewardParams} from "~/domains/clp/hooks/useRewardParams";
import useRemoveLiquidityMutation from "~/domains/clp/hooks/useRemoveLiquidityMutation";

const UnlockLiquidityForm = (props: ManageLiquidityModalProps) => {
const liquidityProviderQuery = useLiquidityProviderQuery(props.denom);
const rewardParams = useRewardParams();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanrsoares this is returning undefined, can you see any issues?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the client might be outdated


const { indexedByDenom } = useTokenRegistryQuery();
const externalToken = indexedByDenom[props.denom];
Expand All @@ -22,23 +25,30 @@ const UnlockLiquidityForm = (props: ManageLiquidityModalProps) => {
externalAssetAmount,
} = useUnlockLiquidity(props.denom);

const removeLiquidityMutation = useRemoveLiquidityMutation();

const buttonMessage = useMemo(() => {
if (!unlockLiquidityMutation.isReady) {
return "Please connect Sifchain wallet";
}
if (unlockLiquidityMutation.isError || unlockLiquidityMutation.isSuccess) {
return "Close";
}
var msg = "Unbond liquidity"
if (rewardParams.data?.params?.liquidityRemovalLockPeriod.isZero()) {
msg = "Remove liquidity"
}

return <>{unlockLiquidityMutation.isLoading && <RacetrackSpinnerIcon />}Unbond liquidity</>;
return <>{unlockLiquidityMutation.isLoading && <RacetrackSpinnerIcon />}{msg}</>;
}, [
unlockLiquidityMutation.isError,
unlockLiquidityMutation.isLoading,
unlockLiquidityMutation.isReady,
unlockLiquidityMutation.isSuccess,
rewardParams,
]);

const onSubmit = useCallback<FormEventHandler<HTMLFormElement>>(
const onSubmitUnlock = useCallback<FormEventHandler<HTMLFormElement>>(
(event) => {
event.preventDefault();

Expand All @@ -58,6 +68,27 @@ const UnlockLiquidityForm = (props: ManageLiquidityModalProps) => {
[liquidityProviderQuery.data?.liquidityProvider?.unlocks, props, units, unlockLiquidityMutation],
);

const onSubmitRemove = useCallback<FormEventHandler<HTMLFormElement>>(
(event) => {
event.preventDefault();

if (removeLiquidityMutation.isSuccess || removeLiquidityMutation.isError) {
props.onClose(false);
} else {
removeLiquidityMutation.mutate({ denom: props.denom, units });
}
},
[props, units, removeLiquidityMutation],
);

const onSubmit = (event: FormEvent<HTMLFormElement>) => {
if (rewardParams.data?.params?.liquidityRemovalLockPeriod.isZero()) {
return onSubmitRemove(event);
} else {
return onSubmitUnlock(event);
}
};

const onChangePercentageInput = useCallback<ChangeEventHandler<HTMLInputElement>>(
(event) => {
const value = event.target.value.replace(".", "").trim();
Expand Down
34 changes: 34 additions & 0 deletions apps/dex/src/domains/clp/hooks/useRewardParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Decimal } from "@cosmjs/math";
import { useQuery } from "@tanstack/react-query";
import invariant from "tiny-invariant";

import { useDexEnvironment } from "~/domains/core/envs";
import { useTokenRegistryQuery } from "~/domains/tokenRegistry";
import useSifnodeQuery from "~/hooks/useSifnodeQuery";

export function useRewardParams(
options: {
refetchInterval?: number;
enabled?: boolean;
} = {},
) {
const { data: paramsRes } = useSifnodeQuery("clp.getRewardParams", [{}]);
const { data: env } = useDexEnvironment();


return useQuery(
["rewardParams"],
() => {
invariant(paramsRes !== undefined, "paramsRes is undefined");
invariant(env !== undefined, "env is undefined");

return {
...paramsRes,
};
},
{
enabled: paramsRes !== undefined && env !== undefined,
...options,
},
);
}
39 changes: 0 additions & 39 deletions package.json

This file was deleted.

Loading