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

refactor: v2.0.1 syntax #55

Merged
merged 4 commits into from
Jul 7, 2023
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 .github/workflows/verify-cairo-programs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ runs:
- name: Install scarb
shell: bash
run: |
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 0.4.0
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 0.5.1

- name: Run build script
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verify_cairo_programs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- name: Install scarb
run: |
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 0.4.0
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v 0.5.1

- name: Run build script
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ version = "0.1.0"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest

[dependencies]
starknet = "1.1.0"
starknet = ">=2.0.1"

[[target.starknet-contract]]
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#[contract]
#[starknet::contract]
mod Callee {
#[storage]
struct Storage {
_x: u128,
value: u128,
}

#[external]
fn set_x(x: u128) -> u128 {
_x::write(x);
x
#[external(v0)]
#[generate_trait]
impl ICalleeImpl of ICallee {
fn set_value(ref self: ContractState, value: u128) -> u128 {
self.value.write(value);
value
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
#[abi]
trait ICallee {
fn set_x(x: u128) -> u128;
}
use starknet::ContractAddress;

// We need to have the interface of the callee contract defined
// so that we can import the Dispatcher.
#[starknet::interface]
trait ICallee<TContractState> {
fn set_value(ref self: TContractState, value: u128) -> u128;
}

#[contract]
#[starknet::contract]
mod Caller {
// We import the Dispatcher of the called contract
use super::{ICalleeDispatcher, ICalleeDispatcherTrait};
use starknet::ContractAddress;

#[external]
fn set_x_from_address(addr: ContractAddress, x: u128) {
let x = ICalleeDispatcher { contract_address: addr }.set_x(x);
#[storage]
struct Storage {}

#[generate_trait]
#[external(v0)]
impl ICallerImpl of ICaller {
fn set_value_from_address(ref self: ContractState, addr: ContractAddress, value: u128) {
ICalleeDispatcher { contract_address: addr }.set_value(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod callee;
mod caller;
mod callee;
2 changes: 1 addition & 1 deletion listings/ch00-introduction/constructor/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ version = "0.1.0"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest

[dependencies]
starknet = "1.1.0"
starknet = ">=2.0.1"

[[target.starknet-contract]]
13 changes: 8 additions & 5 deletions listings/ch00-introduction/constructor/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#[contract]
mod constructor {
#[starknet::contract]
mod ExampleConstructor {
use starknet::ContractAddress;

#[storage]
struct Storage {
_names: LegacyMap::<ContractAddress, felt252>,
names: LegacyMap::<ContractAddress, felt252>,
}

// The constructor is decorated with a `#[constructor]` attribute.
// It is not inside an `impl` block.
#[constructor]
fn constructor(name: felt252, address: ContractAddress) {
_names::write(address, name);
fn constructor(ref self: ContractState, name: felt252, address: ContractAddress) {
self.names.write(address, name);
}
}
2 changes: 1 addition & 1 deletion listings/ch00-introduction/counter/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ name = "counter"
version = "0.1.0"

[dependencies]
starknet = "1.1.0"
starknet = ">=2.0.1"

[[target.starknet-contract]]
48 changes: 27 additions & 21 deletions listings/ch00-introduction/counter/src/counter.cairo
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
#[contract]
#[starknet::interface]
trait ISimpleCounter<TContractState> {
fn get_current_count(self: @TContractState) -> u256;
fn increment(ref self: TContractState);
fn decrement(ref self: TContractState);
}


#[starknet::contract]
mod SimpleCounter {
#[storage]
struct Storage {
// Counter variable
_counter: u256,
counter: u256,
}

#[constructor]
fn constructor() {}

#[view]
fn get_current_count() -> u256 {
return _counter::read();
}

#[external]
fn increment() {
// Store counter value + 1
let counter: u256 = _counter::read() + 1;
_counter::write(counter);
}
#[generate_trait]
#[external(v0)]
impl SimpleCounter of ISimpleCounter {
fn get_current_count(self: @ContractState) -> u256 {
return self.counter.read();
}

#[external]
fn decrement() {
// Store counter value - 1
let counter: u256 = _counter::read() - 1;
_counter::write(counter);
fn increment(ref self: ContractState) {
// Store counter value + 1
let mut counter: u256 = self.counter.read() + 1;
self.counter.write(counter);
}
fn decrement(ref self: ContractState) {
// Store counter value - 1
let mut counter: u256 = self.counter.read() - 1;
self.counter.write(counter);
}
}
}
2 changes: 1 addition & 1 deletion listings/ch00-introduction/errors/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ version = "0.1.0"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest

[dependencies]
starknet = "1.1.0"
starknet = ">=2.0.1"

[[target.starknet-contract]]
25 changes: 16 additions & 9 deletions listings/ch00-introduction/errors/src/custom_errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ mod Errors {
const NOT_NULL: felt252 = 'must not be null';
}

#[contract]

#[starknet::contract]
mod CustomErrorsExample {
use super::Errors;

#[view]
fn test_assert(i: u256) {
assert(i > 0, Errors::NOT_POSITIVE);
}
#[storage]
struct Storage {}

#[generate_trait]
#[external(v0)]
impl CustomErrorsExample of ICustomErrorsExample{
fn test_assert(self: @ContractState, i: u256) {
assert(i > 0, Errors::NOT_POSITIVE);
}

#[view]
fn test_panic(i: u256) {
if (i == 0) {
panic_with_felt252(Errors::NOT_NULL);
#[view]
fn test_panic(self: @ContractState, i: u256) {
if (i == 0) {
panic_with_felt252(Errors::NOT_NULL);
}
}
}
}
29 changes: 17 additions & 12 deletions listings/ch00-introduction/errors/src/simple_errors.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#[contract]
#[starknet::contract]
mod ErrorsExample {
#[view]
fn test_assert(i: u256) {
// Assert used to validate a condition
// and abort execution if the condition is not met
assert(i > 0, 'i must be greater than 0');
}
#[storage]
struct Storage {}

#[generate_trait]
#[external(v0)]
impl ErrorsExample of IErrorsExample {
fn test_assert(self: @ContractState, i: u256) {
// Assert used to validate a condition
// and abort execution if the condition is not met
assert(i > 0, 'i must be greater than 0');
}

#[view]
fn test_panic(i: u256) {
if (i == 0) {
// Panic used to abort execution directly
panic_with_felt252('i must not be 0');
fn test_panic(self: @ContractState, i: u256) {
if (i == 0) {
// Panic used to abort execution directly
panic_with_felt252('i must not be 0');
}
}
}
}
38 changes: 20 additions & 18 deletions listings/ch00-introduction/errors/src/vault_errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@ mod VaultErrors {
// you can define more errors here
}

#[contract]
#[starknet::contract]
mod VaultErrorsExample {
use super::VaultErrors;

#[storage]
struct Storage {
balance: u256,
}

#[external]
fn deposit(amount: u256) {
let old_balance = balance::read();
let new_balance = old_balance + amount;
#[generate_trait]
#[external(v0)]
impl VaultErrorsExample of IVaultErrorsExample {
fn deposit(ref self: ContractState, amount: u256) {
let mut balance = self.balance.read();
balance = balance + amount;
self.balance.write(balance);
}

balance::write(new_balance);
}
fn withdraw(ref self: ContractState, amount: u256) {
let mut balance = self.balance.read();

#[external]
fn withdraw(amount: u256) {
let current_balance = balance::read();
assert(balance >= amount, VaultErrors::INSUFFICIENT_BALANCE);

assert(current_balance >= amount, VaultErrors::INSUFFICIENT_BALANCE);
// Or using panic:
if (balance >= amount) {
panic_with_felt252(VaultErrors::INSUFFICIENT_BALANCE);
}

// Or using panic:
if (current_balance >= amount) {
panic_with_felt252(VaultErrors::INSUFFICIENT_BALANCE);
}
let balance = balance - amount;

let new_balance = current_balance - amount;

balance::write(new_balance);
self.balance.write(balance);
}
}
}
2 changes: 1 addition & 1 deletion listings/ch00-introduction/events/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ name = "counter"
version = "0.1.0"

[dependencies]
starknet = "1.1.0"
starknet = ">=2.0.1"

[[target.starknet-contract]]
40 changes: 26 additions & 14 deletions listings/ch00-introduction/events/src/counter.cairo
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
#[contract]
mod SimpleCounter {
#[starknet::contract]
mod EventCounter {
#[storage]
struct Storage {
// Counter value
_counter: u256,
counter: u128,
}

#[event]
// Increment event - emitted when the counter is incremented.
fn Increment(counterVal: u256) {}
#[derive(Drop, starknet::Event)]
// The event enum must be annotated with the `#[event]` attribute.
// It must also derive the `Drop` and `starknet::Event` traits.
enum Event {
CounterIncreased: CounterIncreased
}

#[constructor]
fn constructor() {}
// By deriving the `starknet::Event` trait, we indicate to the compiler that
// this struct will be used when emitting events.
#[derive(Drop, starknet::Event)]
struct CounterIncreased {
amount: u128
}

#[external]
fn increment() {
let mut counter: u256 = _counter::read();
counter += 1;
_counter::write(counter);
// Emit event
Increment(counter);
#[generate_trait]
#[external(v0)]
impl EventCounter of IEventCounter {
fn increment(ref self: ContractState) {
let mut counter = self.counter.read();
counter += 1;
self.counter.write(counter);
// Emit event
self.emit(Event::CounterIncreased(CounterIncreased { amount: 1 }))
}
}
}
Loading