Skip to content

Commit

Permalink
feat: merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
okhaimie-dev committed Jul 22, 2024
2 parents 59eedba + b442f2d commit 7d2c465
Show file tree
Hide file tree
Showing 169 changed files with 8,279 additions and 1,905 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/verify_cairo_programs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- main
workflow_dispatch:

jobs:
compile_and_verify:
Expand All @@ -14,7 +15,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Configure upstream repository
run: |
git remote add upstream https://github.com/NethermindEth/StarknetByExample
Expand All @@ -23,6 +24,9 @@ jobs:
- name: Install scarb
uses: software-mansion/setup-scarb@v1

- name: Install snforge
uses: foundry-rs/setup-snfoundry@v3

- name: Run build script
run: |
chmod +x scripts/cairo_programs_verifier.sh
Expand Down
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
scarb 2.6.4
scarb 2.6.5
starknet-foundry 0.25.0
63 changes: 61 additions & 2 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "advanced_factory"
version = "0.1.0"
dependencies = [
"alexandria_storage",
"components",
"crowdfunding",
"snforge_std",
]

[[package]]
name = "alexandria_storage"
version = "0.3.0"
Expand Down Expand Up @@ -44,6 +54,15 @@ version = "0.1.0"
name = "counter"
version = "0.1.0"

[[package]]
name = "crowdfunding"
version = "0.1.0"
dependencies = [
"components",
"openzeppelin",
"snforge_std",
]

[[package]]
name = "custom_type_serde"
version = "0.1.0"
Expand Down Expand Up @@ -88,14 +107,26 @@ version = "0.1.0"
name = "interfaces_traits"
version = "0.1.0"

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

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

[[package]]
name = "nft_dutch_auction"
version = "0.1.0"
dependencies = [
"erc20",
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.11.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.11.0#a83f36b23f1af6e160288962be4a2701c3ecbcda"
version = "0.14.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.14.0#f091c4f51ddeb10297db984acae965328c5a4e5b"

[[package]]
name = "pragma_lib"
Expand All @@ -106,10 +137,29 @@ source = "git+https://github.com/astraly-labs/pragma-lib#2eca9b70dc505788423da1e
name = "scarb"
version = "0.1.0"

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

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

[[package]]
name = "snforge_std"
version = "0.25.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.25.0#5b366e24821e530fea97f11b211d220e8493fbea"

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

[[package]]
name = "storage"
version = "0.1.0"
Expand All @@ -134,6 +184,15 @@ version = "0.1.0"
name = "testing_how_to"
version = "0.1.0"

[[package]]
name = "timelock"
version = "0.1.0"
dependencies = [
"components",
"openzeppelin",
"snforge_std",
]

[[package]]
name = "upgradeable_contract"
version = "0.1.0"
Expand Down
13 changes: 11 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ members = [
"listings/getting-started/*",
"listings/applications/*",
"listings/advanced-concepts/*",
"listings/templates/*"
"listings/templates/*",
]

[workspace.scripts]
test = "$(git rev-parse --show-toplevel)/scripts/test_resolver.sh"

# [workspace.tool.snforge]
[workspace.tool.snforge]

[workspace.dependencies]
<<<<<<< HEAD
starknet = ">=2.6.3"
# snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.11.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.11.0" }
pragma_lib = { git = "https://github.com/astraly-labs/pragma-lib" }

# [workspace.dev-dependencies]
# openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.11.0" }
=======
starknet = ">=2.6.4"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.14.0" }
components = { path = "listings/applications/components" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.25.0" }
# The latest Alexandria release supports only Cairo v2.6.0, so using explicit rev that supports Cairo v2.6.3
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev="800f5ad" }
>>>>>>> main

[workspace.package]
description = "Collection of examples of how to use the Cairo programming language to create smart contracts on Starknet."
Expand Down
1 change: 1 addition & 0 deletions listings/advanced-concepts/library_calls/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
14 changes: 14 additions & 0 deletions listings/advanced-concepts/library_calls/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "library_calls"
version.workspace = true
edition = "2023_11"

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

[dependencies]
starknet.workspace = true

[scripts]
test.workspace = true

[[target.starknet-contract]]
4 changes: 4 additions & 0 deletions listings/advanced-concepts/library_calls/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod library_call;

#[cfg(test)]
mod tests;
51 changes: 51 additions & 0 deletions listings/advanced-concepts/library_calls/src/library_call.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ANCHOR: library_dispatcher
#[starknet::interface]
pub trait IMathUtils<T> {
fn add(ref self: T, x: u32, y: u32) -> u32;
fn set_class_hash(ref self: T, class_hash: starknet::ClassHash);
}

// contract A
#[starknet::contract]
pub mod MathUtils {
#[storage]
struct Storage {}

#[abi(embed_v0)]
impl ImathUtilsImpl of super::IMathUtils<ContractState> {
fn add(ref self: ContractState, x: u32, y: u32) -> u32 {
x + y
}

fn set_class_hash(ref self: ContractState, class_hash: starknet::ClassHash) {}
}
}


// contract B to make library call to the class of contract A
#[starknet::contract]
pub mod MathUtilsLibraryCall {
use starknet::{class_hash::class_hash_const, ContractAddress};
use super::{IMathUtilsDispatcherTrait, IMathUtilsLibraryDispatcher};

#[storage]
struct Storage {
value: u32,
lib_class_hash: starknet::ClassHash,
}

#[abi(embed_v0)]
impl MathUtils of super::IMathUtils<ContractState> {
fn add(ref self: ContractState, x: u32, y: u32) -> u32 {
IMathUtilsLibraryDispatcher { class_hash: self.lib_class_hash.read() }.add(x, y)
}

#[abi(embed_v0)]
fn set_class_hash(ref self: ContractState, class_hash: starknet::ClassHash) {
self.lib_class_hash.write(class_hash);
}
}
}
// ANCHOR_END: library_dispatcher


25 changes: 25 additions & 0 deletions listings/advanced-concepts/library_calls/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod tests {
use starknet::syscalls::{deploy_syscall};
use starknet::SyscallResultTrait;
use library_calls::library_call::{
MathUtils, MathUtilsLibraryCall, IMathUtilsDispatcher, IMathUtilsDispatcherTrait
};

#[test]
#[available_gas(20000000)]
fn test_library_dispatcher() {
let math_utils_class_hash: starknet::ClassHash = MathUtils::TEST_CLASS_HASH
.try_into()
.unwrap();
let mut calldata: Array<felt252> = array![];
let (address, _) = deploy_syscall(
MathUtilsLibraryCall::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap_syscall();
let mut contract = IMathUtilsDispatcher { contract_address: address };

contract.set_class_hash(math_utils_class_hash);
let mut result = contract.add(30, 5);
assert_eq!(result, 35, "Wrong result");
}
}
1 change: 1 addition & 0 deletions listings/advanced-concepts/simple_account/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
6 changes: 6 additions & 0 deletions listings/advanced-concepts/simple_account/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "ecdsa_verification"
version = "0.1.0"
14 changes: 14 additions & 0 deletions listings/advanced-concepts/simple_account/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "simple_account"
version.workspace = true
edition = '2023_11'


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

[scripts]
test.workspace = true

[[target.starknet-contract]]
4 changes: 4 additions & 0 deletions listings/advanced-concepts/simple_account/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod simple_account;

#[cfg(test)]
mod tests;
90 changes: 90 additions & 0 deletions listings/advanced-concepts/simple_account/src/simple_account.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use starknet::account::Call;

#[starknet::interface]
trait ISRC6<TContractState> {
fn execute_calls(self: @TContractState, calls: Array<Call>) -> Array<Span<felt252>>;
fn validate_calls(self: @TContractState, calls: Array<Call>) -> felt252;
fn is_valid_signature(
self: @TContractState, hash: felt252, signature: Array<felt252>
) -> felt252;
}

#[starknet::contract]
mod simpleAccount {
use super::ISRC6;
use starknet::account::Call;
use core::num::traits::Zero;
use core::ecdsa::check_ecdsa_signature;

// Implement SRC5 with openzeppelin
use openzeppelin::account::interface;
use openzeppelin::introspection::src5::SRC5Component;
component!(path: SRC5Component, storage: src5, event: SRC5Event);

#[abi(embed_v0)]
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;
impl SRC5InternalImpl = SRC5Component::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
src5: SRC5Component::Storage,
public_key: felt252
}

#[constructor]
fn constructor(ref self: ContractState, public_key: felt252) {
self.src5.register_interface(interface::ISRC6_ID);
self.public_key.write(public_key);
}

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

#[abi(embed_v0)]
impl SRC6 of ISRC6<ContractState> {
fn execute_calls(self: @ContractState, calls: Array<Call>) -> Array<Span<felt252>> {
assert(starknet::get_caller_address().is_zero(), 'Not Starknet Protocol');
let Call { to, selector, calldata } = calls.at(0);
let res = starknet::syscalls::call_contract_syscall(*to, *selector, *calldata).unwrap();
array![res]
}

fn validate_calls(self: @ContractState, calls: Array<Call>) -> felt252 {
assert(starknet::get_caller_address().is_zero(), 'Not Starknet Protocol');
let tx_info = starknet::get_tx_info().unbox();
let tx_hash = tx_info.transaction_hash;
let signature = tx_info.signature;
if self._is_valid_signature(tx_hash, signature) {
starknet::VALIDATED
} else {
0
}
}

fn is_valid_signature(
self: @ContractState, hash: felt252, signature: Array<felt252>
) -> felt252 {
if self._is_valid_signature(hash, signature.span()) {
starknet::VALIDATED
} else {
0
}
}
}

#[generate_trait]
impl SignatureVerificationImpl of SignatureVerification {
fn _is_valid_signature(
self: @ContractState, hash: felt252, signature: Span<felt252>
) -> bool {
check_ecdsa_signature(
hash, self.public_key.read(), *signature.at(0_u32), *signature.at(1_u32)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[cfg(test)]
mod tests { // TODO
}
Loading

0 comments on commit 7d2c465

Please sign in to comment.