-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add interface traits cairo programs
- Loading branch information
Showing
6 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "interfaces_traits" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
starknet = ">=2.0.1" | ||
|
||
[[target.starknet-contract]] |
64 changes: 64 additions & 0 deletions
64
listings/ch00-introduction/interfaces_traits/src/explicit.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// ANCHOR: code | ||
#[starknet::interface] | ||
trait IExplicitInterfaceContract<TContractState> { | ||
fn get_value(self: @TContractState) -> u32; | ||
fn set_value(ref self: TContractState, value: u32); | ||
} | ||
|
||
#[starknet::contract] | ||
mod ExplicitInterfaceContract { | ||
use super::IExplicitInterfaceContract; | ||
|
||
#[storage] | ||
struct Storage { | ||
value: u32 | ||
} | ||
|
||
#[external(v0)] | ||
impl ExplicitInterfaceContract of IExplicitInterfaceContract<ContractState> { | ||
fn get_value(self: @ContractState) -> u32 { | ||
self.value.read() | ||
} | ||
|
||
fn set_value(ref self: ContractState, value: u32) { | ||
self.value.write(value); | ||
} | ||
} | ||
} | ||
// ANCHOR_END: code | ||
|
||
#[cfg(test)] | ||
mod explicit_interface_contract_tests { | ||
use super::{ | ||
IExplicitInterfaceContract, ExplicitInterfaceContract, IExplicitInterfaceContractDispatcher, | ||
IExplicitInterfaceContractDispatcherTrait | ||
}; | ||
use debug::PrintTrait; | ||
use starknet::{deploy_syscall, ContractAddress}; | ||
use option::OptionTrait; | ||
use array::ArrayTrait; | ||
use traits::{Into, TryInto}; | ||
use starknet::class_hash::Felt252TryIntoClassHash; | ||
use result::ResultTrait; | ||
|
||
#[test] | ||
#[available_gas(2000000000)] | ||
fn test_interface() { | ||
let (contract_address, _) = deploy_syscall( | ||
ExplicitInterfaceContract::TEST_CLASS_HASH.try_into().unwrap(), | ||
0, | ||
ArrayTrait::new().span(), | ||
false | ||
) | ||
.unwrap(); | ||
let mut contract = IExplicitInterfaceContractDispatcher { contract_address }; | ||
|
||
let value: u32 = 20; | ||
contract.set_value(value); | ||
|
||
let read_value = contract.get_value(); | ||
read_value.print(); | ||
|
||
assert(read_value == value, 'wrong value'); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
listings/ch00-introduction/interfaces_traits/src/implicit.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// ANCHOR: code | ||
#[starknet::contract] | ||
mod ImplicitInterfaceContract { | ||
#[storage] | ||
struct Storage { | ||
value: u32 | ||
} | ||
|
||
#[external(v0)] | ||
#[generate_trait] | ||
impl ImplicitInterfaceContract of IImplicitInterfaceContract { | ||
fn get_value(self: @ContractState) -> u32 { | ||
self.value.read() | ||
} | ||
|
||
fn set_value(ref self: ContractState, value: u32) { | ||
self.value.write(value); | ||
} | ||
} | ||
} | ||
// ANCHOR_END: code |
83 changes: 83 additions & 0 deletions
83
listings/ch00-introduction/interfaces_traits/src/implicit_internal.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// ANCHOR: code | ||
#[starknet::interface] | ||
trait IImplicitInternalContract<TContractState> { | ||
fn add(ref self: TContractState, nb: u32); | ||
fn get_value(self: @TContractState) -> u32; | ||
fn get_const(self: @TContractState) -> u32; | ||
} | ||
|
||
#[starknet::contract] | ||
mod ImplicitInternalContract { | ||
use super::IImplicitInternalContract; | ||
|
||
#[storage] | ||
struct Storage { | ||
value: u32 | ||
} | ||
|
||
#[generate_trait] | ||
impl InternalFunctions of InternalFunctionsTrait { | ||
fn set_value(ref self: ContractState, value: u32) { | ||
self.value.write(value); | ||
} | ||
fn get_const() -> u32 { | ||
42 | ||
} | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState) { | ||
self.set_value(0); | ||
} | ||
|
||
#[external(v0)] | ||
impl ImplicitInternalContract of IImplicitInternalContract<ContractState> { | ||
fn add(ref self: ContractState, nb: u32) { | ||
self.set_value(self.value.read() + nb); | ||
} | ||
fn get_value(self: @ContractState) -> u32 { | ||
self.value.read() | ||
} | ||
fn get_const(self: @ContractState) -> u32 { | ||
self.get_const() | ||
} | ||
} | ||
} | ||
// ANCHOR_END: code | ||
|
||
#[cfg(test)] | ||
mod implicit_internal_contract_tests { | ||
use super::{ | ||
IImplicitInternalContract, ImplicitInternalContract, IImplicitInternalContractDispatcher, | ||
IImplicitInternalContractDispatcherTrait | ||
}; | ||
use debug::PrintTrait; | ||
use starknet::{deploy_syscall, ContractAddress}; | ||
use option::OptionTrait; | ||
use array::ArrayTrait; | ||
use traits::{Into, TryInto}; | ||
use starknet::class_hash::Felt252TryIntoClassHash; | ||
use result::ResultTrait; | ||
|
||
#[test] | ||
#[available_gas(2000000000)] | ||
fn test_interface() { | ||
// Set up. | ||
let (contract_address, _) = deploy_syscall( | ||
ImplicitInternalContract::TEST_CLASS_HASH.try_into().unwrap(), | ||
0, | ||
ArrayTrait::new().span(), | ||
false | ||
) | ||
.unwrap(); | ||
let mut contract = IImplicitInternalContractDispatcher { contract_address }; | ||
|
||
let initial_value: u32 = 0; | ||
assert(contract.get_value() == initial_value, 'wrong value'); | ||
|
||
let add_value: u32 = 10; | ||
contract.add(add_value); | ||
|
||
assert(contract.get_value() == initial_value + add_value, 'wrong value after add'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod explicit; | ||
mod implicit; | ||
mod implicit_internal; |