Skip to content

Commit

Permalink
Added examples to contract, and cahnge cairo to 2.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marijamijailovic committed Sep 25, 2024
1 parent 309f63b commit a88b0b5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/Editor/EditorFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function EditorFooter({ withoutContent = false }) {
!isFullScreen && 'rounded-b-lg',
)}
>
<span>Cairo Compiler v2.6.3</span>
<span>Cairo Compiler v2.8.0</span>

{isFullScreen && (
<div className="flex items-center justify-end divide-x divide-gray-200 dark:divide-black-500">
Expand Down
69 changes: 69 additions & 0 deletions components/Editor/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -434,4 +501,6 @@ export const CairoExampleNames = [
'Dictionaries',
'Ownership',
'Fibonacci',
'SimpleContract',
'FibonacciContract',
]

0 comments on commit a88b0b5

Please sign in to comment.