Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit 98f31f2

Browse files
tdelabroBernardstanislas
authored andcommitted
feat(exercises): create patches with solution
1 parent 92dd238 commit 98f31f2

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

.patches/beginner/ex00.cairo.patch

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- exercises/beginner/ex00.cairo 2022-05-10 13:54:32.000000000 +0200
2+
+++ .solutions/ex00.cairo 2022-05-10 13:56:26.000000000 +0200
3+
@@ -10,6 +10,9 @@
4+
# https://starknet.io/documentation/contracts/#contracts_storage
5+
# https://www.cairo-lang.org/docs/hello_starknet/intro.html
6+
# https://www.cairo-lang.org/docs/hello_starknet/user_auth.html#storage-maps
7+
+@storage_var
8+
+func dust(address : felt) -> (amount : felt):
9+
+end
10+
11+
# This code block define an `external` function
12+
# It can be called by other contracts (wallet or other)

.patches/beginner/ex01.cairo.patch

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--- exercises/beginner/ex01.cairo 2022-05-10 13:54:32.000000000 +0200
2+
+++ .solutions/ex01.cairo 2022-05-10 14:08:56.000000000 +0200
3+
@@ -14,6 +14,13 @@
4+
# Create two storages `star` and `slot`
5+
# `slot` will map an `address` to the next available `slot` this `address` can use
6+
# `star` will map an `address` and a `slot` to a `size`
7+
+@storage_var
8+
+func slot(address : felt) -> (slot : felt):
9+
+end
10+
+
11+
+@storage_var
12+
+func star(address : felt, slot : felt) -> (size : felt):
13+
+end
14+
15+
# TODO
16+
# Create an event `a_star_is_born`
17+
@@ -22,6 +29,9 @@
18+
# - the `slot` where this `star` has been registered
19+
# - the size of the given `star`
20+
# https://starknet.io/documentation/events/
21+
+@event
22+
+func a_star_is_born(account : felt, slot : felt, size : felt):
23+
+end
24+
25+
@external
26+
func collect_dust{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(amount : felt):
27+
@@ -43,13 +53,21 @@
28+
):
29+
# TODO
30+
# Get the caller address
31+
+ let (address) = get_caller_address()
32+
# Get the amount on dust owned by the caller
33+
+ let (current_amount) = dust.read(address)
34+
# Make sure this amount is at least equal to `dust_amount`
35+
+ assert_le(dust_amount, current_amount)
36+
# Get the caller next available `slot`
37+
+ let (next_slot) = slot.read(address)
38+
# Update the amount of dust owned by the caller
39+
+ dust.write(address, current_amount - dust_amount)
40+
# Register the newly created star, with a size equal to `dust_amount`
41+
+ star.write(address, next_slot, dust_amount)
42+
# Increment the caller next available slot
43+
+ slot.write(address, next_slot + 1)
44+
# Emit an `a_star_is_born` even with appropiate valued
45+
+ a_star_is_born.emit(address, next_slot, dust_amount)
46+
47+
return ()
48+
end
49+
@@ -64,6 +82,21 @@
50+
51+
# TODO
52+
# Write two views, for the `star` and `slot` storages
53+
+@view
54+
+func view_slot{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
55+
+ address : felt
56+
+) -> (amount : felt):
57+
+ let (res) = slot.read(address)
58+
+ return (res)
59+
+end
60+
+
61+
+@view
62+
+func view_star{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
63+
+ address : felt, slot : felt
64+
+) -> (amount : felt):
65+
+ let (res) = star.read(address, slot)
66+
+ return (res)
67+
+end
68+
69+
#########
70+
# TESTS #

.patches/beginner/ex02.cairo.patch

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--- exercises/beginner/ex02.cairo 2022-05-10 14:26:44.000000000 +0200
2+
+++ .solutions/ex02.cairo 2022-05-10 14:26:17.000000000 +0200
3+
@@ -16,6 +16,10 @@
4+
# - size
5+
# Both members are of type `felt`
6+
# https://www.cairo-lang.org/docs/reference/syntax.html#structs
7+
+struct Star:
8+
+ member name : felt
9+
+ member size : felt
10+
+end
11+
12+
@storage_var
13+
func dust(address : felt) -> (amount : felt):
14+
@@ -23,6 +27,9 @@
15+
16+
# TODO
17+
# Update the `star` storage to store `Star` instead of `felt`
18+
+@storage_var
19+
+func star(address : felt, slot : felt) -> (size : Star):
20+
+end
21+
22+
@storage_var
23+
func slot(address : felt) -> (slot : felt):
24+
@@ -45,6 +52,21 @@
25+
# TODO
26+
# Update the `light_star` external so it take a `Star` struct instead of the amount of dust
27+
# Caller `dust` storage must be deducted form a amount equal to the star size
28+
+@external
29+
+func light_star{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(new_star : Star):
30+
+ let (address) = get_caller_address()
31+
+ let (current_amount) = dust.read(address)
32+
+ assert_le(new_star.size, current_amount)
33+
+ let (next_slot) = slot.read(address)
34+
+
35+
+ dust.write(address, current_amount - new_star.size)
36+
+ star.write(address, next_slot, new_star)
37+
+ slot.write(address, next_slot + 1)
38+
+
39+
+ a_star_is_born.emit(address, next_slot, new_star)
40+
+
41+
+ return ()
42+
+end
43+
44+
@view
45+
func view_dust{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
46+
@@ -65,6 +87,13 @@
47+
# TODO
48+
# Create a view for `star`
49+
# It must return an instance of `Star` instead of a `felt`
50+
+@view
51+
+func view_star{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(
52+
+ address : felt, slot : felt
53+
+) -> (star : Star):
54+
+ let (res) = star.read(address, slot)
55+
+ return (res)
56+
+end
57+
58+
#########
59+
# TESTS #

.patches/beginner/ex03.cairo.patch

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--- exercises/beginner/ex03.cairo 2022-05-10 14:33:02.000000000 +0200
2+
+++ .solutions/ex03.cairo 2022-05-10 14:31:17.000000000 +0200
3+
@@ -58,8 +58,13 @@
4+
5+
# TODO
6+
# Write a stop condition
7+
+ if array_len == 0:
8+
+ return ()
9+
+ end
10+
# Insert the star at index 0 of the array
11+
+ insert_star(address, array[0])
12+
# recursively call `batch_create_stars`
13+
+ batch_create_stars(address, array_len - 1, array + Star.SIZE)
14+
15+
return ()
16+
end

.patches/beginner/ex04.cairo.patch

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--- exercises/beginner/ex04.cairo 2022-05-10 14:33:56.000000000 +0200
2+
+++ .solutions/ex04.cairo 2022-05-10 14:43:23.000000000 +0200
3+
@@ -92,6 +92,37 @@
4+
# https://www.cairo-lang.org/docs/how_cairo_works/consts.html#revoked-references
5+
# That's one of the most tricky feature of cairo. Treat yourself !
6+
# Also, Cairo doesn't support `elif`, neither chaining multiple comparaisons on a single `if` arm 😔
7+
+ let new_slot = current_slot + 1
8+
+ if new_slot == 1:
9+
+ increase_rank(address)
10+
+ tempvar syscall_ptr = syscall_ptr
11+
+ tempvar pedersen_ptr = pedersen_ptr
12+
+ tempvar range_check_ptr = range_check_ptr
13+
+ else:
14+
+ tempvar syscall_ptr = syscall_ptr
15+
+ tempvar pedersen_ptr = pedersen_ptr
16+
+ tempvar range_check_ptr = range_check_ptr
17+
+ end
18+
+ if new_slot == 10:
19+
+ increase_rank(address)
20+
+ tempvar syscall_ptr = syscall_ptr
21+
+ tempvar pedersen_ptr = pedersen_ptr
22+
+ tempvar range_check_ptr = range_check_ptr
23+
+ else:
24+
+ tempvar syscall_ptr = syscall_ptr
25+
+ tempvar pedersen_ptr = pedersen_ptr
26+
+ tempvar range_check_ptr = range_check_ptr
27+
+ end
28+
+ if new_slot == 100:
29+
+ increase_rank(address)
30+
+ tempvar syscall_ptr = syscall_ptr
31+
+ tempvar pedersen_ptr = pedersen_ptr
32+
+ tempvar range_check_ptr = range_check_ptr
33+
+ else:
34+
+ tempvar syscall_ptr = syscall_ptr
35+
+ tempvar pedersen_ptr = pedersen_ptr
36+
+ tempvar range_check_ptr = range_check_ptr
37+
+ end
38+
39+
a_star_is_born.emit(address, current_slot, new_star)
40+

0 commit comments

Comments
 (0)