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

Maintain: chapter00 #181

Merged
merged 4 commits into from
May 2, 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: 1 addition & 1 deletion listings/getting-started/bytearray/src/bytearray.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ANCHOR: contract
#[starknet::interface]
pub trait IMessage<TContractState> {
fn append(ref self: TContractState, str: ByteArray);
fn prepend(ref self: TContractState, str: ByteArray);
}

// ANCHOR: contract
#[starknet::contract]
pub mod MessageContract {
#[storage]
Expand Down
55 changes: 49 additions & 6 deletions listings/getting-started/custom_type_serde/src/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#[starknet::interface]
pub trait ISerdeCustomType<TContractState> {
fn person_input(ref self: TContractState, person: Person);
fn person_output(self: @TContractState) -> Person;
}

// ANCHOR: contract
// Deriving the `Serde` trait allows us to use
// the Person type as an entrypoint parameter and return value
#[derive(Drop, Serde)]
Expand All @@ -6,12 +13,6 @@ pub struct Person {
pub name: felt252
}

#[starknet::interface]
pub trait ISerdeCustomType<TContractState> {
fn person_input(ref self: TContractState, person: Person);
fn person_output(self: @TContractState) -> Person;
}

#[starknet::contract]
pub mod SerdeCustomType {
use super::Person;
Expand All @@ -28,3 +29,45 @@ pub mod SerdeCustomType {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod tests {
use super::{
SerdeCustomType, Person, ISerdeCustomTypeDispatcher, ISerdeCustomTypeDispatcherTrait
};
use starknet::{ContractAddress, syscalls::deploy_syscall, SyscallResultTrait};

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

#[test]
fn should_deploy() {
deploy();
}

#[test]
fn should_get_person_output() {
let contract = deploy();
let expected_person = Person { age: 10, name: 'Joe' };
let received_person = contract.person_output();
let age_received = received_person.age;
let name_received = received_person.name;

assert(age_received == expected_person.age, 'Wrong age value');
assert(name_received == expected_person.name, 'Wrong name value');
}

#[test]
#[available_gas(2000000000)]
fn should_call_person_input() {
let contract = deploy();
let expected_person = Person { age: 10, name: 'Joe' };
contract.person_input(expected_person);
}
}
3 changes: 0 additions & 3 deletions listings/getting-started/custom_type_serde/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
mod contract;

#[cfg(test)]
mod tests;
47 changes: 0 additions & 47 deletions listings/getting-started/custom_type_serde/src/tests.cairo

This file was deleted.

42 changes: 37 additions & 5 deletions listings/getting-started/errors/src/custom_errors.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pub mod Errors {
pub const NOT_POSITIVE: felt252 = 'must be greater than 0';
pub const NOT_NULL: felt252 = 'must not be null';
}

#[starknet::interface]
pub trait ICustomErrorsExample<TContractState> {
fn test_assert(self: @TContractState, i: u256);
fn test_panic(self: @TContractState, i: u256);
}

// ANCHOR: contract
pub mod Errors {
pub const NOT_POSITIVE: felt252 = 'must be greater than 0';
pub const NOT_NULL: felt252 = 'must not be null';
}

#[starknet::contract]
pub mod CustomErrorsExample {
use super::Errors;
Expand All @@ -29,3 +30,34 @@ pub mod CustomErrorsExample {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod test {
use super::{
CustomErrorsExample, ICustomErrorsExampleDispatcher, ICustomErrorsExampleDispatcherTrait
};
use starknet::{ContractAddress, SyscallResultTrait, syscalls::deploy_syscall};

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

#[test]
#[should_panic(expected: ('must not be null', 'ENTRYPOINT_FAILED'))]
fn should_panic() {
let contract = deploy();
contract.test_panic(0);
}

#[test]
#[should_panic(expected: ('must be greater than 0', 'ENTRYPOINT_FAILED'))]
fn should_assert() {
let contract = deploy();
contract.test_assert(0);
}
}
3 changes: 0 additions & 3 deletions listings/getting-started/errors/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod simple_errors;
mod custom_errors;
mod vault_errors;

#[cfg(test)]
mod tests;
30 changes: 30 additions & 0 deletions listings/getting-started/errors/src/simple_errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub trait IErrorsExample<TContractState> {
fn test_panic(self: @TContractState, i: u256);
}

// ANCHOR: contract
#[starknet::contract]
pub mod ErrorsExample {
#[storage]
Expand All @@ -25,3 +26,32 @@ pub mod ErrorsExample {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod test {
use super::{ErrorsExample, IErrorsExampleDispatcher, IErrorsExampleDispatcherTrait};
use starknet::{ContractAddress, SyscallResultTrait, syscalls::deploy_syscall};

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

#[test]
#[should_panic(expected: ('i must not be 0', 'ENTRYPOINT_FAILED'))]
fn should_panic() {
let contract = deploy();
contract.test_panic(0);
}

#[test]
#[should_panic(expected: ('i must be greater than 0', 'ENTRYPOINT_FAILED'))]
fn should_assert() {
let contract = deploy();
contract.test_assert(0);
}
}
2 changes: 0 additions & 2 deletions listings/getting-started/errors/src/tests.cairo

This file was deleted.

45 changes: 39 additions & 6 deletions listings/getting-started/errors/src/vault_errors.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pub mod VaultErrors {
pub const INSUFFICIENT_BALANCE: felt252 = 'insufficient_balance';
// you can define more errors here
}

#[starknet::interface]
pub trait IVaultErrorsExample<TContractState> {
fn deposit(ref self: TContractState, amount: u256);
fn withdraw(ref self: TContractState, amount: u256);
}

// ANCHOR: contract
pub mod VaultErrors {
pub const INSUFFICIENT_BALANCE: felt252 = 'insufficient_balance';
// you can define more errors here
}

#[starknet::contract]
pub mod VaultErrorsExample {
use super::VaultErrors;
Expand All @@ -32,7 +33,7 @@ pub mod VaultErrorsExample {
assert(balance >= amount, VaultErrors::INSUFFICIENT_BALANCE);

// Or using panic:
if (balance >= amount) {
if (balance < amount) {
core::panic_with_felt252(VaultErrors::INSUFFICIENT_BALANCE);
}

Expand All @@ -42,3 +43,35 @@ pub mod VaultErrorsExample {
}
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod test {
use super::{
VaultErrorsExample, IVaultErrorsExampleDispatcher, IVaultErrorsExampleDispatcherTrait
};
use starknet::{ContractAddress, SyscallResultTrait, syscalls::deploy_syscall};

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

#[test]
fn should_deposit_and_withdraw() {
let mut contract = deploy();
contract.deposit(10);
contract.withdraw(5);
}

#[test]
#[should_panic(expected: ('insufficient_balance', 'ENTRYPOINT_FAILED'))]
fn should_panic_on_insufficient_balance() {
let mut contract = deploy();
contract.deposit(10);
contract.withdraw(15);
}
}
Loading