Understanding Openzeppelin Governance contract

So, I have been reading this https://docs.openzeppelin.com/contracts/4.x/governance . First of all, great job at writing this, and thanks for writing such a descriptive documentation. While reading this, I came across a sentence which I don't completely understand. Can someone provide some context for this?

"The token has to implement the ERC20Votes extension. This extension will keep track of historical balances so that voting power is retrieved from past snapshots rather than the current balance, which is an important protection that prevents double voting."

I don't completely understand that the part about keeping historical balances for voting power.

I am assuming that it means storing tokens held by an account while voting on a particular proposal. For example, I am on BanklessDAO and held 150K BANK while I voted on proposal A. Then, I sold some and now have 100K BANK in my wallet while I vote on proposal B. So, it's going to store how many tokens I had while voting. Am I right here?

(PS:- Example is based on my experience using Snapshot)

Hello @PradhumnaPancholi

Traditional ERC20 tokens only keep track of the "latest" balance for each user. You can query what is the balanceOf(X), and what is the balanceOf(Y). But if X transfers to Y you lose that information.

This is not good enough for voting systems. You don't want X to vote with its 100 tokens, then transfer them to Y, then Y vote with the same 100 tokens, then Y transfer the tokens to Z, ...

You want to make sure you use a "snapshot of the voting power at a given time". Snapshot does that offchain, but since the OZ Governor is onchain, we need onchain logic to support these snapshots.

This is what the ERC20Votes extension provides. You can not only query balanceOf(X) but also getPastVotes(X, blockNumber), wich fixes the problem of X transferring its tokens to Y after he votes. The transfer will happen but won't allow Y to have more voting power.

Additionally, ERC20Votes provides a vote delegation mechanism.

1 Like

Got it. My brain didn't stumble upon the malicious voting power issue. I will look into deeper while studying contracts for "how," but thanks for giving such a good explanation and example of "why". I really appreciate it.