-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace external(v0) by abi(embed_v0) (#126)
* refactor: replace externalv0 by abi(embed_v0) * refactor: remove implicit interfaces * fix: restore implicit interface abi_per_item based
- Loading branch information
Showing
24 changed files
with
238 additions
and
157 deletions.
There are no files selected for viewing
35 changes: 19 additions & 16 deletions
35
listings/ch00-getting-started/cairo_cheatsheet/src/array_example.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 |
---|---|---|
@@ -1,29 +1,32 @@ | ||
#[starknet::interface] | ||
trait IArrayExample<TContractState> { | ||
fn createarray(self: @TContractState, num_one: u32, num_two: u32, num_three: u32) -> bool; | ||
} | ||
|
||
#[starknet::contract] | ||
mod arrayExample { | ||
mod ArrayExample { | ||
#[storage] | ||
struct Storage {} | ||
|
||
|
||
#[external(v0)] | ||
#[generate_trait] | ||
impl external of externalTrait { | ||
fn createArray(self: @ContractState, numOne: u32, numTwo: u32, numThree: u32) -> bool { | ||
let mut Arr = ArrayTrait::<u32>::new(); | ||
Arr.append(numOne); | ||
Arr.append(numTwo); | ||
Arr.append(numThree); | ||
#[abi(embed_v0)] | ||
impl External of super::IArrayExample<ContractState> { | ||
fn createarray(self: @ContractState, num_one: u32, num_two: u32, num_three: u32) -> bool { | ||
let mut arr = ArrayTrait::<u32>::new(); | ||
arr.append(num_one); | ||
arr.append(num_two); | ||
arr.append(num_three); | ||
|
||
let ArrLength: usize = Arr.len(); | ||
assert(ArrLength == 3, 'Array Length should be 3'); | ||
assert(arr.len() == 3, 'array length should be 3'); | ||
|
||
let first_value = Arr.pop_front().unwrap(); | ||
assert(first_value == numOne, 'Both values should match'); | ||
let first_value = arr.pop_front().unwrap(); | ||
assert(first_value == num_one, 'first value should match'); | ||
|
||
let second_value = *Arr.at(0); | ||
assert(second_value == numTwo, 'Both values should match too'); | ||
let second_value = *arr.at(0); | ||
assert(second_value == num_two, 'second value should match'); | ||
|
||
//Returns true if an array is empty, then false if it isn't. | ||
Arr.is_empty() | ||
arr.is_empty() | ||
} | ||
} | ||
} |
28 changes: 17 additions & 11 deletions
28
listings/ch00-getting-started/cairo_cheatsheet/src/felt_example.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 |
---|---|---|
@@ -1,24 +1,30 @@ | ||
use starknet::ContractAddress; | ||
#[starknet::interface] | ||
trait IFelt252Example<TContractState> { | ||
fn store_name(ref self: TContractState, name: felt252) -> felt252; | ||
fn view_name(self: @TContractState, address: ContractAddress) -> felt252; | ||
} | ||
|
||
#[starknet::contract] | ||
mod feltExample { | ||
mod Felt252Example { | ||
use starknet::{ContractAddress, get_caller_address}; | ||
|
||
#[storage] | ||
struct Storage { | ||
userName: LegacyMap::<ContractAddress, felt252>, | ||
user_name: LegacyMap::<ContractAddress, felt252>, | ||
} | ||
|
||
#[external(v0)] | ||
#[generate_trait] | ||
impl external of externlalTrait { | ||
fn storeName(ref self: ContractState, name: felt252) -> felt252 { | ||
self.userName.write(get_caller_address(), name); | ||
#[abi(embed_v0)] | ||
impl External of super::IFelt252Example<ContractState> { | ||
fn store_name(ref self: ContractState, name: felt252) -> felt252 { | ||
self.user_name.write(get_caller_address(), name); | ||
|
||
let welcomeMsg: felt252 = 'Welcome to StarknetByExample'; | ||
welcomeMsg | ||
let welcome_msg: felt252 = 'Welcome to StarknetByExample'; | ||
welcome_msg | ||
} | ||
|
||
fn viewName(self: @ContractState, Add: ContractAddress) -> felt252 { | ||
self.userName.read(Add) | ||
fn view_name(self: @ContractState, address: ContractAddress) -> felt252 { | ||
self.user_name.read(address) | ||
} | ||
} | ||
} |
20 changes: 12 additions & 8 deletions
20
listings/ch00-getting-started/cairo_cheatsheet/src/loop_example.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 |
---|---|---|
@@ -1,25 +1,29 @@ | ||
#[starknet::interface] | ||
trait ILoopExample<TContractState> { | ||
fn gather_evens(ref self: TContractState, maxLimit: u32) -> Array<u32>; | ||
} | ||
|
||
#[starknet::contract] | ||
mod loopExample { | ||
mod LoopExample { | ||
#[storage] | ||
struct Storage {} | ||
|
||
#[external(v0)] | ||
#[generate_trait] | ||
impl external of externlalTrait { | ||
fn gatherEvens(ref self: ContractState, maxLimit: u32) -> Array<u32> { | ||
#[abi(embed_v0)] | ||
impl External of super::ILoopExample<ContractState> { | ||
fn gather_evens(ref self: ContractState, maxLimit: u32) -> Array<u32> { | ||
let mut i: u32 = 0; | ||
let mut Arr = ArrayTrait::new(); | ||
let mut arr = ArrayTrait::new(); | ||
loop { | ||
if i == maxLimit { | ||
break; | ||
}; | ||
if (i % 2 == 0) { | ||
Arr.append(i); | ||
arr.append(i); | ||
} | ||
i += 1; | ||
}; | ||
|
||
return Arr; | ||
return arr; | ||
} | ||
} | ||
} |
47 changes: 30 additions & 17 deletions
47
listings/ch00-getting-started/cairo_cheatsheet/src/mapping_example.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 |
---|---|---|
@@ -1,37 +1,50 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IMappingExample<TContractState> { | ||
fn register_user(ref self: TContractState, student_add: ContractAddress, studentName: felt252); | ||
fn record_student_score( | ||
ref self: TContractState, student_add: ContractAddress, subject: felt252, score: u16 | ||
); | ||
fn view_student_name(self: @TContractState, student_add: ContractAddress) -> felt252; | ||
fn view_student_score( | ||
self: @TContractState, student_add: ContractAddress, subject: felt252 | ||
) -> u16; | ||
} | ||
|
||
#[starknet::contract] | ||
mod mappingContract { | ||
mod MappingContract { | ||
use starknet::ContractAddress; | ||
|
||
#[storage] | ||
struct Storage { | ||
studentsName: LegacyMap::<ContractAddress, felt252>, | ||
studentsResultRecord: LegacyMap::<(ContractAddress, felt252), u16>, | ||
students_name: LegacyMap::<ContractAddress, felt252>, | ||
students_result_record: LegacyMap::<(ContractAddress, felt252), u16>, | ||
} | ||
|
||
#[external(v0)] | ||
#[generate_trait] | ||
impl external of externalTrait { | ||
fn registerUser( | ||
ref self: ContractState, studentAdd: ContractAddress, studentName: felt252 | ||
#[abi(embed_v0)] | ||
impl External of super::IMappingExample<ContractState> { | ||
fn register_user( | ||
ref self: ContractState, student_add: ContractAddress, studentName: felt252 | ||
) { | ||
self.studentsName.write(studentAdd, studentName); | ||
self.students_name.write(student_add, studentName); | ||
} | ||
|
||
fn recordStudentScore( | ||
ref self: ContractState, studentAdd: ContractAddress, Subject: felt252, score: u16 | ||
fn record_student_score( | ||
ref self: ContractState, student_add: ContractAddress, subject: felt252, score: u16 | ||
) { | ||
self.studentsResultRecord.write((studentAdd, Subject), score); | ||
self.students_result_record.write((student_add, subject), score); | ||
} | ||
|
||
fn viewStudentName(self: @ContractState, studentAdd: ContractAddress) -> felt252 { | ||
self.studentsName.read(studentAdd) | ||
fn view_student_name(self: @ContractState, student_add: ContractAddress) -> felt252 { | ||
self.students_name.read(student_add) | ||
} | ||
|
||
fn viewStudentScore( | ||
self: @ContractState, studentAdd: ContractAddress, Subject: felt252 | ||
fn view_student_score( | ||
self: @ContractState, student_add: ContractAddress, subject: felt252 | ||
) -> u16 { | ||
// for a 2D mapping its important to take note of the amount of brackets being used. | ||
self.studentsResultRecord.read((studentAdd, Subject)) | ||
self.students_result_record.read((student_add, subject)) | ||
} | ||
} | ||
} |
51 changes: 30 additions & 21 deletions
51
listings/ch00-getting-started/cairo_cheatsheet/src/match_example.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
31 changes: 19 additions & 12 deletions
31
listings/ch00-getting-started/cairo_cheatsheet/src/struct_example.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
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
27 changes: 16 additions & 11 deletions
27
listings/ch00-getting-started/cairo_cheatsheet/src/type_casting_example.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
10 changes: 7 additions & 3 deletions
10
listings/ch00-getting-started/calling_other_contracts/src/callee.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
Oops, something went wrong.