This repository was archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmock_contract.cairo
More file actions
66 lines (57 loc) · 1.7 KB
/
mock_contract.cairo
File metadata and controls
66 lines (57 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Declare this file as a StarkNet contract.
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
@storage_var
func mapping(key) -> (value: felt) {
}
@storage_var
func mapping_length() -> (value: felt) {
}
@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
arr_len: felt, arr: felt*
) {
mapping_length.write(arr_len);
fill_mapping(arr_len, arr);
return ();
}
func fill_mapping{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
arr_len: felt, arr: felt*
) -> () {
if (arr_len == 0) {
return ();
}
// This could be useful for debugging...
let val = [arr];
%{
from requests import post
json = { # creating the body of the post request so it's printed in the python script
"1": f"Still {ids.arr_len} values to save...",
"2": f"Saving {ids.val} to the storage..."
}
post(url="http://localhost:5000", json=json) # sending the request to our small "server"
%}
mapping.write(arr_len, [arr]);
fill_mapping(arr_len - 1, arr + 1);
return ();
}
@view
func product_mapping{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
res: felt
) {
let (length) = mapping_length.read();
let (res) = product_mapping_internal(length);
return (res,);
}
func product_mapping_internal{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
len: felt
) -> (res: felt) {
if (len == 0) {
let (res) = mapping.read(len);
return (res,);
}
let (temp) = product_mapping_internal(len - 1);
let (mapping_val) = mapping.read(len);
let res = temp * mapping_val;
return (res,);
}