Skip to content

Commit

Permalink
refactor: replace external(v0) by abi(embed_v0) (#126)
Browse files Browse the repository at this point in the history
* refactor: replace externalv0 by abi(embed_v0)

* refactor: remove implicit interfaces

* fix: restore implicit interface abi_per_item based
  • Loading branch information
enitrat authored Nov 26, 2023
1 parent 2f153c0 commit 35ec815
Show file tree
Hide file tree
Showing 24 changed files with 238 additions and 157 deletions.
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()
}
}
}
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)
}
}
}
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;
}
}
}
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))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
#[starknet::interface]
trait IMatchExample<TContractState> {
fn value_in_cents(self: @TContractState, coin: Coin) -> felt252;
fn specified_colour(self: @TContractState, colour: Colour) -> felt252;
fn quiz(self: @TContractState, num: felt252) -> felt252;
}


#[derive(Drop, Serde)]
enum Colour {
Red,
Blue,
Green,
Orange,
Black
}

#[derive(Drop, Serde)]
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}


#[starknet::contract]
mod matchExample {
mod MatchExample {
use super::{Colour, Coin};
#[storage]
struct Storage {}

#[derive(Drop, Serde)]
enum Colour {
Red,
Blue,
Green,
Orange,
Black
}

#[derive(Drop, Serde)]
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}

#[external(v0)]
#[generate_trait]
impl external of externlalTrait {
#[abi(embed_v0)]
impl External of super::IMatchExample<ContractState> {
fn value_in_cents(self: @ContractState, coin: Coin) -> felt252 {
match coin {
Coin::Penny => 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
use starknet::ContractAddress;

#[starknet::interface]
trait IStructExample<TContractState> {
fn store_struct(ref self: TContractState, age: u8);
fn read_struct(self: @TContractState) -> (ContractAddress, u8);
}

#[starknet::contract]
mod StructExample {
use starknet::{ContractAddress, get_caller_address};

#[storage]
struct Storage {
userData: data
user_data: Data
}

#[derive(Drop, starknet::Store)]
struct data {
Add: ContractAddress,
Age: u8
struct Data {
address: ContractAddress,
age: u8
}

#[external(v0)]
#[generate_trait]
impl StoreStructImpl of IStoreStructContract {
#[abi(embed_v0)]
impl StoreStructImpl of super::IStructExample<ContractState> {
fn store_struct(ref self: ContractState, age: u8) {
let newStruct = data { Add: get_caller_address(), Age: age };
self.userData.write(newStruct);
let new_struct = Data { address: get_caller_address(), age: age };
self.user_data.write(new_struct);
}

fn read_struct(self: @ContractState) -> (ContractAddress, u8) {
let lastUser = self.userData.read();
let add = lastUser.Add;
let age = lastUser.Age;
let last_user = self.user_data.read();
let add = last_user.address;
let age = last_user.age;
(add, age)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use starknet::ContractAddress;

#[starknet::interface]
trait ITupleExample<TContractState> {
fn store_tuple(self: @TContractState, address: ContractAddress, age: u64, active: bool);
fn read_tuple(self: @TContractState) -> (ContractAddress, u64, bool);
}

#[starknet::contract]
mod TupleExample {
use starknet::{ContractAddress, get_caller_address};
Expand All @@ -7,9 +15,8 @@ mod TupleExample {
user_data: (ContractAddress, u64, bool)
}

#[external(v0)]
#[generate_trait]
impl TupleExampleImpl of ITupleExampleImpl {
#[abi(embed_v0)]
impl TupleExampleImpl of super::ITupleExample<ContractState> {
fn store_tuple(ref self: ContractState, address: ContractAddress, age: u64, active: bool) {
let user_tuple = (address, age, active);
self.user_data.write(user_tuple);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
use starknet::ContractAddress;

#[starknet::interface]
trait ITypecastingExample<TContractState> {
fn type_casting(self: @TContractState, rand_number: u32);
}
#[starknet::contract]
mod typecasting_Example {
mod TypecastingExample {
#[storage]
struct Storage {}

#[external(v0)]
#[generate_trait]
impl external of externlalTrait {
fn typeCasting(self: @ContractState, randNumber: u32) {
#[abi(embed_v0)]
impl External of super::ITypecastingExample<ContractState> {
fn type_casting(self: @ContractState, rand_number: u32) {
let my_felt252 = 15;

// Since a u32 might not fit in a u8 and a u16, we need to use try_into,
// then unwrap the Option<T> type thats returned.
let new_u8: u8 = randNumber.try_into().unwrap();
let new_u16: u16 = randNumber.try_into().unwrap();
let new_u8: u8 = rand_number.try_into().unwrap();
let new_u16: u16 = rand_number.try_into().unwrap();

// since new_u32 is the of the same type (u32) as randNumber, we can directly assign them,
// since new_u32 is the of the same type (u32) as rand_number, we can directly assign them,
// or use the .into() method.
let new_u32: u32 = randNumber;
let new_u32: u32 = rand_number;

// When typecasting from a smaller size to an equal or larger size we use the .into() method.
// Note: u64 and u128 are larger than u32, so a u32 type will always fit into them.
let new_u64: u64 = randNumber.into();
let new_u128: u128 = randNumber.into();
let new_u64: u64 = rand_number.into();
let new_u128: u128 = rand_number.into();

// Since a felt252 is smaller than a u256, we can use the into() method
let new_u256: u256 = my_felt252.into();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#[starknet::interface]
trait ICallee<TContractState> {
fn set_value(ref self: TContractState, value: u128) -> u128;
}

#[starknet::contract]
mod Callee {
#[storage]
struct Storage {
value: u128,
}

#[abi(per_item)]
#[generate_trait]
impl ICalleeImpl of ICallee {
#[abi(embed_v0)]
impl ICalleeImpl of super::ICallee<ContractState> {
fn set_value(ref self: ContractState, value: u128) -> u128 {
self.value.write(value);
value
Expand Down
Loading

0 comments on commit 35ec815

Please sign in to comment.