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

ERC20 Permit Component #1140

Merged
merged 29 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a055099
Add ERC20Permit component
immrsd Sep 5, 2024
cfa836a
Add ERC20Permit preset
immrsd Sep 5, 2024
9973d9a
Add ERC20Pemit mock and update visibility modifiers
immrsd Sep 10, 2024
23ba5da
Add tests for ERC20Permit component
immrsd Sep 10, 2024
87f1859
Add tests for ERC20Permit preset
immrsd Sep 10, 2024
67c3fc7
Run linter
immrsd Sep 10, 2024
a499394
Merge main
immrsd Sep 25, 2024
4bfbe36
Remove ERC20Permit preset
immrsd Sep 25, 2024
09fb2d2
Add ERC20Permit as a trait of ERC20Component
immrsd Sep 25, 2024
950205e
Remove ERC20Permit component, restructure files
immrsd Sep 25, 2024
1c2fc82
Update tests
immrsd Sep 25, 2024
5702d0c
Fix test error messages
immrsd Sep 25, 2024
3e48a9a
Make slight changes to functions doc
immrsd Sep 27, 2024
8fbbe48
Address review issues
immrsd Oct 5, 2024
26ce488
Update changelog
immrsd Oct 5, 2024
acad601
Restructure files
immrsd Oct 5, 2024
2cef6d1
Support fixes in tests
immrsd Oct 5, 2024
4d23651
Merge main
immrsd Oct 5, 2024
72ab63d
Fix changelog
immrsd Oct 5, 2024
d90a50d
Merge main and fix conflicts
immrsd Oct 14, 2024
0204425
Change signature type to Span
immrsd Oct 14, 2024
68e45a4
Support sig changes in tests
immrsd Oct 14, 2024
e023847
Update in-code doc
immrsd Oct 14, 2024
78fa616
Refactor tests
immrsd Oct 14, 2024
6776641
Add ERC20Mixin interface, remove ERC20PermitABI interface
immrsd Oct 14, 2024
c44cf42
Update doc
immrsd Oct 14, 2024
979117b
Merge main
immrsd Oct 14, 2024
34a3d72
Remove unnecessary constant
immrsd Oct 14, 2024
a144d66
Make StructHashStarknetDomainImpl non-public back
immrsd 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- ERC20Permit component and preset
immrsd marked this conversation as resolved.
Show resolved Hide resolved

## 0.16.0 (2024-08-30)

### Added
Expand Down
92 changes: 92 additions & 0 deletions packages/presets/src/erc20_permit.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.16.0 (presets/erc20_permit.cairo)

/// # ERC20Permit Preset
///
/// The ERC20Permit preset integrates standard ERC20 token functionality with the ERC20Permit
/// extension, as defined by EIP-2612. This preset allows for token approvals via off-chain
/// signatures, thus enhancing transaction efficiency by reducing the need for on-chain approval
/// transactions.
immrsd marked this conversation as resolved.
Show resolved Hide resolved
///
/// This implementation features a fixed-supply model and the initial owner is specified in the
/// constructor.
///
/// The preset implements SNIP12Metadata with hardcoded values. These values are part of a
/// signature (following SNIP-12 standard) used for ERC20Permit functionality. It's crucial that the
/// SNIP12Metadata name remains unique to avoid confusion and potential security issues.
///
/// For more complex or custom contracts, use Wizard for Cairo
immrsd marked this conversation as resolved.
Show resolved Hide resolved
/// https://wizard.openzeppelin.com/cairo
#[starknet::contract]
pub mod ERC20Permit {
use openzeppelin_token::erc20::extensions::ERC20PermitComponent;
use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
use openzeppelin_utils::cryptography::nonces::NoncesComponent;
use openzeppelin_utils::cryptography::snip12::SNIP12Metadata;
use starknet::ContractAddress;

component!(path: ERC20Component, storage: erc20, event: ERC20Event);
component!(path: ERC20PermitComponent, storage: erc20_permit, event: ERC20PermitEvent);
component!(path: NoncesComponent, storage: nonces, event: NoncesEvent);

// ERC20Mixin
#[abi(embed_v0)]
impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>;
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;

// ERC20Permit
#[abi(embed_v0)]
impl ERC20PermitImpl = ERC20PermitComponent::ERC20PermitImpl<ContractState>;

// SNIP12Metadata
#[abi(embed_v0)]
impl SNIP12MetadataExternalImpl =
ERC20PermitComponent::SNIP12MetadataExternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
erc20: ERC20Component::Storage,
#[substorage(v0)]
erc20_permit: ERC20PermitComponent::Storage,
#[substorage(v0)]
nonces: NoncesComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
ERC20Event: ERC20Component::Event,
#[flat]
ERC20PermitEvent: ERC20PermitComponent::Event,
#[flat]
NoncesEvent: NoncesComponent::Event
}

/// 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);
}

impl SNIP12MetadataImpl of SNIP12Metadata {
/// Returns token name to be used for SNIP-12 signature.
fn name() -> felt252 {
'My unique token name'
}

/// Returns token version to be used for SNIP-12 signature.
fn version() -> felt252 {
'v1'
}
}
}
2 changes: 2 additions & 0 deletions packages/presets/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod account;
pub mod erc1155;
pub mod erc20;
pub mod erc20_permit;
pub mod erc721;
pub mod eth_account;
pub mod interfaces;
Expand All @@ -11,6 +12,7 @@ pub mod universal_deployer;
pub use account::AccountUpgradeable;
pub use erc1155::ERC1155Upgradeable;
pub use erc20::ERC20Upgradeable;
pub use erc20_permit::ERC20Permit;
pub use erc721::ERC721Upgradeable;
pub use eth_account::EthAccountUpgradeable;
pub use universal_deployer::UniversalDeployer;
2 changes: 2 additions & 0 deletions packages/presets/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod test_erc1155;
#[cfg(test)]
mod test_erc20;
#[cfg(test)]
mod test_erc20_permit;
#[cfg(test)]
mod test_erc721;
#[cfg(test)]
mod test_eth_account;
Expand Down
Loading