From 3f82348c6e45eadaa567214c70c4942397e6c79c Mon Sep 17 00:00:00 2001 From: Abdulhakeem Abdulazeez Date: Thu, 9 May 2024 10:13:21 +0100 Subject: [PATCH] feat: assignment done --- src/addition.cairo | 5 ---- src/aggregator_contract.cairo | 45 ---------------------------- src/counter_contract.cairo | 28 ------------------ src/errors.cairo | 6 ---- src/hello_starknet.cairo | 0 src/lib.cairo | 55 +++++++++++++++++++++++++++++++---- src/ownable_contract.cairo | 41 -------------------------- 7 files changed, 50 insertions(+), 130 deletions(-) delete mode 100644 src/addition.cairo delete mode 100644 src/aggregator_contract.cairo delete mode 100644 src/counter_contract.cairo delete mode 100644 src/errors.cairo delete mode 100644 src/hello_starknet.cairo delete mode 100644 src/ownable_contract.cairo diff --git a/src/addition.cairo b/src/addition.cairo deleted file mode 100644 index 066250a..0000000 --- a/src/addition.cairo +++ /dev/null @@ -1,5 +0,0 @@ -pub fn add_num(num1: u32, num2: u32) -> u32 { - // assert(num1 != 0, deep_dive::errors::Errors::ZERO_AMOUNT); - // assert(num2 != 0, deep_dive::errors::Errors::ZERO_AMOUNT); - num1 + num2 -} diff --git a/src/aggregator_contract.cairo b/src/aggregator_contract.cairo deleted file mode 100644 index 5283f1d..0000000 --- a/src/aggregator_contract.cairo +++ /dev/null @@ -1,45 +0,0 @@ -#[starknet::contract] -pub mod AggregatorContract { - use starknet::ContractAddress; - use core::num::traits::zero::Zero; - use hello_cairo::{ - ownable_contract::{ IOwnableContractDispatcher, IOwnableContractDispatcherTrait }, - counter_contract::{ ICounterDispatcher, ICounterDispatcherTrait } - }; - - #[storage] - struct Storage { - aggr_count: u32 - } - - - #[generate_trait] - // #[abi(embed_v0)] - #[external(v0)] - impl AggregatorImpl of IAggregator { - - fn fetch_ownable_contract_owner( - self: @ContractState, ownable_contract_address: ContractAddress - ) -> ContractAddress { - // here we are passing the contract address of OwnerContract contract into IOwnableDispatcher - // then we call the get owner function - IOwnableContractDispatcher { contract_address: ownable_contract_address }.get_owner() - } - - fn set_new_ownable_contract_owner(ref self: ContractState, ownable_contract_address : ContractAddress, new_owner: ContractAddress) { - assert(new_owner.is_non_zero(), hello_cairo::errors::Errors::ZERO_ADDRESS_OWNER); - IOwnableContractDispatcher { contract_address: ownable_contract_address }.set_owner(new_owner); - } - - fn increase_aggr_count_by_two(ref self: ContractState, counter_contract_adress: ContractAddress) { - let current_count = ICounterDispatcher { contract_address: counter_contract_adress }.get_count(); - self.aggr_count.write(current_count * 2); - } - - fn get_aggr_count(self: @ContractState) -> u32 { - self.aggr_count.read() - } - - } -} -// 0x1b963d5bcc6bcacd3fdd0d2fec50a6e4ac8b150a74ef2221a4b3622ffe94ec3 \ No newline at end of file diff --git a/src/counter_contract.cairo b/src/counter_contract.cairo deleted file mode 100644 index 71204bd..0000000 --- a/src/counter_contract.cairo +++ /dev/null @@ -1,28 +0,0 @@ -#[starknet::interface] -pub trait ICounter { - fn increase_count(ref self: TContractState, amount: u32); - fn get_count(self: @TContractState) -> u32; -} - -#[starknet::contract] -mod CounterContract { - #[storage] - struct Storage { - count: u32, - } - - #[abi(embed_v0)] - impl ICounterImpl of super::ICounter { - fn increase_count(ref self: ContractState, amount: u32) { - assert(amount != 0, hello_cairo::errors::Errors::ZERO_AMOUNT); - let current_count: u32 = self.count.read(); - let result = hello_cairo::addition::add_num(current_count, amount); - self.count.write(result); - } - - fn get_count(self: @ContractState) -> u32 { - self.count.read() - } - } -} -// 0x5afdb452821d219d5b8bc68a44ca3c6fa4dfc313150d30d093a279c090d69e3 - counter_contract_addr \ No newline at end of file diff --git a/src/errors.cairo b/src/errors.cairo deleted file mode 100644 index 6cc2dcd..0000000 --- a/src/errors.cairo +++ /dev/null @@ -1,6 +0,0 @@ -pub mod Errors { - pub const ZERO_AMOUNT: felt252 = 'amount cannot be zero'; - pub const ZERO_ADDRESS_CALLER: felt252 = 'Caller cannot be zero addr'; - pub const ZERO_ADDRESS_OWNER: felt252 = 'Owner cannot be zero addr'; - pub const NOT_OWNER: felt252 = 'Caller not owner'; -} diff --git a/src/hello_starknet.cairo b/src/hello_starknet.cairo deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.cairo b/src/lib.cairo index 318ce5d..8a72152 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,5 +1,50 @@ -mod addition; -mod errors; -mod ownable_contract; -mod counter_contract; -mod aggregator_contract; \ No newline at end of file +fn main() { + short_str(); + let result = check_num(30); + println!("result here: {result}"); + let sum: u16 = add_num(10, 5); + println!("sum here: {sum}"); + let sub: u32 = sub_num(8, 53); + println!("subtraction of two numbers is: {sub}"); + let mul: u64 = multiply_num(4, 6); + println!("multiplying two numbers gives us: {mul}"); + let div: u16 = divide_nums(2, 12); + println!("dividing two numbers gives us: {div}"); +} + +// short string data-type +fn short_str() { + println!("GM Cairo!"); +} + +// bool data type +fn check_num(n1: u8) -> bool { + if n1 > 20 { + return true; + } + return false; +} + +// function to perform addition arithmetic operation +fn add_num(n1: u16, n2: u16) -> u16 { + let result: u16 = n1 + n2; + result +} + +// subtract function +fn sub_num(n1: u32, n2: u32) -> u32 { + let result: u32 = n2 - n1; + result +} + +// multiply function +fn multiply_num(n1: u64, n2: u64) -> u64 { + let result: u64 = n1 * n2; + result +} + +// divide function +fn divide_nums(n1: u16, n2: u16) -> u16 { + let result: u16 = n2 / n1; + result +} \ No newline at end of file diff --git a/src/ownable_contract.cairo b/src/ownable_contract.cairo deleted file mode 100644 index ab827af..0000000 --- a/src/ownable_contract.cairo +++ /dev/null @@ -1,41 +0,0 @@ -use starknet::{get_caller_address, ContractAddress}; - -#[starknet::interface] -pub trait IOwnableContract { - fn set_owner(ref self: T, new_owner: ContractAddress); - fn get_owner(self: @T) -> ContractAddress; -} - - -#[starknet::contract] -mod OwnerContract { - use core::num::traits::zero::Zero; - use starknet::{get_caller_address, ContractAddress}; - use super::IOwnableContract; - - #[storage] - struct Storage { - owner: ContractAddress - } - - #[constructor] - fn constructor(ref self: ContractState, init_owner: ContractAddress) { - assert(!init_owner.is_zero(), hello_cairo::errors::Errors::ZERO_ADDRESS_CALLER); - self.owner.write(init_owner); - } - - #[abi(embed_v0)] - impl OwnerContractImpl of IOwnableContract { - fn set_owner(ref self: ContractState, new_owner: ContractAddress) { - assert(!new_owner.is_zero(), hello_cairo::errors::Errors::ZERO_ADDRESS_OWNER); - let caller = get_caller_address(); - assert(caller == self.owner.read(), hello_cairo::errors::Errors::NOT_OWNER); - self.owner.write(caller); - } - - fn get_owner(self: @ContractState) -> ContractAddress { - self.owner.read() - } - } -} -// 0x10643c1ad9f6effd8af79b32163977e803db69aae21444005044aeea85efc52 - ownable_contract_address \ No newline at end of file