Skip to content

Commit

Permalink
Merge branch 'main' into nft-auction
Browse files Browse the repository at this point in the history
  • Loading branch information
princeibs authored Jun 5, 2024
2 parents 9cd9bcb + c0ab62f commit 73e0cad
Show file tree
Hide file tree
Showing 82 changed files with 2,459 additions and 237 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.4
starknet-foundry 0.24.0
20 changes: 20 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ 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"
Expand Down Expand Up @@ -110,6 +114,13 @@ name = "snforge_std"
version = "0.24.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.24.0#95e9fb09cb91b3c05295915179ee1b55bf923653"

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

[[package]]
name = "storage"
version = "0.1.0"
Expand All @@ -134,6 +145,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
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ test = "$(git rev-parse --show-toplevel)/scripts/test_resolver.sh"
[workspace.dependencies]
starknet = ">=2.6.3"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.24.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.11.0" }

[workspace.dev-dependencies]
# openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.11.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.11.0" }
components = { path = "listings/applications/components" }

[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");
}
}
10 changes: 5 additions & 5 deletions listings/applications/components/src/ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait IOwnable<TContractState> {
fn renounce_ownership(ref self: TContractState);
}

mod Errors {
pub mod Errors {
pub const UNAUTHORIZED: felt252 = 'Not owner';
pub const ZERO_ADDRESS_OWNER: felt252 = 'Owner cannot be zero';
pub const ZERO_ADDRESS_CALLER: felt252 = 'Caller cannot be zero';
Expand Down Expand Up @@ -43,7 +43,7 @@ pub mod ownable_component {
}

#[embeddable_as(Ownable)]
impl OwnableImpl<
pub impl OwnableImpl<
TContractState, +HasComponent<TContractState>
> of super::IOwnable<ComponentState<TContractState>> {
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
Expand All @@ -67,17 +67,17 @@ pub mod ownable_component {
> of OwnableInternalTrait<TContractState> {
fn _assert_only_owner(self: @ComponentState<TContractState>) {
let caller = get_caller_address();
assert(!caller.is_zero(), Errors::ZERO_ADDRESS_CALLER);
assert(caller.is_non_zero(), Errors::ZERO_ADDRESS_CALLER);
assert(caller == self.ownable_owner.read(), Errors::UNAUTHORIZED);
}

fn _init(ref self: ComponentState<TContractState>, owner: ContractAddress) {
assert(!owner.is_zero(), Errors::ZERO_ADDRESS_OWNER);
assert(owner.is_non_zero(), Errors::ZERO_ADDRESS_OWNER);
self.ownable_owner.write(owner);
}

fn _transfer_ownership(ref self: ComponentState<TContractState>, new: ContractAddress) {
assert(!new.is_zero(), Errors::ZERO_ADDRESS_OWNER);
assert(new.is_non_zero(), Errors::ZERO_ADDRESS_OWNER);
let previous = self.ownable_owner.read();
self.ownable_owner.write(new);
self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ANCHOR: contract
#[starknet::component]
pub mod countable_component {
use components::countable::ICountable;
Expand Down Expand Up @@ -25,3 +26,67 @@ pub mod countable_component {
}
// ANCHOR_END: impl
}

//ANCHOR_END: contract

#[starknet::contract]
mod MockContract {
use super::countable_component;
use components::switchable::ISwitchable;

component!(path: countable_component, storage: counter, event: CountableEvent);

#[storage]
struct Storage {
#[substorage(v0)]
counter: countable_component::Storage,
switch: bool,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
CountableEvent: countable_component::Event,
}

#[abi(embed_v0)]
impl CountableImpl = countable_component::Countable<ContractState>;
#[abi(embed_v0)]
impl Switchable of ISwitchable<ContractState> {
fn switch(ref self: ContractState) {}

fn is_on(self: @ContractState) -> bool {
true
}
}
}


#[cfg(test)]
mod test {
use super::MockContract;
use components::countable::{ICountableDispatcher, ICountableDispatcherTrait};
use starknet::syscalls::deploy_syscall;
use starknet::SyscallResultTrait;

fn deploy_countable() -> ICountableDispatcher {
let (contract_address, _) = deploy_syscall(
MockContract::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
ICountableDispatcher { contract_address: contract_address }
}

#[test]
fn test_get() {
let countable = deploy_countable();
assert_eq!(countable.get(), 0);
}

#[test]
fn test_increment() {
let countable = deploy_countable();
countable.increment();
assert_eq!(countable.get(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//ANCHOR: contract
#[starknet::component]
pub mod countable_component {
use components::countable::ICountable;
Expand Down Expand Up @@ -57,3 +58,6 @@ pub mod countable_component {
}
}
}
//ANCHOR_END: contract


Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub mod ConstantProductAmm {
assert(reserve0 * amount1 == reserve1 * amount0, 'x / y != dx / dy');
}

// How much shares to mint?
// How many shares to mint?
//
// f(x, y) = value of liquidity
// We will define f(x, y) = sqrt(xy)
Expand Down Expand Up @@ -178,11 +178,11 @@ pub mod ConstantProductAmm {
//
// Multiply by sqrt(x) / sqrt(x)
// Equation 2 = (sqrt(x^2y + 2xydx + dx^2 * y) - sqrt(x^2y)) / sqrt(x^2y)
// = (sqrt(y)(sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / (sqrt(y)sqrt(x^2))
// = (sqrt(y)(sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2))) / (sqrt(y)sqrt(x^2))
// sqrt(y) on top and bottom cancels out
//
// --- Equation 3 ---
// Equation 2 = (sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / (sqrt(x^2)
// Equation 2 = (sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / sqrt(x^2)
// = (sqrt((x + dx)^2) - sqrt(x^2)) / sqrt(x^2)
// = ((x + dx) - x) / x
// = dx / x
Expand Down
2 changes: 1 addition & 1 deletion listings/applications/constant_product_amm/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod contracts;
pub mod contracts;

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion listings/applications/erc20/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod token;

#[cfg(test)]
mod tests;
mod tests;
2 changes: 0 additions & 2 deletions listings/applications/erc20/src/tests.cairo

This file was deleted.

Loading

0 comments on commit 73e0cad

Please sign in to comment.