Skip to content

Commit

Permalink
feat: upgrade dependency and add owner && contract upgradability
Browse files Browse the repository at this point in the history
  • Loading branch information
codeWhizperer committed Feb 11, 2024
1 parent 7975a3d commit e5b7154
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
10 changes: 8 additions & 2 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "openzeppelin"
version = "0.9.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.9.0#861fc416f87addbe23a3b47f9d19ab27c10d5dc8"

[[package]]
name = "snforge_std"
version = "0.14.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.14.0#e8cbecee4e31ed428c76d5173eaa90c8df796fe3"
version = "0.17.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.17.0#63f7f0b533a3d852e2b60214e0f40b99c9dcbb26"

[[package]]
name = "token_bound_accounts"
version = "0.1.0"
dependencies = [
"openzeppelin",
"snforge_std",
]
6 changes: 3 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ sierra = true
casm = true

[dependencies]
starknet = "2.4.3"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.14.0" }

starknet = "2.5.3"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.17.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.9.0" }
[tool.snforge]
# exit_first = true
39 changes: 37 additions & 2 deletions src/registry/registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,40 @@ mod Registry {

use token_bound_accounts::interfaces::IERC721::{IERC721DispatcherTrait, IERC721Dispatcher};
use token_bound_accounts::interfaces::IRegistry::IRegistry;
use openzeppelin::{
access::ownable::OwnableComponent, upgrades::{UpgradeableComponent, interface::IUpgradeable}
};

component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

// add an owner
#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;
// make it upgradable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
admin: ContractAddress,
registry_deployed_accounts: LegacyMap<
(ContractAddress, u256), u8
>, // tracks no. of deployed accounts by registry for an NFT
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
AccountCreated: AccountCreated
AccountCreated: AccountCreated,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
}

/// @notice Emitted when a new tokenbound account is deployed/created
Expand All @@ -42,7 +64,20 @@ mod Registry {
token_id: u256,
}

#[external(v0)]
#[constructor]
fn constructor(ref self: ContractState, admin: ContractAddress) {
self.admin.write(admin)
}

#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.ownable.assert_only_owner();
self.upgradeable._upgrade(new_class_hash);
}
}

#[abi(embed_v0)]
impl IRegistryImpl of IRegistry<ContractState> {
/// @notice deploys a new tokenbound account for an NFT
/// @param implementation_hash the class hash of the reference account
Expand Down

0 comments on commit e5b7154

Please sign in to comment.