Skip to content

Commit

Permalink
Merge branch 'main' into fix/folder_names
Browse files Browse the repository at this point in the history
  • Loading branch information
raizo07 committed Jun 8, 2024
2 parents c4b783d + 5e758e6 commit ceb6f06
Show file tree
Hide file tree
Showing 85 changed files with 2,060 additions and 289 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
21 changes: 21 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ version = "0.1.0"
name = "simple_vault"
version = "0.1.0"

[[package]]
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 @@ -125,6 +137,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
3 changes: 2 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ 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.11.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.11.0" }
components = { path = "listings/applications/components" }

# [workspace.dev-dependencies]
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.package]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub mod TimeContract {
fn pack(value: Time) -> felt252 {
let msb: felt252 = 256 * value.hour.into();
let lsb: felt252 = value.minute.into();
return msb + lsb;
msb + lsb
}
fn unpack(value: felt252) -> Time {
let value: u16 = value.try_into().unwrap();
let (q, r) = DivRem::div_rem(value, 256_u16.try_into().unwrap());
let hour: u8 = Into::<u16, felt252>::into(q).try_into().unwrap();
let minute: u8 = Into::<u16, felt252>::into(r).try_into().unwrap();
return Time { hour, minute };
Time { hour, minute }
}
}

Expand Down
7 changes: 3 additions & 4 deletions listings/advanced-concepts/storing_arrays/src/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use starknet::SyscallResultTrait;
use starknet::{Store, SyscallResult};
use starknet::storage_access::{StorageBaseAddress, storage_address_from_base_and_offset};
use starknet::syscalls::{storage_read_syscall, storage_write_syscall};
use starknet::storage_access::StorageBaseAddress;

// ANCHOR: StorageAccessImpl
impl StoreFelt252Array of Store<Array<felt252>> {
Expand All @@ -20,7 +19,7 @@ impl StoreFelt252Array of Store<Array<felt252>> {
) -> SyscallResult<Array<felt252>> {
let mut arr: Array<felt252> = array![];

// Read the stored array's length. If the length is superior to 255, the read will fail.
// Read the stored array's length. If the length is greater than 255, the read will fail.
let len: u8 = Store::<u8>::read_at_offset(address_domain, base, offset)
.expect('Storage Span too large');
offset += 1;
Expand All @@ -44,7 +43,7 @@ impl StoreFelt252Array of Store<Array<felt252>> {
fn write_at_offset(
address_domain: u32, base: StorageBaseAddress, mut offset: u8, mut value: Array<felt252>
) -> SyscallResult<()> {
// // Store the length of the array in the first storage slot.
// Store the length of the array in the first storage slot.
let len: u8 = value.len().try_into().expect('Storage - Span too large');
Store::<u8>::write_at_offset(address_domain, base, offset, len).unwrap();
offset += 1;
Expand Down
16 changes: 8 additions & 8 deletions listings/advanced-concepts/using_lists/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub mod ListExample {

#[storage]
pub struct Storage {
amount: List<u128>,
amounts: List<u128>,
tasks: List<Task>
}

#[abi(embed_v0)]
impl ListExample of super::IListExample<ContractState> {
fn add_in_amount(ref self: ContractState, number: u128) {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.append(number).unwrap();
}

Expand All @@ -41,31 +41,31 @@ pub mod ListExample {
}

fn is_empty_list(self: @ContractState) -> bool {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.is_empty()
}

fn list_length(self: @ContractState) -> u32 {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.len()
}

fn get_from_index(self: @ContractState, index: u32) -> u128 {
self.amount.read()[index]
self.amounts.read()[index]
}

fn set_from_index(ref self: ContractState, index: u32, number: u128) {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.set(index, number).unwrap();
}

fn pop_front_list(ref self: ContractState) {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.pop_front().unwrap().unwrap();
}

fn array_conversion(self: @ContractState) -> Array<u128> {
let mut current_amount_list = self.amount.read();
let mut current_amount_list = self.amounts.read();
current_amount_list.array().unwrap()
}
}
Expand Down
6 changes: 3 additions & 3 deletions listings/advanced-concepts/using_lists/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use using_lists::contract::IListExample;
use using_lists::contract::{Task, ListExample};
use using_lists::contract::ListExample::{
amountContractMemberStateTrait, tasksContractMemberStateTrait
amountsContractMemberStateTrait, tasksContractMemberStateTrait
};

fn STATE() -> ListExample::ContractState {
Expand All @@ -13,7 +13,7 @@ fn STATE() -> ListExample::ContractState {
fn test_add_in_amount() {
let mut state = STATE();
state.add_in_amount(200);
assert(state.amount.read()[0] == 200, 'should be 200');
assert(state.amounts.read()[0] == 200, 'should be 200');
}

#[test]
Expand Down Expand Up @@ -67,7 +67,7 @@ fn test_set_from_index() {
let mut state = STATE();
state.add_in_amount(200);
state.set_from_index(0, 400);
assert(state.amount.read()[0] == 400, 'should be 400');
assert(state.amounts.read()[0] == 400, 'should be 400');
}

#[test]
Expand Down
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;
10 changes: 5 additions & 5 deletions listings/applications/erc20/src/token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub mod erc20 {
recipient: ContractAddress,
amount: felt252
) {
assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);
assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);
assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);
assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);
self.balances.write(sender, self.balances.read(sender) - amount);
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.emit(Transfer { from: sender, to: recipient, value: amount });
Expand All @@ -188,13 +188,13 @@ pub mod erc20 {
spender: ContractAddress,
amount: felt252
) {
assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);
assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);
self.allowances.write((owner, spender), amount);
self.emit(Approval { owner, spender, value: amount });
}

fn mint(ref self: ContractState, recipient: ContractAddress, amount: felt252) {
assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);
assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);
let supply = self.total_supply.read() + amount;
self.total_supply.write(supply);
let balance = self.balances.read(recipient) + amount;
Expand Down Expand Up @@ -249,7 +249,7 @@ mod tests {
fn test_deploy_when_recipient_is_address_zero() {
let recipient: ContractAddress = Zero::zero();

let (_contract_address, _) = deploy_syscall(
let (contract_address, _) = deploy_syscall(
erc20::TEST_CLASS_HASH.try_into().unwrap(),
recipient.into(),
array![recipient.into(), token_name, decimals.into(), initial_supply, symbols].span(),
Expand Down
1 change: 1 addition & 0 deletions listings/applications/staking/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
Loading

0 comments on commit ceb6f06

Please sign in to comment.