Skip to content

Commit

Permalink
fix(#91): remove external(v0) usage on impl block
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Oct 19, 2023
1 parent dcadbd1 commit c4fb7c0
Show file tree
Hide file tree
Showing 30 changed files with 120 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod Callee {
value: u128,
}

#[external(v0)]
#[abi(per_item)]
#[generate_trait]
impl ICalleeImpl of ICallee {
fn set_value(ref self: ContractState, value: u128) -> u128 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ mod Caller {
#[storage]
struct Storage {}

#[abi(per_item)]
#[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);
Expand Down
6 changes: 2 additions & 4 deletions listings/ch00-getting-started/counter/src/counter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ trait ISimpleCounter<TContractState> {
fn decrement(ref self: TContractState);
}


#[starknet::contract]
mod SimpleCounter {
#[storage]
Expand All @@ -20,9 +19,8 @@ mod SimpleCounter {
self.counter.write(init_value);
}

#[generate_trait]
#[external(v0)]
impl SimpleCounter of ISimpleCounter {
#[abi(embed_v0)]
impl SimpleCounter of super::ISimpleCounter<ContractState> {
fn get_current_count(self: @ContractState) -> u256 {
return self.counter.read();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ mod SerdeCustomType {
age: u8,
name: felt252
}
#[external(v0)]

#[abi(per_item)]
#[generate_trait]
impl SerdeCustomType of ISerdeCustomType {
#[external(v0)]
fn person_input(ref self: ContractState, person: Person) {}

#[external(v0)]
fn person_output(self: @ContractState) -> Person {
Person { age: 10, name: 'Joe' }
}
Expand Down
4 changes: 3 additions & 1 deletion listings/ch00-getting-started/errors/src/custom_errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ mod CustomErrorsExample {
#[storage]
struct Storage {}

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

#[external(v0)]
fn test_panic(self: @ContractState, i: u256) {
if (i == 0) {
panic_with_felt252(Errors::NOT_NULL);
Expand Down
4 changes: 3 additions & 1 deletion listings/ch00-getting-started/errors/src/simple_errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ mod ErrorsExample {
#[storage]
struct Storage {}

#[abi(per_item)]
#[generate_trait]
#[external(v0)]
impl ErrorsExample of IErrorsExample {
#[external(v0)]
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');
}

#[external(v0)]
fn test_panic(self: @ContractState, i: u256) {
if (i == 0) {
// Panic used to abort execution directly
Expand Down
4 changes: 3 additions & 1 deletion listings/ch00-getting-started/errors/src/vault_errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ mod VaultErrorsExample {
balance: u256,
}

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

#[external(v0)]
fn withdraw(ref self: ContractState, amount: u256) {
let mut balance = self.balance.read();

Expand Down
2 changes: 1 addition & 1 deletion listings/ch00-getting-started/events/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "counter"
name = "events"
version = "0.1.0"

[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion listings/ch00-getting-started/events/src/counter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ mod EventCounter {
new_value: u128,
}

#[abi(per_item)]
#[generate_trait]
#[external(v0)]
impl EventCounter of IEventCounter {
#[external(v0)]
fn increment(ref self: ContractState) {
let mut counter = self.counter.read();
counter += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ trait IExplicitInterfaceContract<TContractState> {

#[starknet::contract]
mod ExplicitInterfaceContract {
use super::IExplicitInterfaceContract;

#[storage]
struct Storage {
value: u32
}

#[external(v0)]
impl ExplicitInterfaceContract of IExplicitInterfaceContract<ContractState> {
#[abi(embed_v0)]
impl ExplicitInterfaceContract of super::IExplicitInterfaceContract<ContractState> {
fn get_value(self: @ContractState) -> u32 {
self.value.read()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ mod ImplicitInterfaceContract {
value: u32
}

#[external(v0)]
#[abi(per_item)]
#[generate_trait]
impl ImplicitInterfaceContract of IImplicitInterfaceContract {
#[external(v0)]
fn get_value(self: @ContractState) -> u32 {
self.value.read()
}

#[external(v0)]
fn set_value(ref self: ContractState, value: u32) {
self.value.write(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod ImplicitInternalContract {

#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {
#[external(v0)]
fn set_value(ref self: ContractState, value: u32) {
self.value.write(value);
}
Expand All @@ -29,7 +30,7 @@ mod ImplicitInternalContract {
self.set_value(0);
}

#[external(v0)]
#[abi(embed_v0)]
impl ImplicitInternalContract of IImplicitInternalContract<ContractState> {
fn add(ref self: ContractState, nb: u32) {
self.set_value(self.value.read() + nb);
Expand Down
4 changes: 3 additions & 1 deletion listings/ch00-getting-started/mappings/src/mappings.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ mod MapContract {
map: LegacyMap::<ContractAddress, felt252>,
}

#[abi(per_item)]
#[generate_trait]
#[external(v0)]
impl MapContractImpl of IMapContract {
#[external(v0)]
fn set(ref self: ContractState, key: ContractAddress, value: felt252) {
self.map.write(key, value);
}

#[external(v0)]
fn get(self: @ContractState, key: ContractAddress) -> felt252 {
self.map.read(key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ mod StoringCustomType {
name: felt252
}

#[external(v0)]
#[abi(per_item)]
#[generate_trait]
impl StoringCustomType of IStoringCustomType {
#[external(v0)]
fn set_person(ref self: ContractState) {
self.person.write(Person { age: 10, name: 'Joe' });
}

#[external(v0)]
fn get_person(self: @ContractState) {
self.person.read();
}
Expand Down
2 changes: 1 addition & 1 deletion listings/ch00-getting-started/testing/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod SimpleContract {
self.owner.write(get_caller_address());
}

#[external(v0)]
#[abi(embed_v0)]
impl SimpleContract of super::ISimpleContract<ContractState> {
fn get_value(self: @ContractState) -> u32 {
self.value.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ mod GlobalExample {
#[storage]
struct Storage {}

#[abi(per_item)]
#[generate_trait]
#[external(v0)]
impl GlobalExampleImpl of IGlobalExample {
#[external(v0)]
fn foo(ref self: ContractState) {
// Call the get_caller_address function to get the sender address
let caller = get_caller_address();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ mod LocalVariablesExample {
#[storage]
struct Storage {}

#[abi(per_item)]
#[generate_trait]
#[external(v0)]
impl LocalVariablesExample of ILocalVariablesExample {
#[external(v0)]
fn do_something(self: @ContractState, value: u32) -> u32 {
// This variable is local to the current block. It can't be accessed once it goes out of scope.
let increment = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ mod StorageVariablesExample {
value: u32
}

#[abi(per_item)]
#[generate_trait]
#[external(v0)]
impl StorageVariablesExample of IStorageVariableExample {
// Write to storage variables by sending a transaction that calls an external function
#[external(v0)]
fn set(ref self: ContractState, value: u32) {
self.value.write(value);
}

// Read from storage variables without sending transactions
#[external(v0)]
fn get(ref self: ContractState) -> u32 {
self.value.read()
}
Expand Down
15 changes: 10 additions & 5 deletions listings/ch00-getting-started/visibility/src/visibility.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[starknet::interface]
trait IExampleContract<TContractState> {
fn set(ref self: TContractState, value: u32);
fn get(self: @TContractState) -> u32;
}

#[starknet::contract]
mod ExampleContract {
#[storage]
Expand All @@ -6,11 +12,10 @@ mod ExampleContract {
}


// The `external(v0)` attribute indicates that all the functions in this implementation can be called externally.
// The `abi(embed_v0)` attribute indicates that all the functions in this implementation can be called externally.
// Omitting this attribute would make all the functions in this implementation internal.
#[external(v0)]
#[generate_trait]
impl ExampleContract of IExampleContract {
#[abi(embed_v0)]
impl ExampleContract of super::IExampleContract<ContractState> {
// The `set` function can be called externally because it is written inside an implementation marked as `#[external]`.
// It can modify the contract's state as it is passed as a reference.
fn set(ref self: ContractState, value: u32) {
Expand All @@ -29,7 +34,7 @@ mod ExampleContract {
// We name the trait `PrivateFunctionsTrait` to indicate that it is an internal trait allowing us to call internal functions.
#[generate_trait]
impl PrivateFunctions of PrivateFunctionsTrait {
// The `_read_value` function is outside the implementation that is marked as `#[external(v0)]`, so it's an _internal_ function
// The `_read_value` function is outside the implementation that is marked as `#[abi(embed_v0)]`, so it's an _internal_ function
// and can only be called from within the contract.
// It can modify the contract's state as it is passed as a reference.
fn _read_value(self: @ContractState) -> u32 {
Expand Down
11 changes: 8 additions & 3 deletions listings/ch01-applications/simple_vault/src/simple_vault.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ trait IERC20<TContractState> {
fn approve(ref self: TContractState, spender: ContractAddress, amount: u256) -> bool;
}

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

#[starknet::contract]
mod SimpleVault {
use super::{IERC20Dispatcher, IERC20DispatcherTrait};
Expand Down Expand Up @@ -46,9 +52,8 @@ mod SimpleVault {
}
}

#[external(v0)]
#[generate_trait]
impl SimpleVault of ISimpleVault {
#[abi(embed_v0)]
impl SimpleVault of super::ISimpleVault<ContractState> {
fn deposit(ref self: ContractState, amount: u256) {
// a = amount
// B = balance of token before deposit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use starknet::class_hash::ClassHash;

#[starknet::interface]
trait IUpgradeableContract<TContractState> {
fn upgrade(ref self: TContractState, impl_hash: ClassHash);
fn version(self: @TContractState) -> u8;
}

#[starknet::contract]
mod UpgradeableContract_V0 {
use starknet::class_hash::ClassHash;
Expand All @@ -18,9 +26,8 @@ mod UpgradeableContract_V0 {
implementation: ClassHash
}

#[generate_trait]
#[external(v0)]
impl UpgradeableContract of IUpgradeableContract {
#[abi(embed_v0)]
impl UpgradeableContract of super::IUpgradeableContract<ContractState> {
fn upgrade(ref self: ContractState, impl_hash: ClassHash) {
assert(!impl_hash.is_zero(), 'Class hash cannot be zero');
starknet::replace_class_syscall(impl_hash).unwrap_syscall();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use starknet::class_hash::ClassHash;

#[starknet::interface]
trait IUpgradeableContract<TContractState> {
fn upgrade(ref self: TContractState, impl_hash: ClassHash);
fn version(self: @TContractState) -> u8;
}

#[starknet::contract]
mod UpgradeableContract_V1 {
use starknet::class_hash::ClassHash;
Expand All @@ -18,9 +26,8 @@ mod UpgradeableContract_V1 {
implementation: ClassHash
}

#[generate_trait]
#[external(v0)]
impl UpgradeableContract of IUpgradeableContract {
#[abi(embed_v0)]
impl UpgradeableContract of super::IUpgradeableContract<ContractState> {
fn upgrade(ref self: ContractState, impl_hash: ClassHash) {
assert(!impl_hash.is_zero(), 'Class hash cannot be zero');
starknet::replace_class_syscall(impl_hash).unwrap_syscall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod TimeContract {
}
}

#[external(v0)]
#[abi(embed_v0)]
impl TimeContract of super::ITime<ContractState> {
fn set(ref self: ContractState, value: Time) {
// This will call the pack method of the TimePackable trait
Expand Down
Loading

0 comments on commit c4fb7c0

Please sign in to comment.