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

feat: add proposal reward interval time #118

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import { useCommune } from "@commune-ts/providers/use-commune";
import { formatToken } from "@commune-ts/utils";

export default function ProposalRewardCard() {
const { rewardAllocation } = useCommune();
const {
rewardAllocation,
globalGovernanceConfig,
unrewardedProposals,
lastBlock,
} = useCommune();

console.log("proposalRewardInterval", globalGovernanceConfig);
console.log("lastBlock", lastBlock?.blockNumber);
console.log(unrewardedProposals);

return (
<div className="flex w-full animate-fade-down border-b border-white/20 py-2.5">
Expand All @@ -27,3 +36,17 @@ export default function ProposalRewardCard() {
</div>
);
}

function calc_target_reward_time(
current_block: bigint,
proposal_block: bigint,
reward_interval: bigint,
) {
// round up to the next reward interval

const _target_time =
((proposal_block + reward_interval - 1n) / reward_interval) *
reward_interval;

return _target_time;
}
15 changes: 15 additions & 0 deletions packages/providers/src/context/commune.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { createContext, useContext, useEffect, useState } from "react";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { toast } from "react-toastify";

import { GovernanceConfiguration } from "@commune-ts/subspace/queries";

import type {
AddCustomProposal,
AddDaoApplication,
Expand All @@ -33,6 +35,7 @@ import {
useCustomMetadata,
useDaos,
useDaoTreasury,
useGlobalGovernanceConfig,
useLastBlock,
useNotDelegatingVoting,
useProposals,
Expand Down Expand Up @@ -97,6 +100,9 @@ interface CommuneContextType {
rewardAllocation: bigint | null | undefined;
isRewardAllocationLoading: boolean;

globalGovernanceConfig: GovernanceConfiguration | undefined;
isGlobalGovernanceConfigLoading: boolean;

stakeOut: StakeOutData | undefined;
isStakeOutLoading: boolean;

Expand Down Expand Up @@ -446,6 +452,12 @@ export function CommuneProvider({
const { data: rewardAllocation, isLoading: isRewardAllocationLoading } =
useRewardAllocation(lastBlock?.apiAtBlock);

// Reward Allocation
const {
data: globalGovernanceConfig,
isLoading: isGlobalGovernanceConfigLoading,
} = useGlobalGovernanceConfig(lastBlock?.apiAtBlock);

// Stake Out
const { data: stakeOut, isLoading: isStakeOutLoading } = useAllStakeOut(
lastBlock?.apiAtBlock,
Expand Down Expand Up @@ -541,6 +553,9 @@ export function CommuneProvider({
notDelegatingVoting,
isNotDelegatingVotingLoading,

globalGovernanceConfig,
isGlobalGovernanceConfigLoading,

unrewardedProposals,
isUnrewardedProposalsLoading,

Expand Down
11 changes: 11 additions & 0 deletions packages/providers/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
queryBalance,
queryDaosEntries,
queryDaoTreasuryAddress,
queryGlobalGovernanceConfig,
queryLastBlock,
queryNotDelegatingVotingPower,
queryProposalsEntries,
Expand Down Expand Up @@ -125,6 +126,16 @@ export function useRewardAllocation(api: Api | Nullish) {
});
}

export function useGlobalGovernanceConfig(api: Api | Nullish) {
return useQuery({
queryKey: ["global_governance_config"],
enabled: api != null,
queryFn: () => queryGlobalGovernanceConfig(api!),
staleTime: LAST_BLOCK_STALE_TIME,
refetchOnWindowFocus: false,
});
}

// == subspaceModule ==

export function useAllStakeOut(api: Api | Nullish) {
Expand Down
4 changes: 3 additions & 1 deletion packages/subspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "@polkadot/api-augment";

import { ApiPromise, WsProvider } from "@polkadot/api";

// import { something } from "./queries";
import { queryUnrewardedProposals } from "./queries";

// == To run this file: npx tsx index.ts ==

Expand All @@ -22,3 +22,5 @@ if (!api.isConnected) {
console.log("API connected");

// test something here

console.log(await queryUnrewardedProposals(api));
11 changes: 10 additions & 1 deletion packages/subspace/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export async function queryUnrewardedProposals(api: Api): Promise<number[]> {
const unrewardedProposals =
await api.query.governanceModule?.unrewardedProposals?.entries();

console.log("unrewardedProposals", unrewardedProposals);

if (!unrewardedProposals) {
throw new Error("unrewardedProposals is falsey");
}
Expand All @@ -100,11 +102,18 @@ export async function queryUnrewardedProposals(api: Api): Promise<number[]> {
.filter((id): id is number => !isNaN(id));
}

export async function queryUnrewardedProposalsN(api: Api, n: number) {
const unrewardedProposals =
await api.query.governanceModule?.unrewardedProposals?.();

return unrewardedProposals;
}

enum VoteMode {
Vote,
}

interface GovernanceConfiguration {
export interface GovernanceConfiguration {
proposalCost?: bigint;
proposalExpiration?: number;
voteMode?: VoteMode;
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading