Skip to content

Commit

Permalink
Make mintable pausable
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Dec 7, 2023
1 parent a8e3d2f commit 0894e65
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 62 deletions.
66 changes: 34 additions & 32 deletions packages/core-cairo/src/erc20.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,42 +789,42 @@ Generated by [AVA](https://avajs.dev).
mod MyToken {␊
use openzeppelin::token::erc20::ERC20Component;␊
use openzeppelin::token::erc20::interface;␊
use openzeppelin::security::pausable::PausableComponent;␊
use openzeppelin::access::ownable::OwnableComponent;␊
use openzeppelin::security::pausable::PausableComponent;␊
use openzeppelin::upgrades::UpgradeableComponent;␊
use openzeppelin::upgrades::interface::IUpgradeable;␊
use starknet::ContractAddress;␊
use starknet::get_caller_address;␊
use starknet::ClassHash;␊
component!(path: ERC20Component, storage: erc20, event: ERC20Event);␊
component!(path: PausableComponent, storage: pausable, event: PausableEvent);␊
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);␊
component!(path: PausableComponent, storage: pausable, event: PausableEvent);␊
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);␊
#[abi(embed_v0)]␊
impl ERC20MetadataImpl = ERC20Component::ERC20MetadataImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl PausableImpl = PausableComponent::PausableImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl OwnableCamelOnlyImpl = OwnableComponent::OwnableCamelOnlyImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl PausableImpl = PausableComponent::PausableImpl<ContractState>;␊
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;␊
impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;␊
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;␊
impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;␊
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;␊
#[storage]␊
struct Storage {␊
#[substorage(v0)]␊
erc20: ERC20Component::Storage,␊
#[substorage(v0)]␊
pausable: PausableComponent::Storage,␊
#[substorage(v0)]␊
ownable: OwnableComponent::Storage,␊
#[substorage(v0)]␊
pausable: PausableComponent::Storage,␊
#[substorage(v0)]␊
upgradeable: UpgradeableComponent::Storage,␊
}␊
Expand All @@ -834,10 +834,10 @@ Generated by [AVA](https://avajs.dev).
#[flat]␊
ERC20Event: ERC20Component::Event,␊
#[flat]␊
PausableEvent: PausableComponent::Event,␊
#[flat]␊
OwnableEvent: OwnableComponent::Event,␊
#[flat]␊
PausableEvent: PausableComponent::Event,␊
#[flat]␊
UpgradeableEvent: UpgradeableComponent::Event,␊
}␊
Expand Down Expand Up @@ -934,6 +934,12 @@ Generated by [AVA](https://avajs.dev).
#[generate_trait]␊
#[external(v0)]␊
impl ExternalImpl of ExternalTrait {␊
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {␊
self.ownable.assert_only_owner();␊
self.pausable.assert_not_paused();␊
self.erc20._mint(recipient, amount);␊
}␊
fn burn(ref self: ContractState, value: u256) {␊
self.pausable.assert_not_paused();␊
let caller = get_caller_address();␊
Expand All @@ -949,11 +955,6 @@ Generated by [AVA](https://avajs.dev).
self.ownable.assert_only_owner();␊
self.pausable._unpause();␊
}␊
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {␊
self.ownable.assert_only_owner();␊
self.erc20._mint(recipient, amount);␊
}␊
}␊
#[external(v0)]␊
Expand All @@ -972,58 +973,58 @@ Generated by [AVA](https://avajs.dev).
`// SPDX-License-Identifier: MIT␊
const PAUSER_ROLE: felt252 = selector!("PAUSER_ROLE");␊
const MINTER_ROLE: felt252 = selector!("MINTER_ROLE");␊
const PAUSER_ROLE: felt252 = selector!("PAUSER_ROLE");␊
const UPGRADER_ROLE: felt252 = selector!("UPGRADER_ROLE");␊
#[starknet::contract]␊
mod MyToken {␊
use openzeppelin::token::erc20::ERC20Component;␊
use openzeppelin::token::erc20::interface;␊
use openzeppelin::security::pausable::PausableComponent;␊
use openzeppelin::access::accesscontrol::AccessControlComponent;␊
use openzeppelin::introspection::src5::SRC5Component;␊
use openzeppelin::security::pausable::PausableComponent;␊
use openzeppelin::upgrades::UpgradeableComponent;␊
use openzeppelin::access::accesscontrol::DEFAULT_ADMIN_ROLE;␊
use openzeppelin::upgrades::interface::IUpgradeable;␊
use starknet::ContractAddress;␊
use starknet::get_caller_address;␊
use starknet::ClassHash;␊
use super::{PAUSER_ROLE, MINTER_ROLE, UPGRADER_ROLE};␊
use super::{MINTER_ROLE, PAUSER_ROLE, UPGRADER_ROLE};␊
component!(path: ERC20Component, storage: erc20, event: ERC20Event);␊
component!(path: PausableComponent, storage: pausable, event: PausableEvent);␊
component!(path: AccessControlComponent, storage: accesscontrol, event: AccessControlEvent);␊
component!(path: SRC5Component, storage: src5, event: SRC5Event);␊
component!(path: PausableComponent, storage: pausable, event: PausableEvent);␊
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);␊
#[abi(embed_v0)]␊
impl ERC20MetadataImpl = ERC20Component::ERC20MetadataImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl PausableImpl = PausableComponent::PausableImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl AccessControlImpl = AccessControlComponent::AccessControlImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl AccessControlCamelImpl = AccessControlComponent::AccessControlCamelImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;␊
#[abi(embed_v0)]␊
impl PausableImpl = PausableComponent::PausableImpl<ContractState>;␊
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;␊
impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;␊
impl AccessControlInternalImpl = AccessControlComponent::InternalImpl<ContractState>;␊
impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;␊
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;␊
#[storage]␊
struct Storage {␊
#[substorage(v0)]␊
erc20: ERC20Component::Storage,␊
#[substorage(v0)]␊
pausable: PausableComponent::Storage,␊
#[substorage(v0)]␊
accesscontrol: AccessControlComponent::Storage,␊
#[substorage(v0)]␊
src5: SRC5Component::Storage,␊
#[substorage(v0)]␊
pausable: PausableComponent::Storage,␊
#[substorage(v0)]␊
upgradeable: UpgradeableComponent::Storage,␊
}␊
Expand All @@ -1033,12 +1034,12 @@ Generated by [AVA](https://avajs.dev).
#[flat]␊
ERC20Event: ERC20Component::Event,␊
#[flat]␊
PausableEvent: PausableComponent::Event,␊
#[flat]␊
AccessControlEvent: AccessControlComponent::Event,␊
#[flat]␊
SRC5Event: SRC5Component::Event,␊
#[flat]␊
PausableEvent: PausableComponent::Event,␊
#[flat]␊
UpgradeableEvent: UpgradeableComponent::Event,␊
}␊
Expand All @@ -1047,17 +1048,17 @@ Generated by [AVA](https://avajs.dev).
ref self: ContractState,␊
recipient: ContractAddress,␊
default_admin: ContractAddress,␊
pauser: ContractAddress,␊
minter: ContractAddress,␊
pauser: ContractAddress,␊
upgrader: ContractAddress,␊
) {␊
self.erc20.initializer('MyToken', 'MTK');␊
self.accesscontrol.initializer();␊
self.erc20._mint(recipient, 2000000000000000000000);␊
self.accesscontrol._grant_role(DEFAULT_ADMIN_ROLE, default_admin);␊
self.accesscontrol._grant_role(PAUSER_ROLE, pauser);␊
self.accesscontrol._grant_role(MINTER_ROLE, minter);␊
self.accesscontrol._grant_role(PAUSER_ROLE, pauser);␊
self.accesscontrol._grant_role(UPGRADER_ROLE, upgrader);␊
}␊
Expand Down Expand Up @@ -1146,6 +1147,12 @@ Generated by [AVA](https://avajs.dev).
#[generate_trait]␊
#[external(v0)]␊
impl ExternalImpl of ExternalTrait {␊
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {␊
self.accesscontrol.assert_only_role(MINTER_ROLE);␊
self.pausable.assert_not_paused();␊
self.erc20._mint(recipient, amount);␊
}␊
fn burn(ref self: ContractState, value: u256) {␊
self.pausable.assert_not_paused();␊
let caller = get_caller_address();␊
Expand All @@ -1161,11 +1168,6 @@ Generated by [AVA](https://avajs.dev).
self.accesscontrol.assert_only_role(PAUSER_ROLE);␊
self.pausable._unpause();␊
}␊
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {␊
self.accesscontrol.assert_only_role(MINTER_ROLE);␊
self.erc20._mint(recipient, amount);␊
}␊
}␊
#[external(v0)]␊
Expand Down
Binary file modified packages/core-cairo/src/erc20.test.ts.snap
Binary file not shown.
11 changes: 7 additions & 4 deletions packages/core-cairo/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export function buildERC20(opts: ERC20Options): Contract {
addPremint(c, allOpts.premint);
}

if (allOpts.mintable) {
addMintable(c, allOpts.access);
}

if (allOpts.burnable) {
addBurnable(c);
}
Expand All @@ -80,10 +84,9 @@ export function buildERC20(opts: ERC20Options): Contract {
if (allOpts.burnable) {
setPausable(c, externalTrait, functions.burn);
}
}

if (allOpts.mintable) {
addMintable(c, allOpts.access);
if (allOpts.mintable) {
setPausable(c, externalTrait, functions.mint);
}
}

setAccessControl(c, allOpts.access);
Expand Down
45 changes: 23 additions & 22 deletions packages/core-cairo/src/erc721.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,18 +474,18 @@ Generated by [AVA](https://avajs.dev).
use openzeppelin::token::erc721::ERC721Component;␊
use openzeppelin::token::erc721::interface;␊
use openzeppelin::introspection::src5::SRC5Component;␊
use openzeppelin::security::pausable::PausableComponent;␊
use openzeppelin::access::ownable::OwnableComponent;␊
use openzeppelin::security::pausable::PausableComponent;␊
use openzeppelin::upgrades::UpgradeableComponent;␊
use openzeppelin::upgrades::interface::IUpgradeable;␊
use starknet::get_caller_address;␊
use starknet::ContractAddress;␊
use starknet::get_caller_address;␊
use starknet::ClassHash;␊
component!(path: ERC721Component, storage: erc721, event: ERC721Event);␊
component!(path: SRC5Component, storage: src5, event: SRC5Event);␊
component!(path: PausableComponent, storage: pausable, event: PausableEvent);␊
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);␊
component!(path: PausableComponent, storage: pausable, event: PausableEvent);␊
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);␊
#[abi(embed_v0)]␊
Expand All @@ -495,15 +495,15 @@ Generated by [AVA](https://avajs.dev).
#[abi(embed_v0)]␊
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;␊
#[abi(embed_v0)]␊
impl PausableImpl = PausableComponent::PausableImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl OwnableCamelOnlyImpl = OwnableComponent::OwnableCamelOnlyImpl<ContractState>;␊
#[abi(embed_v0)]␊
impl PausableImpl = PausableComponent::PausableImpl<ContractState>;␊
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>;␊
impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;␊
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;␊
impl PausableInternalImpl = PausableComponent::InternalImpl<ContractState>;␊
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;␊
#[storage]␊
Expand All @@ -513,10 +513,10 @@ Generated by [AVA](https://avajs.dev).
#[substorage(v0)]␊
src5: SRC5Component::Storage,␊
#[substorage(v0)]␊
pausable: PausableComponent::Storage,␊
#[substorage(v0)]␊
ownable: OwnableComponent::Storage,␊
#[substorage(v0)]␊
pausable: PausableComponent::Storage,␊
#[substorage(v0)]␊
upgradeable: UpgradeableComponent::Storage,␊
}␊
Expand All @@ -528,10 +528,10 @@ Generated by [AVA](https://avajs.dev).
#[flat]␊
SRC5Event: SRC5Component::Event,␊
#[flat]␊
PausableEvent: PausableComponent::Event,␊
#[flat]␊
OwnableEvent: OwnableComponent::Event,␊
#[flat]␊
PausableEvent: PausableComponent::Event,␊
#[flat]␊
UpgradeableEvent: UpgradeableComponent::Event,␊
}␊
Expand Down Expand Up @@ -639,6 +639,19 @@ Generated by [AVA](https://avajs.dev).
#[generate_trait]␊
#[external(v0)]␊
impl ExternalImpl of ExternalTrait {␊
fn safe_mint(␊
ref self: ContractState,␊
recipient: ContractAddress,␊
token_id: u256,␊
data: Span<felt252>,␊
token_uri: felt252,␊
) {␊
self.ownable.assert_only_owner();␊
self.pausable.assert_not_paused();␊
self.erc721._safe_mint(recipient, token_id, data);␊
self.erc721._set_token_uri(token_id, token_uri);␊
}␊
fn burn(ref self: ContractState, token_id: u256) {␊
self.pausable.assert_not_paused();␊
let caller = get_caller_address();␊
Expand All @@ -655,18 +668,6 @@ Generated by [AVA](https://avajs.dev).
self.ownable.assert_only_owner();␊
self.pausable._unpause();␊
}␊
fn safe_mint(␊
ref self: ContractState,␊
recipient: ContractAddress,␊
token_id: u256,␊
data: Span<felt252>,␊
token_uri: felt252,␊
) {␊
self.ownable.assert_only_owner();␊
self.erc721._safe_mint(recipient, token_id, data);␊
self.erc721._set_token_uri(token_id, token_uri);␊
}␊
}␊
#[external(v0)]␊
Expand Down
Binary file modified packages/core-cairo/src/erc721.test.ts.snap
Binary file not shown.
11 changes: 7 additions & 4 deletions packages/core-cairo/src/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export function buildERC721(opts: ERC721Options): Contract {
addBase(c, toShortString(allOpts.name, 'name'), toShortString(allOpts.symbol, 'symbol'));
addERC721ImplAndCamelOnlyImpl(c, allOpts.pausable);

if (allOpts.mintable) {
addMintable(c, allOpts.access);
}

if (allOpts.burnable) {
addBurnable(c);
}
Expand All @@ -66,10 +70,9 @@ export function buildERC721(opts: ERC721Options): Contract {
if (allOpts.burnable) {
setPausable(c, externalTrait, functions.burn);
}
}

if (allOpts.mintable) {
addMintable(c, allOpts.access);
if (allOpts.mintable) {
setPausable(c, externalTrait, functions.safe_mint);
}
}

setAccessControl(c, allOpts.access);
Expand Down

0 comments on commit 0894e65

Please sign in to comment.