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

Convert katana/contracts into a Scarb workspace #2523

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions crates/katana/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.snfoundry_cache/
1 change: 1 addition & 0 deletions crates/katana/contracts/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.8.3
1 change: 1 addition & 0 deletions crates/katana/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Contract classes used in Katana
115 changes: 115 additions & 0 deletions crates/katana/contracts/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "katana_account"
version = "0.1.0"
dependencies = [
"openzeppelin",
]

[[package]]
name = "katana_messaging"
version = "0.1.0"

[[package]]
name = "openzeppelin"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_access",
"openzeppelin_account",
"openzeppelin_finance",
"openzeppelin_governance",
"openzeppelin_introspection",
"openzeppelin_merkle_tree",
"openzeppelin_presets",
"openzeppelin_security",
"openzeppelin_token",
"openzeppelin_upgrades",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_access"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_introspection",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_account"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_introspection",
"openzeppelin_utils",
]

[[package]]
name = "openzeppelin_finance"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_access",
"openzeppelin_token",
]

[[package]]
name = "openzeppelin_governance"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_access",
"openzeppelin_introspection",
]

[[package]]
name = "openzeppelin_introspection"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"

[[package]]
name = "openzeppelin_merkle_tree"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"

[[package]]
name = "openzeppelin_presets"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_access",
"openzeppelin_account",
"openzeppelin_finance",
"openzeppelin_introspection",
"openzeppelin_token",
"openzeppelin_upgrades",
]

[[package]]
name = "openzeppelin_security"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"

[[package]]
name = "openzeppelin_token"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
dependencies = [
"openzeppelin_account",
"openzeppelin_governance",
"openzeppelin_introspection",
]

[[package]]
name = "openzeppelin_upgrades"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"

[[package]]
name = "openzeppelin_utils"
version = "0.17.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.17.0#bf5d02c25c989ccc24f3ab42ec649617d3f21289"
14 changes: 14 additions & 0 deletions crates/katana/contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[workspace]
members = [ "account", "messaging/cairo" ]

[workspace.package]
version = "0.1.0"
edition = "2023_11"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[workspace.dependencies]
starknet = "2.8.2"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.17.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.31.0" }
assert_macros = "2.8.2"
Comment on lines +10 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ohayo again, sensei!

I've spotted some version discrepancies in our dependencies:

  • OpenZeppelin: Specified as v0.17.0 but the latest tag is v0.1.0.
  • Starknet Foundry: Specified as v0.31.0 but the latest tag is v0.20.1.

These mismatches suggest that the versions in Scarb.toml might be incorrect or ahead of the repository's actual releases. Please verify the intended versions to ensure compatibility and stability.

🔗 Analysis chain

Ohayo again, sensei! Dependencies are looking sharp!

The choice of dependencies is spot-on for our Starknet project. I appreciate the use of specific versions and tags - it's crucial for reproducibility.

Let's double-check if we're using the latest stable versions:

This script will help us ensure we're using the most up-to-date versions. If any discrepancies are found, we might want to consider updating.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the latest versions of our dependencies

# Check starknet version
echo "Latest starknet version:"
cargo search starknet | grep "starknet ="

# Check assert_macros version
echo "Latest assert_macros version:"
cargo search assert_macros | grep "assert_macros ="

# Check latest OpenZeppelin tag
echo "Latest OpenZeppelin tag:"
git ls-remote --tags https://github.com/OpenZeppelin/cairo-contracts.git | sort -V | tail -n 1

# Check latest Starknet Foundry tag
echo "Latest Starknet Foundry tag:"
git ls-remote --tags https://github.com/foundry-rs/starknet-foundry | sort -V | tail -n 1

Length of output: 804

13 changes: 13 additions & 0 deletions crates/katana/contracts/account/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "katana_account"
version.workspace = true
edition.workspace = true

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet.workspace = true
openzeppelin.workspace = true

[[target.starknet-contract]]
sierra = true
62 changes: 62 additions & 0 deletions crates/katana/contracts/account/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.17.0 (presets/account.cairo)

/// # Account Preset
///
/// OpenZeppelin's upgradeable account which can change its public key and declare, deploy, or call
/// contracts.
#[starknet::contract(account)]
pub mod Account {
use openzeppelin_account::AccountComponent;
use openzeppelin_introspection::src5::SRC5Component;
use openzeppelin_upgrades::UpgradeableComponent;
use openzeppelin_upgrades::interface::IUpgradeable;
use starknet::ClassHash;

component!(path: AccountComponent, storage: account, event: AccountEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);

// Account Mixin
#[abi(embed_v0)]
pub(crate) impl AccountMixinImpl =
AccountComponent::AccountMixinImpl<ContractState>;
impl AccountInternalImpl = AccountComponent::InternalImpl<ContractState>;

// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[storage]
pub struct Storage {
#[substorage(v0)]
pub account: AccountComponent::Storage,
#[substorage(v0)]
pub src5: SRC5Component::Storage,
#[substorage(v0)]
pub upgradeable: UpgradeableComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
AccountEvent: AccountComponent::Event,
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event
}

#[constructor]
pub fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
}
Comment on lines +51 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure all components are properly initialized in the constructor.

Ohayo, sensei! In the constructor, only the account component is initialized. To ensure all components function correctly, consider initializing the upgradeable and src5 components as well.

Apply this diff to initialize all components:

pub fn constructor(ref self: ContractState, public_key: felt252) {
    self.account.initializer(public_key);
+   self.upgradeable.initializer();
+   self.src5.initializer();
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
}
pub fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
self.upgradeable.initializer();
self.src5.initializer();
}


#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.account.assert_only_self();
self.upgradeable.upgrade(new_class_hash);
}
Comment on lines +57 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider emitting an event upon upgrading the contract.

Ohayo, sensei! Emitting an event when the contract is upgraded improves transparency and allows off-chain services to track upgrades. Consider emitting the UpgradeableEvent after the upgrade operation.

Apply this diff to emit the event:

fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
    self.account.assert_only_self();
    self.upgradeable.upgrade(new_class_hash);
+   UpgradeableEvent::Upgraded { new_class_hash }.emit();
}

Committable suggestion was skipped due to low confidence.

}
}
1 change: 1 addition & 0 deletions crates/katana/contracts/legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Legacy (Cairo 0) contracts. Not compilable as they are mainly used as references.
1 change: 0 additions & 1 deletion crates/katana/contracts/messaging/cairo/.tool-versions

This file was deleted.

4 changes: 2 additions & 2 deletions crates/katana/contracts/messaging/cairo/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "katana_messaging"
version = "0.1.0"
version.workspace = true

[dependencies]
starknet = "2.3.1"
starknet.workspace = true

[[target.starknet-contract]]
sierra = true
Expand Down
9 changes: 9 additions & 0 deletions crates/katana/contracts/snfoundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Visit https://foundry-rs.github.io/starknet-foundry/appendix/snfoundry-toml.html for more information

# [sncast.myprofile1] # Define a profile name
# url = "http://127.0.0.1:5050/" # Url of the RPC provider
# accounts_file = "../account-file" # Path to the file with the account data
# account = "mainuser" # Account from `accounts_file` or default account file that will be used for the transactions
# keystore = "~/keystore" # Path to the keystore file
# wait_params = { timeout = 500, retry_interval = 10 } # Wait for submitted transaction parameters
# block_explorer = "StarkScan" # Block explorer service used to display links to transaction details
Loading