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

[HIP-124] Helium-vote implementation #169

Open
wants to merge 6 commits 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
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/basic-features/typescript for more information.
38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
},
"dependencies": {
"@coral-xyz/anchor": "^0.29.0",
"@helium/account-fetch-cache": "^0.9.7",
"@helium/account-fetch-cache-hooks": "^0.9.7",
"@helium/helium-react-hooks": "^0.9.7",
"@helium/modular-governance-hooks": "^0.0.13",
"@helium/modular-governance-idls": "^0.0.13",
"@helium/organization-sdk": "^0.0.13",
"@helium/spl-utils": "^0.9.7",
"@helium/state-controller-sdk": "^0.0.13",
"@helium/voter-stake-registry-hooks": "^0.9.7",
"@helium/voter-stake-registry-sdk": "^0.9.7",
"@helium/account-fetch-cache": "^0.9.10",
"@helium/account-fetch-cache-hooks": "^0.9.10",
"@helium/helium-react-hooks": "^0.9.10",
"@helium/modular-governance-hooks": "^0.0.14",
"@helium/modular-governance-idls": "^0.0.14",
"@helium/organization-sdk": "^0.0.14",
"@helium/position-voting-rewards-sdk": "^0.9.10",
"@helium/spl-utils": "^0.9.10",
"@helium/state-controller-sdk": "^0.0.14",
"@helium/voter-stake-registry-hooks": "^0.9.10",
"@helium/voter-stake-registry-sdk": "^0.9.10",
"@hookform/resolvers": "^3.3.4",
"@metaplex-foundation/mpl-token-metadata": "2.10.0",
"@project-serum/anchor": "^0.26.0",
Expand Down Expand Up @@ -71,17 +72,18 @@
"yargs": "^17.7.2"
},
"resolutions": {
"@helium/position-voting-rewards-sdk": "^0.9.10",
"@tanstack/react-query": "5.45.1",
"@solana/web3.js": "^1.90.0",
"@helium/account-fetch-cache": "^0.9.7",
"@helium/account-fetch-cache-hooks": "^0.9.7",
"@helium/helium-react-hooks": "^0.9.7",
"@helium/voter-stake-registry-hooks": "^0.9.7",
"@helium/modular-governance-idls": "^0.0.13",
"@helium/spl-utils": "^0.9.7",
"@helium/modular-governance-hooks": "^0.0.13",
"@helium/account-fetch-cache": "^0.9.10",
"@helium/account-fetch-cache-hooks": "^0.9.10",
"@helium/helium-react-hooks": "^0.9.10",
"@helium/voter-stake-registry-hooks": "^0.9.10",
"@helium/modular-governance-idls": "^0.0.14",
"@helium/spl-utils": "^0.9.10",
"@helium/modular-governance-hooks": "^0.0.14",
"@solana/wallet-adapter-react": "^0.15.35",
"@helium/voter-stake-registry-sdk": "^0.9.7"
"@helium/voter-stake-registry-sdk": "^0.9.10"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.10",
Expand Down
18 changes: 10 additions & 8 deletions scripts/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function run(args: any = process.argv) {
},
realmName: {
type: "string",
default: "Helium",
required: true,
},
name: {
type: "string",
Expand Down Expand Up @@ -80,15 +80,17 @@ export async function run(args: any = process.argv) {
// Must have 100,000,000 veHNT, 67% of the vote. Choose the top one as the winner.
const nodes = settings()
.and(
settings().offsetFromStartTs(new anchor.BN(60 * 60 * 24 * 7)),
settings().and(
settings().top(1),
settings().and(
settings().choicePercentage(67),
// 100,000,000 veHNT
settings().choiceVoteWeight(new anchor.BN(argv.threshold))
)
),
settings().offsetFromStartTs(new anchor.BN(60 * 60 * 24 * 7))
settings().totalWeight(new anchor.BN(argv.threshold)),
settings().and(
settings().not("Abstain"),
settings().choicePercentageOfCurrent(67)
)
),
settings().top(1)
)
)
.build();

Expand Down
1 change: 1 addition & 0 deletions src/components/CreatePositionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const CreatePositionModal: FC<React.PropsWithChildren<{}>> = ({
}
} catch (e: any) {
setIsSubmitting(false);
console.error(e)
ChewingGlass marked this conversation as resolved.
Show resolved Hide resolved
if (!(e instanceof WalletSignTransactionError)) {
toast(e.message || "Position creation failed, please try again");
}
Expand Down
46 changes: 33 additions & 13 deletions src/components/PositionManager/ExtendPositionPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import {
PositionWithMeta,
calcLockupMultiplier,
useEnrolledPosition,
} from "@helium/voter-stake-registry-hooks";
import React, { FC, useCallback, useState } from "react";
import React, { FC, useCallback, useMemo, useState } from "react";
import { LockTokensForm, LockTokensFormValues } from "../LockTokensForm";
import { StepIndicator } from "../StepIndicator";
import { useGovernance } from "@/providers/GovernanceProvider";
Expand All @@ -19,6 +20,7 @@ import { Button } from "../ui/button";
import { Loader2, X } from "lucide-react";
import { FaCircleArrowRight } from "react-icons/fa6";
import { ConfirmationItem } from "./ConfirmationItem";
import { enrolledPositionKey } from "@helium/position-voting-rewards-sdk";

export const ExtendPositionPrompt: FC<{
position: PositionWithMeta;
Expand All @@ -41,6 +43,13 @@ export const ExtendPositionPrompt: FC<{
)
: secsToDays(position.lockup.endTs.sub(new BN(unixNow)).toNumber())
);
const enrolledPositionK = useMemo(
() => enrolledPositionKey(position.pubkey)[0],
[position.pubkey]
);
const enrolledPosition = useEnrolledPosition(enrolledPositionK);
const numVotedProposals =
(enrolledPosition && enrolledPosition.info?.recentProposals.length) || 0;

const handleCalcLockupMultiplier = useCallback(
(lockupPeriodInDays: number) =>
Expand Down Expand Up @@ -88,19 +97,29 @@ export const ExtendPositionPrompt: FC<{
)}
</div>
{step === 1 && (
<LockTokensForm
initValues={formValues}
mode="extend"
submitText="Next"
maxLockupAmount={maxActionableAmount}
minLockupTimeInDays={minLockupTimeInDays}
maxLockupTimeInDays={secsToDays(
votingMint.lockupSaturationSecs.toNumber()
<div className="flex flex-col gap-2">
{numVotedProposals > 0 && (
<span className="text-red-500">
{numVotedProposals >= 2 &&
"Position is currently eligible for voting rewards. If you extend this position, you will need to vote on 2 more proposals to gain eligibility again."}
{numVotedProposals === 1 &&
"You have voted on 1 proposal. Extending your position resets your voting progress. If you extend this position, you will need to vote on 2 more proposals before this position is eligible for rewards."}
</span>
)}
calcMultiplierFn={handleCalcLockupMultiplier}
onCancel={onCancel}
onSubmit={handleFormSubmit}
/>
<LockTokensForm
initValues={formValues}
mode="extend"
submitText="Next"
maxLockupAmount={maxActionableAmount}
minLockupTimeInDays={minLockupTimeInDays}
maxLockupTimeInDays={secsToDays(
votingMint.lockupSaturationSecs.toNumber()
)}
calcMultiplierFn={handleCalcLockupMultiplier}
onCancel={onCancel}
onSubmit={handleFormSubmit}
/>
</div>
)}
{step === 2 && (
<>
Expand Down Expand Up @@ -141,6 +160,7 @@ export const ExtendPositionPrompt: FC<{
>
Go Back
</Button>

<Button
className="flex-1 text-foreground gap-2"
disabled={isSubmitting}
Expand Down
21 changes: 19 additions & 2 deletions src/components/PositionManager/MergePositionPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import {
precision,
} from "@/lib/utils";
import { useGovernance } from "@/providers/GovernanceProvider";
import { PositionWithMeta } from "@helium/voter-stake-registry-hooks";
import { PositionWithMeta, useEnrolledPosition } from "@helium/voter-stake-registry-hooks";
import { Mint } from "@solana/spl-token";
import { PublicKey } from "@solana/web3.js";
import classNames from "classnames";
import { Loader2, X } from "lucide-react";
import React, { FC, useState } from "react";
import React, { FC, useMemo, useState } from "react";
import { FaCircleArrowRight } from "react-icons/fa6";
import { PositionCard } from "../PositionCard";
import { StepIndicator } from "../StepIndicator";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { ConfirmationItem } from "./ConfirmationItem";
import { enrolledPositionKey } from "@helium/position-voting-rewards-sdk";

const PositionBlurb = ({
position,
Expand Down Expand Up @@ -77,6 +78,14 @@ export const MergePositionPrompt: FC<{
const mintMinAmount = mintAcc ? getMintMinAmountAsDecimal(mintAcc) : 1;
const currentPrecision = precision(mintMinAmount);

const enrolledPositionK = useMemo(
() => enrolledPositionKey(position.pubkey)[0],
[position.pubkey]
);
const enrolledPosition = useEnrolledPosition(enrolledPositionK);
const numVotedProposals =
(enrolledPosition && enrolledPosition.info?.recentProposals.length) || 0;

const selectedPosition =
selectedPosPk && positions.find((pos) => pos.pubkey.equals(selectedPosPk))!;

Expand Down Expand Up @@ -192,6 +201,14 @@ export const MergePositionPrompt: FC<{
);
})}
</div>
{numVotedProposals > 0 && (
<span className="text-red-500">
{numVotedProposals >= 2 &&
"Position is currently eligible for voting rewards. If you extend this position, you will need to vote on 2 more proposals to gain eligibility again."}
{numVotedProposals === 1 &&
"You have voted on 1 proposal. Extending your position resets your voting progress. If you extend this position, you will need to vote on 2 more proposals before this position is eligible for rewards."}
</span>
)}
<div className="flex flex-row flex-1 gap-2 w-full">
<Button variant="secondary" className="flex-1" onClick={onCancel}>
Cancel
Expand Down
46 changes: 33 additions & 13 deletions src/components/PositionManager/SplitPositionPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import { useSolanaUnixNow } from "@helium/helium-react-hooks";
import {
PositionWithMeta,
calcLockupMultiplier,
useEnrolledPosition,
} from "@helium/voter-stake-registry-hooks";
import BN from "bn.js";
import { Loader2, X } from "lucide-react";
import React, { FC, useCallback, useState } from "react";
import React, { FC, useCallback, useMemo, useState } from "react";
import { FaCircleArrowDown } from "react-icons/fa6";
import { LockTokensForm, LockTokensFormValues } from "../LockTokensForm";
import { PositionCard } from "../PositionCard";
import { StepIndicator } from "../StepIndicator";
import { Button } from "../ui/button";
import { ConfirmationItem } from "./ConfirmationItem";
import { enrolledPositionKey } from "@helium/position-voting-rewards-sdk";

export const SplitPositionPrompt: FC<{
position: PositionWithMeta;
Expand All @@ -45,6 +47,14 @@ export const SplitPositionPrompt: FC<{
: secsToDays(position.lockup.endTs.sub(new BN(unixNow)).toNumber())
);

const enrolledPositionK = useMemo(
() => enrolledPositionKey(position.pubkey)[0],
[position.pubkey]
);
const enrolledPosition = useEnrolledPosition(enrolledPositionK);
const numVotedProposals =
(enrolledPosition && enrolledPosition.info?.recentProposals.length) || 0;

const handleCalcLockupMultiplier = useCallback(
(lockupPeriodInDays: number) =>
(registrar &&
Expand Down Expand Up @@ -94,19 +104,29 @@ export const SplitPositionPrompt: FC<{
)}
</div>
{step === 1 && (
<LockTokensForm
initValues={formValues}
mode="split"
submitText="Next"
maxLockupAmount={maxActionableAmount}
minLockupTimeInDays={minLockupTimeInDays}
maxLockupTimeInDays={secsToDays(
votingMint.lockupSaturationSecs.toNumber()
<div className="flex flex-col gap-2">
{numVotedProposals > 0 && (
<span className="text-red-500">
{numVotedProposals >= 2 &&
"Position is currently eligible for voting rewards. If you extend this position, you will need to vote on 2 more proposals to gain eligibility again."}
{numVotedProposals === 1 &&
"You have voted on 1 proposal. Extending your position resets your voting progress. If you extend this position, you will need to vote on 2 more proposals before this position is eligible for rewards."}
</span>
)}
calcMultiplierFn={handleCalcLockupMultiplier}
onCancel={onCancel}
onSubmit={handleFormSubmit}
/>
<LockTokensForm
initValues={formValues}
mode="split"
submitText="Next"
maxLockupAmount={maxActionableAmount}
minLockupTimeInDays={minLockupTimeInDays}
maxLockupTimeInDays={secsToDays(
votingMint.lockupSaturationSecs.toNumber()
)}
calcMultiplierFn={handleCalcLockupMultiplier}
onCancel={onCancel}
onSubmit={handleFormSubmit}
/>
</div>
)}
{step === 2 && formValues && (
<>
Expand Down
53 changes: 26 additions & 27 deletions src/components/Positions.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"use client";

import { onInstructions } from "@/lib/utils";
import { useGovernance } from "@/providers/GovernanceProvider";
import React, { FC, useMemo } from "react";
import { Button } from "./ui/button";
import { FaStar } from "react-icons/fa6";
import { Loader2 } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
import BN from "bn.js";
import { useAnchorProvider } from "@helium/helium-react-hooks";
import { useClaimAllPositionsRewards } from "@helium/voter-stake-registry-hooks";
import { PositionCard, PositionCardSkeleton } from "./PositionCard";
import { WalletSignTransactionError } from "@solana/wallet-adapter-base";
import { useWallet } from "@solana/wallet-adapter-react";
import BN from "bn.js";
import { Loader2 } from "lucide-react";
import { FC, useMemo } from "react";
import { FaStar } from "react-icons/fa6";
import { toast } from "sonner";
import { Skeleton } from "./ui/skeleton";
import { CreatePositionButton } from "./CreatePositionButton";
import { onInstructions } from "@/lib/utils";
import { useAnchorProvider, useSolanaUnixNow } from "@helium/helium-react-hooks";
import { ContentSection } from "./ContentSection";
import { WalletSignTransactionError } from "@solana/wallet-adapter-base";
import { CreatePositionButton } from "./CreatePositionButton";
import { PositionCard, PositionCardSkeleton } from "./PositionCard";
import { Button } from "./ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
import { Skeleton } from "./ui/skeleton";

export const Positions: FC = () => {
const provider = useAnchorProvider();
Expand Down Expand Up @@ -100,6 +100,7 @@ export const Positions: FC = () => {
toast("Rewards claimed!");
refetchState();
} catch (e: any) {
console.error(e)
if (!(e instanceof WalletSignTransactionError)) {
toast(e.message || "Claiming rewards failed, please try again");
}
Expand Down Expand Up @@ -142,21 +143,19 @@ export const Positions: FC = () => {
<h4>All Positions</h4>
<div className="flex max-md:flex-col gap-2">
<CreatePositionButton showText />
{network === "hnt" && (
<Button
variant="default"
className="text-foreground flex flex-row gap-2 items-center"
disabled={!hasRewards || claimingAllRewards}
onClick={handleClaimRewards}
>
{claimingAllRewards ? (
<Loader2 className="size-4 animate-spin" />
) : (
<FaStar className="size-4" />
)}
{claimingAllRewards ? "Claiming Rewards..." : "Claim Rewards"}
</Button>
)}
<Button
variant="default"
className="text-foreground flex flex-row gap-2 items-center"
disabled={!hasRewards || claimingAllRewards}
onClick={handleClaimRewards}
>
{claimingAllRewards ? (
<Loader2 className="size-4 animate-spin" />
) : (
<FaStar className="size-4" />
)}
{claimingAllRewards ? "Claiming Rewards..." : "Claim Rewards"}
</Button>
</div>
</div>
{!notDecayedPositions?.length && !decayedPositions?.length && (
Expand Down
Loading