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

Erc721 votes and general Votes component #1114

Merged
merged 49 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
4324006
votes first iteration
ggonzalez94 Aug 19, 2024
dd0eb38
consolidate votes and delegation to governace module
ggonzalez94 Aug 22, 2024
5141fb5
fmt
ggonzalez94 Aug 22, 2024
2a76341
add ERC20Votes impl, code docs and remove interface for internal iml
ggonzalez94 Aug 27, 2024
6c02234
add basic tests and mocks
ggonzalez94 Sep 4, 2024
64461b9
refactor, remove unused usings
ggonzalez94 Sep 4, 2024
6f8c2d0
cleanup mocks, remove public impl
ggonzalez94 Sep 5, 2024
0836234
merge main and fix compile issues
ggonzalez94 Sep 17, 2024
05b69d1
merge main
ggonzalez94 Oct 4, 2024
de076af
refactor
ggonzalez94 Oct 4, 2024
6f5a8a6
export VotesComponent for easier use
ggonzalez94 Oct 4, 2024
fefcfeb
remove erc20votes from token module
ggonzalez94 Oct 4, 2024
0116f03
fmt
ggonzalez94 Oct 4, 2024
175af4e
add more tests
ggonzalez94 Oct 5, 2024
ce72985
fmt
ggonzalez94 Oct 6, 2024
4ca0ae4
add changelog entry
ggonzalez94 Oct 6, 2024
f866b4c
add burn tests
ggonzalez94 Oct 7, 2024
43a11b4
use hooks in mocks
ggonzalez94 Oct 8, 2024
92bf75e
use crate for local imports
ggonzalez94 Oct 8, 2024
5a8f860
refactor account names in tests
ggonzalez94 Oct 8, 2024
48b0e7b
improve docs and remove ERC20Votes
ggonzalez94 Oct 10, 2024
5afc32c
merge main and refactor
ggonzalez94 Oct 10, 2024
1ad7037
+
ggonzalez94 Oct 10, 2024
70f04b5
use vec in trace and remove storage array
ggonzalez94 Oct 10, 2024
e2eb675
add tests for trace & checkpoint
ggonzalez94 Oct 10, 2024
a0f6ab6
refactor tests
ggonzalez94 Oct 10, 2024
37c073e
fixes
ggonzalez94 Oct 13, 2024
b382639
fmt
ggonzalez94 Oct 13, 2024
02b1b3b
improve docs
ggonzalez94 Oct 13, 2024
46c5b5a
typos
ggonzalez94 Oct 13, 2024
246f4cf
Update CHANGELOG.md
ggonzalez94 Oct 13, 2024
378ba76
+
ggonzalez94 Oct 13, 2024
e68b3dc
+
ggonzalez94 Oct 13, 2024
a5110b2
++
ggonzalez94 Oct 13, 2024
d1e1a2e
add more tests and refactor
ggonzalez94 Oct 13, 2024
cc4c9a7
add missing internal functions and tests
ggonzalez94 Oct 13, 2024
52695d7
add VotesABI and format
ggonzalez94 Oct 14, 2024
9a601c8
docs: trait implementations
ggonzalez94 Oct 14, 2024
1f17d1a
remove debug and partialeq from checkpoint
ggonzalez94 Oct 14, 2024
23ef982
refactor hook and improve docs
ggonzalez94 Oct 14, 2024
36b6184
merge main
ggonzalez94 Oct 14, 2024
ad2c11b
+
ggonzalez94 Oct 15, 2024
67e98e1
merge from main
ggonzalez94 Oct 15, 2024
37ddb41
+
ggonzalez94 Oct 15, 2024
879ced3
improve tests
ggonzalez94 Oct 15, 2024
0fe82eb
fix test
ggonzalez94 Oct 15, 2024
865919e
fmt
ggonzalez94 Oct 15, 2024
84b2aec
Update CHANGELOG.md
ggonzalez94 Oct 15, 2024
1f1d549
move _delegate
ggonzalez94 Oct 15, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- `VotesComponent` with implementation for ERC721 and ERC20 tokens (#1114)

### Changed

- Bump scarb to v2.8.4 (#1146)

### Changed (Breaking)

- Remove `ERC20Votes` component in favor of `VotesComponent` (#1114)
- `Trace` is now declared as a `storage_node` and now uses `Vec` instead of `StorageArray`.
ggonzalez94 marked this conversation as resolved.
Show resolved Hide resolved
- Remove `StorageArray` from `openzeppelin_utils` (#1114)
- Bump snforge to 0.31.0
- Remove openzeppelin_utils::selectors (#1163)
- Remove `DualCase dispatchers` (#1163)
Expand Down
4 changes: 3 additions & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ name = "openzeppelin_governance"
version = "0.17.0"
dependencies = [
"openzeppelin_access",
"openzeppelin_account",
"openzeppelin_introspection",
"openzeppelin_test_common",
"openzeppelin_testing",
"openzeppelin_token",
"openzeppelin_utils",
"snforge_std",
]
Expand Down Expand Up @@ -113,6 +115,7 @@ dependencies = [
"openzeppelin_access",
"openzeppelin_account",
"openzeppelin_finance",
"openzeppelin_governance",
"openzeppelin_introspection",
"openzeppelin_security",
"openzeppelin_testing",
Expand All @@ -134,7 +137,6 @@ name = "openzeppelin_token"
version = "0.17.0"
dependencies = [
"openzeppelin_account",
"openzeppelin_governance",
"openzeppelin_introspection",
"openzeppelin_test_common",
"openzeppelin_testing",
Expand Down
196 changes: 1 addition & 195 deletions docs/modules/ROOT/pages/api/erc20.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -456,200 +456,6 @@ See <<IERC20-Transfer,IERC20::Transfer>>.

See <<IERC20-Approval,IERC20::Approval>>.

== Extensions

[.contract]
[[ERC20VotesComponent]]
=== `++ERC20VotesComponent++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-v0.17.0/packages/token/src/erc20/extensions/erc20_votes.cairo[{github-icon},role=heading-link]

```cairo
use openzeppelin_token::extensions::ERC20VotesComponent;
```

:DelegateChanged: xref:ERC20VotesComponent-DelegateChanged[DelegateChanged]
:DelegateVotesChanged: xref:ERC20VotesComponent-DelegateVotesChanged[DelegateVotesChanged]

Extension of ERC20 to support voting and delegation.

NOTE: Implementing xref:#ERC20Component[ERC20Component] is a requirement for this component to be implemented.

WARNING: To track voting units, this extension requires that the
xref:#ERC20VotesComponent-transfer_voting_units[transfer_voting_units] function is called after every transfer,
mint, or burn operation. For this, the xref:ERC20Component-ERC20HooksTrait[ERC20HooksTrait] must be used.

This extension keeps a history (checkpoints) of each account’s vote power. Vote power can be delegated either by calling
the xref:#ERC20VotesComponent-delegate[delegate] function directly, or by providing a signature to be used with
xref:#ERC20VotesComponent-delegate_by_sig[delegate_by_sig]. Voting power can be queried through the public accessors
xref:#ERC20VotesComponent-get_votes[get_votes] and xref:#ERC20VotesComponent-get_past_votes[get_past_votes].

By default, token balance does not account for voting power. This makes transfers cheaper. The downside is that it requires users to delegate to themselves in order to activate checkpoints and have their voting power tracked.

[.contract-index#ERC20VotesComponent-Embeddable-Impls]
.Embeddable Implementations
--
[.sub-index#ERC20VotesComponent-Embeddable-Impls-ERC20VotesImpl]
.ERC20VotesImpl
* xref:#ERC20VotesComponent-get_votes[`++get_votes(self, account)++`]
* xref:#ERC20VotesComponent-get_past_votes[`++get_past_votes(self, account, timepoint)++`]
* xref:#ERC20VotesComponent-get_past_total_supply[`++get_past_total_supply(self, timepoint)++`]
* xref:#ERC20VotesComponent-delegates[`++delegates(self, account)++`]
* xref:#ERC20VotesComponent-delegate[`++delegate(self, delegatee)++`]
* xref:#ERC20VotesComponent-delegate_by_sig[`++delegate_by_sig(self, delegator, delegatee, nonce, expiry, signature)++`]
--

[.contract-index]
.Internal implementations
--
.InternalImpl
* xref:#ERC20VotesComponent-get_total_supply[`++get_total_supply(self)++`]
* xref:#ERC20VotesComponent-_delegate[`++_delegate(self, account, delegatee)++`]
* xref:#ERC20VotesComponent-move_delegate_votes[`++move_delegate_votes(self, from, to, amount)++`]
* xref:#ERC20VotesComponent-transfer_voting_units[`++transfer_voting_units(self, from, to, amount)++`]
* xref:#ERC20VotesComponent-num_checkpoints[`++num_checkpoints(self, account)++`]
* xref:#ERC20VotesComponent-checkpoints[`++checkpoints(self, account, pos)++`]
* xref:#ERC20VotesComponent-get_voting_units[`++get_voting_units(self, account)++`]
--

[.contract-index]
.Events
--
* xref:#ERC20VotesComponent-DelegateChanged[`++DelegateChanged(delegator, from_delegate, to_delegate)++`]
* xref:#ERC20VotesComponent-DelegateVotesChanged[`++DelegateVotesChanged(delegate, previous_votes, new_votes)++`]
--

[#ERC20VotesComponent-Embeddable-functions]
==== Embeddable functions

[.contract-item]
[[ERC20VotesComponent-get_votes]]
==== `[.contract-item-name]#++get_votes++#++(self: @ContractState, account: ContractAddress) → u256++` [.item-kind]#external#

Returns the current amount of votes that `account` has.

[.contract-item]
[[ERC20VotesComponent-get_past_votes]]
==== `[.contract-item-name]#++get_past_votes++#++(self: @ContractState, account: ContractAddress, timepoint: u64) → u256++` [.item-kind]#external#

Returns the amount of votes that `account` had at a specific moment in the past.

Requirements:

- `timepoint` must be in the past.

[.contract-item]
[[ERC20VotesComponent-get_past_total_supply]]
==== `[.contract-item-name]#++get_past_total_supply++#++(self: @ContractState, timepoint: u64) → u256++` [.item-kind]#external#

Returns the total supply of votes available at a specific moment in the past.

NOTE: This value is the sum of all available votes, which is not necessarily the sum of all delegated votes.
Votes that have not been delegated are still part of total supply, even though they would not participate in a
vote.

[.contract-item]
[[ERC20VotesComponent-delegates]]
==== `[.contract-item-name]#++delegates++#++(self: @ContractState, account: ContractAddress) → ContractAddress++` [.item-kind]#external#

Returns the delegate that `account` has chosen.

[.contract-item]
[[ERC20VotesComponent-delegate]]
==== `[.contract-item-name]#++delegate++#++(ref self: ContractState, delegatee: ContractAddress)++` [.item-kind]#external#

Delegates votes from the caller to `delegatee`.

Emits a {DelegateChanged} event.

May emit one or two {DelegateVotesChanged} events.

[.contract-item]
[[ERC20VotesComponent-delegate_by_sig]]
==== `[.contract-item-name]#++delegate_by_sig++#++(ref self: ContractState, delegator: ContractAddress, delegatee: ContractAddress, nonce: felt252, expiry: u64, signature: Array<felt252>)++` [.item-kind]#external#

Delegates votes from `delegator` to `delegatee` through a SNIP12 message signature validation.

Requirements:

- `expiry` must not be in the past.
- `nonce` must match the account's current nonce.
- `delegator` must implement `SRC6::is_valid_signature`.
- `signature` should be valid for the message hash.

Emits a {DelegateChanged} event.

May emit one or two {DelegateVotesChanged} events.

[#ERC20VotesComponent-Internal-functions]
==== Internal functions

[.contract-item]
[[ERC20VotesComponent-get_total_supply]]
==== `[.contract-item-name]#++get_total_supply++#++(self: @ContractState) → u256++` [.item-kind]#internal#

Returns the current total supply of votes.

[.contract-item]
[[ERC20VotesComponent-_delegate]]
==== `[.contract-item-name]#++_delegate++#++(ref self: ContractState, account: ContractAddress, delegatee: ContractAddress)++` [.item-kind]#internal#

Delegates all of ``account``'s voting units to `delegatee`.

Emits a {DelegateChanged} event.

May emit one or two {DelegateVotesChanged} events.

[.contract-item]
[[ERC20VotesComponent-move_delegate_votes]]
==== `[.contract-item-name]#++move_delegate_votes++#++(ref self: ContractState, from: ContractAddress, to: ContractAddress, amount: u256)++` [.item-kind]#internal#

Moves `amount` of delegated votes from `from` to `to`.

May emit one or two {DelegateVotesChanged} events.

[.contract-item]
[[ERC20VotesComponent-transfer_voting_units]]
==== `[.contract-item-name]#++transfer_voting_units++#++(ref self: ContractState, from: ContractAddress, to: ContractAddress, amount: u256)++` [.item-kind]#internal#

Transfers, mints, or burns voting units.

To register a mint, `from` should be zero. To register a burn, `to`
should be zero. Total supply of voting units will be adjusted with mints and burns.

May emit one or two {DelegateVotesChanged} events.

[.contract-item]
[[ERC20VotesComponent-num_checkpoints]]
==== `[.contract-item-name]#++num_checkpoints++#++(self: @ContractState, account: ContractAddress) → u32++` [.item-kind]#internal#

Returns the number of checkpoints for `account`.

[.contract-item]
[[ERC20VotesComponent-checkpoints]]
==== `[.contract-item-name]#++checkpoints++#++(self: @ContractState, account: ContractAddress, pos: u32) → Checkpoint++` [.item-kind]#internal#

Returns the `pos`-th checkpoint for `account`.

[.contract-item]
[[ERC20VotesComponent-get_voting_units]]
==== `[.contract-item-name]#++get_voting_units++#++(self: @ContractState, account: ContractAddress) → u256++` [.item-kind]#internal#

Returns the voting units of an `account`.

[#ERC20VotesComponent-Events]
==== Events

[.contract-item]
[[ERC20VotesComponent-DelegateChanged]]
==== `[.contract-item-name]#++DelegateChanged++#++(delegator: ContractAddress, from_delegate: ContractAddress, to_delegate: ContractAddress)++` [.item-kind]#event#

Emitted when `delegator` delegates their votes from `from_delegate` to `to_delegate`.

[.contract-item]
[[ERC20VotesComponent-DelegateVotesChanged]]
==== `[.contract-item-name]#++DelegateVotesChanged++#++(delegate: ContractAddress, previous_votes: u256, new_votes: u256)++` [.item-kind]#event#

Emitted when `delegate` votes are updated from `previous_votes` to `new_votes`.

== Presets

[.contract]
Expand Down Expand Up @@ -716,4 +522,4 @@ Upgrades the contract to a new implementation given by `new_class_hash`.
Requirements:

- The caller is the contract owner.
- `new_class_hash` cannot be zero.
- `new_class_hash` cannot be zero.
ggonzalez94 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/api/finance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ use openzeppelin_finance::vesting::VestingComponent;
Vesting component implementing the xref:IVesting[`IVesting`] interface.

[.contract-index]
.Vesting Schedule Trait
.Vesting Schedule Trait Implementations
--
.functions
* xref:#VestingComponent-calculate_vested_amount[`++calculate_vested_amount(self, token, total_allocation,
Expand Down
Loading