Cairo 1.0 Q&A #2267
Replies: 18 comments 37 replies
-
Hi, Is it possible to execute bitwise operations on felts?.
|
Beta Was this translation helpful? Give feedback.
-
I try to declare the official hello_starknet contract with command: And I get error:
Maybe my starknet version is wrong? Or just the official contract does't work? starknet version: |
Beta Was this translation helpful? Give feedback.
-
When I update a mutable variable inside a break condition of a loop it throw an error. This is the code:
and this is the error:
This error is kind of odd, shouldn't this logic valid in Cairo1? |
Beta Was this translation helpful? Give feedback.
-
How to write code in Cairo that is similar to Rust's
use case:
|
Beta Was this translation helpful? Give feedback.
-
In /// Generate getters and setters skeleton for a non-mapping member in the storage struct.
fn handle_legacy_mapping_storage_var(address: &str) -> String {
format!(
"
mod $storage_var_name$ {{$extra_uses$
use starknet::SyscallResultTrait;
use starknet::SyscallResultTraitImpl;
fn address(key: $key_type$) -> starknet::StorageBaseAddress {{
starknet::storage_base_address_from_felt252(
hash::LegacyHash::<$key_type$>::hash({address}, key))
}}
fn read(key: $key_type$) -> $value_type$ {{
// Only address_domain 0 is currently supported.
let address_domain = 0_u32;
starknet::StorageAccess::<$value_type$>::read(
address_domain,
address(key),
).unwrap_syscall()
}}
fn write(key: $key_type$, value: $value_type$) {{
// Only address_domain 0 is currently supported.
let address_domain = 0_u32;
starknet::StorageAccess::<$value_type$>::write(
address_domain,
address(key),
value,
).unwrap_syscall()
}}
}}"
)
} Note the line
The result of the inner hash is in the |
Beta Was this translation helpful? Give feedback.
-
How can I generate from a cairo file the |
Beta Was this translation helpful? Give feedback.
-
how can i get current block number in cairo contract? Just like block.number in solidity |
Beta Was this translation helpful? Give feedback.
-
Hello! I'm currently utilizing the following crate: Here are my questions:
Thanks in advance for any clarification here! |
Beta Was this translation helpful? Give feedback.
-
Hello! How can this be done? Here's an example: #[starknet::interface]
trait ITest<TContractState> {}
#[starknet::contract]
mod Test {
#[storage]
struct Storage {
byte_array: ByteArray
}
#[constructor]
fn constructor(ref self: ContractState, byte_array: ByteArray) {
self.byte_array.write(byte_array);
}
}
use starknet::ClassHash;
#[starknet::interface]
trait IFactory<TContractState> {
fn create_test(self: @TContractState, class_hash: ClassHash, byte_array: ByteArray);
}
#[starknet::contract]
mod Factory {
use starknet::syscalls::{deploy_syscall};
use starknet::ClassHash;
use starknet::SyscallResultTrait;
#[storage]
struct Storage {}
#[abi(embed_v0)]
impl Factory of super::IFactory<ContractState> {
fn create_test(self: @ContractState, class_hash: ClassHash, byte_array: ByteArray) {
let mut constructor_calldata: Array::<felt252> = array![];
constructor_calldata.append(byte_array.into()); // <-- here's the problem
// Trait has no implementation in context: core::traits::Into::<core::byte_array::ByteArray, core::felt252>.
let (deployed_address, _) = deploy_syscall(
class_hash, 0, constructor_calldata.span(), false
)
.unwrap_syscall();
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Is there a way to get the current class hash inside a contract? Something like: #[starknet::interface]
trait ITest<TContractState> {
fn test(self: @TContractState);
}
#[starknet::contract]
mod Test {
use starknet::ClassHash;
#[storage]
struct Storage {}
fn test(self: @ContractState) {
let cur_class_hash: ClassHash = // what function to use to get the current contract's class hash??
}
} |
Beta Was this translation helpful? Give feedback.
-
What is the most gas-efficient way to accumulate string concatenations in a loop? fn print_he_10000_times() {
let mut he_10000_times: ByteArray = "";
let mut n: u8 = 0;
while n < 10000 {
he_10000_times.append(@"he");
n += 1;
};
println!("{he_10000_times}");
}
fn main() {
print_he_10000_times();
} P.S. Unrelated, but if I change the |
Beta Was this translation helpful? Give feedback.
-
In general, this issue isn't really scalable, I suggest asking in the cairo discord, or the cairo questions tg group (the latter requires filling a form to show you're a real person). |
Beta Was this translation helpful? Give feedback.
-
Fixed-size arrays are available in Cairo, but I how can I use array-related function with them? E.g. for this code: fn main() {
let arr: [felt252; 2] = [1, 2];
let sp: Span<felt252> = arr.span();
} Does that mean fixed-size arrays are not yet fully supported in the language? |
Beta Was this translation helpful? Give feedback.
-
I am trying to understand why the compiler is not able to infer the type of Example code: fn to_option(num: usize) -> Option<usize> {
if num > 10 {
Option::Some(num)
} else {
Option::None
}
}
fn from_option(opt_num: Option<usize>) -> usize {
if let Option::Some(num) = opt_num {
num
} else {
0
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_to_option() {
let num: usize = 5;
assert_eq!(super::to_option(num), Option::None); // Type annotations needed. Failed to infer ?15
}
#[test]
fn test_from_option() {
assert_eq!(super::from_option(Option::None), 0); // this infers the Option::None type with no issues
}
} Why is the compiler able to infer the type of the |
Beta Was this translation helpful? Give feedback.
-
Hey guys! I am using scrab and cairo with version 2.6.3. I installed Cairo 1.0 plugin in my VSC, but the syntax highlighting seems not working properly. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to understand smart pointers and how to use them to implement a doubly linked list. When I have two boxes See the below example: type List<T> = Option<Box<Node<T>>>;
#[derive(Drop, Copy, Debug)]
struct Node<T> {
data: T,
next: List<T>,
previous: List<T>
}
#[derive(Drop, Copy, Debug)]
struct DoublyLinkedList<T> {
head: List<T>,
tail: List<T>,
len: usize,
}
#[generate_trait]
impl DoublyLinkedListImpl<T, +Drop<T>, +core::fmt::Debug<T>, +Copy<T>> of DoublyLinkedListTrait<T> {
fn new() -> DoublyLinkedList<T> {
DoublyLinkedList { head: Option::None, tail: Option::None, len: 0 }
}
fn push(ref self: DoublyLinkedList<T>, data: T) {
match self.tail {
Option::None => {
let node = BoxTrait::new(Node { data, next: Option::None, previous: Option::None });
self.tail = Option::Some(node);
// head & tail should now point to the same node
self.head = self.tail;
self.len += 1;
},
Option::Some(tail) => {
let node = BoxTrait::new(
Node { data, next: Option::None, previous: Option::Some(tail) }
);
let mut tail = tail.unbox();
// Here's the problem! Since both head & tail point to the same node,
// I would expect the below assignment to update *both* of the boxes
// **But only `tail` gets updated!!!** (see below printlns)
tail.next = Option::Some(node);
self.tail = tail.next;
self.len += 1;
},
}
}
}
fn main() {
let mut list = DoublyLinkedListTrait::<u32>::new();
println!("init");
println!("head: {:?}", list.head); // Option::None(())
println!("tail: {:?}\n", list.tail); // Option::None(())
list.push(23);
println!("push(23)");
println!("head: {:?}", list.head); // Option::Some(&Node { data: 23, next: Option::None(()), previous: Option::None(()) })
println!("tail: {:?}\n", list.tail); // Option::Some(&Node { data: 23, next: Option::None(()), previous: Option::None(()) })
list.push(5);
println!("push(5)");
println!("head: {:?}", list.head); // Option::Some(&Node { data: 23, next: Option::None(()), previous: Option::None(()) })
println!("tail: {:?}\n", list.tail); // Option::Some(&Node { data: 5, next: Option::None(()), previous: Option::Some(&Node { data: 23, next: Option::None(()), previous: Option::None(()) }) })
} Why does this behavior occur? Also I would really appreciate an explanation on how to accomplish this the correct Cairo way. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to compile and run a Cairo file without the use of Scarb? For context, in Rust I can create a main.rs file and run it with |
Beta Was this translation helpful? Give feedback.
-
Ask all your questions about Cairo 1.0
Beta Was this translation helpful? Give feedback.
All reactions