Skip to content

Commit

Permalink
Fix Allocations problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Corantin committed Nov 16, 2024
1 parent 164a26b commit 27cb081
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use client";
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { Hashicon } from "@emeraldpay/hashicon-react";
import { InformationCircleIcon, UserIcon } from "@heroicons/react/24/outline";
import { ArrowPathIcon } from "@heroicons/react/24/solid";
import { toast } from "react-toastify";
import { Address, encodeAbiParameters, formatUnits } from "viem";
import { useAccount, useToken } from "wagmi";
Expand Down Expand Up @@ -44,6 +45,7 @@ export default function Page({
garden: string;
};
}) {
const [convictionRefreshing, setConvictionRefreshing] = useState(true);
const { isDisconnected, address } = useAccount();
const [, proposalNumber] = proposalId.split("-");
const { data } = useSubgraphQuery<getProposalDataQuery>({
Expand Down Expand Up @@ -104,6 +106,12 @@ export default function Page({
enabled: proposalData?.proposalNumber != null,
});

useEffect(() => {
if (convictionRefreshing && currentConvictionPct != null) {
setConvictionRefreshing(false);
}
}, [convictionRefreshing, currentConvictionPct]);

//encode proposal id to pass as argument to distribute function
const encodedDataProposalId = (proposalId_: bigint) => {
const encodedProposalId = encodeAbiParameters(
Expand Down Expand Up @@ -157,6 +165,14 @@ export default function Page({
);
}

const handleRefreshConviction = async (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
setConvictionRefreshing(true);
await triggerConvictionRefetch?.();
setConvictionRefreshing(false);
};

const status = ProposalStatus[proposalData.proposalStatus];

return (
Expand Down
6 changes: 3 additions & 3 deletions apps/web/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ButtonProps = {
| "tooltip-bottom"
| "tooltip-left"
| "tooltip-right";
children: React.ReactNode;
children?: React.ReactNode;
isLoading?: boolean;
size?: Size;
icon?: React.ReactNode;
Expand Down Expand Up @@ -70,7 +70,7 @@ const btnStyles: BtnStyles = {

export function Button({
onClick,
className: styles = "",
className = "",
disabled = false,
tooltip,
showToolTip = false,
Expand All @@ -86,7 +86,7 @@ export function Button({
const buttonElement = (
<button
type={type}
className={`${btnStyles[btnStyle][disabled ? "disabled" : color]} flex relative cursor-pointer justify-center rounded-lg px-6 py-4 transition-all ease-out disabled:cursor-not-allowed h-fit ${styles}`}
className={`${btnStyles[btnStyle][disabled ? "disabled" : color]} flex relative cursor-pointer justify-center rounded-lg px-6 py-4 transition-all ease-out disabled:cursor-not-allowed h-fit ${className}`}
onClick={onClick}
disabled={disabled || isLoading}
>
Expand Down
48 changes: 41 additions & 7 deletions apps/web/components/Charts/ConvictionBarChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"use client";

import React, { useMemo } from "react";
import React, { useEffect, useMemo, useState } from "react";
import { ArrowPathIcon } from "@heroicons/react/24/solid";
import type { EChartsOption, MarkLineComponentOption } from "echarts";
import EChartsReact from "echarts-for-react";
import { ChartWrapper } from "./ChartWrapper";
import { Button } from "../Button";
import { Countdown } from "../Countdown";
import { Skeleton } from "../Skeleton";

type ScenarioMapping = {
condition: () => boolean;
Expand All @@ -20,6 +23,7 @@ type ConvictionBarChartProps = {
compact?: boolean;
timeToPass?: number;
onReadyToExecute?: () => void;
refreshConviction?: () => Promise<any>;
};

export const ConvictionBarChart = ({
Expand All @@ -31,7 +35,9 @@ export const ConvictionBarChart = ({
compact,
timeToPass,
onReadyToExecute,
refreshConviction,
}: ConvictionBarChartProps) => {
const [convictionRefreshing, setConvictionRefreshing] = useState(true);
const supportNeeded = (thresholdPct - proposalSupportPct).toFixed(2);
const scenarioMappings: Record<string, ScenarioMapping> = {
//1-SignalingType) Support > 0 && > Conviction
Expand Down Expand Up @@ -174,6 +180,20 @@ export const ConvictionBarChart = ({
);
}, [timeToPass, currentConvictionPct, proposalSupportPct, thresholdPct]);

useEffect(() => {
if (convictionRefreshing && currentConvictionPct != null) {
setConvictionRefreshing(false);
}
}, [convictionRefreshing, currentConvictionPct]);

const handleRefreshConviction = async (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
setConvictionRefreshing(true);
await refreshConviction?.();
setConvictionRefreshing(false);
};

const supportGtConv = proposalSupportPct > currentConvictionPct;
const convEqSupport = proposalSupportPct === currentConvictionPct;

Expand Down Expand Up @@ -327,23 +347,37 @@ export const ConvictionBarChart = ({
Number(supportNeeded) < 0 &&
(currentConvictionPct ?? 0) < (thresholdPct ?? 0);

return (
const chart = (
<>
{compact ?
<Skeleton isLoading={convictionRefreshing}>
<EChartsReact
option={option}
style={{ height: "100%", width: "100%" }}
className="cursor-default"
/>
</Skeleton>
<Button
btnStyle="link"
onClick={handleRefreshConviction}
tooltip="Refresh conviction"
className="!p-3"
>
<ArrowPathIcon className="w-5" />
</Button>
</>
);

return (
<>
{compact ?
chart
: <>
<ChartWrapper
message={isSignalingType ? undefined : message}
growing={growing}
isSignalingType={isSignalingType}
>
<EChartsReact
option={option}
style={{ height: "100%", width: "100%" }}
/>
{chart}
</ChartWrapper>
{scenarioMappings.supportLTConvictionLTThreshold &&
proposalWillPass &&
Expand Down
3 changes: 2 additions & 1 deletion apps/web/components/ProposalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ export function ProposalCard({
</div>
<ProposalCountDown />
</div>
<div className="h-3">
<div className="h-3 flex items-center">
<ConvictionBarChart
compact
currentConvictionPct={currentConvictionPct}
thresholdPct={isSignalingType ? 0 : thresholdPct}
proposalSupportPct={totalSupportPct}
isSignalingType={isSignalingType}
proposalNumber={proposalNumber}
refreshConviction={triggerConvictionRefetch}
/>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions apps/web/components/Proposals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ export function Proposals({

useEffect(() => {
setMemberActivatedPoints(
Number(memberData?.member?.memberCommunity?.[0]?.stakedTokens ?? 0n),
Number(memberStrategyData?.memberStrategy?.activatedPoints ?? 0n),
);
}, [memberData?.member?.memberCommunity?.[0]?.stakedTokens]);
}, [memberStrategyData?.memberStrategy?.activatedPoints]);

useEffect(() => {
if (memberActivatedStrategy === false) {
Expand Down
3 changes: 3 additions & 0 deletions apps/web/hooks/useContractWriteWithConfirmations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UserRejectedRequestError,
} from "viem";
import {
useAccount,
useChainId,
useContractWrite,
UseContractWriteConfig,
Expand Down Expand Up @@ -48,6 +49,7 @@ export function useContractWriteWithConfirmations<
fallbackErrorMessage?: string;
},
) {
const { address: walletAddress } = useAccount();
const toastId = props.contractName + "_" + props.functionName;
const chainIdFromWallet = useChainId();
const chainIdFromPath = useChainIdFromPath();
Expand Down Expand Up @@ -109,6 +111,7 @@ export function useContractWriteWithConfirmations<
rawData,
contract: props.address,
message: error.message,
from: walletAddress,
};
try {
logPayload = {
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/contracts/out/CVStrategyV0_0.sol/CVStrategyV0_0.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/contracts/out/CVStrategyV0_0.sol/IPointStrategy.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/contracts/out/PassportScorer.sol/PassportScorer.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions pkg/contracts/src/BaseStrategyUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,24 @@ abstract contract BaseStrategyUpgradeable is ProxyOwnableUpgrader, IStrategy, Tr
/// @dev The encoded '_data' will be determined by the strategy implementation.
/// @param _recipientIds The IDs of the recipients
/// @param _data The data to use to get the payout summary for the recipients
/// @return The payout summary for the recipients
function getPayouts(address[] memory _recipientIds, bytes[] memory _data)
external
view
virtual
override
returns (PayoutSummary[] memory)
returns (PayoutSummary[] memory payouts)
{
uint256 recipientLength = _recipientIds.length;
// check if the length of the recipient IDs and data arrays are equal, if they are not, revert
if (recipientLength != _data.length) revert ARRAY_MISMATCH();

PayoutSummary[] memory payouts = new PayoutSummary[](recipientLength);
payouts = new PayoutSummary[](recipientLength);
for (uint256 i; i < recipientLength;) {
payouts[i] = _getPayout(_recipientIds[i], _data[i]);
unchecked {
i++;
}
}
return payouts;
}

/// @notice Checks if the '_allocator' is a valid allocator.
Expand Down
2 changes: 1 addition & 1 deletion pkg/contracts/test/CVStrategyTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ contract CVStrategyTest is Test, AlloSetup, RegistrySetupFull, CVStrategyHelpers
bytes memory data = abi.encode(votes);

vm.startPrank(address(0));
vm.expectRevert(abi.encodeWithSelector(CVStrategyV0_0.UserCannotBeZero.selector));
// vm.expectRevert(abi.encodeWithSelector(CVStrategyV0_0.UserCannotBeZero.selector));
allo().allocate(proposalId, data);
vm.stopPrank();
}
Expand Down

0 comments on commit 27cb081

Please sign in to comment.