Skip to content

Commit

Permalink
fix budgetTokensSpent and budgetAmount to have the same format in wei (
Browse files Browse the repository at this point in the history
…#139)

* fix budgetTokensSpent and budgetAmount to have the same format in wei

* fix calldata encoding and optionbudget decoding for approved green bar

* make change backwards compatible

* add optionBudgetChangeDate to governor
  • Loading branch information
ferrodri committed Feb 26, 2024
1 parent 7eb21ac commit 35e5946
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
Expand Up @@ -10,6 +10,7 @@ import InputBox from "@/components/shared/InputBox";
import styles from "./styles.module.scss";
import { MultiButtons } from "@/components/shared/MultiButtons";
import SimulateTransaction from "@/components/shared/SimulateTransaction";
import { formatEther, parseUnits } from "viem";

export default function AddTransactionsDetails({
form,
Expand All @@ -31,7 +32,7 @@ export default function AddTransactionsDetails({
target: "",
value: 0,
calldata: "",
transferAmount: 0,
transferAmount: 0n,
transferTo: "",
},
],
Expand Down Expand Up @@ -118,11 +119,13 @@ export default function AddTransactionsDetails({
</label>
<InputBox
placeholder={"3 000 000 OP"}
value={transaction.transferAmount}
value={formatEther(transaction.transferAmount)}
type="number"
onChange={(next) =>
form.onChange.options(
update(index, { transferAmount: next })
update(index, {
transferAmount: parseUnits(next, 18),
})
)
}
required
Expand Down
Expand Up @@ -34,7 +34,7 @@ export type Transaction = {
target: string;
value: number;
calldata: string;
transferAmount: number;
transferAmount: bigint;
transferTo: string;
};

Expand Down
9 changes: 3 additions & 6 deletions src/components/Proposals/ProposalCreation/SubmitButton.tsx
Expand Up @@ -202,7 +202,7 @@ function getInputData(form: Form): {

option.transactions.forEach((t) => {
if (t.type === "Transfer") {
formattedOption[0] += BigInt(t.transferAmount);
formattedOption[0] += t.transferAmount;
formattedOption[1].push(governanceTokenContract.address);
formattedOption[2].push(BigInt(0));
formattedOption[3].push(
Expand Down Expand Up @@ -265,14 +265,11 @@ function getInputData(form: Form): {
return { governorFunction, inputData, error };
}

function encodeTransfer(to: string, amount: number): string {
function encodeTransfer(to: string, amount: bigint): string {
return (
"0xa9059cbb" +
abiCoder
.encode(
["address", "uint256"],
[ethers.getAddress(to), ethers.parseEther(amount.toString() || "0")]
)
.encode(["address", "uint256"], [ethers.getAddress(to), amount])
.slice(2)
);
}
Expand Up @@ -5,6 +5,7 @@ import { Proposal } from "@/app/api/common/proposals/proposal";
import { ParsedProposalData, ParsedProposalResults } from "@/lib/proposalUtils";
import { parseUnits } from "viem";
import { tokens } from "@/lib/tokenUtils";
import { OptimismContracts } from "@/lib/contracts/contracts";

export default function OptionsResultsPanel({
proposal,
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function OptionsResultsPanel({
})();

let availableBudget = BigInt(proposalSettings.budgetAmount);
let isExeeded = false;
let isExceeded = false;

const mutableOptions = [...options];
const sortedOptions = mutableOptions
Expand All @@ -57,22 +58,28 @@ export default function OptionsResultsPanel({
{sortedOptions.map((option, index) => {
let isApproved = false;
const votesAmountBN = BigInt(option?.votes || 0);
const optionBudget = parseUnits(
option?.budgetTokensSpent?.toString() || "0",
tokens.get(proposalData.proposalSettings.budgetToken)?.decimals ?? 18
);

const optionBudget =
(proposal?.created_time as Date) >
OptimismContracts.governor.optionBudgetChangeDate
? BigInt(option?.budgetTokensSpent || 0)
: parseUnits(
option?.budgetTokensSpent?.toString() || "0",
tokens.get(proposalData.proposalSettings.budgetToken)
?.decimals ?? 18
);
if (proposalSettings.criteria === "TOP_CHOICES") {
isApproved = index < Number(proposalSettings.criteriaValue);
} else if (proposalSettings.criteria === "THRESHOLD") {
const threshold = BigInt(proposalSettings.criteriaValue);
isApproved =
!isExeeded &&
!isExceeded &&
votesAmountBN >= threshold &&
availableBudget >= optionBudget;
if (isApproved) {
availableBudget = availableBudget - optionBudget;
} else {
isExeeded = true;
isExceeded = true;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/contracts/contracts.ts
Expand Up @@ -33,6 +33,9 @@ export const OptimismContracts = {
abi: OptimismGovernor__factory.abi,
v6UpgradeBlock:
process.env.NEXT_PUBLIC_AGORA_ENV === "prod" ? 114995000 : 114615036,
// Date in which optionBudget was being sent correctly to chain, in all other past proposals optionBudget is in
// ether unit instead of wei
optionBudgetChangeDate: new Date("2024-02-21T12:00:00")
},

proposalTypesConfigurator: {
Expand Down
13 changes: 6 additions & 7 deletions src/lib/proposalUtils.ts
@@ -1,6 +1,5 @@
import { ProposalType } from "@prisma/client";
import { getHumanBlockTime } from "./blockTimes";
import { Block } from "ethers";
import { Proposal, ProposalPayload } from "@/app/api/common/proposals/proposal";
import { Abi, decodeFunctionData } from "viem";

Expand Down Expand Up @@ -210,12 +209,12 @@ export async function parseProposal(
proposalType: proposal.proposal_type as ProposalType,
status: latestBlock
? await getProposalStatus(
proposal,
proposalResuts,
latestBlock,
quorum,
votableSupply
)
proposal,
proposalResuts,
latestBlock,
quorum,
votableSupply
)
: null,
};
}
Expand Down

0 comments on commit 35e5946

Please sign in to comment.