-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate ERC20Votes to component (#951)
* feat: add main logic * feat: docs * feat: add Nonces and tests * docs: finish v1 * feat: update CHANGELOG * fix: format * Update docs/modules/ROOT/pages/guides/snip12.adoc Co-authored-by: Andrew Fleming <[email protected]> * Update docs/modules/ROOT/pages/guides/snip12.adoc Co-authored-by: Andrew Fleming <[email protected]> * Update docs/modules/ROOT/pages/guides/snip12.adoc Co-authored-by: Andrew Fleming <[email protected]> * Update docs/modules/ROOT/pages/guides/snip12.adoc Co-authored-by: Andrew Fleming <[email protected]> * Update docs/modules/ROOT/pages/guides/snip12.adoc Co-authored-by: Andrew Fleming <[email protected]> * feat: apply update reviews * fix: tests * fix: linter * feat: finish main logic * feat: add tests * fix: tests * feat: update CHANGELOG * refactor: hooks names * fix: tests * feat: add overflow events to ERC20 * feat: bump scarb * feat: set scarb to 2.6.3 * fix: tests * Update src/utils/structs/checkpoint.cairo Co-authored-by: Andrew Fleming <[email protected]> * Update src/tests/presets/test_erc20_votes.cairo Co-authored-by: Andrew Fleming <[email protected]> * feat: apply review updates * feat: apply review updates * feat: apply review updates * fix: test * fix: comment --------- Co-authored-by: Andrew Fleming <[email protected]>
- Loading branch information
1 parent
bb15d2a
commit ec99fc3
Showing
26 changed files
with
1,413 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod interfaces; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod votes; | ||
|
||
use votes::IVotes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use starknet::ContractAddress; | ||
|
||
/// Common interface for Votes-enabled contracts. | ||
#[starknet::interface] | ||
trait IVotes<TState> { | ||
/// Returns the current amount of votes that `account` has. | ||
fn get_votes(self: @TState, account: ContractAddress) -> u256; | ||
|
||
/// Returns the amount of votes that `account` had at a specific moment in the past. | ||
fn get_past_votes(self: @TState, account: ContractAddress, timepoint: u64) -> u256; | ||
|
||
/// 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. | ||
fn get_past_total_supply(self: @TState, timepoint: u64) -> u256; | ||
|
||
/// Returns the delegate that `account` has chosen. | ||
fn delegates(self: @TState, account: ContractAddress) -> ContractAddress; | ||
|
||
/// Delegates votes from the sender to `delegatee`. | ||
fn delegate(ref self: TState, delegatee: ContractAddress); | ||
|
||
/// Delegates votes from `delegator` to `delegatee`. | ||
fn delegate_by_sig( | ||
ref self: TState, | ||
delegator: ContractAddress, | ||
delegatee: ContractAddress, | ||
nonce: felt252, | ||
expiry: u64, | ||
signature: Array<felt252> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod access; | ||
mod account; | ||
mod governance; | ||
mod introspection; | ||
mod presets; | ||
mod security; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#[starknet::contract] | ||
mod DualCaseERC20VotesMock { | ||
use openzeppelin::token::erc20::ERC20Component; | ||
use openzeppelin::token::erc20::extensions::ERC20VotesComponent::InternalTrait as ERC20VotesInternalTrait; | ||
use openzeppelin::token::erc20::extensions::ERC20VotesComponent; | ||
use openzeppelin::utils::cryptography::nonces::NoncesComponent; | ||
use openzeppelin::utils::cryptography::snip12::SNIP12Metadata; | ||
use starknet::ContractAddress; | ||
|
||
component!(path: ERC20VotesComponent, storage: erc20_votes, event: ERC20VotesEvent); | ||
component!(path: ERC20Component, storage: erc20, event: ERC20Event); | ||
component!(path: NoncesComponent, storage: nonces, event: NoncesEvent); | ||
|
||
// ERC20Votes | ||
#[abi(embed_v0)] | ||
impl ERC20VotesComponentImpl = | ||
ERC20VotesComponent::ERC20VotesImpl<ContractState>; | ||
|
||
// ERC20Mixin | ||
#[abi(embed_v0)] | ||
impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>; | ||
impl InternalImpl = ERC20Component::InternalImpl<ContractState>; | ||
|
||
// Nonces | ||
#[abi(embed_v0)] | ||
impl NoncesImpl = NoncesComponent::NoncesImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
erc20_votes: ERC20VotesComponent::Storage, | ||
#[substorage(v0)] | ||
erc20: ERC20Component::Storage, | ||
#[substorage(v0)] | ||
nonces: NoncesComponent::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
ERC20VotesEvent: ERC20VotesComponent::Event, | ||
#[flat] | ||
ERC20Event: ERC20Component::Event, | ||
#[flat] | ||
NoncesEvent: NoncesComponent::Event | ||
} | ||
|
||
/// Required for hash computation. | ||
impl SNIP12MetadataImpl of SNIP12Metadata { | ||
fn name() -> felt252 { | ||
'DAPP_NAME' | ||
} | ||
fn version() -> felt252 { | ||
'DAPP_VERSION' | ||
} | ||
} | ||
|
||
// | ||
// Hooks | ||
// | ||
|
||
impl ERC20VotesHooksImpl< | ||
TContractState, | ||
impl ERC20Votes: ERC20VotesComponent::HasComponent<TContractState>, | ||
impl HasComponent: ERC20Component::HasComponent<TContractState>, | ||
+NoncesComponent::HasComponent<TContractState>, | ||
+Drop<TContractState> | ||
> of ERC20Component::ERC20HooksTrait<TContractState> { | ||
fn before_update( | ||
ref self: ERC20Component::ComponentState<TContractState>, | ||
from: ContractAddress, | ||
recipient: ContractAddress, | ||
amount: u256 | ||
) {} | ||
|
||
fn after_update( | ||
ref self: ERC20Component::ComponentState<TContractState>, | ||
from: ContractAddress, | ||
recipient: ContractAddress, | ||
amount: u256 | ||
) { | ||
let mut erc20_votes_component = get_dep_component_mut!(ref self, ERC20Votes); | ||
erc20_votes_component.transfer_voting_units(from, recipient, amount); | ||
} | ||
} | ||
|
||
/// Sets the token `name` and `symbol`. | ||
/// Mints `fixed_supply` tokens to `recipient`. | ||
#[constructor] | ||
fn constructor( | ||
ref self: ContractState, | ||
name: ByteArray, | ||
symbol: ByteArray, | ||
fixed_supply: u256, | ||
recipient: ContractAddress | ||
) { | ||
self.erc20.initializer(name, symbol); | ||
self.erc20._mint(recipient, fixed_supply); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.