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

decayed position fix #182

Merged
merged 3 commits into from
Dec 10, 2024
Merged
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
24 changes: 16 additions & 8 deletions src/components/PositionManager/PositionCallout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import {
EPOCH_LENGTH,
getMinDurationFmt,
getPositionVoteMultiplier,
getTimeLeftFromNowFmt,
Expand Down Expand Up @@ -32,14 +33,22 @@ export const PositionCallout: FC<{
setManagerAction,
handleClaimRewards,
}) => {
const { lockup, hasGenesisMultiplier } = position;
const { lockup, isDelegated, hasGenesisMultiplier } = position;
const { loading: loadingGov, network, mintAcc, subDaos } = useGovernance();
const unixNow = useSolanaUnixNow() || Date.now() / 1000;
const lockupKind = Object.keys(lockup.kind)[0] as string;
const isConstant = lockupKind === "constant";
const isDecayed = !isConstant && lockup.endTs.lte(new BN(unixNow));
const decayedEpoch = lockup.endTs.div(new BN(EPOCH_LENGTH));
const currentEpoch = new BN(unixNow).div(new BN(EPOCH_LENGTH));
const isDecayed =
!isConstant &&
(isDelegated
? currentEpoch.gt(decayedEpoch)
: lockup.endTs.lte(new BN(unixNow)));
const elapsedTime = new BN(unixNow).sub(lockup.startTs);
const totalTime = lockup.endTs.sub(lockup.startTs);
const totalTime = isDelegated
? decayedEpoch.add(new BN(1)).mul(new BN(EPOCH_LENGTH)).sub(lockup.startTs)
: lockup.endTs.sub(lockup.startTs);
const decayedPercentage = elapsedTime.muln(100).div(totalTime);
const canDelegate = network === "hnt";

Expand Down Expand Up @@ -93,11 +102,10 @@ export const PositionCallout: FC<{
<div className="flex flex-col gap-1">
<p className="text-base font-normal text-right">
{isConstant
? getMinDurationFmt(
position.lockup.startTs,
position.lockup.endTs
)
: getTimeLeftFromNowFmt(position.lockup.endTs)}{" "}
? getMinDurationFmt(lockup.startTs, lockup.endTs)
: isDelegated
? getTimeLeftFromNowFmt(lockup.endTs.add(new BN(EPOCH_LENGTH)))
: getTimeLeftFromNowFmt(lockup.endTs)}{" "}
time left
</p>
<div className="flex flex-row justify-end gap-2">
Expand Down
19 changes: 12 additions & 7 deletions src/components/PositionManager/PositionManager.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { onInstructions, secsToDays } from "@/lib/utils";
import { EPOCH_LENGTH, onInstructions, secsToDays } from "@/lib/utils";
import { useGovernance } from "@/providers/GovernanceProvider";
import {
useAnchorProvider,
Expand Down Expand Up @@ -97,6 +97,7 @@ export const PositionManager: FC<PositionManagerProps> = ({
position,
initAction,
}) => {
const unixNow = useSolanaUnixNow() || Date.now() / 1000;
const [action, setAction] = useState<PositionAction | undefined>(initAction);
const provider = useAnchorProvider();
const {
Expand All @@ -107,11 +108,15 @@ export const PositionManager: FC<PositionManagerProps> = ({
refetch: refetchState,
} = useGovernance();
const router = useRouter();
const { lockup } = position;

const { lockup, isDelegated } = position;
const isConstant = Object.keys(lockup.kind)[0] === "constant";
const unixNow = useSolanaUnixNow() || Date.now() / 1000;
const isDecayed = !isConstant && lockup.endTs.lte(new BN(unixNow));
const decayedEpoch = lockup.endTs.div(new BN(EPOCH_LENGTH));
const currentEpoch = new BN(unixNow).div(new BN(EPOCH_LENGTH));
const isDecayed =
!isConstant &&
(isDelegated
? currentEpoch.gt(decayedEpoch)
: lockup.endTs.lte(new BN(unixNow)));
const canDelegate = network === "hnt";
const mergablePositions: PositionWithMeta[] = useMemo(() => {
if (!unixNow || !positions || !positions.length) {
Expand Down Expand Up @@ -187,7 +192,7 @@ export const PositionManager: FC<PositionManagerProps> = ({
await unassignProxies({
positions: [position],
onInstructions: onInstructions(provider, {
useFirstEstimateForAll: true
useFirstEstimateForAll: true,
}),
});
} else {
Expand All @@ -196,7 +201,7 @@ export const PositionManager: FC<PositionManagerProps> = ({
recipient: new PublicKey(proxy || ""),
expirationTime: new BN(expirationTime || 0),
onInstructions: onInstructions(provider, {
useFirstEstimateForAll: true
useFirstEstimateForAll: true,
}),
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export const onInstructions =
: HELIUM_COMMON_LUT,
],
useFirstEstimateForAll,
computeScaleUp: useFirstEstimateForAll ? 1.4 : 1.1
computeScaleUp: useFirstEstimateForAll ? 1.4 : 1.1,
}
);
const asVersionedTx = transactions.map(toVersionedTx);
Expand Down Expand Up @@ -413,3 +413,5 @@ export function debounce<T extends unknown[], U>(
});
};
}

export const EPOCH_LENGTH = 60 * 60 * 24;
Loading