From a88b0b5b007bb1301163d20ea81e6e3f997cf88f Mon Sep 17 00:00:00 2001 From: marijamijailovic Date: Wed, 25 Sep 2024 22:13:31 +0200 Subject: [PATCH] Added examples to contract, and cahnge cairo to 2.8.0 --- components/Editor/EditorFooter.tsx | 2 +- components/Editor/examples.ts | 69 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/components/Editor/EditorFooter.tsx b/components/Editor/EditorFooter.tsx index 6c509a7..fa74768 100644 --- a/components/Editor/EditorFooter.tsx +++ b/components/Editor/EditorFooter.tsx @@ -16,7 +16,7 @@ function EditorFooter({ withoutContent = false }) { !isFullScreen && 'rounded-b-lg', )} > - Cairo Compiler v2.6.3 + Cairo Compiler v2.8.0 {isFullScreen && (
diff --git a/components/Editor/examples.ts b/components/Editor/examples.ts index e6a2fcd..b35a107 100644 --- a/components/Editor/examples.ts +++ b/components/Editor/examples.ts @@ -208,6 +208,73 @@ fn fib(a: felt252, b: felt252, n: felt252) -> felt252 { 0 => a, _ => fib(b, a + b, n - 1), } +}`, + `#[starknet::contract] +mod SimpleContract { + #[storage] + struct Storage { + balance: felt252, + } + + #[generate_trait] + impl InternalImpl of InternalTrait { + fn internal_function(self: @ContractState) -> felt252 { + self.balance.read() + } + } + + fn other_internal_function(self: @ContractState) -> felt252 { + self.balance.read() + 5 + } +} + +use SimpleContract::{ InternalTrait, other_internal_function }; + +fn add(a: felt252, b: felt252, c: felt252) -> felt252 { + a + b + c +} + +fn main() -> felt252 { + let mut state = SimpleContract::contract_state_for_testing(); + state.balance.write(10); + + let balance = state.balance.read(); + let internal_balance = state.internal_function(); + let other_balance = other_internal_function(@state); + + let res = add(balance, internal_balance, other_balance); + res +}`, + `#[starknet::contract] +mod Fibonacci { + #[storage] + struct Storage { + n: u128, + } + + #[generate_trait] + impl CalculationImpl of CalculationTrait { + fn calculate_fib(self: @ContractState) -> u128 { + fib(1, 1, self.n.read()) + } + } + + fn fib(a: u128, b: u128, n: u128) -> u128 { + match n { + 0 => a, + _ => fib(b, a + b, n - 1), + } + } +} + +use Fibonacci::CalculationTrait; + +fn main() -> u128 { + let mut state = Fibonacci::contract_state_for_testing(); + state.n.write(5); + + let result = state.calculate_fib(); + result }`, ], Sierra: [ @@ -434,4 +501,6 @@ export const CairoExampleNames = [ 'Dictionaries', 'Ownership', 'Fibonacci', + 'SimpleContract', + 'FibonacciContract', ]