Skip to content

Commit

Permalink
feat: add interface traits cairo programs
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jul 13, 2023
1 parent 7877977 commit ab3d58b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions listings/ch00-introduction/interfaces_traits/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
8 changes: 8 additions & 0 deletions listings/ch00-introduction/interfaces_traits/Scarb.toml
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 listings/ch00-introduction/interfaces_traits/src/explicit.cairo
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 listings/ch00-introduction/interfaces_traits/src/implicit.cairo
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
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');
}
}
3 changes: 3 additions & 0 deletions listings/ch00-introduction/interfaces_traits/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod explicit;
mod implicit;
mod implicit_internal;

0 comments on commit ab3d58b

Please sign in to comment.