-
Notifications
You must be signed in to change notification settings - Fork 359
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
ERC20 Permit Component #1140
ERC20 Permit Component #1140
Changes from 13 commits
a055099
cfa836a
9973d9a
23ba5da
87f1859
67c3fc7
a499394
4bfbe36
09fb2d2
950205e
1c2fc82
5702d0c
3e48a9a
8fbbe48
26ce488
acad601
2cef6d1
4d23651
72ab63d
d90a50d
0204425
68e45a4
e023847
78fa616
6776641
c44cf42
979117b
34a3d72
a144d66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod erc20_permit; | ||
pub mod erc20_votes; | ||
|
||
pub use erc20_votes::ERC20VotesComponent; |
immrsd marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
// OpenZeppelin Contracts for Cairo v0.17.0 (token/erc20/extensions/erc20_permit.cairo) | ||
|
||
use core::hash::{HashStateTrait, HashStateExTrait}; | ||
use core::poseidon::PoseidonTrait; | ||
use openzeppelin_utils::cryptography::snip12::StructHash; | ||
use starknet::ContractAddress; | ||
|
||
#[derive(Copy, Drop, Hash)] | ||
pub struct Permit { | ||
pub token: ContractAddress, | ||
pub spender: ContractAddress, | ||
pub amount: u256, | ||
pub nonce: felt252, | ||
pub deadline: u64, | ||
} | ||
|
||
// Since there's no u64 type in SNIP-12, the type used for `deadline` parameter is u128 | ||
// selector!( | ||
// "\"Permit\"( | ||
// \"token\":\"ContractAddress\", | ||
// \"spender\":\"ContractAddress\", | ||
// \"amount\":\"u256\", | ||
// \"nonce\":\"felt\", | ||
// \"deadline\":\"u128\" | ||
// )" | ||
// ); | ||
pub const PERMIT_TYPE_HASH: felt252 = | ||
0x2a8eb238e7cde741a544afcc79fe945d4292b089875fd068633854927fd5a96; | ||
|
||
impl StructHashImpl of StructHash<Permit> { | ||
fn hash_struct(self: @Permit) -> felt252 { | ||
PoseidonTrait::new().update_with(PERMIT_TYPE_HASH).update_with(*self).finalize() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,20 @@ pub trait IERC20CamelOnly<TState> { | |
) -> bool; | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait IERC20Permit<TState> { | ||
fn permit( | ||
ref self: TState, | ||
owner: ContractAddress, | ||
spender: ContractAddress, | ||
amount: u256, | ||
deadline: u64, | ||
signature: Array<felt252> | ||
immrsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
fn nonces(self: @TState, owner: ContractAddress) -> felt252; | ||
fn DOMAIN_SEPARATOR(self: @TState) -> felt252; | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait ERC20ABI<TState> { | ||
// IERC20 | ||
|
@@ -107,3 +121,43 @@ pub trait ERC20VotesABI<TState> { | |
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256 | ||
) -> bool; | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait ERC20PermitABI<TState> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to add an extra ABI trait, but include the new implementations in the ERC20ABI trait, even if the user can choose not to use some implementations, the ABI should contain the full ABI of the component. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This way it makes total sense to have an additional interface named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ERC20Mixin (and other mixins) implement the ABI of the component by coincidence, but not by definition. Mixin and ABIs are different things, even when they sometimes match. A Mixin is a set of implementations that are commonly used together (as mentioned here) whose objective is to avoid verbosity. A component ABI trait, on the other hand, is intended as a single dispatcher for the component full interface, even if it doesn't have to be implemented all-together, as explained in this recently added note: We didn't follow this principle in Ownable and OwnableTwoStep because the same function (transfer ownership) with the same interface has different implementations that can be problematic if they are used thinking the other one is in place, so we favoured clarity over the rule. In this case, the permit implementation doesn't have a problematic intersection and can be added to the ABI as intended. In this case, we can use Note that adding a different ABI per feature would create a lot of repetition in the interface file if the component grows bigger in features. TLDR: Mixin shouldn't be necessarily pegged to component ABIs, since they are not defined as the same thing. cc @andrew-fleming that may correct me if I recall something the wrong way from when we worked on this definitions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds accurate to me. Looking at the docs now, I do think we can improve our definition of ABI traits. We mention that they're useful for preset contracts; however, this isn't necessarily true anymore. We have a preset interfaces dir now and the ABI doesn't include the interface for upgrades. Further clarity and purpose would be helpful IMO
Maybe it's worth using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// IERC20 | ||
fn total_supply(self: @TState) -> u256; | ||
fn balance_of(self: @TState, account: ContractAddress) -> u256; | ||
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256; | ||
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool; | ||
fn transfer_from( | ||
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256 | ||
) -> bool; | ||
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool; | ||
|
||
// IERC20CamelOnly | ||
fn totalSupply(self: @TState) -> u256; | ||
fn balanceOf(self: @TState, account: ContractAddress) -> u256; | ||
fn transferFrom( | ||
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256 | ||
) -> bool; | ||
|
||
// IERC20Metadata | ||
fn name(self: @TState) -> ByteArray; | ||
fn symbol(self: @TState) -> ByteArray; | ||
fn decimals(self: @TState) -> u8; | ||
|
||
// IERC20Permit | ||
fn permit( | ||
ref self: TState, | ||
owner: ContractAddress, | ||
spender: ContractAddress, | ||
amount: u256, | ||
deadline: u64, | ||
signature: Array<felt252> | ||
); | ||
fn nonces(self: @TState, owner: ContractAddress) -> felt252; | ||
fn DOMAIN_SEPARATOR(self: @TState) -> felt252; | ||
|
||
// ISNIP12Metadata | ||
fn snip12_metadata(self: @TState) -> (felt252, felt252); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod test_dual20; | ||
mod test_erc20; | ||
mod test_erc20_permit; | ||
mod test_erc20_votes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to update this. Also adding subentries will be helpful to users, like the embeddable implementations added and the utilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated